avalanche-sdk 0.73.1

Avalanche API/SDK
Documentation
pub mod transfer;

use std::io::{self, Error, ErrorKind};

use avalanche_types::key;
use ethers::prelude::*;
use ethers_providers::Middleware;

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

impl<T> C<T>
where
    T: key::secp256k1::ReadOnly + key::secp256k1::SignOnly + Clone,
{
    /// Fetches the current native token's balance of the wallet owner from the specified HTTP endpoint.
    pub async fn balance_with_endpoint_index(&self, idx: usize) -> io::Result<U256> {
        self.inner.c_providers[idx]
            .get_balance(self.inner.h160_address, None)
            .await
            .map_err(|e| Error::new(ErrorKind::Other, format!("failed to get_balance '{}'", e)))
    }

    /// Fetches the current native token's balance of the wallet owner from all endpoints
    /// in the same order of "self.http_rpcs".
    pub async fn balances(&self) -> io::Result<Vec<U256>> {
        let mut balances = Vec::new();
        for tuple in self.inner.http_rpcs.iter().enumerate() {
            let balance = self.balance_with_endpoint_index(tuple.0).await?;
            balances.push(balance);
        }
        Ok(balances)
    }

    /// Fetches the current native token's balance of the wallet owner.
    pub async fn balance(&self) -> io::Result<U256> {
        self.balance_with_endpoint_index(self.inner.pick_http_rpc().0)
            .await
    }

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