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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! # Proxy Asset
//! Proxy assets are objects that describe an asset and a way to calculate that asset's value against a base asset.
//!
//! ## Details
//! A proxy asset is composed of two components.
//! * The `asset`, which is an [`AssetEntry`] and maps to an [`AssetInfo`].
//! * The [`ValueRef`] which is an enum that indicates how to calculate the value for that asset.
//!
//! The base asset is the asset for which `value_reference` in `None`.
//! **There should only be ONE base asset when configuring your proxy**

use std::convert::TryInto;

use cosmwasm_std::{
    to_binary, Addr, Decimal, Deps, Env, QuerierWrapper, QueryRequest, StdError, StdResult,
    Uint128, WasmQuery,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cw_asset::{Asset, AssetInfo};

use crate::{
    manager::state::OS_MODULES,
    proxy::{
        state::{ADMIN, VAULT_ASSETS},
        ExternalValueResponse, ValueQueryMsg,
    },
};

use super::{
    ans_host::AnsHost,
    asset_entry::AssetEntry,
    contract_entry::{ContractEntry, UncheckedContractEntry},
};

/// A proxy asset with unchecked ans_host entry fields.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct UncheckedProxyAsset {
    /// The asset that's held by the proxy
    pub asset: String,
    /// The value reference provides the tooling to get the value of the asset
    /// relative to the base asset.
    /// If None, the provided asset is set as the base asset.
    /// **You can only have one base asset!**
    pub value_reference: Option<UncheckedValueRef>,
}

impl UncheckedProxyAsset {
    pub fn new(asset: impl Into<String>, value_reference: Option<UncheckedValueRef>) -> Self {
        Self {
            asset: asset.into(),
            value_reference,
        }
    }

    /// Perform checks on the proxy asset to ensure it can be resolved by the AnsHost
    pub fn check(self, deps: Deps, ans_host: &AnsHost) -> StdResult<ProxyAsset> {
        let entry: AssetEntry = self.asset.into();
        ans_host.query_asset(&deps.querier, &entry)?;
        let value_reference = self
            .value_reference
            .map(|val| val.check(deps, ans_host, &entry));
        Ok(ProxyAsset {
            asset: entry,
            value_reference: value_reference.transpose()?,
        })
    }
}

/// Provides information on how to calculate the value of an asset
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, JsonSchema)]

pub enum UncheckedValueRef {
    /// A pool address of an asset/asset pair
    /// Both assets must be defined in the Proxy_assets state
    Pool {
        pair: String,
        exchange: String,
    },
    // Liquidity Pool token
    LiquidityToken {},
    // a Proxy, the proxy also takes a Decimal (the multiplier)
    // Asset will be valued as if they are Proxy tokens
    ValueAs {
        asset: String,
        multiplier: Decimal,
    },
    // Query an external contract to get the value
    External {
        api_name: String,
    },
}

impl UncheckedValueRef {
    pub fn check(self, deps: Deps, ans_host: &AnsHost, entry: &AssetEntry) -> StdResult<ValueRef> {
        match self {
            UncheckedValueRef::Pool { pair, exchange } => {
                let lowercase = pair.to_ascii_lowercase();
                let mut composite: Vec<&str> = lowercase.split('_').collect();
                if composite.len() != 2 {
                    return Err(StdError::generic_err(
                        "trading pair should be formatted as \"asset1_asset2\".",
                    ));
                }
                composite.sort();
                let pair_name = format!("{}_{}", composite[0], composite[1]);
                // verify pair is available
                let pair_contract: ContractEntry =
                    UncheckedContractEntry::new(&exchange, &pair_name).check();
                ans_host.query_contract(&deps.querier, &pair_contract)?;
                Ok(ValueRef::Pool {
                    pair: pair_contract,
                })
            }
            UncheckedValueRef::LiquidityToken {} => {
                let maybe_pair: UncheckedContractEntry = entry.to_string().try_into()?;
                // Ensure lp pair is registered
                ans_host.query_contract(&deps.querier, &maybe_pair.check())?;
                Ok(ValueRef::LiquidityToken {})
            }
            UncheckedValueRef::ValueAs { asset, multiplier } => {
                let replacement_asset: AssetEntry = asset.into();
                ans_host.query_asset(&deps.querier, &replacement_asset)?;
                Ok(ValueRef::ValueAs {
                    asset: replacement_asset,
                    multiplier,
                })
            }
            UncheckedValueRef::External { api_name } => Ok(ValueRef::External { api_name }),
        }
    }
}

