1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! # Represents chain infrastructure
//!
//! [`Environment`] allows you to get the execution environment of the object.
//!
//! You might want to do this to get the cw-orchestrator type of the infrastructure which enables you to
//! call some environment-specific methods or do low-level operations.
//!
//! You also sometimes need to provide the environment as a parameter to some methods, e.g. when you want to deploy a contract.

use abstract_interface::{Abstract, AbstractInterfaceError};
use cw_orch::prelude::*;

use crate::{account::Account, AbstractClient};

/// Trait for retrieving the CosmWasm environment that is being used.
pub trait Environment<Chain: CwEnv> {
    /// Get the execution environment
    fn environment(&self) -> Chain;
}

pub(crate) trait Infrastructure<Chain: CwEnv>: Environment<Chain> {
    /// Get the infrastructure on the execution environment
    fn infrastructure(&self) -> Result<Abstract<Chain>, AbstractInterfaceError> {
        let chain = self.environment();
        Abstract::load_from(chain)
    }
}

impl<Chain: CwEnv, T> Infrastructure<Chain> for T where T: Environment<Chain> {}

impl<Chain: CwEnv> Environment<Chain> for Account<Chain> {
    fn environment(&self) -> Chain {
        self.abstr_account.proxy.get_chain().clone()
    }
}

impl<Chain: CwEnv> Environment<Chain> for AbstractClient<Chain> {
    fn environment(&self) -> Chain {
        self.abstr.version_control.get_chain().clone()
    }
}