Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max short cleanup #193

Draft
wants to merge 38 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cb26810
add retries to txns to account for anvil failure
dpaiton Dec 3, 2024
6e314ad
add test tolerance
dpaiton Dec 3, 2024
ee0901d
bump up tolerance
dpaiton Dec 3, 2024
0c8ed69
increase tolerance
dpaiton Dec 3, 2024
4452746
fix typo
dpaiton Dec 3, 2024
11a6ce7
add retry loop
dpaiton Dec 3, 2024
542afc2
fix import
dpaiton Dec 3, 2024
2ff99c3
clone logs before filtering
dpaiton Dec 3, 2024
4d8c79e
bump hyperdrive to 1.0.20
dpaiton Nov 26, 2024
49e9fbb
ignore build.rs
dpaiton Nov 27, 2024
51df4eb
add adminController argument to deploy fn
dpaiton Nov 27, 2024
43ff243
add instructions for debugging
dpaiton Dec 2, 2024
fd89887
fix test
dpaiton Dec 2, 2024
208cb2e
fix registry to deploy & initialize
dpaiton Dec 2, 2024
f3e75a5
code cleanup
dpaiton Dec 2, 2024
804b1b4
deploy factory in test deploy
dpaiton Dec 2, 2024
b71ee2f
update factory config to match Hyperdrive factory.ts deployer defaults
dpaiton Dec 2, 2024
21e504c
add test tolerance
dpaiton Dec 2, 2024
1e98853
clean up comments for consistency
dpaiton Oct 2, 2024
28b4d7b
add a failing test
dpaiton Nov 24, 2024
ce396b4
update docstring
dpaiton Nov 24, 2024
44fcbf0
rename fn
dpaiton Nov 24, 2024
1e4d839
calc_spot_price_up
dpaiton Nov 24, 2024
cd9394e
move calculate to own fn
dpaiton Nov 24, 2024
c6edcf3
solvency_after_short cleanup & comments on checks.
dpaiton Nov 24, 2024
338eb4c
more iterations so the test passes
dpaiton Nov 24, 2024
b6a9857
adds docs to explain final share reserves
dpaiton Nov 24, 2024
c852acf
move min share calc to own fn
dpaiton Nov 24, 2024
0272fd7
rework the abs max short guess fn
dpaiton Nov 24, 2024
6053dad
put this back
dpaiton Nov 25, 2024
b148198
remove test
dpaiton Nov 25, 2024
12f2a29
lint fixes; update hyperdrivetypes
dpaiton Nov 25, 2024
cf542a2
add failing test
dpaiton Dec 4, 2024
d238ae6
remove spot price argument
dpaiton Dec 4, 2024
f2d377b
skips 51% of tests
dpaiton Dec 4, 2024
194071a
fix test
dpaiton Dec 4, 2024
d563ede
abs max short
dpaiton Nov 24, 2024
2751b01
parity test will not pass until we update solidity
dpaiton Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
skips 51% of tests
  • Loading branch information
dpaiton committed Dec 4, 2024
commit f2d377b288800f863f4c0dd4ee4ea464277d2571
33 changes: 19 additions & 14 deletions crates/hyperdrive-math/src/short/max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ impl State {
// Calculate the next iteration of Newton's method. If the candidate
// is larger than the absolute max, we've gone too far and something
// has gone wrong.
let derivative = match self.solvency_after_short_derivative(max_bond_guess) {
let derivative = match self.solvency_after_short_derivative_negation(max_bond_guess) {
Ok(derivative) => derivative,
Err(_) => break,
};
Expand Down Expand Up @@ -745,7 +745,7 @@ impl State {
/// Since solvency decreases as the short amount increases, we negate the
/// derivative. This avoids issues with the fixed point library which
/// doesn't support negative values.
fn solvency_after_short_derivative(
fn solvency_after_short_derivative_negation(
&self,
bond_amount: FixedPoint<U256>,
) -> Result<FixedPoint<U256>> {
Expand Down Expand Up @@ -787,10 +787,11 @@ mod tests {

#[tokio::test]
async fn fuzz_solvency_after_short_derivative() -> Result<()> {
let empirical_derivative_epsilon = fixed!(1e5);
let empirical_derivative_epsilon = fixed!(1e14);
let test_tolerance = fixed!(1e14);
let mut rng = thread_rng();
for _ in 0..*FAST_FUZZ_RUNS {
for iter in 0..*FAST_FUZZ_RUNS {
println!("iter {:#?}", iter);
let state = rng.gen::<State>();
let checkpoint_exposure = {
let value = rng.gen_range(fixed!(0)..=FixedPoint::from(U256::from(U128::MAX)));
Expand All @@ -804,20 +805,19 @@ mod tests {
// Min trade amount should be at least 1,000x the derivative epsilon
let bond_amount = rng.gen_range(fixed!(1e18)..=fixed!(10_000_000e18));

let input = bond_amount;
let f_x = match panic::catch_unwind(|| {
state.solvency_after_short(bond_amount, checkpoint_exposure)
state.solvency_after_short(input, checkpoint_exposure)
}) {
Ok(result) => match result {
Ok(result) => result,
Err(_) => continue, // The amount resulted in the pool being insolvent.
},
Err(_) => continue, // Overflow or underflow error from FixedPoint<U256>.
};
let input = bond_amount + empirical_derivative_epsilon;
let f_x_plus_delta = match panic::catch_unwind(|| {
state.solvency_after_short(
bond_amount + empirical_derivative_epsilon,
checkpoint_exposure,
)
state.solvency_after_short(input, checkpoint_exposure)
}) {
Ok(result) => match result {
Ok(result) => result,
Expand All @@ -826,14 +826,19 @@ mod tests {
Err(_) => continue, // Overflow or underflow error from FixedPoint<U256>.
};

// Sanity check the sign of the difference.
// Greater bond amount results in less solvency.
assert!(f_x < f_x_plus_delta);
// Because the computation includes a linear fee term subtracted
// from a non-linear short principal, it is possible for the linear
// estimate here to be non-monotonic. In this case, we want to skip
// the test because the estimate will never match the true value.
if f_x <= f_x_plus_delta {
continue;
}

// Compute the empirical and analytical derivatives.
let empirical_derivative = (f_x_plus_delta - f_x) / empirical_derivative_epsilon;
// We are actually computing the negative derivative to keep the sign positive.
let empirical_derivative = (f_x - f_x_plus_delta) / empirical_derivative_epsilon;
let solvency_after_short_derivative =
state.solvency_after_short_derivative(bond_amount)?;
state.solvency_after_short_derivative_negation(bond_amount)?;

// Ensure that the empirical and analytical derivatives match.
let derivative_diff = if empirical_derivative > solvency_after_short_derivative {
Expand Down