EVM RPC: Why is there no eth_getBalance endpoint?

Typically, you can execute eth_getBalance on EVM RPCs to get the balance for a wallet. Currently, there is no such endpoint in ICP’s RPC canister.

Is there a reason why?

Not all RPC methods are exposed as direct endpoints. You can use the request endpoint for such a RPC call, see Using the EVM RPC canister | Internet Computer

If you’re developing in rust, check out Announcing ic-alloy - ICP signers and providers for the Ethereum support library Alloy

3 Likes

Getting the balance of an ETH account using the Alloy support libraries is as simple as:

#[ic_cdk::update]
async fn get_balance(address: Option<String>) -> Result<String, String> {
    let address = match address {
        Some(val) => val,
        None => {
            let signer = create_icp_sepolia_signer().await;
            signer.address().to_string()
        }
    };
    let address = address.parse::<Address>().map_err(|e| e.to_string())?;
    let rpc_service = get_rpc_service_sepolia();
    let config = IcpConfig::new(rpc_service);
    let provider = ProviderBuilder::new().on_icp(config);
    let result = provider.get_balance(address).await;

    match result {
        Ok(balance) => Ok(balance.to_string()),
        Err(e) => Err(e.to_string()),
    }
}
4 Likes