RPC Client
ic-alloy
makes some additions to the default rpc-client
provided by Alloy:
- Adds the
icp()
function to theClientBuilder
, a convenience function to create a newRpcClient
with anIcpTransport
using the givenIcpConfig
details. - Adapts the
new_batch()
function to supportIcpTransport
for batch requests. - Adds the
IcpPollerBuilder
used for watching for logs, transactions and blocks. The ICP poller relies on IC timers for continuously listening to events. - Adds the
IcpClient
type that maps toIcpTransport
pub type IcpClient = RpcClient<alloy_transport_icp::IcpTransport>;
Example: Batch getting balances
rust
#[ic_cdk::update]
async fn get_batch_balances(addresses: Vec<String>) -> Result<String, String> {
let rpc_service = get_rpc_service_sepolia();
let config = IcpConfig::new(rpc_service);
let client: IcpClient = ClientBuilder::default().icp(config);
let mut batch = client.new_batch();
let tag = BlockNumberOrTag::Latest;
let mut get_balance_calls = Vec::new();
for address in addresses {
let address = match address.parse::<Address>() {
Ok(addr) => addr,
Err(e) => return Err(e.to_string()),
};
let call = batch.add_call("eth_getBalance", &(address, tag)).unwrap();
get_balance_calls.push(call);
}
batch.send().await.unwrap();
let mut balances = Vec::new();
for call in get_balance_calls {
let balance: U256 = call.await.unwrap();
balances.push(balance.to_string());
}
Ok(balances.join(","))
}
Request the balances of multiple ETH accounts in one batch. Batch requests can contain different types of requests.