This commit is contained in:
Jeffrey Paul 2018-10-04 11:41:39 -07:00
commit 053e178942
Signed by: sneak
GPG Key ID: 052443F4DF2A55C2
5 changed files with 234 additions and 0 deletions

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
default: run
run:
go run *.go

63
badgerdb.go Normal file
View File

@ -0,0 +1,63 @@
package main
import "log"
import "io/ioutil"
import "github.com/dgraph-io/badger"
type KeyValueStore struct {
db *badger.DB
}
func NewKeyValueStore() *KeyValueStore {
kv := new(KeyValueStore)
kv.init()
return kv
}
func (kv *KeyValueStore) init() {
dir, err := ioutil.TempDir("", "badger")
if err != nil {
log.Fatal(err)
}
opts := badger.DefaultOptions
opts.Dir = dir
opts.ValueDir = dir
kv.db, err = badger.Open(opts)
if err != nil {
log.Fatal(err)
}
}
func (kv *KeyValueStore) Close() {
kv.db.Close()
}
func (kv *KeyValueStore) Put(key *string, value *string) error {
txn := kv.db.NewTransaction(true) // Read-write txn
err := txn.Set([]byte(*key), []byte(*value))
if err != nil {
log.Fatal(err)
}
err = txn.Commit(nil)
if err != nil {
log.Fatal(err)
}
return nil
}
func (kv *KeyValueStore) Get(key *string) (*string, error) {
txn := kv.db.NewTransaction(true) // Read-write txn
item, err := txn.Get([]byte(*key))
if err != nil {
return nil, err
}
val, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
s := string(val)
return &s, nil
}

112
jsonrpc.go Normal file
View File

@ -0,0 +1,112 @@
package main
// thanks to https://github.com/onrik/ethrpc/blob/master/ethrpc.go for
// example
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
)
type logger interface {
Println(v ...interface{})
}
type httpClient interface {
Post(url string, contentType string, body io.Reader) (*http.Response, error)
}
type JsonRpcError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (err JsonRpcError) Error() string {
return fmt.Sprintf("Error %d (%s)", err.Code, err.Message)
}
type JsonRpcResponse struct {
ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result json.RawMessage `json:"result"`
Error *JsonRpcError `json:"error"`
}
type JsonRpcRequest struct {
ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
// JsonRpc Client Object
type JsonRpc struct {
url string
client httpClient
log logger
Debug bool
}
// New create new rpc client with given url
func JsonRpcClient(url string, options ...func(rpc *JsonRpc)) *JsonRpc {
rpc := &JsonRpc{
url: url,
client: http.DefaultClient,
log: log.New(os.Stderr, "", log.LstdFlags),
}
for _, option := range options {
option(rpc)
}
return rpc
}
// Call returns raw response of method call
func (rpc *JsonRpc) Call(method string, params ...interface{}) (json.RawMessage, error) {
request := JsonRpcRequest{
ID: "1",
JSONRPC: "2.0",
Method: method,
Params: params,
}
body, err := json.Marshal(request)
if err != nil {
return nil, err
}
response, err := rpc.client.Post(rpc.url, "application/json", bytes.NewBuffer(body))
if response != nil {
defer response.Body.Close()
}
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
if rpc.Debug {
rpc.log.Println(fmt.Sprintf("%s\nRequest: %s\nResponse: %s\n", method, body, data))
}
resp := new(JsonRpcResponse)
if err := json.Unmarshal(data, resp); err != nil {
return nil, err
}
if resp.Error != nil {
return nil, *resp.Error
}
return resp.Result, nil
}

21
main.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"log"
)
func main() {
c := JsonRpcClient("https://api.steemit.com", func(x *JsonRpc) { x.Debug = true })
r, err := c.GetVirtualOpsInBlock(20000000)
//r, err := c.GetOpsInBlock(1)
if err != nil {
log.Fatal(err)
}
spew.Dump(r)
fmt.Println(r)
}

34
steemitapi.go Normal file
View File

@ -0,0 +1,34 @@
package main
import "encoding/json"
import "github.com/joeshaw/iso8601"
type UnparsedSteemVirtualOp struct {
}
type SteemVirtualTransaction struct {
TxID string `json:"trx_id"`
BlockNum uint64 `json:"block"`
TxInBlock uint64 `json:"trx_in_block"`
OpInTx int `json:"op_in_trx"`
IsVirtual int `json:"virtual_op"`
Timestamp iso8601.Time `json:"timestamp"`
Op *[]json.RawMessage `json:"op"`
}
func (rpc *JsonRpc) GetVirtualOpsInBlock(blockNum int) ([]*SteemVirtualTransaction, error) {
raw, err1 := rpc.Call("condenser_api.get_ops_in_block", blockNum, true)
if err1 != nil {
return nil, err1
}
var result []*SteemVirtualTransaction
//var result []interface{}
err2 := json.Unmarshal(raw, &result)
return result, err2
}
func (rpc *JsonRpc) GetOpsInBlock(blockNum int) (json.RawMessage, error) {
r, err := rpc.Call("condenser_api.get_ops_in_block", blockNum, false)
return r, err
}