You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.7KB

  1. package main
  2. import "encoding/json"
  3. //import log "github.com/sirupsen/logrus"
  4. type SteemAPI struct {
  5. url string
  6. rpc *JSONRPC
  7. }
  8. type SteemAPIShape interface {
  9. GetDynamicGlobalProperties() (GetDynamicGlobalPropertiesResponse, error)
  10. GetOpsInBlock(blockNum BlockNumber) (GetOpsInBlockResponse, error)
  11. }
  12. var EmptyParams = []string{}
  13. var EmptyParamsRaw, _ = json.Marshal(EmptyParams)
  14. func NewSteemAPI(url string, options ...func(s SteemAPIShape)) *SteemAPI {
  15. rpc := NewJSONRPC(url, func(x *JSONRPC) {})
  16. self := &SteemAPI{
  17. rpc: rpc,
  18. }
  19. for _, option := range options {
  20. option(self)
  21. }
  22. return self
  23. }
  24. func (self *SteemAPI) GetDynamicGlobalProperties() (GetDynamicGlobalPropertiesResponse, error) {
  25. var resp DynamicGlobalProperties
  26. raw, err := self.rpc.Call("condenser_api.get_dynamic_global_properties", EmptyParamsRaw)
  27. if err != nil {
  28. return nil, err
  29. }
  30. json.Unmarshal(raw, &resp)
  31. return &resp, nil
  32. }
  33. func (self *SteemAPI) GetOpsInBlock(blockNum BlockNumber) (GetOpsInBlockResponse, error) {
  34. // i was mistaken, i thought the second param == true meant only
  35. // virtualops, and == false meant only non-virtualops. turns out the
  36. // arg should be named "excludenonvirtualops", as setting it to false
  37. // returns both real ops *and* virtual ops in a single call. not sure if
  38. // this was always the case, but it is as of 20181101 against
  39. // api.steemit.com.
  40. realOpsParams := &GetOpsInBlockRequestParams{BlockNum: blockNum, VirtualOps: false}
  41. rop, err := realOpsParams.MarshalJSON()
  42. if err != nil {
  43. return nil, err
  44. }
  45. rawOpsResponse, err := self.rpc.Call("condenser_api.get_ops_in_block", rop)
  46. var result []OperationObject
  47. err = json.Unmarshal(rawOpsResponse, &result)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return &result, nil
  52. }