avalanche-sdk 0.93.0

Avalanche API/SDK
Documentation
pub mod transfer;

use std::io;

use crate::eth as api_eth;
use avalanche_types::key;
use num_bigint::BigInt;

#[derive(Clone, Debug)]
pub struct Eth<T>
where
    T: key::secp256k1::ReadOnly + key::secp256k1::SignOnly + Clone,
{
    pub inner: crate::wallet::Wallet<T>,

    /// Either "C" or subnet_evm chain Id.
    pub chain_id_alias: String,
    pub chain_rpc_url_path: String,
}

impl<T> Eth<T>
where
    T: key::secp256k1::ReadOnly + key::secp256k1::SignOnly + Clone,
{
    /// Fetches the current balance of the wallet owner from the specified HTTP endpoint.
    pub async fn balance_with_endpoint(&self, http_rpc: &str) -> io::Result<BigInt> {
        let resp =
            api_eth::get_balance(http_rpc, &self.chain_id_alias, &self.inner.x_address).await?;
        let cur_balance = resp.result;
        Ok(cur_balance)
    }

    /// Fetches the current balance of the wallet owner from all endpoints
    /// in the same order of "self.http_rpcs".
    pub async fn balances(&self) -> io::Result<Vec<BigInt>> {
        let mut balances = Vec::new();
        for http_rpc in self.inner.http_rpcs.iter() {
            let balance = self.balance_with_endpoint(http_rpc).await?;
            balances.push(balance);
        }
        Ok(balances)
    }

    /// Fetches the current balance of the wallet owner.
    pub async fn balance(&self) -> io::Result<BigInt> {
        self.balance_with_endpoint(&self.inner.pick_http_rpc().1)
            .await
    }

    #[must_use]
    pub fn transfer(&self) -> transfer::Tx<T> {
        transfer::Tx::new(self)
    }
}