Skip to content

Commit

Permalink
apias/database: Add GetOpsInBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
tchap committed Aug 10, 2017
1 parent bf16641 commit b923545
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions apis/database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
Expand Down
10 changes: 10 additions & 0 deletions apis/database/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions apis/database/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
53 changes: 53 additions & 0 deletions types/operation_object.go
Original file line number Diff line number Diff line change
@@ -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,
})
}

0 comments on commit b923545

Please sign in to comment.