Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ impl HyperdriveState {
err
))
})?;

let result = U256::from(result_fp).to_string();
let result = U256::try_from(result_fp)
.map_err(|err| {
PyErr::new::<PyValueError, _>(format!("Failed to convert to U256 {}", err))
})?
.to_string();
Ok(result)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/hyperdrive-math/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl State {
/// ```
pub fn calculate_solvency(&self) -> Result<FixedPoint<U256>> {
let share_reserves = self.share_reserves();
let long_exposure_shares = self.long_exposure() / self.vault_share_price();
let long_exposure_shares = self.long_exposure().div_up(self.vault_share_price());
let min_share_reserves = self.minimum_share_reserves();
if share_reserves > long_exposure_shares + min_share_reserves {
Ok((share_reserves - long_exposure_shares) - min_share_reserves)
Expand Down
32 changes: 18 additions & 14 deletions crates/hyperdrive-math/src/long/max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ mod tests {
#[tokio::test]
async fn fuzz_sol_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 {
Expand Down Expand Up @@ -504,7 +503,6 @@ mod tests {
),
}
}

Ok(())
}

Expand All @@ -516,24 +514,21 @@ mod tests {
/// functions are equivalent.
#[tokio::test]
async fn fuzz_sol_calculate_max_long() -> Result<()> {
let test_tolerance = fixed!(1e13);
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 {
// Snapshot the chain.
let id = chain.snapshot().await?;

// Gen a random state.
let state = rng.gen::<State>();

// Generate a random checkpoint exposure.
let checkpoint_exposure = rng.gen_range(0..=i128::MAX).flip_sign_if(rng.gen()).into();

// Check Solidity against Rust.
let max_iterations = 8usize;
// We need to catch panics because of overflows.
let actual = panic::catch_unwind(|| {
let rust_base_amount = panic::catch_unwind(|| {
state.calculate_max_long(
U256::MAX,
checkpoint_exposure,
Expand Down Expand Up @@ -563,19 +558,28 @@ mod tests {
.call()
.await
{
Ok((expected_base_amount, ..)) => {
assert_eq!(
actual.unwrap().unwrap(),
FixedPoint::from(expected_base_amount)
Ok((sol_base_amount, ..)) => {
let rust_base_amount = rust_base_amount.unwrap().unwrap();
let sol_base_amount = FixedPoint::from(sol_base_amount);
let error = if rust_base_amount > sol_base_amount {
rust_base_amount - sol_base_amount
} else {
sol_base_amount - rust_base_amount
};
assert!(
error <= test_tolerance,
"abs(rust_base_amount={:#?}-sol_base_amount={:#?})={:#?} > tolerance={:#?}",
rust_base_amount,
sol_base_amount,
error,
test_tolerance
);
}
Err(_) => assert!(actual.is_err() || actual.unwrap().is_err()),
Err(_) => assert!(rust_base_amount.is_err() || rust_base_amount.unwrap().is_err()),
}

// Reset chain snapshot.
chain.revert(id).await?;
}

Ok(())
}

Expand Down
Loading