Skip to content

Commit bcf2e9a

Browse files
committed
eth_call
1 parent e897bdc commit bcf2e9a

2 files changed

Lines changed: 79 additions & 3 deletions

File tree

src/ethereum-json-rpc-client/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::pin::Pin;
44
use anyhow::Context;
55
use did::transaction::StorableExecutionResult;
66
use ethers_core::types::{
7-
Block, BlockNumber, Log, Transaction, TransactionReceipt, H160, H256, U256, U64,
7+
Block, BlockNumber, Log, Transaction, TransactionReceipt, TransactionRequest, H160, H256, U256,
8+
U64,
89
};
910
use itertools::Itertools;
1011
use jsonrpc_core::{Call, Id, MethodCall, Output, Params, Request, Response, Version};
@@ -27,6 +28,7 @@ const ETH_GET_TRANSACTION_COUNT_METHOD: &str = "eth_getTransactionCount";
2728
const ETH_GET_BLOCK_BY_NUMBER_METHOD: &str = "eth_getBlockByNumber";
2829
const ETH_BLOCK_NUMBER_METHOD: &str = "eth_blockNumber";
2930
const ETH_GET_TRANSACTION_RECEIPT_METHOD: &str = "eth_getTransactionReceipt";
31+
const ETH_CALL_METHOD: &str = "eth_call";
3032
const ETH_SEND_RAW_TRANSACTION_METHOD: &str = "eth_sendRawTransaction";
3133
const ETH_GET_LOGS_METHOD: &str = "eth_getLogs";
3234
const IC_GET_TX_EXECUTION_RESULT_BY_HASH_METHOD: &str = "ic_getExeResultByHash";
@@ -186,12 +188,26 @@ impl<C: Client> EthJsonRpcClient<C> {
186188
.map(|v| v.as_u64())
187189
}
188190

191+
/// Performs eth call and return the result.
192+
pub async fn eth_call(
193+
&self,
194+
params: TransactionRequest,
195+
block: BlockNumber,
196+
) -> anyhow::Result<String> {
197+
self.single_request(
198+
ETH_CALL_METHOD.to_string(),
199+
make_params_array!(params, block),
200+
Id::Str("eth_call".to_string()),
201+
)
202+
.await
203+
}
204+
189205
/// Sends raw transaction and returns transaction hash
190206
pub async fn send_raw_transaction(&self, transaction: Transaction) -> anyhow::Result<H256> {
191207
let bytes = transaction.rlp();
192208
let transaction = format!("0x{}", hex::encode(bytes));
193209

194-
self.single_request::<H256>(
210+
self.single_request(
195211
ETH_SEND_RAW_TRANSACTION_METHOD.to_string(),
196212
make_params_array!(transaction),
197213
Id::Str("send_rawTransaction".to_string()),

src/ethereum-json-rpc-client/tests/reqwest/mod.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use ethereum_json_rpc_client::reqwest::ReqwestClient;
22
use ethereum_json_rpc_client::{EthGetLogsParams, EthJsonRpcClient};
3-
use ethers_core::types::{BlockNumber, Log, H256};
3+
use ethers_core::abi::{Function, Param, ParamType, StateMutability, Token};
4+
use ethers_core::types::{BlockNumber, Log, TransactionRequest, H160, H256};
45

56
const ETHEREUM_JSON_API_URL: &str = "https://cloudflare-eth.com/";
67
const MAX_BATCH_SIZE: usize = 5;
@@ -47,6 +48,65 @@ async fn should_get_code() {
4748
assert_eq!(result, ERC_1820_EXPECTED_CODE);
4849
}
4950

51+
/// Calls the funtction of ERC-1820:
52+
///
53+
///```solidity
54+
/// function getManager(address _addr) public view returns(address)
55+
///```
56+
#[tokio::test]
57+
async fn should_perform_eth_call() {
58+
let erc_1820_address = "0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24"
59+
.parse::<H160>()
60+
.unwrap();
61+
62+
let caller = "0xf990077c3205cbDf861e17Fa532eeB069cE9fF96"
63+
.parse()
64+
.unwrap();
65+
66+
#[allow(deprecated)]
67+
let func = Function {
68+
name: "getManager".to_string(),
69+
inputs: vec![Param {
70+
name: "getManager".to_string(),
71+
kind: ParamType::Address,
72+
internal_type: None,
73+
}],
74+
outputs: vec![Param {
75+
name: "".to_string(),
76+
kind: ParamType::Address,
77+
internal_type: None,
78+
}],
79+
constant: None,
80+
state_mutability: StateMutability::View,
81+
};
82+
83+
let params = TransactionRequest {
84+
from: Some(caller),
85+
to: Some(erc_1820_address.into()),
86+
gas: Some(1000000u64.into()),
87+
gas_price: None,
88+
value: None,
89+
data: Some(func.encode_input(&[Token::Address(caller)]).unwrap().into()),
90+
..Default::default()
91+
};
92+
93+
let result = reqwest_client()
94+
.eth_call(params, BlockNumber::Latest)
95+
.await
96+
.unwrap();
97+
98+
let result_address = func
99+
.decode_output(&hex::decode(result.trim_start_matches("0x")).unwrap())
100+
.unwrap()
101+
.first()
102+
.cloned()
103+
.unwrap()
104+
.into_address()
105+
.unwrap();
106+
107+
assert_eq!(result_address, caller);
108+
}
109+
50110
#[tokio::test]
51111
async fn should_get_transaction_count() {
52112
let erc_1820_deployer_address = "0xa990077c3205cbDf861e17Fa532eeB069cE9fF96"

0 commit comments

Comments
 (0)