use ethers::types::U256;
use eyre::{eyre, Result};
use fixedpointmath::{fixed, FixedPoint};
use crate::{State, YieldSpace};
impl State {
pub fn calculate_close_long<F: Into<FixedPoint>>(
&self,
bond_amount: F,
maturity_time: U256,
current_time: U256,
) -> Result<FixedPoint> {
let bond_amount = bond_amount.into();
if bond_amount < self.config.minimum_transaction_amount.into() {
return Err(eyre!("MinimumTransactionAmount: Input amount too low"));
}
Ok(
self.calculate_close_long_flat_plus_curve(bond_amount, maturity_time, current_time)?
- self.close_long_curve_fee(bond_amount, maturity_time, current_time)?
- self.close_long_flat_fee(bond_amount, maturity_time, current_time),
)
}
fn calculate_close_long_flat_plus_curve<F: Into<FixedPoint>>(
&self,
bond_amount: F,
maturity_time: U256,
current_time: U256,
) -> Result<FixedPoint> {
let bond_amount = bond_amount.into();
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
let flat = bond_amount.mul_div_down(
fixed!(1e18) - normalized_time_remaining,
self.vault_share_price(),
);
let curve = if normalized_time_remaining > fixed!(0) {
let curve_bonds_in = bond_amount * normalized_time_remaining;
self.calculate_shares_out_given_bonds_in_down(curve_bonds_in)?
} else {
fixed!(0)
};
Ok(flat + curve)
}
}
#[cfg(test)]
mod tests {
use hyperdrive_test_utils::{chain::TestChain, constants::FAST_FUZZ_RUNS};
use rand::{thread_rng, Rng};
use super::*;
#[tokio::test]
async fn fuzz_calculate_close_long_flat_plus_curve() -> Result<()> {
let chain = TestChain::new().await?;
let mut rng = thread_rng();
for _ in 0..*FAST_FUZZ_RUNS {
let state = rng.gen::<State>();
let in_ = rng.gen_range(fixed!(0)..=state.effective_share_reserves()?);
let maturity_time = state.checkpoint_duration();
let current_time = rng.gen_range(fixed!(0)..=maturity_time);
let normalized_time_remaining = state
.calculate_normalized_time_remaining(maturity_time.into(), current_time.into());
let actual = state.calculate_close_long_flat_plus_curve(
in_,
maturity_time.into(),
current_time.into(),
);
match chain
.mock_hyperdrive_math()
.calculate_close_long(
state.effective_share_reserves()?.into(),
state.bond_reserves().into(),
in_.into(),
normalized_time_remaining.into(),
state.t().into(),
state.c().into(),
state.mu().into(),
)
.call()
.await
{
Ok(expected) => assert_eq!(actual.unwrap(), FixedPoint::from(expected.2)),
Err(_) => assert!(actual.is_err()),
}
}
Ok(())
}
#[tokio::test]
async fn test_close_long_min_txn_amount() -> Result<()> {
let mut rng = thread_rng();
let state = rng.gen::<State>();
let result = state.calculate_close_long(
state.config.minimum_transaction_amount - 10,
0.into(),
0.into(),
);
assert!(result.is_err());
Ok(())
}
}