templar_common/market/
mod.rs

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
use std::collections::HashMap;
use std::num::NonZeroU16;

use near_sdk::{env, near, AccountId};

use crate::{
    asset::{BorrowAssetAmount, CollateralAssetAmount},
    number::Decimal,
};
mod configuration;
pub use configuration::{MarketConfiguration, APY_LIMIT};
mod external;
pub use external::*;
mod r#impl;
pub use r#impl::*;
mod price_oracle_configuration;
pub use price_oracle_configuration::PriceOracleConfiguration;

pub mod error {
    pub use super::configuration::error::*;
    pub use super::price_oracle_configuration::error::*;
}

#[derive(Clone, Debug)]
#[near(serializers = [borsh, json])]
pub struct BorrowAssetMetrics {
    pub available: BorrowAssetAmount,
    pub deposited_active: BorrowAssetAmount,
    pub deposited_incoming: HashMap<u32, BorrowAssetAmount>,
    pub borrowed: BorrowAssetAmount,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[near(serializers = [json, borsh])]
pub struct YieldWeights {
    pub supply: NonZeroU16,
    pub r#static: HashMap<AccountId, u16>,
}

impl YieldWeights {
    /// # Panics
    /// - If `supply` is zero.
    #[allow(clippy::unwrap_used, reason = "Only used during initial construction")]
    pub fn new_with_supply_weight(supply: u16) -> Self {
        Self {
            supply: supply.try_into().unwrap(),
            r#static: HashMap::new(),
        }
    }

    #[must_use]
    pub fn with_static(mut self, account_id: AccountId, weight: u16) -> Self {
        self.r#static.insert(account_id, weight);
        self
    }

    pub fn total_weight(&self) -> NonZeroU16 {
        self.r#static
            .values()
            .try_fold(self.supply, |a, b| a.checked_add(*b))
            .unwrap_or_else(|| env::panic_str("Total weight overflow"))
    }

    pub fn static_share(&self, account_id: &AccountId) -> Decimal {
        self.r#static
            .get(account_id)
            .map_or(Decimal::ZERO, |weight| {
                Decimal::from(*weight) / u16::from(self.total_weight())
            })
    }
}

/// Parsed from the string parameter `msg` passed by `*_transfer_call` to
/// `*_on_transfer` calls.
#[near(serializers = [json])]
pub enum DepositMsg {
    /// Add the attached tokens to the sender's supply position's deposit.
    Supply,
    /// Add the attached tokens to the sender's borrow position's collateral
    /// deposit.
    Collateralize,
    /// Use the attached tokens to pay down the sender's borrow position's
    /// liability (sans fees).
    Repay,
    /// Liquidate an account that is below the configured liquidation
    /// collateralization ratio threshold.
    Liquidate(LiquidateMsg),
}

/// Indicate an account to liquidate.
#[near(serializers = [json])]
pub struct LiquidateMsg {
    pub account_id: AccountId,
    /// How much collateral to liquidate?
    /// Attempts to liquidate the whole position if `None`.
    pub amount: Option<CollateralAssetAmount>,
}

#[derive(Clone, Debug)]
#[near(serializers = [json, borsh])]
pub struct WithdrawalResolution {
    pub account_id: AccountId,
    pub amount_to_account: BorrowAssetAmount,
    pub amount_to_fees: BorrowAssetAmount,
}