steem-block-db/steemapi.go

73 lines
1.6 KiB
Go

package main
import "encoding/json"
import log "github.com/sirupsen/logrus"
type SteemAPI struct {
url string
rpc *JSONRPC
}
var EmptyParams = []string{}
var EmptyParamsRaw, _ = json.Marshal(EmptyParams)
func NewSteemAPI(url string, options ...func(s *SteemAPI)) *SteemAPI {
rpc := NewJSONRPC(url, func(x *JSONRPC) {})
self := &SteemAPI{
rpc: rpc,
}
for _, option := range options {
option(self)
}
return self
}
func (self *SteemAPI) GetDynamicGlobalProperties() (GetDynamicGlobalPropertiesResponse, error) {
var resp DynamicGlobalProperties
raw, err := self.rpc.Call("get_dynamic_global_properties", EmptyParamsRaw)
if err != nil {
return nil, err
}
json.Unmarshal(raw, &resp)
return &resp, nil
}
func (self *SteemAPI) GetOpsInBlock(blockNum BlockNumber) (GetOpsInBlockResponse, error) {
// first fetch virtual ops
vOpsParams := &GetOpsInBlockRequestParams{BlockNum: blockNum, VirtualOps: true}
vop, err := vOpsParams.MarshalJSON()
vOpsResponse, err := self.rpc.Call("condenser_api.get_ops_in_block", vop)
if err != nil {
return nil, err
}
var result []OperationObject
err = json.Unmarshal(vOpsResponse, &result)
if err != nil {
return nil, err
}
// result is now populated with vops, now get real ops
realOpsParams := &GetOpsInBlockRequestParams{BlockNum: blockNum, VirtualOps: false}
rop, err := realOpsParams.MarshalJSON()
realOpsResponse, err := self.rpc.Call("condenser_api.get_ops_in_block", rop)
var secondResult []OperationObject
err = json.Unmarshal(realOpsResponse, &secondResult)
if err != nil {
return nil, err
}
result = append(result, secondResult...)
return &result, nil
}