mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
add getbalance rpc call
This commit is contained in:
parent
953d405330
commit
196a0a91a5
1 changed files with 28 additions and 2 deletions
|
|
@ -9,6 +9,9 @@ import (
|
||||||
"github.com/ShyftNetwork/go-empyrean/crypto"
|
"github.com/ShyftNetwork/go-empyrean/crypto"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -22,6 +25,8 @@ const (
|
||||||
var testAddrHex = "14791697260E4c9A71f18484C9f997B308e59325"
|
var testAddrHex = "14791697260E4c9A71f18484C9f997B308e59325"
|
||||||
var testPrivHex = "0123456789012345678901234567890123456789012345678901234567890123"
|
var testPrivHex = "0123456789012345678901234567890123456789012345678901234567890123"
|
||||||
|
|
||||||
|
var client = &http.Client{}
|
||||||
|
|
||||||
// This gives context to the signed message and prevents signing of transactions.
|
// This gives context to the signed message and prevents signing of transactions.
|
||||||
func signHash(data []byte) []byte {
|
func signHash(data []byte) []byte {
|
||||||
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
|
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
|
||||||
|
|
@ -29,7 +34,6 @@ func signHash(data []byte) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error listening:", err.Error())
|
fmt.Println("Error listening:", err.Error())
|
||||||
|
|
@ -139,7 +143,8 @@ func readerConn(conn net.Conn, channel chan []byte) {
|
||||||
|
|
||||||
func checkBalance(checkBalanceChan chan []byte) {
|
func checkBalance(checkBalanceChan chan []byte) {
|
||||||
address := <-checkBalanceChan
|
address := <-checkBalanceChan
|
||||||
fmt.Println("The address for balance check is ", address)
|
bal := getBalance(string(address[:]), client)
|
||||||
|
fmt.Println("The balance for address ", address, " is ", bal)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendRingSignedMsg(conn net.Conn){
|
func sendRingSignedMsg(conn net.Conn){
|
||||||
|
|
@ -162,3 +167,24 @@ func sendRingSignedMsg(conn net.Conn){
|
||||||
conn.Write(new_sig)
|
conn.Write(new_sig)
|
||||||
conn.Write([]byte("\n"))
|
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)
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue