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
use crate::{
ensure, round_up, to_balance, to_u256, MathError,
MathError::{InsufficientOutReserve, Overflow, ZeroReserve},
};
use core::convert::TryFrom;
use num_traits::Zero;
use primitive_types::U256;
type Balance = u128;
const FIXED_ROUND_UP: Balance = 1;
pub fn calculate_spot_price(in_reserve: Balance, out_reserve: Balance, amount: Balance) -> Result<Balance, MathError> {
ensure!(in_reserve != 0, ZeroReserve);
if amount == 0 || out_reserve == 0 {
return to_balance!(0);
}
let (amount_hp, out_reserve_hp, in_reserve_hp) = to_u256!(amount, out_reserve, in_reserve);
let spot_price_hp = out_reserve_hp
.checked_mul(amount_hp)
.ok_or(Overflow)?
.checked_div(in_reserve_hp)
.ok_or(Overflow)?;
to_balance!(spot_price_hp)
}
pub fn calculate_out_given_in(
in_reserve: Balance,
out_reserve: Balance,
amount_in: Balance,
) -> Result<Balance, MathError> {
if amount_in == 0 {
return Ok(0);
};
let (in_reserve_hp, out_reserve_hp, amount_in_hp) = to_u256!(in_reserve, out_reserve, amount_in);
let denominator = in_reserve_hp.checked_add(amount_in_hp).ok_or(Overflow)?;
ensure!(!denominator.is_zero(), ZeroReserve);
let numerator = out_reserve_hp.checked_mul(amount_in_hp).ok_or(Overflow)?;
let sale_price_hp = numerator.checked_div(denominator).ok_or(Overflow)?;
to_balance!(sale_price_hp)
}
pub fn calculate_in_given_out(
out_reserve: Balance,
in_reserve: Balance,
amount_out: Balance,
) -> Result<Balance, MathError> {
if amount_out == 0 {
return Ok(0);
};
ensure!(amount_out <= out_reserve, InsufficientOutReserve);
let (out_reserve_hp, in_reserve_hp, amount_out_hp) = to_u256!(out_reserve, in_reserve, amount_out);
let numerator = in_reserve_hp.checked_mul(amount_out_hp).ok_or(Overflow)?;
let denominator = out_reserve_hp.checked_sub(amount_out_hp).ok_or(Overflow)?;
ensure!(!denominator.is_zero(), ZeroReserve);
let buy_price_hp = numerator.checked_div(denominator).ok_or(Overflow)?;
let result = to_balance!(buy_price_hp).ok();
round_up!(result.ok_or(Overflow)?)
}
pub fn calculate_liquidity_in(
asset_a_reserve: Balance,
asset_b_reserve: Balance,
amount: Balance,
) -> Result<Balance, MathError> {
ensure!(asset_a_reserve != 0, ZeroReserve);
if amount.is_zero() || asset_b_reserve.is_zero() {
return Ok(Balance::zero());
}
let (a_reserve_hp, b_reserve_hp, amount_hp) = to_u256!(asset_a_reserve, asset_b_reserve, amount);
let b_required_hp = amount_hp
.checked_mul(b_reserve_hp)
.and_then(|v| v.checked_div(a_reserve_hp))
.and_then(|v| v.checked_add(U256::one()))
.ok_or(Overflow)?;
to_balance!(b_required_hp)
}
pub fn calculate_liquidity_out(
asset_a_reserve: Balance,
asset_b_reserve: Balance,
amount: Balance,
total_liquidity: Balance,
) -> Result<(Balance, Balance), MathError> {
ensure!(total_liquidity != 0, ZeroReserve);
let (a_reserve_hp, b_reserve_hp, amount_hp, liquidity_hp) =
to_u256!(asset_a_reserve, asset_b_reserve, amount, total_liquidity);
let remove_amount_a_hp = amount_hp
.checked_mul(a_reserve_hp)
.ok_or(Overflow)?
.checked_div(liquidity_hp)
.ok_or(Overflow)?;
let remove_amount_a = to_balance!(remove_amount_a_hp)?;
let remove_amount_b_hp = b_reserve_hp
.checked_mul(amount_hp)
.ok_or(Overflow)?
.checked_div(liquidity_hp)
.ok_or(Overflow)?;
let remove_amount_b = to_balance!(remove_amount_b_hp)?;
Ok((remove_amount_a, remove_amount_b))
}
pub fn calculate_shares(asset_reserve: Balance, asset_amount: Balance, share_issuance: Balance) -> Option<Balance> {
if asset_reserve.is_zero() {
return None;
}
let (reserve_hp, amount_hp, issuance_hp) = to_u256!(asset_reserve, asset_amount, share_issuance);
let result = issuance_hp
.checked_mul(amount_hp)
.and_then(|v| v.checked_div(reserve_hp))?;
to_balance!(result).ok()
}