hopr_api/chain/
safe.rs

1use hopr_primitive_types::{
2    balance::{Balance, Currency},
3    prelude::Address,
4};
5
6/// Information about a deployed Safe.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub struct DeployedSafe {
9    /// Safe address.
10    pub address: Address,
11    /// Address of the Safe owner (typically the node chain key).
12    pub owner: Address,
13    /// Address of the Safe module.
14    pub module: Address,
15}
16
17/// Selector for [deployed Safes](DeployedSafe).
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub enum SafeSelector {
20    /// Selects Safes owned by the given address.
21    Owner(Address),
22    /// Selects Safes with the given address.
23    Address(Address),
24}
25
26impl SafeSelector {
27    pub fn satisfies(&self, safe: &DeployedSafe) -> bool {
28        match self {
29            SafeSelector::Owner(owner) => &safe.owner == owner,
30            SafeSelector::Address(address) => &safe.address == address,
31        }
32    }
33}
34
35/// Operations for reading Safe information.
36#[async_trait::async_trait]
37#[auto_impl::auto_impl(&, Box, Arc)]
38pub trait ChainReadSafeOperations {
39    type Error: std::error::Error + Send + Sync + 'static;
40
41    /// Returns the native or token currency Safe allowance.
42    async fn safe_allowance<C: Currency, A: Into<Address> + Send>(
43        &self,
44        safe_address: A,
45    ) -> Result<Balance<C>, Self::Error>;
46    /// Retrieves [`DeployedSafe`] information using the given [`selector`](SafeSelector).
47    ///
48    /// Returns `None` if no deployed Safe matched the given `selector`.
49    async fn safe_info(&self, selector: SafeSelector) -> Result<Option<DeployedSafe>, Self::Error>;
50    /// Waits for a Safe matching the given [`selector`](SafeSelector) to be deployed up to the given `timeout`.
51    ///
52    /// Returns immediately if the matching Safe is already deployed.
53    async fn await_safe_deployment(
54        &self,
55        selector: SafeSelector,
56        timeout: std::time::Duration,
57    ) -> Result<DeployedSafe, Self::Error>;
58    /// Predicts the Module address based on the given `nonce`, `owner` and `safe_address` of the Safe.
59    async fn predict_module_address(
60        &self,
61        nonce: u64,
62        owner: &Address,
63        safe_address: &Address,
64    ) -> Result<Address, Self::Error>;
65}