From b923545498424134c9da989c7ba8406bbe26c194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Kupka?= Date: Fri, 11 Aug 2017 00:22:49 +0200 Subject: [PATCH] apias/database: Add GetOpsInBlock --- apis/database/README.md | 1 + apis/database/api.go | 10 ++++++++ apis/database/data.go | 10 ++++++++ types/operation_object.go | 53 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 types/operation_object.go diff --git a/apis/database/README.md b/apis/database/README.md index 6754090..a4d7a50 100644 --- a/apis/database/README.md +++ b/apis/database/README.md @@ -40,6 +40,7 @@ It is a bit confusing to see `set_` prefix. Needs research. | ----------------------- |:-----------:|:--------------:| | get_block_header | DONE | | | get_block | DONE | PARTIALLY DONE | +| get_ops_in_block | DONE | DONE | | get_state | DONE | | | get_trending_categories | DONE | | | get_best_categories | DONE | | diff --git a/apis/database/api.go b/apis/database/api.go index 32e299e..f33573d 100644 --- a/apis/database/api.go +++ b/apis/database/api.go @@ -8,6 +8,7 @@ import ( // RPC "github.com/go-steem/rpc/interfaces" "github.com/go-steem/rpc/internal/call" + "github.com/go-steem/rpc/types" ) const ( @@ -368,3 +369,12 @@ func (api *API) GetOpsInBlockRaw(blockNum uint32, onlyVirtual bool) (*json.RawMe return call.Raw( api.caller, "get_ops_in_block", []interface{}{blockNum, onlyVirtual}) } + +func (api *API) GetOpsInBlock(blockNum uint32, onlyVirtual bool) ([]*types.OperationObject, error) { + var resp []*types.OperationObject + err := api.caller.Call("get_ops_in_block", []interface{}{blockNum, onlyVirtual}, &resp) + if err != nil { + return nil, err + } + return resp, nil +} diff --git a/apis/database/data.go b/apis/database/data.go index c9c04f9..c768331 100644 --- a/apis/database/data.go +++ b/apis/database/data.go @@ -143,3 +143,13 @@ type VoteState struct { Percent *types.Int `json:"percent"` Time *types.Time `json:"time"` } + +type OperationObject struct { + BlockNumber uint32 `json:"block"` + TransactionID string `json:"trx_id"` + TransactionInBlock uint32 `json:"trx_in_block"` + Operation types.Operation `json:"op"` + OperationInTransaction uint16 `json:"op_in_trx"` + VirtualOperation uint64 `json:"virtual_op"` + Timestamp *types.Time `json:"timestamp"` +} diff --git a/types/operation_object.go b/types/operation_object.go new file mode 100644 index 0000000..d3cfc47 --- /dev/null +++ b/types/operation_object.go @@ -0,0 +1,53 @@ +package types + +import ( + "encoding/json" +) + +type OperationObject struct { + BlockNumber uint32 `json:"block"` + TransactionID string `json:"trx_id"` + TransactionInBlock uint32 `json:"trx_in_block"` + Operation Operation `json:"op"` + OperationInTransaction uint16 `json:"op_in_trx"` + VirtualOperation uint64 `json:"virtual_op"` + Timestamp *Time `json:"timestamp"` +} + +type rawOperationObject struct { + BlockNumber uint32 `json:"block"` + TransactionID string `json:"trx_id"` + TransactionInBlock uint32 `json:"trx_in_block"` + Operation *operationTuple `json:"op"` + OperationInTransaction uint16 `json:"op_in_trx"` + VirtualOperation uint64 `json:"virtual_op"` + Timestamp *Time `json:"timestamp"` +} + +func (op *OperationObject) UnmarshalJSON(p []byte) error { + var raw rawOperationObject + if err := json.Unmarshal(p, &raw); err != nil { + return err + } + + op.BlockNumber = raw.BlockNumber + op.TransactionID = raw.TransactionID + op.TransactionInBlock = raw.TransactionInBlock + op.Operation = raw.Operation.Data + op.OperationInTransaction = raw.OperationInTransaction + op.VirtualOperation = raw.VirtualOperation + op.Timestamp = raw.Timestamp + return nil +} + +func (op *OperationObject) MarshalJSON() ([]byte, error) { + return json.Marshal(&rawOperationObject{ + BlockNumber: op.BlockNumber, + TransactionID: op.TransactionID, + TransactionInBlock: op.TransactionInBlock, + Operation: &operationTuple{op.Operation.Type(), op.Operation}, + OperationInTransaction: op.OperationInTransaction, + VirtualOperation: op.VirtualOperation, + Timestamp: op.Timestamp, + }) +}