hyperdrive_math/lp/
utils.rs

1use ethers::types::{I256, U256};
2use eyre::Result;
3use fixedpointmath::fixed;
4
5use crate::State;
6
7impl State {
8    // Helper method that calculates the average time remaining for the longs
9    // and shorts and the net curve trade.
10    pub fn calculate_net_curve_trade_from_timestamp(
11        &self,
12        current_block_timestamp: U256,
13    ) -> Result<I256> {
14        // To keep precision of long and short average maturity time (from contract call)
15        // we scale the block timestamp and position duration by 1e18 to calculate
16        // the normalized time remaining.
17        let long_average_time_remaining = self.calculate_scaled_normalized_time_remaining(
18            self.long_average_maturity_time(),
19            current_block_timestamp,
20        );
21        let short_average_time_remaining = self.calculate_scaled_normalized_time_remaining(
22            self.short_average_maturity_time(),
23            current_block_timestamp,
24        );
25
26        self.calculate_net_curve_trade(long_average_time_remaining, short_average_time_remaining)
27    }
28
29    /// Calculates the result of closing the net flat position.
30    pub fn calculate_net_flat_trade_from_timestamp(
31        &self,
32        current_block_timestamp: U256,
33    ) -> Result<I256> {
34        let long_average_time_remaining = self.calculate_scaled_normalized_time_remaining(
35            self.long_average_maturity_time(),
36            current_block_timestamp,
37        );
38        let short_average_time_remaining = self.calculate_scaled_normalized_time_remaining(
39            self.short_average_maturity_time(),
40            current_block_timestamp,
41        );
42
43        self.calculate_net_flat_trade(long_average_time_remaining, short_average_time_remaining)
44    }
45
46    /// Gets the resulting state when updating liquidity.
47    pub fn get_state_after_liquidity_update(&self, share_reserves_delta: I256) -> Result<State> {
48        let share_reserves = self.share_reserves();
49        let share_adjustment = self.share_adjustment();
50        let bond_reserves = self.bond_reserves();
51        let minimum_share_reserves = self.minimum_share_reserves();
52
53        // Calculate new reserve and adjustment levels.
54        let (updated_share_reserves, updated_share_adjustment, updated_bond_reserves) = match self
55            .calculate_update_liquidity(
56                share_reserves,
57                share_adjustment,
58                bond_reserves,
59                minimum_share_reserves,
60                share_reserves_delta,
61            ) {
62            Ok(result) => result,
63            Err(_) => (fixed!(0), I256::from(0), fixed!(0)),
64        };
65
66        // Update and return the new state.
67        let mut new_info = self.info.clone();
68        new_info.share_reserves = U256::from(updated_share_reserves);
69        new_info.share_adjustment = updated_share_adjustment;
70        new_info.bond_reserves = U256::from(updated_bond_reserves);
71        Ok(State {
72            config: self.config.clone(),
73            info: new_info,
74        })
75    }
76}