Skip to main content

avalanche_types/wallet/x/
mod.rs

1pub mod export;
2pub mod import;
3pub mod transfer;
4
5use crate::{errors::Result, jsonrpc::client::x as client_x, key, txs, wallet};
6
7impl<T> wallet::Wallet<T>
8where
9    T: key::secp256k1::ReadOnly + key::secp256k1::SignOnly + Clone,
10{
11    #[must_use]
12    pub fn x(&self) -> X<T> {
13        X {
14            inner: self.clone(),
15        }
16    }
17}
18
19#[derive(Clone, Debug)]
20pub struct X<T>
21where
22    T: key::secp256k1::ReadOnly + key::secp256k1::SignOnly + Clone,
23{
24    pub inner: crate::wallet::Wallet<T>,
25}
26
27impl<T> X<T>
28where
29    T: key::secp256k1::ReadOnly + key::secp256k1::SignOnly + Clone,
30{
31    /// Fetches the current balance of the wallet owner from the specified HTTP endpoint.
32    pub async fn balance_with_endpoint(&self, http_rpc: &str) -> Result<u64> {
33        let resp = client_x::get_balance(http_rpc, &self.inner.x_address).await?;
34        let cur_balance = resp
35            .result
36            .expect("unexpected None GetBalanceResult")
37            .balance;
38        Ok(cur_balance)
39    }
40
41    /// Fetches the current balance of the wallet owner from all endpoints
42    /// in the same order of "self.http_rpcs".
43    pub async fn balances(&self) -> Result<Vec<u64>> {
44        let mut balances = Vec::new();
45        for http_rpc in self.inner.base_http_urls.iter() {
46            let balance = self.balance_with_endpoint(http_rpc).await?;
47            balances.push(balance);
48        }
49        Ok(balances)
50    }
51
52    /// Fetches the current balance of the wallet owner.
53    pub async fn balance(&self) -> Result<u64> {
54        self.balance_with_endpoint(&self.inner.pick_base_http_url().1)
55            .await
56    }
57
58    /// Fetches UTXOs for "X" chain.
59    /// TODO: cache this like avalanchego
60    pub async fn utxos(&self) -> Result<Vec<txs::utxo::Utxo>> {
61        // ref. https://github.com/ava-labs/avalanchego/blob/v1.7.9/wallet/chain/p/builder.go
62        // ref. https://github.com/ava-labs/avalanchego/blob/v1.7.9/vms/platformvm/add_validator_tx.go#L263
63        // ref. https://github.com/ava-labs/avalanchego/blob/v1.7.9/vms/platformvm/spend.go#L39 "stake"
64        // ref. https://github.com/ava-labs/subnet-cli/blob/6bbe9f4aff353b812822af99c08133af35dbc6bd/client/p.go#L355 "AddValidator"
65        // ref. https://github.com/ava-labs/subnet-cli/blob/6bbe9f4aff353b812822af99c08133af35dbc6bd/client/p.go#L614 "stake"
66        let resp =
67            client_x::get_utxos(&self.inner.pick_base_http_url().1, &self.inner.p_address).await?;
68        let utxos = resp
69            .result
70            .expect("unexpected None GetUtxosResult")
71            .utxos
72            .expect("unexpected None Utxos");
73        Ok(utxos)
74    }
75
76    #[must_use]
77    pub fn transfer(&self) -> transfer::Tx<T> {
78        transfer::Tx::new(self)
79    }
80
81    #[must_use]
82    pub fn export(&self) -> export::Tx<T> {
83        export::Tx::new(self)
84    }
85
86    #[must_use]
87    pub fn import(&self) -> import::Tx<T> {
88        import::Tx::new(self)
89    }
90}