MCPcopy Create free account
hub / github.com/DNAProject/DNA / sendRpcRequest

Function sendRpcRequest

cmd/utils/rpc.go:77–109  ·  view source on GitHub ↗
(method string, params []interface{})

Source from the content-addressed store, hash-verified

75}
76
77func sendRpcRequest(method string, params []interface{}) ([]byte, *RpcError) {
78 rpcReq := &JsonRpcRequest{
79 Version: JSON_RPC_VERSION,
80 Id: "cli",
81 Method: method,
82 Params: params,
83 }
84 data, err := json.Marshal(rpcReq)
85 if err != nil {
86 return nil, NewRpcError(fmt.Errorf("JsonRpcRequest json.Marshal error:%s", err))
87 }
88
89 addr := fmt.Sprintf("http://localhost:%d", config.DefConfig.Rpc.HttpJsonPort)
90 resp, err := http.Post(addr, "application/json", strings.NewReader(string(data)))
91 if err != nil {
92 return nil, NewRpcError(err)
93 }
94 defer resp.Body.Close()
95
96 body, err := ioutil.ReadAll(resp.Body)
97 if err != nil {
98 return nil, NewRpcError(fmt.Errorf("read rpc response body error:%s", err))
99 }
100 rpcRsp := &JsonRpcResponse{}
101 err = json.Unmarshal(body, rpcRsp)
102 if err != nil {
103 return nil, NewRpcError(fmt.Errorf("json.Unmarshal JsonRpcResponse:%s error:%s", body, err))
104 }
105 if rpcRsp.Error != 0 {
106 return nil, NewRpcError(fmt.Errorf("\n %s ", string(body)), rpcRsp.Error)
107 }
108 return rpcRsp.Result, nil
109}

Callers 12

GetBalanceFunction · 0.85
GetAllowanceFunction · 0.85
SendRawTransactionDataFunction · 0.85
GetSmartContractEventFunction · 0.85
GetRawTransactionFunction · 0.85
GetBlockFunction · 0.85
GetNetworkIdFunction · 0.85
GetBlockDataFunction · 0.85
GetBlockCountFunction · 0.85
GetTxHeightFunction · 0.85

Calls 5

NewRpcErrorFunction · 0.85
MarshalMethod · 0.80
ErrorfMethod · 0.80
PostMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected