Skip to content

Commit

Permalink
feat: add support for some json rpc queries
Browse files Browse the repository at this point in the history
  • Loading branch information
yutianwu committed Oct 23, 2023
1 parent 749e6cf commit be110a6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
36 changes: 29 additions & 7 deletions rpc/jsonrpc/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand Down Expand Up @@ -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"))

Check warning on line 293 in rpc/jsonrpc/types/types.go

View workflow job for this annotation

GitHub Actions / golangci-lint

error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
//return int string for net_version
case NetVersion:
hexStr := hex.EncodeToString(bz)
netVersion, err := strconv.ParseInt(hexStr, 16, 64)
Expand Down

0 comments on commit be110a6

Please sign in to comment.