/// Every ProxyAsset provides a way to determine its value recursively relative to
/// a base asset.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct ProxyAsset {
    /// Asset entry that maps to an AssetInfo using raw-queries on ans_host
    pub asset: AssetEntry,
    /// The value reference provides the tooling to get the value of the asset
    /// relative to the base asset.
    pub value_reference: Option<ValueRef>,
}

/// Provides information on how to calculate the value of an asset
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, JsonSchema)]

pub enum ValueRef {
    /// A pool name of an asset/asset pair
    /// Both assets must be defined in the Vault_assets state
    Pool { pair: ContractEntry },
    /// Liquidity pool token
    LiquidityToken {},
    /// Asset will be valued as if they are ValueAs.asset tokens
    ValueAs {
        asset: AssetEntry,
        multiplier: Decimal,
    },
    /// Query an external contract to get the value
    External { api_name: String },
}

impl ProxyAsset {
    /// Calculates the value of the asset through the optionally provided ValueReference
    // TODO: improve efficiency
    // We could cache each asset/contract address and store each asset in a stack with the most complex (most hops) assets on top.
    // Doing this would prevent an asset value from being calculated multiple times.
    pub fn value(
        &mut self,
        deps: Deps,
        env: &Env,
        ans_host: &AnsHost,
        set_holding: Option<Uint128>,
    ) -> StdResult<Uint128> {
        // Query how many of these tokens are held in the contract if not set.
        let asset_info = ans_host.query_asset(&deps.querier, &self.asset)?;
        let holding: Uint128 = match set_holding {
            Some(setter) => setter,
            None => asset_info.query_balance(&deps.querier, env.contract.address.clone())?,
        };

        let valued_asset = Asset::new(asset_info, holding);

        // Is there a reference to calculate the value?
        if let Some(value_reference) = self.value_reference.clone() {
            match value_reference {
                // A Pool refers to a swap pair that recursively leads to an asset/base_asset pool.
                ValueRef::Pool { pair } => {
                    return self.trade_pair_value(deps, env, ans_host, valued_asset, pair)
                }
                // Liquidity is an LP token, value() fn is called recursively on both assets in the pool
                ValueRef::LiquidityToken {} => {
                    // We map the LP token to its pair address.
                    // lp tokens are stored as "dex/asset1_asset2" in the asset store.
                    // pairs are stored as ContractEntry{protocol: dex, contract: asset1_asset2} in the contract store.
                    let maybe_pair: UncheckedContractEntry = self.asset.to_string().try_into()?;
                    let pair = maybe_pair.check();
                    return self.lp_value(deps, env, ans_host, valued_asset, pair);
                }
                // A proxy asset is used instead
                ValueRef::ValueAs { asset, multiplier } => {
                    return value_as_value(deps, env, ans_host, asset, multiplier, holding)
                }
                ValueRef::External { api_name } => {
                    let manager = ADMIN.get(deps)?.unwrap();
                    let maybe_api_addr = OS_MODULES.query(&deps.querier, manager, &api_name)?;
                    if let Some(api_addr) = maybe_api_addr {
                        let response: ExternalValueResponse =
                            deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
                                contract_addr: api_addr.to_string(),
                                msg: to_binary(&ValueQueryMsg {
                                    asset: self.asset.clone(),
                                    amount: valued_asset.amount,
                                })?,
                            }))?;
                        return Ok(response.value);
                    } else {
                        return Err(StdError::generic_err(format!(
                            "external contract api {} must be enabled on OS",
                            api_name
                        )));
                    }
                }
            }
        }
        // If there is no valueref, it means this token is the base token.
        Ok(holding)
    }

    /// Calculates the value of an asset compared to some base asset through the provided trading pair.
    pub fn trade_pair_value(
        &self,
        deps: Deps,
        env: &Env,
        ans_host: &AnsHost,
        valued_asset: Asset,
        pair: ContractEntry,
    ) -> StdResult<Uint128> {
        let other_pool_asset: AssetEntry =
            other_asset_name(self.asset.as_str(), &pair.contract)?.into();

        let pair_address = ans_host.query_contract(&deps.querier, &pair)?;
        let other_asset_info = ans_host.query_asset(&deps.querier, &other_pool_asset)?;

        // query assets held in pool, gives price
        let pool_info = (
            other_asset_info.query_balance(&deps.querier, &pair_address)?,
            valued_asset
                .info
                .query_balance(&deps.querier, pair_address)?,
        );

        // other / this
        let ratio = Decimal::from_ratio(pool_info.0.u128(), pool_info.1.u128());

        // Get the value of the current asset in the denom of the other asset
        let mut recursive_vault_asset = VAULT_ASSETS.load(deps.storage, other_pool_asset)?;

        // #other = #this * (pool_other/pool_this)
        let amount_in_other_denom = valued_asset.amount * ratio;
        // Call value on this other asset.
        recursive_vault_asset.value(deps, env, ans_host, Some(amount_in_other_denom))
    }

    /// Calculate the value of an LP token
    /// Uses the lp token name to query pair pool for both assets
    pub fn lp_value(
        &self,
        deps: Deps,
        env: &Env,
        ans_host: &AnsHost,
        lp_asset: Asset,
        pair: ContractEntry,
    ) -> StdResult<Uint128> {
        let supply: Uint128;
        if let AssetInfo::Cw20(addr) = &lp_asset.info {
            supply = query_cw20_supply(&deps.querier, addr)?;
        } else {
            return Err(StdError::generic_err("Can't have a native LP token"));
        }

        // Get total supply of LP tokens and calculate share
        let share: Decimal = Decimal::from_ratio(lp_asset.amount, supply.u128());

        let other_pool_asset_names = get_pair_asset_names(pair.contract.as_str());

        if other_pool_asset_names.len() != 2 {
            return Err(StdError::generic_err(format!(
                "lp pair contract {} must be composed of two assets.",
                pair
            )));
        }

        let pair_address = ans_host.query_contract(&deps.querier, &pair)?;

        let asset_1 = ans_host.query_asset(&deps.querier, &other_pool_asset_names[0].into())?;
        let asset_2 = ans_host.query_asset(&deps.querier, &other_pool_asset_names[1].into())?;
        // query assets held in pool, gives price
        let (amount1, amount2) = (
            asset_1.query_balance(&deps.querier, &pair_address)?,
            asset_2.query_balance(&deps.querier, pair_address)?,
        );

        // load the assets
        let mut vault_asset_1: ProxyAsset =
            VAULT_ASSETS.load(deps.storage, other_pool_asset_names[0].into())?;
        let mut vault_asset_2: ProxyAsset =
            VAULT_ASSETS.load(deps.storage, other_pool_asset_names[1].into())?;

        // set the amounts to the LP holdings
        let vault_asset_1_amount = share * Uint128::new(amount1.u128());
        let vault_asset_2_amount = share * Uint128::new(amount2.u128());
        // Call value on these assets.
        Ok(
            vault_asset_1.value(deps, env, ans_host, Some(vault_asset_1_amount))?
                + vault_asset_2.value(deps, env, ans_host, Some(vault_asset_2_amount))?,
        )
    }
}

