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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! # Accountant
//! The Accountant object provides function for querying balances and asset values for the Account.

use abstract_std::{
    objects::{
        oracle::{self, AccountValue},
        AssetEntry,
    },
    proxy::{AssetsInfoResponse, BaseAssetResponse, QueryMsg, TokenValueResponse},
};
use cosmwasm_std::{Deps, Uint128};

use super::{AbstractApi, ApiIdentification};
use crate::{
    cw_helpers::ApiQuery,
    features::{AbstractNameService, AccountIdentification, ModuleIdentification},
    AbstractSdkResult,
};

/// Retrieve asset-registration information from the Account.
/// Query asset values and balances.
pub trait AccountingInterface:
    AbstractNameService + AccountIdentification + ModuleIdentification
{
    /**
        API for querying the Account's asset values and accounting configuration.

        # Example
        ```
        use abstract_sdk::prelude::*;
        # use cosmwasm_std::testing::mock_dependencies;
        # use abstract_sdk::mock_module::MockModule;
        # let module = MockModule::new();
        # let deps = mock_dependencies();

        let accountant: Accountant<MockModule>  = module.accountant(deps.as_ref());
        ```
    */
    fn accountant<'a>(&'a self, deps: Deps<'a>) -> Accountant<Self> {
        Accountant { base: self, deps }
    }
}

impl<T> AccountingInterface for T where
    T: AbstractNameService + AccountIdentification + ModuleIdentification
{
}

impl<'a, T: AccountingInterface> AbstractApi<T> for Accountant<'a, T> {
    fn base(&self) -> &T {
        self.base
    }
    fn deps(&self) -> Deps {
        self.deps
    }
}

impl<'a, T: AccountingInterface> ApiIdentification for Accountant<'a, T> {
    fn api_id() -> String {
        "Accountant".to_owned()
    }
}

#[derive(Clone)]
/**
    API for querying the Account's asset values and accounting configuration.

    # Example
    ```
    use abstract_sdk::prelude::*;
    # use cosmwasm_std::testing::mock_dependencies;
    # use abstract_sdk::mock_module::MockModule;
    # let module = MockModule::new();
    # let deps = mock_dependencies();

    let accountant: Accountant<MockModule>  = module.accountant(deps.as_ref());
    ```
*/
pub struct Accountant<'a, T: AccountingInterface> {
    base: &'a T,
    deps: Deps<'a>,
}

impl<'a, T: AccountingInterface> Accountant<'a, T> {
    /// Query the total value denominated in the base asset
    /// The provided address must implement the TotalValue Query
    pub fn query_total_value(&self) -> AbstractSdkResult<AccountValue> {
        let proxy_address = self.base.proxy_address(self.deps)?;
        let response: AccountValue =
            self.smart_query(proxy_address.to_string(), &QueryMsg::TotalValue {})?;

        Ok(response)
    }

    /// Query the asset value denominated in the base asset
    pub fn asset_value(&self, asset_entry: AssetEntry) -> AbstractSdkResult<Uint128> {
        let proxy_address = self.base.proxy_address(self.deps)?;
        let response: TokenValueResponse = self.smart_query(
            proxy_address.to_string(),
            &QueryMsg::TokenValue {
                identifier: asset_entry,
            },
        )?;

        Ok(response.value)
    }

    /// Return the proxy's base asset
    pub fn base_asset(&self) -> AbstractSdkResult<BaseAssetResponse> {
        let proxy_address = self.base.proxy_address(self.deps)?;
        let response: BaseAssetResponse =
            self.smart_query(proxy_address.to_string(), &QueryMsg::BaseAsset {})?;

        Ok(response)
    }

    /// List enabled assets (AssetInfos)
    pub fn assets_list(&self) -> AbstractSdkResult<AssetsInfoResponse> {
        let proxy_address = self.base.proxy_address(self.deps)?;

        let resp: AssetsInfoResponse = self.smart_query(
            proxy_address,
            &QueryMsg::AssetsInfo {
                start_after: None,
                limit: Some(oracle::LIST_SIZE_LIMIT),
            },
        )?;

        Ok(resp)
    }

    // /// List ProxyAssets raw
    // pub fn proxy_assets_list(&self) -> AbstractSdkResult<Vec<(AssetEntry, ProxyAsset)>> {
    //     let querier = self.deps.querier;
    //     let proxy_address = self.base.proxy_address(self.deps)?;

    //     let mut assets = vec![];
    //     let mut resp: AssetsResponse = querier.query_wasm_smart(
    //         &proxy_address,
    //         &QueryMsg::Assets {
    //             start_after: None,
    //             limit: None,
    //         },
    //     )?;
    //     while !resp.assets.is_empty() {
    //         let start_after = resp.assets.last().unwrap().0.clone();
    //         assets.append(resp.assets.as_mut());
    //         resp = querier.query_wasm_smart(
    //             &proxy_address,
    //             &QueryMsg::Assets {
    //                 start_after: Some(start_after.to_string()),
    //                 limit: None,
    //             },
    //         )?;
    //     }
    //     Ok(assets)
    // }
}

#[cfg(test)]
mod test {
    // use super::*;
    // use crate::mock_module::*;

    mod query_total_value {
        // use super::*;
    }
}