-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmax.rs
729 lines (680 loc) · 29.8 KB
/
max.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use ethers::types::I256;
use fixed_point::FixedPoint;
use fixed_point_macros::{fixed, int256};
use crate::{State, YieldSpace};
impl State {
/// Calculates the pool's max spot price.
///
/// Hyperdrive has assertions to ensure that traders don't purchase bonds at
/// negative interest rates. The maximum spot price that longs can push the
/// market to is given by:
///
/// $$
/// p_max = \frac{1 - \phi_f}{1 + \phi_c * \left( p_0^{-1} - 1 \right) * \left( \phi_f - 1 \right)}
/// $$
pub fn calculate_max_spot_price(&self) -> FixedPoint {
(fixed!(1e18) - self.flat_fee())
/ (fixed!(1e18)
+ self
.curve_fee()
.mul_up(fixed!(1e18).div_up(self.calculate_spot_price()) - fixed!(1e18)))
.mul_up(fixed!(1e18) - self.flat_fee())
}
/// Calculates the pool's solvency.
///
/// $$
/// s = z - \tfrac{exposure}{c} - z_min
/// $$
pub fn calculate_solvency(&self) -> FixedPoint {
self.share_reserves()
- self.long_exposure() / self.vault_share_price()
- self.minimum_share_reserves()
}
/// Calculates the max long that can be opened given a budget.
///
/// We start by calculating the long that brings the pool's spot price to 1.
/// If we are solvent at this point, then we're done. Otherwise, we approach
/// the max long iteratively using Newton's method.
pub fn calculate_max_long<F: Into<FixedPoint>, I: Into<I256>>(
&self,
budget: F,
checkpoint_exposure: I,
maybe_max_iterations: Option<usize>,
) -> FixedPoint {
let budget = budget.into();
let checkpoint_exposure = checkpoint_exposure.into();
// Calculate the maximum long that brings the spot price to 1. If the pool is
// solvent after opening this long, then we're done.
let (absolute_max_base_amount, absolute_max_bond_amount) = self.absolute_max_long();
if self
.solvency_after_long(
absolute_max_base_amount,
absolute_max_bond_amount,
checkpoint_exposure,
)
.is_some()
{
return absolute_max_base_amount.min(budget);
}
// Use Newton's method to iteratively approach a solution. We use pool's
// solvency $S(x)$ as our objective function, which will converge to the
// amount of base that needs to be paid to open the maximum long. The
// derivative of $S(x)$ is negative (since solvency decreases as more
// longs are opened). The fixed point library doesn't support negative
// numbers, so we use the negation of the derivative to side-step the
// issue.
//
// Given the current guess of $x_n$, Newton's method gives us an updated
// guess of $x_{n+1}$:
//
// $$
// x_{n+1} = x_n - \tfrac{S(x_n)}{S'(x_n)} = x_n + \tfrac{S(x_n)}{-S'(x_n)}
// $$
//
// The guess that we make is very important in determining how quickly
// we converge to the solution.
let mut max_base_amount =
self.max_long_guess(absolute_max_base_amount, checkpoint_exposure);
let mut maybe_solvency = self.solvency_after_long(
max_base_amount,
self.calculate_open_long(max_base_amount).unwrap(),
checkpoint_exposure,
);
if maybe_solvency.is_none() {
panic!("Initial guess in `calculate_max_long` is insolvent.");
}
let mut solvency = maybe_solvency.unwrap();
for _ in 0..maybe_max_iterations.unwrap_or(7) {
// If the max base amount is equal to or exceeds the absolute max,
// we've gone too far and the calculation deviated from reality at
// some point.
if max_base_amount >= absolute_max_base_amount {
panic!("Reached absolute max bond amount in `calculate_max_long`.");
}
// If the max base amount exceeds the budget, we know that the
// entire budget can be consumed without running into solvency
// constraints.
if max_base_amount >= budget {
return budget;
}
// TODO: It may be better to gracefully handle crossing over the
// root by extending the fixed point math library to handle negative
// numbers or even just using an if-statement to handle the negative
// numbers.
//
// Proceed to the next step of Newton's method. Once we have a
// candidate solution, we check to see if the pool is solvent after
// a long is opened with the candidate amount. If the pool isn't
// solvent, then we're done.
let maybe_derivative = self.solvency_after_long_derivative(max_base_amount);
if maybe_derivative.is_none() {
break;
}
let possible_max_base_amount = max_base_amount + solvency / maybe_derivative.unwrap();
maybe_solvency = self.solvency_after_long(
possible_max_base_amount,
self.calculate_open_long(possible_max_base_amount).unwrap(),
checkpoint_exposure,
);
if let Some(s) = maybe_solvency {
solvency = s;
max_base_amount = possible_max_base_amount;
} else {
break;
}
}
// Ensure that the final result is less than the absolute max and clamp
// to the budget.
if max_base_amount >= absolute_max_base_amount {
panic!("Reached absolute max bond amount in `calculate_max_long`.");
}
if max_base_amount >= budget {
return budget;
}
max_base_amount
}
/// Calculates the largest long that can be opened without buying bonds at a
/// negative interest rate. This calculation does not take Hyperdrive's
/// solvency constraints into account and shouldn't be used directly.
fn absolute_max_long(&self) -> (FixedPoint, FixedPoint) {
// We are targeting the pool's max spot price of:
//
// p_max = (1 - flatFee) / (1 + curveFee * (1 / p_0 - 1) * (1 - flatFee))
//
// We can derive a formula for the target bond reserves y_t in
// terms of the target share reserves z_t as follows:
//
// p_max = ((mu * z_t) / y_t) ** t_s
//
// =>
//
// y_t = (mu * z_t) * ((1 + curveFee * (1 / p_0 - 1) * (1 - flatFee)) / (1 - flatFee)) ** (1 / t_s)
//
// Our equation for price is the inverse of that used by YieldSpace, which must be considered when
// deriving the invariant from the price equation.
// With this in mind, we can use this formula to solve our YieldSpace invariant for z_t:
//
// k = (c / mu) * (mu * z_t) ** (1 - t_s) +
// (
// (mu * z_t) * ((1 + curveFee * (1 / p_0 - 1) * (1 - flatFee)) / (1 - flatFee)) ** (1 / t_s)
// ) ** (1 - t_s)
//
// =>
//
// z_t = (1 / mu) * (
// k / (
// (c / mu) +
// ((1 + curveFee * (1 / p_0 - 1) * (1 - flatFee)) / (1 - flatFee)) ** ((1 - t_s) / t_s))
// )
// ) ** (1 / (1 - t_s))
let inner = (self.k_down()
/ (self
.vault_share_price()
.div_up(self.initial_vault_share_price())
+ ((fixed!(1e18)
+ self
.curve_fee()
.mul_up(fixed!(1e18).div_up(self.calculate_spot_price()) - fixed!(1e18))
.mul_up(fixed!(1e18) - self.flat_fee()))
.div_up(fixed!(1e18) - self.flat_fee()))
.pow((fixed!(1e18) - self.time_stretch()) / (self.time_stretch()))))
.pow(fixed!(1e18) / (fixed!(1e18) - self.time_stretch()));
let target_share_reserves = inner / self.initial_vault_share_price();
// Now that we have the target share reserves, we can calculate the
// target bond reserves using the formula:
//
// y_t = (mu * z_t) * ((1 + curveFee * (1 / p_0 - 1) * (1 - flatFee)) / (1 - flatFee)) ** (1 / t_s)
//
// `inner` as defined above is `mu * z_t` so we calculate y_t as
//
// y_t = inner * ((1 + curveFee * (1 / p_0 - 1) * (1 - flatFee)) / (1 - flatFee)) ** (1 / t_s)
let target_bond_reserves = inner
* ((fixed!(1e18)
+ self.curve_fee()
* (fixed!(1e18) / (self.calculate_spot_price()) - fixed!(1e18))
* (fixed!(1e18) - self.flat_fee()))
/ (fixed!(1e18) - self.flat_fee()))
.pow(fixed!(1e18).div_up(self.time_stretch()));
// The absolute max base amount is given by:
//
// absoluteMaxBaseAmount = c * (z_t - z)
let absolute_max_base_amount =
(target_share_reserves - self.effective_share_reserves()) * self.vault_share_price();
// The absolute max bond amount is given by:
//
// absoluteMaxBondAmount = (y - y_t) - c(x)
let absolute_max_bond_amount = (self.bond_reserves() - target_bond_reserves)
- self.open_long_curve_fees(absolute_max_base_amount);
(absolute_max_base_amount, absolute_max_bond_amount)
}
/// Calculates an initial guess of the max long that can be opened. This is a
/// reasonable estimate that is guaranteed to be less than the true max
/// long. We use this to get a reasonable starting point for Newton's
/// method.
fn max_long_guess(
&self,
absolute_max_base_amount: FixedPoint,
checkpoint_exposure: I256,
) -> FixedPoint {
// Calculate an initial estimate of the max long by using the spot price as
// our conservative price.
let spot_price = self.calculate_spot_price();
let guess = self.max_long_estimate(spot_price, spot_price, checkpoint_exposure);
// We know that the spot price is 1 when the absolute max base amount is
// used to open a long. We also know that our spot price isn't a great
// estimate (conservative or otherwise) of the realized price that the
// max long will pay, so we calculate a better estimate of the realized
// price by interpolating between the spot price and 1 depending on how
// large the estimate is.
let t = (guess / absolute_max_base_amount)
.pow(fixed!(1e18).div_up(fixed!(1e18) - self.time_stretch()))
* fixed!(0.8e18);
let estimate_price = spot_price * (fixed!(1e18) - t) + fixed!(1e18) * t;
// Recalculate our intial guess using the bootstrapped conservative
// estimate of the realized price.
self.max_long_estimate(estimate_price, spot_price, checkpoint_exposure)
}
/// Estimates the max long based on the pool's current solvency and a
/// conservative price estimate, $p_r$.
///
/// We can use our estimate price $p_r$ to approximate $y(x)$ as
/// $y(x) \approx p_r^{-1} \cdot x - c(x)$. Plugging this into our
/// solvency function $s(x)$, we can calculate the share reserves and
/// exposure after opening a long with $x$ base as:
///
/// \begin{aligned}
/// z(x) &= z_0 + \tfrac{x - g(x)}{c} \\
/// e(x) &= e_0 + min(exposure_{c}, 0) + 2 \cdot y(x) - x + g(x) \\
/// &= e_0 + min(exposure_{c}, 0) + 2 \cdot p_r^{-1} \cdot x -
/// 2 \cdot c(x) - x + g(x)
/// \end{aligned}
///
/// We debit and negative checkpoint exposure from $e_0$ since the
/// global exposure doesn't take into account the negative exposure
/// from non-netted shorts in the checkpoint. These forumulas allow us
/// to calculate the approximate ending solvency of:
///
/// $$
/// s(x) \approx z(x) - \tfrac{e(x) - min(exposure_{c}, 0)}{c} - z_{min}
/// $$
///
/// If we let the initial solvency be given by $s_0$, we can solve for
/// $x$ as:
///
/// $$
/// x = \frac{c}{2} \cdot \frac{s_0 + min(exposure_{c}, 0)}{
/// p_r^{-1} +
/// \phi_{g} \cdot \phi_{c} \cdot \left( 1 - p \right) -
/// 1 -
/// \phi_{c} \cdot \left( p^{-1} - 1 \right)
/// }
/// $$
fn max_long_estimate(
&self,
estimate_price: FixedPoint,
spot_price: FixedPoint,
checkpoint_exposure: I256,
) -> FixedPoint {
let checkpoint_exposure = FixedPoint::from(-checkpoint_exposure.min(int256!(0)));
let mut estimate =
self.calculate_solvency() + checkpoint_exposure / self.vault_share_price();
estimate = estimate.mul_div_down(self.vault_share_price(), fixed!(2e18));
estimate /= fixed!(1e18) / estimate_price
+ self.governance_lp_fee() * self.curve_fee() * (fixed!(1e18) - spot_price)
- fixed!(1e18)
- self.curve_fee() * (fixed!(1e18) / spot_price - fixed!(1e18));
estimate
}
/// Calculates the solvency of the pool $S(x)$ after a long is opened with a base
/// amount $x$.
///
/// Since longs can net out with shorts in this checkpoint, we decrease
/// the global exposure variable by any negative exposure we have
/// in the checkpoint. The pool's solvency is calculated as:
///
/// $$
/// s = z - \tfrac{exposure + min(exposure_{checkpoint}, 0)}{c} - z_{min}
/// $$
///
/// When a long is opened, the share reserves $z$ increase by:
///
/// $$
/// \Delta z = \tfrac{x - g(x)}{c}
/// $$
///
/// Opening the long increases the non-netted longs by the bond amount. From
/// this, the change in the exposure is given by:
///
/// $$
/// \Delta exposure = y(x)
/// $$
///
/// From this, we can calculate $S(x)$ as:
///
/// $$
/// S(x) = \left( z + \Delta z \right) - \left(
/// \tfrac{exposure + min(exposure_{checkpoint}, 0) + \Delta exposure}{c}
/// \right) - z_{min}
/// $$
///
/// It's possible that the pool is insolvent after opening a long. In this
/// case, we return `None` since the fixed point library can't represent
/// negative numbers.
pub(super) fn solvency_after_long(
&self,
base_amount: FixedPoint,
bond_amount: FixedPoint,
checkpoint_exposure: I256,
) -> Option<FixedPoint> {
let governance_fee = self.open_long_governance_fee(base_amount);
let share_reserves = self.share_reserves() + base_amount / self.vault_share_price()
- governance_fee / self.vault_share_price();
let exposure = self.long_exposure() + bond_amount;
let checkpoint_exposure = FixedPoint::from(-checkpoint_exposure.min(int256!(0)));
if share_reserves + checkpoint_exposure / self.vault_share_price()
>= exposure / self.vault_share_price() + self.minimum_share_reserves()
{
Some(
share_reserves + checkpoint_exposure / self.vault_share_price()
- exposure / self.vault_share_price()
- self.minimum_share_reserves(),
)
} else {
None
}
}
/// Calculates the negation of the derivative of the pool's solvency with respect
/// to the base amount that the long pays.
///
/// The derivative of the pool's solvency $S(x)$ with respect to the base
/// amount that the long pays is given by:
///
/// $$
/// S'(x) = \tfrac{1}{c} \cdot \left( 1 - y'(x) - \phi_{g} \cdot p \cdot c'(x) \right) \\
/// = \tfrac{1}{c} \cdot \left(
/// 1 - y'(x) - \phi_{g} \cdot \phi_{c} \cdot \left( 1 - p \right)
/// \right)
/// $$
///
/// This derivative is negative since solvency decreases as more longs are
/// opened. We use the negation of the derivative to stay in the positive
/// domain, which allows us to use the fixed point library.
pub(super) fn solvency_after_long_derivative(
&self,
base_amount: FixedPoint,
) -> Option<FixedPoint> {
let maybe_derivative = self.long_amount_derivative(base_amount);
maybe_derivative.map(|derivative| {
(derivative
+ self.governance_lp_fee()
* self.curve_fee()
* (fixed!(1e18) - self.calculate_spot_price())
- fixed!(1e18))
.mul_div_down(fixed!(1e18), self.vault_share_price())
})
}
/// Calculates the derivative of [long_amount](long_amount) with respect to the
/// base amount.
///
/// We calculate the derivative of the long amount $y(x)$ as:
///
/// $$
/// y'(x) = y_{*}'(x) - c'(x)
/// $$
///
/// Where $y_{*}'(x)$ is the derivative of $y_{*}(x)$ and $c'(x)$ is the
/// derivative of [$c(x)$](long_curve_fee). $y_{*}'(x)$ is given by:
///
/// $$
/// y_{*}'(x) = \left( \mu \cdot (z + \tfrac{x}{c}) \right)^{-t_s}
/// \left(
/// k - \tfrac{c}{\mu} \cdot
/// \left(
/// \mu \cdot (z + \tfrac{x}{c}
/// \right)^{1 - t_s}
/// \right)^{\tfrac{t_s}{1 - t_s}}
/// $$
///
/// and $c'(x)$ is given by:
///
/// $$
/// c'(x) = \phi_{c} \cdot \left( \tfrac{1}{p} - 1 \right)
/// $$
pub(super) fn long_amount_derivative(&self, base_amount: FixedPoint) -> Option<FixedPoint> {
let share_amount = base_amount / self.vault_share_price();
let inner =
self.initial_vault_share_price() * (self.effective_share_reserves() + share_amount);
let mut derivative = fixed!(1e18) / (inner).pow(self.time_stretch());
// It's possible that k is slightly larger than the rhs in the inner
// calculation. If this happens, we are close to the root, and we short
// circuit.
let k = self.k_down();
let rhs = self.vault_share_price().mul_div_down(
inner.pow(self.time_stretch()),
self.initial_vault_share_price(),
);
if k < rhs {
return None;
}
derivative *= (k - rhs).pow(
self.time_stretch()
.div_up(fixed!(1e18) - self.time_stretch()),
);
// Finish computing the derivative.
derivative -=
self.curve_fee() * ((fixed!(1e18) / self.calculate_spot_price()) - fixed!(1e18));
Some(derivative)
}
}
#[cfg(test)]
mod tests {
use std::panic;
use ethers::types::U256;
use eyre::Result;
use fixed_point_macros::uint256;
use hyperdrive_wrappers::wrappers::mock_hyperdrive_math::MaxTradeParams;
use rand::{thread_rng, Rng};
use test_utils::{
chain::TestChain,
constants::{FAST_FUZZ_RUNS, FUZZ_RUNS},
};
use tracing_test::traced_test;
use super::*;
use crate::calculate_effective_share_reserves;
/// This test differentially fuzzes the `absolute_max_long` function against
/// the Solidity analogue `calculateAbsoluteMaxLong`.
#[tokio::test]
async fn fuzz_absolute_max_long() -> Result<()> {
let chain = TestChain::new().await?;
// Fuzz the rust and solidity implementations against each other.
let mut rng = thread_rng();
for _ in 0..*FAST_FUZZ_RUNS {
let state = rng.gen::<State>();
let actual = panic::catch_unwind(|| state.absolute_max_long());
match chain
.mock_hyperdrive_math()
.calculate_absolute_max_long(
MaxTradeParams {
share_reserves: state.info.share_reserves,
bond_reserves: state.info.bond_reserves,
longs_outstanding: state.info.longs_outstanding,
long_exposure: state.info.long_exposure,
share_adjustment: state.info.share_adjustment,
time_stretch: state.config.time_stretch,
vault_share_price: state.info.vault_share_price,
initial_vault_share_price: state.config.initial_vault_share_price,
minimum_share_reserves: state.config.minimum_share_reserves,
curve_fee: state.config.fees.curve,
flat_fee: state.config.fees.flat,
governance_lp_fee: state.config.fees.governance_lp,
},
calculate_effective_share_reserves(
state.info.share_reserves.into(),
state.info.share_adjustment,
)
.into(),
state.calculate_spot_price().into(),
)
.call()
.await
{
Ok((expected_base_amount, expected_bond_amount)) => {
let (actual_base_amount, actual_bond_amount) = actual.unwrap();
assert_eq!(actual_base_amount, FixedPoint::from(expected_base_amount));
assert_eq!(actual_bond_amount, FixedPoint::from(expected_bond_amount));
}
Err(_) => assert!(actual.is_err()),
}
}
Ok(())
}
/// This test differentially fuzzes the `calculate_max_long` function against the
/// Solidity analogue `calculateMaxLong`. `calculateMaxLong` doesn't take
/// a trader's budget into account, so it only provides a subset of
/// `calculate_max_long`'s functionality. With this in mind, we provide
/// `calculate_max_long` with a budget of `U256::MAX` to ensure that the two
/// functions are equivalent.
#[tokio::test]
async fn fuzz_calculate_max_long() -> Result<()> {
let chain = TestChain::new().await?;
// Fuzz the rust and solidity implementations against each other.
let mut rng = thread_rng();
for _ in 0..*FAST_FUZZ_RUNS {
let state = rng.gen::<State>();
let checkpoint_exposure = {
let value = rng.gen_range(fixed!(0)..=FixedPoint::from(I256::MAX));
let sign = rng.gen::<bool>();
if sign {
-I256::try_from(value).unwrap()
} else {
I256::try_from(value).unwrap()
}
};
let actual = panic::catch_unwind(|| {
state.calculate_max_long(U256::MAX, checkpoint_exposure, None)
});
match chain
.mock_hyperdrive_math()
.calculate_max_long(
MaxTradeParams {
share_reserves: state.info.share_reserves,
bond_reserves: state.info.bond_reserves,
longs_outstanding: state.info.longs_outstanding,
long_exposure: state.info.long_exposure,
share_adjustment: state.info.share_adjustment,
time_stretch: state.config.time_stretch,
vault_share_price: state.info.vault_share_price,
initial_vault_share_price: state.config.initial_vault_share_price,
minimum_share_reserves: state.config.minimum_share_reserves,
curve_fee: state.config.fees.curve,
flat_fee: state.config.fees.flat,
governance_lp_fee: state.config.fees.governance_lp,
},
checkpoint_exposure,
uint256!(7),
)
.call()
.await
{
Ok((expected_base_amount, ..)) => {
assert_eq!(actual.unwrap(), FixedPoint::from(expected_base_amount));
}
Err(_) => assert!(actual.is_err()),
}
}
Ok(())
}
/// This test empirically tests the derivative of `long_amount_derivative`
/// by calling `calculate_open_long` at two points and comparing the empirical
/// result with the output of `long_amount_derivative`.
#[traced_test]
#[tokio::test]
async fn test_max_long_derivative() -> Result<()> {
let mut rng = thread_rng();
// We use a relatively large epsilon here due to the underlying fixed point pow
// function not being monotonically increasing.
let empirical_derivative_epsilon = fixed!(1e12);
// TODO pretty big comparison epsilon here
let test_comparison_epsilon = fixed!(10e18);
for _ in 0..*FAST_FUZZ_RUNS {
let state = rng.gen::<State>();
let amount = rng.gen_range(fixed!(10e18)..=fixed!(10_000_000e18));
let p1_result = std::panic::catch_unwind(|| {
state.calculate_open_long(amount - empirical_derivative_epsilon)
});
let p1;
let p2;
match p1_result {
Ok(p) => match p {
Ok(p) => p1 = p,
Err(_) => continue,
},
// If the amount results in the pool being insolvent, skip this iteration
Err(_) => continue,
}
let p2_result = std::panic::catch_unwind(|| {
state.calculate_open_long(amount + empirical_derivative_epsilon)
});
match p2_result {
Ok(p) => match p {
Ok(p) => p2 = p,
Err(_) => continue,
},
// If the amount results in the pool being insolvent, skip this iteration
Err(_) => continue,
}
// Sanity check
assert!(p2 > p1);
let empirical_derivative = (p2 - p1) / (fixed!(2e18) * empirical_derivative_epsilon);
let open_long_derivative = state.long_amount_derivative(amount);
open_long_derivative.map(|derivative| {
let derivative_diff;
if derivative >= empirical_derivative {
derivative_diff = derivative - empirical_derivative;
} else {
derivative_diff = empirical_derivative - derivative;
}
assert!(
derivative_diff < test_comparison_epsilon,
"expected (derivative_diff={}) < (test_comparison_epsilon={}), \
calculated_derivative={}, emperical_derivative={}",
derivative_diff,
test_comparison_epsilon,
derivative,
empirical_derivative
);
});
}
Ok(())
}
#[traced_test]
#[tokio::test]
async fn test_calculate_max_long() -> Result<()> {
// Spawn a test chain and create two agents -- Alice and Bob. Alice
// is funded with a large amount of capital so that she can initialize
// the pool. Bob is funded with a small amount of capital so that we
// can test `calculate_max_long` when budget is the primary constraint.
let mut rng = thread_rng();
let chain = TestChain::new().await?;
let mut alice = chain.alice().await?;
let mut bob = chain.bob().await?;
let config = bob.get_config().clone();
for _ in 0..*FUZZ_RUNS {
// Snapshot the chain.
let id = chain.snapshot().await?;
// Fund Alice and Bob.
let fixed_rate = rng.gen_range(fixed!(0.01e18)..=fixed!(0.1e18));
let contribution = rng.gen_range(fixed!(10_000e18)..=fixed!(500_000_000e18));
let budget = rng.gen_range(fixed!(10e18)..=fixed!(500_000_000e18));
alice.fund(contribution).await?;
bob.fund(budget).await?;
// Alice initializes the pool.
alice.initialize(fixed_rate, contribution, None).await?;
// Some of the checkpoint passes and variable interest accrues.
alice
.checkpoint(alice.latest_checkpoint().await?, uint256!(0), None)
.await?;
let rate = rng.gen_range(fixed!(0)..=fixed!(0.5e18));
alice
.advance_time(
rate,
FixedPoint::from(config.checkpoint_duration) * fixed!(0.5e18),
)
.await?;
// Bob opens a max long.
let max_spot_price = bob.get_state().await?.calculate_max_spot_price();
let max_long = bob.calculate_max_long(None).await?;
let spot_price_after_long = bob
.get_state()
.await?
.calculate_spot_price_after_long(max_long, None)?;
bob.open_long(max_long, None, None).await?;
// One of three things should be true after opening the long:
//
// 1. The pool's spot price reached the max spot price prior to
// considering fees.
// 2. The pool's solvency is close to zero.
// 3. Bob's budget is consumed.
let is_max_price =
max_spot_price - spot_price_after_long.min(max_spot_price) < fixed!(1e15);
let is_solvency_consumed = {
let state = bob.get_state().await?;
let error_tolerance = fixed!(1_000e18).mul_div_down(fixed_rate, fixed!(0.1e18));
state.calculate_solvency() < error_tolerance
};
let is_budget_consumed = {
let error_tolerance = fixed!(1e18);
bob.base() < error_tolerance
};
assert!(
is_max_price || is_solvency_consumed || is_budget_consumed,
"Invalid max long."
);
// Revert to the snapshot and reset the agent's wallets.
chain.revert(id).await?;
alice.reset(Default::default());
bob.reset(Default::default());
}
Ok(())
}
}