steem-block-db/steemapi.go

65 lines
1.7 KiB
Go
Raw Permalink Normal View History

2018-10-18 08:39:41 +00:00
package main
2018-11-01 03:35:30 +00:00
import "encoding/json"
2018-11-01 07:14:13 +00:00
//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-11-03 14:41:53 +00:00
type SteemAPIShape interface {
GetDynamicGlobalProperties() (GetDynamicGlobalPropertiesResponse, error)
GetOpsInBlock(blockNum BlockNumber) (GetOpsInBlockResponse, error)
}
2018-10-28 16:31:26 +00:00
var EmptyParams = []string{}
var EmptyParamsRaw, _ = json.Marshal(EmptyParams)
2018-11-03 14:41:53 +00:00
func NewSteemAPI(url string, options ...func(s SteemAPIShape)) *SteemAPI {
2018-10-18 08:39:41 +00:00
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
2018-11-03 14:59:46 +00:00
raw, err := self.rpc.Call("condenser_api.get_dynamic_global_properties", EmptyParamsRaw)
2018-10-28 16:31:26 +00:00
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-11-01 07:14:13 +00:00
// i was mistaken, i thought the second param == true meant only
// virtualops, and == false meant only non-virtualops. turns out the
// arg should be named "excludenonvirtualops", as setting it to false
// returns both real ops *and* virtual ops in a single call. not sure if
// this was always the case, but it is as of 20181101 against
// api.steemit.com.
2018-10-18 08:39:41 +00:00
realOpsParams := &GetOpsInBlockRequestParams{BlockNum: blockNum, VirtualOps: false}
rop, err := realOpsParams.MarshalJSON()
2018-11-03 14:41:53 +00:00
if err != nil {
return nil, err
}
rawOpsResponse, err := self.rpc.Call("condenser_api.get_ops_in_block", rop)
2018-11-01 07:14:13 +00:00
var result []OperationObject
2018-11-03 14:41:53 +00:00
err = json.Unmarshal(rawOpsResponse, &result)
2018-10-18 08:39:41 +00:00
if err != nil {
return nil, err
}
2018-10-28 16:31:26 +00:00
return &result, nil
2018-10-18 08:39:41 +00:00
}