Skip to content

Commit

Permalink
Merge branch 'main' into remove_extra_return_param_from_calc_fees
Browse files Browse the repository at this point in the history
  • Loading branch information
wakamex authored Nov 9, 2023
2 parents ec960b9 + 2f638cb commit e17f442
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 75 deletions.
8 changes: 4 additions & 4 deletions contracts/src/Hyperdrive.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,16 @@ abstract contract Hyperdrive is
positionsClosed = true;
}

// Update the checkpoint and global longExposure
// Update the checkpoint exposure and global long exposure.
if (positionsClosed) {
uint256 maturityTime = _checkpointTime - _positionDuration;
int128 checkpointExposureBefore = int128(
_checkpoints[maturityTime].longExposure
_checkpoints[maturityTime].exposure
);
_checkpoints[maturityTime].longExposure = 0;
_checkpoints[maturityTime].exposure = 0;
_updateLongExposure(
checkpointExposureBefore,
_checkpoints[maturityTime].longExposure
_checkpoints[maturityTime].exposure
);

// Distribute the excess idle to the withdrawal pool.
Expand Down
16 changes: 8 additions & 8 deletions contracts/src/HyperdriveBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ abstract contract HyperdriveBase is
/// @param _maturityTime The maturity time of the position being closed.
/// @param _sharePrice The current share price.
/// @param _isLong True if the position being closed is long.
function _updateCheckpointLongExposureOnClose(
function _updateCheckpointExposureOnClose(
uint256 _bondAmount,
uint256 _shareReservesDelta,
uint256 _bondReservesDelta,
Expand All @@ -320,11 +320,11 @@ abstract contract HyperdriveBase is
AssetId.encodeAssetId(AssetId.AssetIdPrefix.Short, _maturityTime)
];

// We can zero out long exposure when there are no more open positions
// We can zero out exposure when there are no more open positions
if (checkpointLongs == 0 && checkpointShorts == 0) {
_checkpoints[checkpointTime].longExposure = 0;
_checkpoints[checkpointTime].exposure = 0;
} else {
// The long exposure delta is flat + curve amount + the bonds the
// The exposure delta is flat + curve amount + the bonds the
// user is closing:
//
// (dz_user*c - dz*c) + (dy - dz*c) + dy_user
Expand All @@ -337,13 +337,13 @@ abstract contract HyperdriveBase is
_bondAmount).toUint128()
);

// If the position being closed is long, then the long exposure
// decreases by the delta. If it's short, then the long exposure
// If the position being closed is long, then the exposure
// decreases by the delta. If it's short, then the exposure
// increases by the delta.
if (_isLong) {
_checkpoints[checkpointTime].longExposure -= delta;
_checkpoints[checkpointTime].exposure -= delta;
} else {
_checkpoints[checkpointTime].longExposure += delta;
_checkpoints[checkpointTime].exposure += delta;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion contracts/src/HyperdriveLP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ abstract contract HyperdriveLP is IHyperdriveWrite, HyperdriveTWAP {
uint256 lpSharePrice = lpTotalSupply == 0
? 0
: startingPresentValue.divDown(lpTotalSupply);
uint256 baseContribution = _convertToBaseFromOption(_contribution, sharePrice, _options);
emit AddLiquidity(
_options.destination,
lpShares,
vaultShares.mulDown(sharePrice),
baseContribution,
sharePrice,
lpSharePrice
);
Expand Down
18 changes: 9 additions & 9 deletions contracts/src/HyperdriveLong.sol
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
maturityTime
);

// Update the checkpoint and global longExposure
// Update the checkpoint exposure and global long exposure.
uint256 checkpointTime = maturityTime - _positionDuration;
int128 checkpointExposureBefore = int128(
_checkpoints[checkpointTime].longExposure
_checkpoints[checkpointTime].exposure
);
_updateCheckpointLongExposureOnClose(
_updateCheckpointExposureOnClose(
_bondAmount,
shareCurveDelta,
bondReservesDelta,
Expand All @@ -187,7 +187,7 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
);
_updateLongExposure(
checkpointExposureBefore,
_checkpoints[checkpointTime].longExposure
_checkpoints[checkpointTime].exposure
);

// Distribute the excess idle to the withdrawal pool.
Expand All @@ -197,7 +197,7 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
// Withdraw the profit to the trader.
uint256 proceeds = _withdraw(shareProceeds, _options);

// Enforce min user outputs
// Enforce min user outputs.
uint256 baseProceeds = _convertToBaseFromOption(
proceeds,
sharePrice,
Expand Down Expand Up @@ -262,12 +262,12 @@ abstract contract HyperdriveLong is IHyperdriveWrite, HyperdriveLP {
IHyperdrive.Checkpoint storage checkpoint = _checkpoints[
_checkpointTime
];
int128 checkpointExposureBefore = int128(checkpoint.longExposure);
uint128 longExposureDelta = (2 *
int128 checkpointExposureBefore = int128(checkpoint.exposure);
uint128 exposureDelta = (2 *
_bondProceeds -
_shareReservesDelta.mulDown(_sharePrice)).toUint128();
checkpoint.longExposure += int128(longExposureDelta);
_updateLongExposure(checkpointExposureBefore, checkpoint.longExposure);
checkpoint.exposure += int128(exposureDelta);
_updateLongExposure(checkpointExposureBefore, checkpoint.exposure);

// We need to check solvency because longs increase the system's exposure.
if (!_isSolvent(_sharePrice)) {
Expand Down
16 changes: 8 additions & 8 deletions contracts/src/HyperdriveShort.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ abstract contract HyperdriveShort is IHyperdriveWrite, HyperdriveLP {
maturityTime
);

// Update the checkpoint and global longExposure
// Update the checkpoint exposure and global long exposure.
uint256 checkpointTime = maturityTime - _positionDuration;
int128 checkpointExposureBefore = int128(
_checkpoints[checkpointTime].longExposure
_checkpoints[checkpointTime].exposure
);
_updateCheckpointLongExposureOnClose(
_updateCheckpointExposureOnClose(
bondAmount,
shareCurveDelta,
bondReservesDelta,
Expand All @@ -191,7 +191,7 @@ abstract contract HyperdriveShort is IHyperdriveWrite, HyperdriveLP {
);
_updateLongExposure(
checkpointExposureBefore,
_checkpoints[checkpointTime].longExposure
_checkpoints[checkpointTime].exposure
);

// Distribute the excess idle to the withdrawal pool.
Expand Down Expand Up @@ -277,17 +277,17 @@ abstract contract HyperdriveShort is IHyperdriveWrite, HyperdriveLP {
revert IHyperdrive.InvalidShareReserves();
}

// Update the checkpoint's short deposits and decrease the long exposure.
// Update the checkpoint's short deposits and decrease the exposure.
uint256 _latestCheckpoint = _latestCheckpoint();
int128 checkpointExposureBefore = int128(
_checkpoints[_latestCheckpoint].longExposure
_checkpoints[_latestCheckpoint].exposure
);
_checkpoints[_latestCheckpoint].longExposure -= int128(
_checkpoints[_latestCheckpoint].exposure -= int128(
_bondAmount.toUint128()
);
_updateLongExposure(
checkpointExposureBefore,
_checkpoints[_latestCheckpoint].longExposure
_checkpoints[_latestCheckpoint].exposure
);

// Opening a short decreases the system's exposure because the short's
Expand Down
5 changes: 3 additions & 2 deletions contracts/src/interfaces/IHyperdrive.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ interface IHyperdrive is
/// as well as the share price at closing of matured longs and
/// shorts.
uint128 sharePrice;
/// @dev The amount lp exposure on longs.
int128 longExposure;
/// @dev If exposure is positive, then we have net long exposure, otherwise
/// we have net short exposure in the checkpoint.
int128 exposure;
}

struct WithdrawPool {
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/MockHyperdriveMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ contract MockHyperdriveMath {

function calculateMaxLong(
HyperdriveUtils.MaxTradeParams memory _params,
int256 _checkpointLongExposure,
int256 _checkpointExposure,
uint256 _maxIterations
) external pure returns (uint256, uint256) {
(uint256 result1, uint256 result2) = HyperdriveUtils.calculateMaxLong(
_params,
_checkpointLongExposure,
_checkpointExposure,
_maxIterations
);
return (result1, result2);
Expand Down
4 changes: 2 additions & 2 deletions crates/hyperdrive-math/src/long.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl State {
/// amount $x$.
///
/// Since longs can net out with shorts in this checkpoint, we decrease
/// the global exposure variable by any negative long exposure we have
/// the global exposure variable by any negative exposure we have
/// in the checkpoint. The pool's solvency is calculated as:
///
/// $$
Expand All @@ -337,7 +337,7 @@ impl State {
///
/// ```solidity
/// shareReservesDelta = _shareAmount - governanceCurveFee.divDown(_sharePrice);
/// uint128 longExposureDelta = (2 *
/// uint128 exposureDelta = (2 *
/// _bondProceeds -
/// _shareReservesDelta.mulDown(_sharePrice)).toUint128();
/// ```
Expand Down
2 changes: 1 addition & 1 deletion crates/hyperdrive-math/src/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ mod tests {
let state = alice.get_state().await?;
let Checkpoint {
share_price: open_share_price,
long_exposure: checkpoint_exposure,
exposure: checkpoint_exposure,
..
} = alice
.get_checkpoint(state.to_checkpoint(alice.now().await?))
Expand Down
2 changes: 1 addition & 1 deletion crates/hyperdrive-math/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub async fn test_integration_get_max_short() -> Result<()> {
let state = alice.get_state().await?;
let Checkpoint {
share_price: open_share_price,
long_exposure: checkpoint_exposure,
exposure: checkpoint_exposure,
..
} = alice
.get_checkpoint(state.to_checkpoint(alice.now().await?))
Expand Down
6 changes: 3 additions & 3 deletions crates/test-utils/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,11 +954,11 @@ impl Agent<ChainClient, ChaCha8Rng> {
/// Gets the max long that can be opened in the current checkpoint.
pub async fn get_max_long(&self, maybe_max_iterations: Option<usize>) -> Result<FixedPoint> {
let state = self.get_state().await?;
let Checkpoint { long_exposure, .. } = self
let Checkpoint { exposure, .. } = self
.hyperdrive
.get_checkpoint(state.to_checkpoint(self.now().await?))
.await?;
Ok(state.get_max_long(self.wallet.base, long_exposure, maybe_max_iterations))
Ok(state.get_max_long(self.wallet.base, exposure, maybe_max_iterations))
}

/// Gets the max short that can be opened in the current checkpoint.
Expand All @@ -976,7 +976,7 @@ impl Agent<ChainClient, ChaCha8Rng> {
let state = self.get_state().await?;
let Checkpoint {
share_price: open_share_price,
long_exposure: checkpoint_exposure,
exposure: checkpoint_exposure,
..
} = self
.hyperdrive
Expand Down
10 changes: 5 additions & 5 deletions crates/test-utils/src/crash_reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ impl From<RawPoolInfo> for PoolInfo {
#[serde(rename_all = "camelCase")]
struct RawCheckpoint {
share_price: u128,
long_exposure: i128,
exposure: i128,
}

impl From<RawCheckpoint> for Checkpoint {
fn from(r: RawCheckpoint) -> Self {
Self {
share_price: r.share_price.into(),
long_exposure: r.long_exposure.into(),
exposure: r.exposure.into(),
}
}
}
Expand Down Expand Up @@ -311,7 +311,7 @@ mod tests {
},
"raw_checkpoint": {
"sharePrice": 1000000000000000000,
"longExposure": 0
"exposure": 0
},
"anvil_dump_state": "0x7b22",
"additional_info": {
Expand Down Expand Up @@ -387,7 +387,7 @@ mod tests {
},
"checkpoint_info": {
"sharePrice": "1.0",
"longExposure": "0.0",
"exposure": "0.0",
"blockNumber": 16,
"timestamp": "2023-10-20 14:28:34"
}
Expand Down Expand Up @@ -460,7 +460,7 @@ mod tests {
},
checkpoint: Checkpoint {
share_price: 1000000000000000000,
long_exposure: 0,
exposure: 0,
},
// State Dump
state_dump: "0x7b22".parse()?,
Expand Down
4 changes: 2 additions & 2 deletions test/integrations/hyperdrive/LpWithdrawalTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ contract LpWithdrawalTest is HyperdriveTest {
//
// This test ensures that two LPs (Alice and Celine) will receive a fair
// share of the withdrawal pool's profits if Alice has entirely long
// longExposure, Celine has entirely short exposure, Alice redeems immediately
// exposure, Celine has entirely short exposure, Alice redeems immediately
// after the long is closed, and Celine redeems after the short is redeemed.
function _test_lp_withdrawal_long_short_redemption(
uint256 longBasePaid,
Expand Down Expand Up @@ -920,7 +920,7 @@ contract LpWithdrawalTest is HyperdriveTest {

// This test is designed to find cases where the longs are insolvent after the LP removes funds
// and the short is closed. This will only pass if the long exposure is calculated to account for
// where the cases where the shorts deposit is larger than the long's fixed rate, but the short is
// the cases where the shorts deposit is larger than the long's fixed rate, but the short is
// shorting less bonds than the long is longing.
function _test_single_lp_withdrawal_long_short_redemption(
uint256 longBasePaid,
Expand Down
2 changes: 1 addition & 1 deletion test/units/hyperdrive/ExtremeInputs.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ contract ExtremeInputs is HyperdriveTest {
curveFee: poolConfig.fees.curve,
governanceFee: poolConfig.fees.governance
}),
checkpoint.longExposure,
checkpoint.exposure,
7
);
baseToken.mint(shortAmount);
Expand Down
6 changes: 2 additions & 4 deletions test/units/libraries/HyperdriveMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,7 @@ contract HyperdriveMathTest is HyperdriveTest {
curveFee: config.fees.curve,
governanceFee: config.fees.governance
}),
hyperdrive
.getCheckpoint(hyperdrive.latestCheckpoint())
.longExposure,
hyperdrive.getCheckpoint(hyperdrive.latestCheckpoint()).exposure,
maxIterations
);
(uint256 maturityTime, uint256 longAmount) = openLong(bob, maxLong);
Expand Down Expand Up @@ -1030,7 +1028,7 @@ contract HyperdriveMathTest is HyperdriveTest {
curveFee: config.fees.curve,
governanceFee: config.fees.governance
}),
checkpoint.longExposure,
checkpoint.exposure,
7
);
(uint256 maturityTime, ) = openShort(bob, maxShort);
Expand Down
Loading

0 comments on commit e17f442

Please sign in to comment.