diff --git a/apis/database/api.go b/apis/database/api.go index bad392f..e53e5f1 100644 --- a/apis/database/api.go +++ b/apis/database/api.go @@ -259,12 +259,12 @@ func (api *API) GetBlock(blockNum uint32) (*Block, error) { } //get_ops_in_block -func (api *API) GetOpsInBlock(blockNum uint32, only_virtual bool) ([]*OpsInBlock, error) { +func (api *API) GetOpsInBlock(blockNum uint32, only_virtual bool) ([]*types.OperationObject, error) { raw, err := api.Raw("get_ops_in_block", []interface{}{blockNum, only_virtual}) if err != nil { return nil, err } - var resp []*OpsInBlock + var resp []*types.OperationObject if err := json.Unmarshal([]byte(*raw), &resp); err != nil { return nil, errors.Wrapf(err, "golos-go: %v: failed to unmarshal get_ops_in_block response", APIID) } diff --git a/apis/database/data.go b/apis/database/data.go index 63d0f2c..22fbcab 100644 --- a/apis/database/data.go +++ b/apis/database/data.go @@ -485,16 +485,6 @@ type TrendingTags struct { Comments *types.Int `json:"comments"` } -type OpsInBlock struct { - TrxID string `json:"trx_id"` - Block *types.Int `json:"block"` - TrxInBlock *types.Int `json:"trx_in_block"` - OpInTrx *types.Int `json:"op_in_trx"` - VirtualOp *types.Int `json:"virtual_op"` - Timestamp string `json:"timestamp"` - Op []*interface{} `json:"op"` -} - type Categories struct { ID *types.Int `json:"id"` Name string `json:"name"` 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, + }) +}