Skip to main content

etopay_wallet/
wallet.rs

1use super::error::Result;
2use crate::types::{CryptoAmount, GasCostEstimation, WalletTransaction};
3use async_trait::async_trait;
4use std::fmt::Debug;
5
6/// The intended transaction to perform. Used to perform the transaction and estimate gas fees.
7pub struct TransactionIntent {
8    /// The address to send to.
9    pub address_to: String,
10
11    /// The amount to send.
12    pub amount: CryptoAmount,
13
14    /// Optional data to attach to the transaction.
15    pub data: Option<Vec<u8>>,
16}
17
18/// Options that can be given to customize the mnemonic derivation
19#[derive(Debug, Default)]
20pub struct MnemonicDerivationOption {
21    pub account: u32,
22    pub index: u32,
23}
24
25#[cfg_attr(any(test, feature = "mock"), mockall::automock)]
26#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
27#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
28/// Wallet user interface
29pub trait WalletUser: Debug {
30    /// Gets a new address for the user
31    ///
32    /// # Returns
33    ///
34    /// Returns a `Result` containing the generated address as a `String` if successful, or an `Error` if it fails.
35    ///
36    /// # Errors
37    ///
38    /// This function can return an error if it fails to synchronize the wallet, generate addresses, or encounter any other issues.
39    async fn get_address(&self) -> Result<String>;
40
41    /// Gets the balance of a user.
42    ///
43    /// # Returns
44    ///
45    /// Returns the available balance of the user as a `f64` if successful, or an `Error` if it fails.
46    ///
47    /// # Errors
48    ///
49    /// This function can return an error if it fails to synchronize the wallet or encounters any other issues.
50    async fn get_balance(&self) -> Result<CryptoAmount>;
51
52    /// Send amount to receiver
53    ///
54    /// # Arguments
55    ///
56    /// * `address` - The address of the receiver.
57    /// * `amount` - The amount to send.
58    /// * `tag` - The transactions tag. Optional.
59    /// * `message` - The transactions message. Optional.
60    ///
61    ///
62    /// Returns a `Result` containing the sent transaction ID if successful, or an `Error` if it fails.
63    ///
64    /// # Errors
65    ///
66    /// This function can return an error if it fails to synchronize the wallet, send the transaction, or encounter any other issues.
67    async fn send_amount(&self, intent: &TransactionIntent) -> Result<String>;
68
69    /// Gets the list of transactions
70    ///
71    /// # Arguments
72    ///
73    /// * `start` - The index of the first wallet transaction to return
74    /// * `limit` - The number of following wallet transactions to return
75    ///
76    /// # Returns
77    ///
78    /// The list of wallet transactions.
79    ///
80    /// # Errors
81    ///
82    /// This function can return an error if it cannot retrieve the list of wallet transactions.
83    async fn get_wallet_tx_list(&self, start: usize, limit: usize) -> Result<Vec<String>>;
84
85    /// Get detailed report of a particular transaction in the history
86    ///
87    /// # Arguments
88    ///
89    /// * `tx_id` - The id of the wallet transaction to return the details for.
90    ///
91    /// # Returns
92    ///
93    /// The wallet transaction details.
94    ///
95    /// # Errors
96    ///
97    /// This function can return an error if it cannot retrieve the wallet transaction.
98    async fn get_wallet_tx(&self, tx_id: &str) -> Result<WalletTransaction>;
99
100    /// Estimate gas cost for eip 1559 transaction
101    ///
102    /// # Arguments
103    ///
104    /// * `transaction` - A transaction with a priority fee ([EIP-1559](https://eips.ethereum.org/EIPS/eip-1559))
105    ///
106    /// # Returns the estimated gas cost for the underlying transaction to be executed (gas limit, max fee per gas and max priority fee per gas)
107    ///
108    /// This function can return an error if it cannot parse input transaction or retrieve information from the node.
109    async fn estimate_gas_cost(&self, intent: &TransactionIntent) -> Result<GasCostEstimation>;
110}