Skip to content

Commit

Permalink
Add Eth RPC support into defi-cli (#2282)
Browse files Browse the repository at this point in the history
* Add eth RPC support into defi-cli

* Fix ethlibs script
  • Loading branch information
sieniven authored Aug 4, 2023
1 parent 5bd8d42 commit c84812f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
35 changes: 3 additions & 32 deletions ci/ethlibs_test/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ setup_vars() {
start_node() {
$DEFID_BIN -regtest \
-debug=rpc \
-daemon \
-printtoconsole \
-rpcallowip=0.0.0.0/0 \
-rpcbind=0.0.0.0 \
Expand Down Expand Up @@ -83,42 +84,12 @@ setup_fixtures() {
$DEFI_CLI_BIN -regtest transferdomain '[{"src":{"address":"'"$OWNERAUTHADDR"'", "amount":"200@DFI", "domain":2}, "dst":{"address":"'"$ALICE"'", "amount":"200@DFI", "domain":3}}]'
$DEFI_CLI_BIN -regtest generatetoaddress 1 "$OWNERAUTHADDR"

curl http://localhost:19551 \
-H 'content-type:application/json' \
--data-binary \
'{
"jsonrpc":"2.0",
"id":"fixture",
"method":"eth_sendTransaction",
"params":[{
"from":"'"$ALICE"'",
"data":"'"$CONTRACT_COUNTER"'",
"value":"0x00",
"gas":"0x7a120",
"gasPrice": "0x22ecb25c00"
}]
}'

$DEFI_CLI_BIN -regtest eth_sendTransaction '{"from":"'"$ALICE"'", "data":"'"$CONTRACT_COUNTER"'", "value":"'"0x00"'", "gas":"'"0x7a120"'", "gasPrice": "'"0x22ecb25c00"'"}'
$DEFI_CLI_BIN -regtest generatetoaddress 1 "$OWNERAUTHADDR"
# contract address
# 0x966aaec51a95a737d086d21f015a6991dd5559ae

curl http://localhost:19551 \
-H 'content-type:application/json' \
--data-binary \
'{
"jsonrpc":"2.0",
"id":"fixture",
"method":"eth_sendTransaction",
"params":[{
"from":"'"$ALICE"'",
"data":"'"$CONTRACT_COUNTERCALLER"'",
"value":"0x00",
"gas":"0x7a120",
"gasPrice": "0x22ecb25c00"
}]
}'

$DEFI_CLI_BIN -regtest eth_sendTransaction '{"from":"'"$ALICE"'", "data":"'"$CONTRACT_COUNTERCALLER"'", "value":"'"0x00"'", "gas":"'"0x7a120"'", "gasPrice": "'"0x22ecb25c00"'"}'
$DEFI_CLI_BIN -regtest generatetoaddress 1 "$OWNERAUTHADDR"
# contract address
# 0x007138e9d5bdb3f0b7f3abf2d46ad4f9184ef99d
Expand Down
22 changes: 18 additions & 4 deletions src/defi-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ static void SetupCliArgs()
gArgs.AddArg("-rpcwallet=<walletname>", "Send RPC for non-default wallet on RPC server (needs to exactly match corresponding -wallet option passed to defid). This changes the RPC endpoint used, e.g. http://127.0.0.1:8554/wallet/<walletname>", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-stdin", "Read extra arguments from standard input, one per line until EOF/Ctrl-D (recommended for sensitive information such as passphrases). When combined with -stdinrpcpass, the first line from standard input is used for the RPC password.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-stdinrpcpass", "Read RPC password from standard input as a single line. When combined with -stdin, the first line from standard input is used for the RPC password.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-grpcport=<port>", strprintf("Start GRPC connections on <port> and <port + 1> (default: %u, testnet: %u, changi: %u, devnet: %u, regtest: %u)", defaultBaseParams->GRPCPort(), testnetBaseParams->GRPCPort(), changiBaseParams->GRPCPort(), devnetBaseParams->GRPCPort(), regtestBaseParams->GRPCPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC);
gArgs.AddArg("-ethrpcport=<port>", strprintf("Listen for ETH-JSON-RPC connections on <port>> (default: %u, testnet: %u, changi: %u, devnet: %u, regtest: %u)", defaultBaseParams->ETHRPCPort(), testnetBaseParams->ETHRPCPort(), changiBaseParams->ETHRPCPort(), devnetBaseParams->ETHRPCPort(), regtestBaseParams->ETHRPCPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC);
RPCMetadata::SetupArgs(gArgs);
}

Expand Down Expand Up @@ -307,13 +309,25 @@ class DefaultRequestHandler: public BaseRequestHandler {
static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, const std::vector<std::string>& args)
{
std::string host;
// In preference order, we choose the following for the port:
// Specify a specific host IP, using -rpcconnect.
// For DVM RPCs, in preference order, we choose the following for the port:
// 1. -rpcport
// 2. port in -rpcconnect (ie following : in ipv4 or ]: in ipv6)
// 3. default port for chain
int port = BaseParams().RPCPort();
SplitHostPort(gArgs.GetArg("-rpcconnect", DEFAULT_RPCCONNECT), port, host);
port = gArgs.GetArg("-rpcport", port);
int dvmport = BaseParams().RPCPort();
SplitHostPort(gArgs.GetArg("-rpcconnect", DEFAULT_RPCCONNECT), dvmport, host);
dvmport = gArgs.GetArg("-rpcport", dvmport);

// For EVM RPCs, in preference order, we choose the following for the evm port:
// 1. -ethrpcport
// 2. default evm port for chain
int evmport = BaseParams().ETHRPCPort();
evmport = gArgs.GetArg("-ethrpcport", evmport);

// Check if DVM or EVM RPC
int port = dvmport;
if (strMethod.rfind("eth_", 0 || strMethod == "net_version") == 0)
port = evmport;

// Obtain event base
raii_event_base base = obtain_event_base();
Expand Down
35 changes: 35 additions & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,41 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "vmmap", 1, "type"},
{ "logvmmaps", 0, "type"},
{ "addressmap", 1, "type"},

{ "eth_getBlockByHash", 1, "type"},
{ "eth_getBlockByNumber", 0, "tag"},
{ "eth_getBlockByNumber", 1, "type"},
{ "eth_getBlockTransactionCountByNumber", 0, "blockNumber"},
{ "eth_getUncleCountByBlockNumber", 0, "blockNumber"},

{ "eth_call", 0, "tx"},
{ "eth_call", 1, "tag"},
{ "eth_estimateGas", 0, "tx"},
{ "eth_estimateGas", 1, "tag"},
{ "eth_createAccessList", 0, "tx"},
{ "eth_createAccessList", 1, "tag"},

{ "eth_newFilter", 0, "filter"},
{ "eth_uninstallFilter", 0, "filterId"},
{ "eth_getFilterChanges", 0, "filterId"},
{ "eth_getFilterLogs", 0, "filterId"},
{ "eth_getLogs", 0, "filter"},

{ "eth_sign", 1, "data"},
{ "eth_signTransaction", 0, "tx"},

{ "eth_getBalance", 1, "tag"},
{ "eth_getStorageAt", 1, "storagePos"},
{ "eth_getStorageAt", 2, "tag"},
{ "eth_getTransactionCount", 1, "tag"},
{ "eth_getCode", 1, "tag"},
{ "eth_getProof", 1, "storagePos"},
{ "eth_getProof", 2, "tag"},

{ "eth_sendTransaction", 0, "tx"},
{ "eth_getTransactionByBlockHashAndIndex", 1, "txIndex"},
{ "eth_getTransactionByBlockNumberAndIndex", 0, "tag"},
{ "eth_getTransactionByBlockNumberAndIndex", 1, "txIndex"},
};
// clang-format on

Expand Down

0 comments on commit c84812f

Please sign in to comment.