From be110a6ca993232304a33d5e6d9b5d36c655de49 Mon Sep 17 00:00:00 2001 From: yutianwu Date: Mon, 23 Oct 2023 17:01:10 +0800 Subject: [PATCH] feat: add support for some json rpc queries --- go.mod | 1 + go.sum | 2 +- rpc/jsonrpc/types/types.go | 36 +++++++++++++++++++++++++++++------- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 26a6fd7a0..e6ac9b4c5 100644 --- a/go.mod +++ b/go.mod @@ -315,4 +315,5 @@ replace ( github.com/cometbft/cometbft-db => github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1 // to be compatible with rocksdb v6.22.1 github.com/linxGnu/grocksdb => github.com/linxGnu/grocksdb v1.6.22 + github.com/wercker/journalhook => github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117 ) diff --git a/go.sum b/go.sum index f48337f73..5f14da950 100644 --- a/go.sum +++ b/go.sum @@ -1629,7 +1629,7 @@ github.com/wealdtech/go-eth2-types/v2 v2.5.2/go.mod h1:8lkNUbgklSQ4LZ2oMSuxSdR7W github.com/wealdtech/go-eth2-util v1.6.3/go.mod h1:0hFMj/qtio288oZFHmAbCnPQ9OB3c4WFzs5NVPKTY4k= github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4 v1.1.3/go.mod h1:qiIimacW5NhVRy8o+YxWo9YrecXqDAKKbL0+sOa0SJ4= github.com/wealdtech/go-eth2-wallet-types/v2 v2.8.2/go.mod h1:k6kmiKWSWBTd4OxFifTEkPaBLhZspnO2KFD5XJY9nqg= -github.com/wercker/journalhook v0.0.0-20180428041537-5d0a5ae867b3/go.mod h1:XCsSkdKK4gwBMNrOCZWww0pX6AOt+2gYc5Z6jBRrNVg= +github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117/go.mod h1:XCsSkdKK4gwBMNrOCZWww0pX6AOt+2gYc5Z6jBRrNVg= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= diff --git a/rpc/jsonrpc/types/types.go b/rpc/jsonrpc/types/types.go index 85b9d80f5..2556dd35b 100644 --- a/rpc/jsonrpc/types/types.go +++ b/rpc/jsonrpc/types/types.go @@ -20,12 +20,18 @@ import ( // Supported EVM json-rpc requests const ( - EthBlockNumber = "eth_blockNumber" - EthGetBlockByNumber = "eth_getBlockByNumber" - EthGetBalance = "eth_getBalance" - EthChainID = "eth_chainId" - NetVersion = "net_version" - EthNetworkID = "eth_networkId" + EthBlockNumber = "eth_blockNumber" + EthGetBlockByNumber = "eth_getBlockByNumber" + EthGetBalance = "eth_getBalance" + EthChainID = "eth_chainId" + NetVersion = "net_version" + EthNetworkID = "eth_networkId" + EthGetCode = "eth_getCode" + EthGasPrice = "eth_gasPrice" + EthEstimateGas = "eth_estimateGas" + EthCall = "eth_call" + EthGetTransactionCount = "eth_getTransactionCount" + EthSendRawTransaction = "eth_sendRawTransaction" ) var SupportedEthQueryRequests = []string{ @@ -35,6 +41,12 @@ var SupportedEthQueryRequests = []string{ EthChainID, NetVersion, EthNetworkID, + EthGetCode, + EthGasPrice, + EthEstimateGas, + EthCall, + EthGetTransactionCount, + EthSendRawTransaction, } // a wrapper to emulate a sum type: jsonrpcid = string | int @@ -269,7 +281,17 @@ func NewEthRPCSuccessResponse(id jsonrpcid, res interface{}, method string) RPCR if err != nil { return RPCInternalError(id, fmt.Errorf("error decode response: %w", err)) } - // return int string for net_version + //return hex string for eth_chainId + case EthGasPrice, EthCall, EthGetCode, EthGetTransactionCount, EthEstimateGas: + // metamask do not support the Hex signed 2's complement, need to trim the prefix `0` + resultStr := strings.TrimLeft(hex.EncodeToString(bz), "0") + result, err = json.Marshal("0x" + resultStr) + if err != nil { + return RPCInternalError(id, fmt.Errorf("error decode response: %w", err)) + } + case EthSendRawTransaction: + return RPCInvalidRequestError(id, fmt.Errorf("Transfer BNB through EVM wallet on Greenfield is not available yet, please go to decellar.io or refer to latest docs.\n")) + //return int string for net_version case NetVersion: hexStr := hex.EncodeToString(bz) netVersion, err := strconv.ParseInt(hexStr, 16, 64)