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
use frame_support::dispatch::DispatchError;
use frame_support::ensure;
use assets::AssetIdOf;
use common::prelude::{Balance, Fixed, FixedWrapper};
use common::{fixed_wrapper, AssetInfoProvider, TradingPair};
use crate::{to_balance, to_fixed_wrapper};
use crate::{Config, Error, Pallet};
impl<T: Config> Pallet<T> {
pub fn calculate_quote(
amount_a: &Balance,
reserve_a: &Balance,
reserve_b: &Balance,
) -> Result<Balance, DispatchError> {
Ok(to_balance!(
(to_fixed_wrapper!(amount_a) * to_fixed_wrapper!(reserve_b))
/ to_fixed_wrapper!(reserve_a)
))
}
pub fn calculate_optimal_deposit(
_total_supply: Balance,
reserve_a: Balance,
reserve_b: Balance,
amount_a_desired: Balance,
amount_b_desired: Balance,
amount_a_min: Balance,
amount_b_min: Balance,
) -> Result<(Balance, Balance), DispatchError> {
let opt_am_a_des = Pallet::<T>::calculate_quote(&amount_b_desired, &reserve_b, &reserve_a)?;
let opt_am_b_des = Pallet::<T>::calculate_quote(&amount_a_desired, &reserve_a, &reserve_b)?;
if opt_am_b_des <= amount_b_desired {
ensure!(
opt_am_b_des >= amount_b_min,
Error::<T>::ImpossibleToDecideValidPairValuesFromRangeForThisPool
);
Ok((amount_a_desired, opt_am_b_des))
} else {
ensure!(
opt_am_a_des >= amount_a_min && opt_am_a_des <= amount_a_desired,
Error::<T>::ImpossibleToDecideValidPairValuesFromRangeForThisPool
);
Ok((opt_am_a_des, amount_b_desired))
}
}
pub fn calc_deposit_liquidity_1(
total_supply: Balance,
reserve_a: Balance,
reserve_b: Balance,
amount_a_desired: Balance,
amount_b_desired: Balance,
amount_a_min: Balance,
amount_b_min: Balance,
) -> Result<(Balance, Balance, Balance), DispatchError> {
let (am_a_des, am_b_des) = Pallet::<T>::calculate_optimal_deposit(
total_supply,
reserve_a,
reserve_b,
amount_a_desired,
amount_b_desired,
amount_a_min,
amount_b_min,
)?;
let lhs = to_balance!(
to_fixed_wrapper!(am_a_des) * to_fixed_wrapper!(total_supply)
/ to_fixed_wrapper!(reserve_a)
);
let rhs = to_balance!(
to_fixed_wrapper!(am_b_des) * to_fixed_wrapper!(total_supply)
/ to_fixed_wrapper!(reserve_b)
);
let min_value = lhs.min(rhs);
Ok((am_a_des, am_b_des, min_value))
}
pub fn calc_output_for_exact_input(
fee_fraction: Fixed,
get_fee_from_destination: bool,
x: &Balance,
y: &Balance,
x_in: &Balance,
deduce_fee: bool,
) -> Result<(Balance, Balance), DispatchError> {
let fxw_x = FixedWrapper::from(x.clone());
let fxw_y = FixedWrapper::from(y.clone());
let fxw_x_in = FixedWrapper::from(x_in.clone());
if get_fee_from_destination {
let nominator = fxw_x_in.clone() * fxw_y;
let denominator = fxw_x + fxw_x_in;
let y_out_with_fee = nominator / denominator;
let y_out = if deduce_fee {
y_out_with_fee.clone() * (fixed_wrapper!(1) - fee_fraction)
} else {
y_out_with_fee.clone()
};
Ok((
to_balance!(y_out.clone()),
to_balance!(y_out_with_fee - y_out),
))
} else {
let x_in_without_fee = if deduce_fee {
fxw_x_in.clone() * (fixed_wrapper!(1) - fee_fraction)
} else {
fxw_x_in.clone()
};
let nominator = x_in_without_fee.clone() * fxw_y;
let denominator = fxw_x + x_in_without_fee.clone();
let y_out = nominator / denominator;
Ok((to_balance!(y_out), to_balance!(fxw_x_in - x_in_without_fee)))
}
}
pub fn calc_input_for_exact_output(
fee_fraction: Fixed,
get_fee_from_destination: bool,
x: &Balance,
y: &Balance,
y_out: &Balance,
deduce_fee: bool,
) -> Result<(Balance, Balance), DispatchError> {
let fxw_x = FixedWrapper::from(x.clone());
let fxw_y = FixedWrapper::from(y.clone());
let fxw_y_out = FixedWrapper::from(y_out.clone());
if get_fee_from_destination {
let fxw_y_out = fxw_y_out.clone() + Fixed::from_bits(1); let y_out_with_fee = if deduce_fee {
fxw_y_out.clone() / (fixed_wrapper!(1) - fee_fraction)
} else {
fxw_y_out.clone()
};
let nominator = fxw_x * y_out_with_fee.clone();
let denominator = fxw_y - y_out_with_fee.clone();
let x_in = nominator / denominator;
Ok((to_balance!(x_in), to_balance!(y_out_with_fee - fxw_y_out)))
} else {
let fxw_y_out = fxw_y_out.clone() + Fixed::from_bits(1); let nominator = fxw_x * fxw_y_out.clone();
let denominator = fxw_y - fxw_y_out;
let x_in_without_fee = nominator / denominator;
let x_in = if deduce_fee {
x_in_without_fee.clone() / (fixed_wrapper!(1) - fee_fraction)
} else {
x_in_without_fee.clone()
};
Ok((
to_balance!(x_in.clone()),
to_balance!(x_in - x_in_without_fee),
))
}
}
pub fn get_base_asset_part_from_pool_account(
pool_acc: &T::AccountId,
trading_pair: &TradingPair<AssetIdOf<T>>,
liq_amount: Balance,
) -> Result<Balance, DispatchError> {
let b_in_pool =
assets::Pallet::<T>::free_balance(&trading_pair.base_asset_id.into(), pool_acc)?;
let t_in_pool =
assets::Pallet::<T>::free_balance(&trading_pair.target_asset_id.into(), pool_acc)?;
let fxw_liq_in_pool =
to_fixed_wrapper!(b_in_pool).multiply_and_sqrt(&to_fixed_wrapper!(t_in_pool));
let fxw_piece = fxw_liq_in_pool / to_fixed_wrapper!(liq_amount);
let fxw_value = to_fixed_wrapper!(b_in_pool) / fxw_piece;
let value = to_balance!(fxw_value);
Ok(value)
}
}