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
#![allow(clippy::type_complexity)]
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use super::*;
#[derive(Serialize, Deserialize, Encode, Decode, Eq, PartialEq, Copy, Clone, PartialOrd, Ord, TypeInfo)]
#[serde(rename_all = "camelCase")]
pub struct PairInfo<AccountId, AssetBalance, AssetId> {
pub asset_0: AssetId,
pub asset_1: AssetId,
pub account: AccountId,
pub total_liquidity: AssetBalance,
pub holding_liquidity: AssetBalance,
pub reserve_0: AssetBalance,
pub reserve_1: AssetBalance,
pub lp_asset_id: AssetId,
pub status: u8,
}
impl<T: Config> Pallet<T> {
pub fn supply_out_amount(supply: AssetBalance, path: Vec<T::AssetId>) -> AssetBalance {
Self::get_amount_out_by_path(supply, &path).map_or(AssetBalance::default(), |amounts| {
*amounts.last().unwrap_or(&AssetBalance::default())
})
}
pub fn desired_in_amount(desired_amount: AssetBalance, path: Vec<T::AssetId>) -> AssetBalance {
Self::get_amount_in_by_path(desired_amount, &path).map_or(AssetBalance::default(), |amounts| {
*amounts.first().unwrap_or(&AssetBalance::default())
})
}
pub fn get_estimate_lptoken(
asset_0: T::AssetId,
asset_1: T::AssetId,
amount_0_desired: AssetBalance,
amount_1_desired: AssetBalance,
amount_0_min: AssetBalance,
amount_1_min: AssetBalance,
) -> AssetBalance {
let sorted_pair = Self::sort_asset_id(asset_0, asset_1);
match Self::pair_status(sorted_pair) {
Trading(metadata) => {
let reserve_0 = T::MultiCurrency::free_balance(asset_0, &metadata.pair_account);
let reserve_1 = T::MultiCurrency::free_balance(asset_1, &metadata.pair_account);
Self::calculate_added_amount(
amount_0_desired,
amount_1_desired,
amount_0_min,
amount_1_min,
reserve_0,
reserve_1,
)
.map_or(Zero::zero(), |(amount_0, amount_1)| {
Self::calculate_liquidity(amount_0, amount_1, reserve_0, reserve_1, metadata.total_supply)
})
}
_ => Zero::zero(),
}
}
pub fn get_pair_by_asset_id(
asset_0: T::AssetId,
asset_1: T::AssetId,
) -> Option<PairInfo<T::AccountId, AssetBalance, T::AssetId>> {
let pair_account = Self::pair_account_id(asset_0, asset_1);
let lp_asset_id = Self::lp_asset_id(&asset_0, &asset_1)?;
let status = match Self::pair_status(Self::sort_asset_id(asset_0, asset_1)) {
Trading(_) => 0,
Bootstrap(_) => 1,
Disable => return None,
};
Some(PairInfo {
asset_0,
asset_1,
account: pair_account.clone(),
total_liquidity: T::MultiCurrency::total_issuance(lp_asset_id),
holding_liquidity: Zero::zero(),
reserve_0: T::MultiCurrency::free_balance(asset_0, &pair_account),
reserve_1: T::MultiCurrency::free_balance(asset_1, &pair_account),
lp_asset_id,
status,
})
}
pub fn calculate_remove_liquidity(
asset_0: T::AssetId,
asset_1: T::AssetId,
amount: AssetBalance,
) -> Option<(AssetBalance, AssetBalance)> {
let pair_account = Self::pair_account_id(asset_0, asset_1);
let lp_asset_id = Self::lp_asset_id(&asset_0, &asset_1)?;
let lp_total_supply = T::MultiCurrency::total_issuance(lp_asset_id);
Some((
Self::calculate_share_amount(
amount,
lp_total_supply,
T::MultiCurrency::free_balance(asset_0, &pair_account),
),
Self::calculate_share_amount(
amount,
lp_total_supply,
T::MultiCurrency::free_balance(asset_1, &pair_account),
),
))
}
}