From 48486e2bb202216390a812718ad2d69dffd7be76 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Tue, 31 Jul 2018 15:13:00 -0400 Subject: [PATCH] refactor to use ethclient --- shyftRingWalletConn/app.go | 41 ++++++++++++++------------------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 93ccd26b86..fe0daf40e9 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -10,9 +10,10 @@ import ( "io" "net" "net/http" - "bytes" - "encoding/json" "os" + "github.com/ShyftNetwork/go-empyrean/ethclient" + "github.com/ShyftNetwork/go-empyrean/common" + "context" ) const ( @@ -143,11 +144,20 @@ func readerConn(conn net.Conn, channel chan []byte) { func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { address := <-checkBalanceChan - bal := getBalance(string(address[:]), client) - fmt.Println("The balance for address ", address, " is ", bal) + c, err := ethclient.Dial("http://127.0.0.1:8545") + if err != nil { + fmt.Println("Eth Client not initialized: " , err) + } + + balance, error := c.BalanceAt(context.Background(), common.HexToAddress(string(address[:])),nil) + if error != nil { + fmt.Println("Balance at error ", error) + } + fmt.Println("the bal is ", balance) + fmt.Println("The balance for address ", address, " is ", balance) conn.Write([]byte("Broadcasting Balance")) conn.Write([]byte("\n")) - conn.Write([]byte(bal)) + conn.Write([]byte(balance.String())) conn.Write([]byte("\n")) } @@ -170,25 +180,4 @@ func sendRingSignedMsg(conn net.Conn){ conn.Write([]byte("\n")) conn.Write(new_sig) conn.Write([]byte("\n")) -} - -// http request to the rpc server to get balance for an address -func getBalance(address string, client *http.Client) string { - jsonStr := fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_getBalance","params":["%s", "latest"],"id":67}` , address) - jsonBytes := []byte(jsonStr) - fmt.Println(string(jsonBytes)) - - req, err := http.NewRequest("POST", "http://localhost:8545", bytes.NewBuffer(jsonBytes)) - if err != nil { - fmt.Println("Error: " , err) - } - req.Header.Set("Content-Type", "application/json") - resp, err := client.Do(req) - if err != nil { - fmt.Println("Error: ", err) - } - var target map[string]interface{} - decoder := json.NewDecoder(resp.Body) - decoder.Decode(&target) - return target["result"].(string) } \ No newline at end of file