Json-RPC操作Ethereum 節點
摘要:
準備工作
本地搭建以太坊私鏈,使用POA共識。節點開啟IPC。
功能描述
試過一些rpc包和go-ethereum 的rpc包,都不是很好用。有些命令好使,有些就報錯。沒有去深究這些rpc包生成的json有什麼異常。直接自己實現。實現其實很簡單,而且用起來...
準備工作
- 本地搭建以太坊私鏈,使用POA共識。節點開啟IPC。
功能描述
- 試過一些rpc包和go-ethereum 的rpc包,都不是很好用。有些命令好使,有些就報錯。沒有去深究這些rpc包生成的json有什麼異常。直接自己實現。實現其實很簡單,而且用起來也更靠譜。
上碼
package eth import ( "bytes" "io/ioutil" "net/http" ) type ethRequest struct { general Method string `json:"method"` Params []interface{} `json:"params"` } type general struct { JsonRPC string `json:"jsonrpc"` ID int `json:"id"` } //生成ethRequest,Marshal 成[]byte ,傳入do函式即可操作ethereum 節點 func do(jsonParam []byte)([]byte,error){ reader := bytes.NewReader(jsonParam) url := "http://127.0.0.1:8545" request, err := http.NewRequest("POST", url, reader) if err != nil { return []byte(""),err } request.Header.Set("Content-Type", "application/json;charset=UTF-8") client := http.Client{} resp, err := client.Do(request) if err != nil { return []byte(""),err } respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return []byte(""),err } return respBytes,nil }
其他說明
- 瞭解更多以太坊管理命令所需要的引數。Here:https://github.com/ethereum/g...