pub fn value_as_value(
    deps: Deps,
    env: &Env,
    ans_host: &AnsHost,
    replacement_asset: AssetEntry,
    multiplier: Decimal,
    holding: Uint128,
) -> StdResult<Uint128> {
    // Get the proxy asset
    let mut replacement_vault_asset: ProxyAsset =
        VAULT_ASSETS.load(deps.storage, replacement_asset)?;
    // call value on proxy asset with adjusted multiplier.
    replacement_vault_asset.value(deps, env, ans_host, Some(holding * multiplier))
}
/// Get the other asset's name from a composite name
/// ex: asset= "btc" composite = "btc_eth"
/// returns "eth"
pub fn other_asset_name<'a>(asset: &'a str, composite: &'a str) -> StdResult<&'a str> {
    composite
        .split('_')
        .find(|component| *component != asset)
        .ok_or_else(|| {
            StdError::generic_err(format!(
                "composite {} is not structured correctly",
                composite
            ))
        })
}

/// Composite of form asset1_asset2
pub fn get_pair_asset_names(composite: &str) -> Vec<&str> {
    composite.split('_').collect()
}

fn query_cw20_supply(querier: &QuerierWrapper, contract_addr: &Addr) -> StdResult<Uint128> {
    let response: cw20::TokenInfoResponse =
        querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
            contract_addr: contract_addr.into(),
            msg: to_binary(&cw20::Cw20QueryMsg::TokenInfo {})?,
        }))?;
    Ok(response.total_supply)
}