templar_common/market/
mod.rs1use std::collections::HashMap;
2use std::num::NonZeroU16;
3
4use near_sdk::{near, AccountId};
5
6use crate::{
7 asset::{BorrowAssetAmount, CollateralAssetAmount},
8 number::Decimal,
9};
10mod configuration;
11pub use configuration::{MarketConfiguration, ValidAmountRange, APY_LIMIT};
12mod external;
13pub use external::*;
14mod r#impl;
15pub use r#impl::*;
16mod price_oracle_configuration;
17pub use price_oracle_configuration::PriceOracleConfiguration;
18
19pub mod error {
20 pub use super::configuration::error::*;
21 pub use super::price_oracle_configuration::error::*;
22}
23
24#[derive(Clone, Debug)]
25#[near(serializers = [borsh, json])]
26pub struct BorrowAssetMetrics {
27 pub available: BorrowAssetAmount,
28 pub deposited_active: BorrowAssetAmount,
29 pub deposited_incoming: HashMap<u32, BorrowAssetAmount>,
30 pub borrowed: BorrowAssetAmount,
31}
32
33#[derive(Clone, Debug, PartialEq, Eq)]
34#[near(serializers = [json, borsh])]
35pub struct YieldWeights {
36 pub supply: NonZeroU16,
37 pub r#static: HashMap<AccountId, u16>,
38}
39
40impl YieldWeights {
41 #[allow(clippy::unwrap_used, reason = "Only used during initial construction")]
44 pub fn new_with_supply_weight(supply: u16) -> Self {
45 Self {
46 supply: supply.try_into().unwrap(),
47 r#static: HashMap::new(),
48 }
49 }
50
51 #[must_use]
52 pub fn with_static(mut self, account_id: AccountId, weight: u16) -> Self {
53 self.r#static.insert(account_id, weight);
54 self
55 }
56
57 pub fn total_weight(&self) -> NonZeroU16 {
58 self.r#static
59 .values()
60 .try_fold(self.supply, |a, b| a.checked_add(*b))
61 .unwrap_or_else(|| crate::panic_with_message("Total weight overflow"))
62 }
63
64 pub fn static_share(&self, account_id: &AccountId) -> Decimal {
65 self.r#static
66 .get(account_id)
67 .map_or(Decimal::ZERO, |weight| {
68 Decimal::from(*weight) / u16::from(self.total_weight())
69 })
70 }
71}
72
73#[near(serializers = [json])]
76pub enum DepositMsg {
77 Supply,
79 Collateralize,
82 Repay,
85 Liquidate(LiquidateMsg),
88}
89
90#[near(serializers = [json])]
92pub struct LiquidateMsg {
93 pub account_id: AccountId,
94 pub amount: Option<CollateralAssetAmount>,
97}
98
99#[derive(Clone, Debug)]
100#[near(serializers = [json, borsh])]
101pub struct Withdrawal {
102 pub account_id: AccountId,
103 pub amount_to_account: BorrowAssetAmount,
104 pub amount_to_fees: BorrowAssetAmount,
105}