move rpc code to client.go

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-06-16 12:02:50 -04:00 committed by Jakub
parent 3565768230
commit dfb8d41a35
2 changed files with 35 additions and 9 deletions

26
client.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"github.com/ethereum/go-ethereum/rpc"
)
type client struct {
rpcClient *rpc.Client
}
func newClient(url string) (*client, error) {
rpcClient, err := rpc.Dial(url)
if err != nil {
return nil, err
}
return &client{rpcClient}, nil
}
func (c *client) getPeers() (interface{}, error) {
var rval interface{}
err := c.rpcClient.Call(&rval, "admin_peers")
if err != nil {
return nil, err
}
return rval, nil
}

18
main.go
View File

@ -3,27 +3,27 @@ package main
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/rpc"
"log"
)
type rcpResp map[string]interface{}
const serverAddr = "127.0.0.1"
const serverPort = 8545
const host = "127.0.0.1"
const port = 8545
func main() {
addr := fmt.Sprintf("http://%s:%d", serverAddr, serverPort)
c, err := rpc.DialHTTP(addr)
url := fmt.Sprintf("http://%s:%d", host, port)
fmt.Println("Type :%t", url)
c, err := newClient(url)
if err != nil {
log.Fatal("Failed to connec to RPC:", err)
}
fmt.Println("Connected: ", addr)
var rval interface{}
err = c.Call(&rval, "admin_peers")
fmt.Println("Connected: ", url)
peers, err := c.getPeers()
if err != nil {
log.Fatal("Failed to make RPC call:", err)
}
pretty, err := json.MarshalIndent(rval, "", " ")
pretty, err := json.MarshalIndent(peers, "", " ")
fmt.Println("Response: ", string(pretty))
}