Skip to main content

fedimint_walletv2_client/
api.rs

1use fedimint_api_client::api::{FederationApiExt, FederationResult, IModuleFederationApi};
2use fedimint_core::module::ApiRequestErased;
3use fedimint_core::task::{MaybeSend, MaybeSync};
4use fedimint_core::{OutPoint, apply, async_trait_maybe_send};
5use fedimint_walletv2_common::endpoint_constants::{
6    CONSENSUS_BLOCK_COUNT_ENDPOINT, CONSENSUS_FEERATE_ENDPOINT, DEPOSIT_RANGE_ENDPOINT,
7    FEDERATION_WALLET_ENDPOINT, PENDING_TRANSACTION_CHAIN_ENDPOINT, RECEIVE_FEE_ENDPOINT,
8    SEND_FEE_ENDPOINT, TRANSACTION_CHAIN_ENDPOINT, TRANSACTION_ID_ENDPOINT,
9};
10use fedimint_walletv2_common::{DepositRange, FederationWallet, TxInfo};
11
12#[apply(async_trait_maybe_send!)]
13pub trait WalletFederationApi {
14    async fn consensus_block_count(&self) -> FederationResult<u64>;
15
16    async fn consensus_feerate(&self) -> FederationResult<Option<u64>>;
17
18    async fn federation_wallet(&self) -> FederationResult<Option<FederationWallet>>;
19
20    async fn send_fee(&self) -> FederationResult<Option<bitcoin::Amount>>;
21
22    async fn receive_fee(&self) -> FederationResult<Option<bitcoin::Amount>>;
23
24    async fn pending_tx_chain(&self) -> FederationResult<Vec<TxInfo>>;
25
26    async fn tx_chain(&self, n: usize) -> FederationResult<Vec<TxInfo>>;
27
28    async fn deposit_range(
29        &self,
30        start_index: u64,
31        end_index: u64,
32    ) -> FederationResult<DepositRange>;
33
34    async fn tx_id(&self, outpoint: OutPoint) -> Option<bitcoin::Txid>;
35}
36
37#[apply(async_trait_maybe_send!)]
38impl<T: ?Sized> WalletFederationApi for T
39where
40    T: IModuleFederationApi + MaybeSend + MaybeSync + 'static,
41{
42    async fn consensus_block_count(&self) -> FederationResult<u64> {
43        self.request_current_consensus(
44            CONSENSUS_BLOCK_COUNT_ENDPOINT.to_string(),
45            ApiRequestErased::new(()),
46        )
47        .await
48    }
49
50    async fn consensus_feerate(&self) -> FederationResult<Option<u64>> {
51        self.request_current_consensus(
52            CONSENSUS_FEERATE_ENDPOINT.to_string(),
53            ApiRequestErased::new(()),
54        )
55        .await
56    }
57
58    async fn federation_wallet(&self) -> FederationResult<Option<FederationWallet>> {
59        self.request_current_consensus(
60            FEDERATION_WALLET_ENDPOINT.to_string(),
61            ApiRequestErased::new(()),
62        )
63        .await
64    }
65
66    async fn send_fee(&self) -> FederationResult<Option<bitcoin::Amount>> {
67        self.request_current_consensus(SEND_FEE_ENDPOINT.to_string(), ApiRequestErased::new(()))
68            .await
69    }
70
71    async fn receive_fee(&self) -> FederationResult<Option<bitcoin::Amount>> {
72        self.request_current_consensus(RECEIVE_FEE_ENDPOINT.to_string(), ApiRequestErased::new(()))
73            .await
74    }
75
76    async fn pending_tx_chain(&self) -> FederationResult<Vec<TxInfo>> {
77        self.request_current_consensus(
78            PENDING_TRANSACTION_CHAIN_ENDPOINT.to_string(),
79            ApiRequestErased::new(()),
80        )
81        .await
82    }
83
84    async fn tx_chain(&self, n: usize) -> FederationResult<Vec<TxInfo>> {
85        self.request_current_consensus(
86            TRANSACTION_CHAIN_ENDPOINT.to_string(),
87            ApiRequestErased::new(n),
88        )
89        .await
90    }
91
92    async fn deposit_range(
93        &self,
94        start_index: u64,
95        end_index: u64,
96    ) -> FederationResult<DepositRange> {
97        self.request_current_consensus(
98            DEPOSIT_RANGE_ENDPOINT.to_string(),
99            ApiRequestErased::new((start_index, end_index)),
100        )
101        .await
102    }
103
104    async fn tx_id(&self, outpoint: OutPoint) -> Option<bitcoin::Txid> {
105        self.request_current_consensus_retry(
106            TRANSACTION_ID_ENDPOINT.to_string(),
107            ApiRequestErased::new(outpoint),
108        )
109        .await
110    }
111}