Skip to content

Instantly share code, notes, and snippets.

@tchap
Last active August 22, 2016 16:44
Show Gist options
  • Save tchap/5b19376e31c32ba42d8183cb2552f8f2 to your computer and use it in GitHub Desktop.
Save tchap/5b19376e31c32ba42d8183cb2552f8f2 to your computer and use it in GitHub Desktop.
Accessing steemd RPC programatically using Golang
package main
import (
"flag"
"fmt"
"log"
"net/rpc/jsonrpc"
"time"
"golang.org/x/net/websocket"
)
func main() {
if err := run(); err != nil {
log.Fatalln("Error:", err)
}
}
type Config struct {
BlockInterval int `json:"STEEMIT_BLOCK_INTERVAL"`
}
type DynamicGlobalProperties struct {
LastIrreversibleBlockNum int `json:"last_irreversible_block_num"`
}
func run() error {
// Process flags.
flagAddress := flag.String("rpc_endpoint", "ws://localhost:8090", "steemd RPC endpoint address")
flag.Parse()
// Dial the connection to the JSON-RPC endpoint.
addr := *flagAddress
log.Println("---> CONNECT", addr)
conn, err := websocket.Dial(addr, "", "http://localhost")
if err != nil {
return err
}
defer conn.Close()
// Instantiate a JSON-RPC client.
client := jsonrpc.NewClient(conn)
// Get config.
log.Println("---> CALL get_config")
var config Config
if err := client.Call("get_config", nil, &config); err != nil {
return err
}
// Loop in a similar way as in the Python example.
lastBlock := 160900
for {
log.Println("---> CALL get_dynamic_global_properties")
var props DynamicGlobalProperties
if err := client.Call("get_dynamic_global_properties", nil, &props); err != nil {
return err
}
for props.LastIrreversibleBlockNum-lastBlock > 0 {
log.Printf("---> CALL get_block %v", lastBlock)
var block map[string]interface{}
if err := client.Call("get_block", lastBlock, &block); err != nil {
return err
}
fmt.Println(block)
lastBlock++
}
time.Sleep(time.Duration(config.BlockInterval) * time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment