-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathswap_math.rs
344 lines (327 loc) · 11.3 KB
/
swap_math.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use super::full_math::MulDiv;
use super::liquidity_math;
use super::sqrt_price_math;
use crate::error::ErrorCode;
use crate::states::config::FEE_RATE_DENOMINATOR_VALUE;
use anchor_lang::prelude::*;
/// Result of a swap step
#[derive(Default, Debug)]
pub struct SwapStep {
/// The price after swapping the amount in/out, not to exceed the price target
pub sqrt_price_next_x64: u128,
pub amount_in: u64,
pub amount_out: u64,
pub fee_amount: u64,
}
/// Computes the result of swapping some amount in, or amount out, given the parameters of the swap
pub fn compute_swap_step(
sqrt_price_current_x64: u128,
sqrt_price_target_x64: u128,
liquidity: u128,
amount_remaining: u64,
fee_rate: u32,
is_base_input: bool,
zero_for_one: bool,
block_timestamp: u32,
) -> Result<SwapStep> {
// let exact_in = amount_remaining >= 0;
let mut swap_step = SwapStep::default();
if is_base_input {
// round up amount_in
// In exact input case, amount_remaining is positive
let amount_remaining_less_fee = (amount_remaining as u64)
.mul_div_floor(
(FEE_RATE_DENOMINATOR_VALUE - fee_rate).into(),
u64::from(FEE_RATE_DENOMINATOR_VALUE),
)
.unwrap();
let amount_in = calculate_amount_in_range(
sqrt_price_current_x64,
sqrt_price_target_x64,
liquidity,
zero_for_one,
is_base_input,
block_timestamp,
)?;
if amount_in.is_some() {
swap_step.amount_in = amount_in.unwrap();
}
swap_step.sqrt_price_next_x64 =
if amount_in.is_some() && amount_remaining_less_fee >= swap_step.amount_in {
sqrt_price_target_x64
} else {
sqrt_price_math::get_next_sqrt_price_from_input(
sqrt_price_current_x64,
liquidity,
amount_remaining_less_fee,
zero_for_one,
)
};
} else {
let amount_out = calculate_amount_in_range(
sqrt_price_current_x64,
sqrt_price_target_x64,
liquidity,
zero_for_one,
is_base_input,
block_timestamp,
)?;
if amount_out.is_some() {
swap_step.amount_out = amount_out.unwrap();
}
// In exact output case, amount_remaining is negative
swap_step.sqrt_price_next_x64 =
if amount_out.is_some() && amount_remaining >= swap_step.amount_out {
sqrt_price_target_x64
} else {
sqrt_price_math::get_next_sqrt_price_from_output(
sqrt_price_current_x64,
liquidity,
amount_remaining,
zero_for_one,
)
}
}
// whether we reached the max possible price for the given ticks
let max = sqrt_price_target_x64 == swap_step.sqrt_price_next_x64;
// get the input / output amounts when target price is not reached
if zero_for_one {
// if max is reached for exact input case, entire amount_in is needed
if !(max && is_base_input) {
swap_step.amount_in = liquidity_math::get_delta_amount_0_unsigned(
swap_step.sqrt_price_next_x64,
sqrt_price_current_x64,
liquidity,
true,
)?
};
// if max is reached for exact output case, entire amount_out is needed
if !(max && !is_base_input) {
swap_step.amount_out = liquidity_math::get_delta_amount_1_unsigned(
swap_step.sqrt_price_next_x64,
sqrt_price_current_x64,
liquidity,
false,
)?;
};
} else {
if !(max && is_base_input) {
swap_step.amount_in = liquidity_math::get_delta_amount_1_unsigned(
sqrt_price_current_x64,
swap_step.sqrt_price_next_x64,
liquidity,
true,
)?
};
if !(max && !is_base_input) {
swap_step.amount_out = liquidity_math::get_delta_amount_0_unsigned(
sqrt_price_current_x64,
swap_step.sqrt_price_next_x64,
liquidity,
false,
)?
};
}
// For exact output case, cap the output amount to not exceed the remaining output amount
if !is_base_input && swap_step.amount_out > amount_remaining {
swap_step.amount_out = amount_remaining;
}
swap_step.fee_amount =
if is_base_input && swap_step.sqrt_price_next_x64 != sqrt_price_target_x64 {
// we didn't reach the target, so take the remainder of the maximum input as fee
// swap dust is granted as fee
u64::from(amount_remaining)
.checked_sub(swap_step.amount_in)
.unwrap()
} else {
// take pip percentage as fee
swap_step
.amount_in
.mul_div_ceil(
fee_rate.into(),
(FEE_RATE_DENOMINATOR_VALUE - fee_rate).into(),
)
.unwrap()
};
Ok(swap_step)
}
/// Pre calcumate amount_in or amount_out for the specified price range
/// The amount maybe overflow of u64 due to the `sqrt_price_target_x64` maybe unreasonable.
/// Therefore, this situation needs to be handled in `compute_swap_step` to recalculate the price that can be reached based on the amount.
#[cfg(not(test))]
fn calculate_amount_in_range(
sqrt_price_current_x64: u128,
sqrt_price_target_x64: u128,
liquidity: u128,
zero_for_one: bool,
is_base_input: bool,
_block_timestamp: u32,
) -> Result<Option<u64>> {
if is_base_input {
let result = if zero_for_one {
liquidity_math::get_delta_amount_0_unsigned(
sqrt_price_target_x64,
sqrt_price_current_x64,
liquidity,
true,
)
} else {
liquidity_math::get_delta_amount_1_unsigned(
sqrt_price_current_x64,
sqrt_price_target_x64,
liquidity,
true,
)
};
if result.is_ok() {
return Ok(Some(result.unwrap()));
} else {
if result.err().unwrap() == crate::error::ErrorCode::MaxTokenOverflow.into() {
return Ok(None);
} else {
return Err(ErrorCode::SqrtPriceLimitOverflow.into());
}
}
} else {
let result = if zero_for_one {
liquidity_math::get_delta_amount_1_unsigned(
sqrt_price_target_x64,
sqrt_price_current_x64,
liquidity,
false,
)
} else {
liquidity_math::get_delta_amount_0_unsigned(
sqrt_price_current_x64,
sqrt_price_target_x64,
liquidity,
false,
)
};
if result.is_ok() {
return Ok(Some(result.unwrap()));
} else {
if result.err().unwrap() == crate::error::ErrorCode::MaxTokenOverflow.into() {
return Ok(None);
} else {
return Err(ErrorCode::SqrtPriceLimitOverflow.into());
}
}
}
}
#[cfg(test)]
fn calculate_amount_in_range(
sqrt_price_current_x64: u128,
sqrt_price_target_x64: u128,
liquidity: u128,
zero_for_one: bool,
is_base_input: bool,
block_timestamp: u32,
) -> Result<Option<u64>> {
if is_base_input {
let result = if zero_for_one {
liquidity_math::get_delta_amount_0_unsigned(
sqrt_price_target_x64,
sqrt_price_current_x64,
liquidity,
true,
)
} else {
liquidity_math::get_delta_amount_1_unsigned(
sqrt_price_current_x64,
sqrt_price_target_x64,
liquidity,
true,
)
};
if block_timestamp == 0 {
if result.is_err() {
return Err(ErrorCode::MaxTokenOverflow.into());
} else {
return Ok(Some(result.unwrap()));
}
}
if result.is_ok() {
return Ok(Some(result.unwrap()));
} else {
if result.err().unwrap() == crate::error::ErrorCode::MaxTokenOverflow.into() {
return Ok(None);
} else {
return Err(ErrorCode::SqrtPriceLimitOverflow.into());
}
}
} else {
let result = if zero_for_one {
liquidity_math::get_delta_amount_1_unsigned(
sqrt_price_target_x64,
sqrt_price_current_x64,
liquidity,
false,
)
} else {
liquidity_math::get_delta_amount_0_unsigned(
sqrt_price_current_x64,
sqrt_price_target_x64,
liquidity,
false,
)
};
if result.is_ok() || block_timestamp == 0 {
return Ok(Some(result.unwrap()));
} else {
if result.err().unwrap() == crate::error::ErrorCode::MaxTokenOverflow.into() {
return Ok(None);
} else {
return Err(ErrorCode::SqrtPriceLimitOverflow.into());
}
}
}
}
#[cfg(test)]
mod swap_math_test {
use crate::libraries::tick_math;
use super::*;
use proptest::prelude::*;
proptest! {
#[test]
fn compute_swap_step_test(
sqrt_price_current_x64 in tick_math::MIN_SQRT_PRICE_X64..tick_math::MAX_SQRT_PRICE_X64,
sqrt_price_target_x64 in tick_math::MIN_SQRT_PRICE_X64..tick_math::MAX_SQRT_PRICE_X64,
liquidity in 1..u32::MAX as u128,
amount_remaining in 1..u64::MAX,
fee_rate in 1..FEE_RATE_DENOMINATOR_VALUE/2,
is_base_input in proptest::bool::ANY,
) {
prop_assume!(sqrt_price_current_x64 != sqrt_price_target_x64);
let zero_for_one = sqrt_price_current_x64 > sqrt_price_target_x64;
let swap_step = compute_swap_step(
sqrt_price_current_x64,
sqrt_price_target_x64,
liquidity,
amount_remaining,
fee_rate,
is_base_input,
zero_for_one,
1,
).unwrap();
let amount_in = swap_step.amount_in;
let amount_out = swap_step.amount_out;
let sqrt_price_next_x64 = swap_step.sqrt_price_next_x64;
let fee_amount = swap_step.fee_amount;
let amount_used = if is_base_input {
amount_in + fee_amount
} else {
amount_out
};
if sqrt_price_next_x64 != sqrt_price_target_x64 {
assert!(amount_used == amount_remaining);
} else {
assert!(amount_used <= amount_remaining);
}
let price_lower = sqrt_price_current_x64.min(sqrt_price_target_x64);
let price_upper = sqrt_price_current_x64.max(sqrt_price_target_x64);
assert!(sqrt_price_next_x64 >= price_lower);
assert!(sqrt_price_next_x64 <= price_upper);
}
}
}