Error in fetching balance for ERC20 contracts

I am trying to fetch balance of an address for an ERC20 contract using ic-alloy:

#[update(name = "getNetworkTVL")]
async fn get_network_tvl(new_network_rpc: String, chain_id: Nat) -> Result<Nat, String> {
    let (network, pool_contract_address) = STATE.with(|state| {
        let state = state.borrow();

        let network = match state.supported_networks.get(&chain_id) {
            Some(network) => network,
            None => panic!("Network with chain_id {chain_id} not found"),
        };

        let pool_contract_address = match Address::from_str(&state.icp_contract_address) {
            Ok(address) => address,
            Err(_) => panic!("Error parsing pool contract address"),
        };

        (network.clone(), pool_contract_address)
    });

    let mut total_tvl: Nat = Nat::from(0u64);

    for asset in &network.supported_assets {
        let rpc_service = generate_rpc_service(new_network_rpc.clone());
        let config = IcpConfig::new(rpc_service);
        let provider = ProviderBuilder::new().on_icp(config);
        ic_cdk::println!("Asset: {}", asset);
        ic_cdk::println!("Network: {:?}", network);
        let contract = ERC20Token::new(Address::from_str(asset).unwrap(), provider.clone());
        let result = contract.balanceOf(pool_contract_address).call().await;

        ic_cdk::println!("Result: {:?}", result);

        let balance = match result {
            Ok(value) => value.balance,
            Err(e) => return Err(format!("Invalid balance type:{}", e)),
        };

        let balance_as_nat = Nat::from(
            balance.try_into().unwrap_or(u128::MAX), // Fallback to max u128 if conversion fails
        );

        total_tvl += balance_as_nat;
    }

    Ok(total_tvl)
}

and I run into this error:

Err =  "Invalid balance type:server returned an error response: error code 3: Canister 7hfb6-caaaa-aaaar-qadga-cai not found"

Which means that there is an error from the result from the contract call. Can you please let me know why this is.

Also not sure where the canister id 7hfb6-caaaa-aaaar-qadga-cai is gotten from as it is different from my programs canister ids.

1 Like