steem-block-db/steemapi.go

73 lines
1.6 KiB
Go
Raw Normal View History

2018-10-18 08:39:41 +00:00
package main
2018-11-01 03:35:30 +00:00
import "encoding/json"
import log "github.com/sirupsen/logrus"
2018-10-18 08:39:41 +00:00
type SteemAPI struct {
2018-10-31 09:44:19 +00:00
url string
rpc *JSONRPC
2018-10-18 08:39:41 +00:00
}
2018-10-28 16:31:26 +00:00
var EmptyParams = []string{}
var EmptyParamsRaw, _ = json.Marshal(EmptyParams)
2018-10-18 08:39:41 +00:00
func NewSteemAPI(url string, options ...func(s *SteemAPI)) *SteemAPI {
2018-10-31 09:44:19 +00:00
rpc := NewJSONRPC(url, func(x *JSONRPC) {})
2018-10-18 08:39:41 +00:00
self := &SteemAPI{
rpc: rpc,
}
for _, option := range options {
option(self)
}
return self
}
2018-10-28 16:31:26 +00:00
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
}
2018-10-31 09:44:19 +00:00
func (self *SteemAPI) GetOpsInBlock(blockNum BlockNumber) (GetOpsInBlockResponse, error) {
2018-10-18 08:39:41 +00:00
// 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...)
2018-10-28 16:31:26 +00:00
return &result, nil
2018-10-18 08:39:41 +00:00
}