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
169
170
171
172
173
//! # Proxy Helpers
use abstract_os::{
    ibc_client,
    objects::{proxy_asset::ProxyAsset, AssetEntry},
    proxy::{
        state::VAULT_ASSETS, AssetsResponse, ExecuteMsg, HoldingValueResponse, QueryMsg,
        TotalValueResponse,
    },
};
use cosmwasm_std::{
    to_binary, Addr, CosmosMsg, Deps, QuerierWrapper, QueryRequest, StdError, StdResult, Uint128,
    WasmMsg, WasmQuery,
};
use cw_storage_plus::Item;

use crate::{OsAction, ADMIN};
// Re-export os-id query as proxy is also core-contract.
pub use crate::manager::query_os_id;
/// Constructs the proxy dapp action message to execute CosmosMsgs on the Proxy.
pub fn os_module_action(msgs: Vec<CosmosMsg>, proxy_address: &Addr) -> StdResult<OsAction> {
    Ok(CosmosMsg::Wasm(WasmMsg::Execute {
        contract_addr: proxy_address.to_string(),
        msg: to_binary(&ExecuteMsg::ModuleAction { msgs })?,
        funds: vec![],
    }))
}

pub fn os_ibc_action(
    msgs: Vec<ibc_client::ExecuteMsg>,
    proxy_address: &Addr,
) -> StdResult<OsAction> {
    Ok(CosmosMsg::Wasm(WasmMsg::Execute {
        contract_addr: proxy_address.to_string(),
        msg: to_binary(&ExecuteMsg::IbcAction { msgs })?,
        funds: vec![],
    }))
}

/// Get the manager of the proxy contract
/// Admin always set to manager contract
pub fn query_os_manager_address(querier: &QuerierWrapper, proxy_address: &Addr) -> StdResult<Addr> {
    Item::new(ADMIN).query(querier, proxy_address.clone())
}

/// Query the total value denominated in the base asset
/// The provided address must implement the TotalValue Query
pub fn query_total_value(deps: Deps, proxy_address: &Addr) -> StdResult<Uint128> {
    let response: TotalValueResponse =
        deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
            contract_addr: proxy_address.to_string(),
            msg: to_binary(&QueryMsg::TotalValue {})?,
        }))?;

    Ok(response.value)
}

/// RawQuery the proxy for a ProxyAsset
pub fn query_proxy_asset_raw(
    deps: Deps,
    proxy_address: &Addr,
    asset: &AssetEntry,
) -> StdResult<ProxyAsset> {
    let response = VAULT_ASSETS.query(&deps.querier, proxy_address.clone(), asset.clone())?;
    response.ok_or_else(|| {
        StdError::generic_err(format!(
            "Asset {} is not registered as an asset on your proxy contract.",
            asset
        ))
    })
}

/// Query the holding value denominated in the base asset
/// The provided address must implement the HoldingValue Query
pub fn query_holding_value(
    deps: Deps,
    proxy_address: &Addr,
    identifier: &String,
) -> StdResult<Uint128> {
    let response: HoldingValueResponse =
        deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
            contract_addr: proxy_address.to_string(),
            msg: to_binary(&QueryMsg::HoldingValue {
                identifier: identifier.to_string(),
            })?,
        }))?;

    Ok(response.value)
}

/// Query the token amount of a specific asset
/// The asset must be registered in the proxy contract
pub fn query_token_value(
    deps: Deps,
    proxy_address: &Addr,
    identifier: &String,
    amount: Option<Uint128>,
) -> StdResult<Uint128> {
    let response: TotalValueResponse =
        deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
            contract_addr: proxy_address.to_string(),
            msg: to_binary(&QueryMsg::TokenValue {
                identifier: identifier.to_string(),
                amount,
            })?,
        }))?;

    Ok(response.value)
}

/// List ProxyAssets raw
pub fn query_enabled_asset_names(
    deps: Deps,
    proxy_address: &Addr,
) -> StdResult<(Vec<AssetEntry>, AssetEntry)> {
    let mut asset_keys = vec![];
    let mut base_asset: Option<AssetEntry> = None;
    let mut resp: AssetsResponse = deps.querier.query_wasm_smart(
        proxy_address,
        &QueryMsg::Assets {
            page_token: None,
            page_size: None,
        },
    )?;
    while !resp.assets.is_empty() {
        let page_token = resp.assets.last().unwrap().0.clone();
        for (k, v) in resp.assets {
            maybe_set_base(&v, &mut base_asset);
            asset_keys.push(k);
        }
        resp = deps.querier.query_wasm_smart(
            proxy_address,
            &QueryMsg::Assets {
                page_token: Some(page_token.to_string()),
                page_size: None,
            },
        )?;
    }
    Ok((asset_keys, base_asset.unwrap()))
}

/// List ProxyAssets raw
pub fn query_enabled_assets(
    deps: Deps,
    proxy_address: &Addr,
) -> StdResult<Vec<(AssetEntry, ProxyAsset)>> {
    let mut assets = vec![];
    let mut resp: AssetsResponse = deps.querier.query_wasm_smart(
        proxy_address,
        &QueryMsg::Assets {
            page_token: None,
            page_size: None,
        },
    )?;
    while !resp.assets.is_empty() {
        let page_token = resp.assets.last().unwrap().0.clone();
        assets.append(resp.assets.as_mut());
        resp = deps.querier.query_wasm_smart(
            proxy_address,
            &QueryMsg::Assets {
                page_token: Some(page_token.to_string()),
                page_size: None,
            },
        )?;
    }
    Ok(assets)
}

#[inline(always)]
fn maybe_set_base(value: &ProxyAsset, base: &mut Option<AssetEntry>) {
    if value.value_reference.is_none() {
        *base = Some(value.asset.clone());
    }
}