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

_distributeExcessIdle overhaul #688

Merged
merged 76 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
f955eda
Cleaned up the `calculatePresentValue` function
jalextowle Nov 20, 2023
8c46fc3
Simplied the max buy case of `calculatePresentValue`
jalextowle Nov 21, 2023
751fa49
Fixed the broken test
jalextowle Nov 21, 2023
8dbbda6
Optimize the happy path of `calculateCloseLong` and `calculateCloseSh…
jalextowle Nov 21, 2023
6c79343
Fixed the Rust tests
jalextowle Nov 21, 2023
91ce575
Optimized `calculatePresentValue`
jalextowle Nov 21, 2023
0ff67c8
Moved `permitForAll` to `HyperdriveTarget0`
jalextowle Nov 21, 2023
c3d60e6
Addressed review feedback from @jrhea
jalextowle Nov 21, 2023
3adeb6e
Re-applied review feedback from @jrhea
jalextowle Nov 21, 2023
d2bb6d2
Cleaned up the comments a bit more in `YieldSpaceMath`
jalextowle Nov 21, 2023
5048f70
Merge remote-tracking branch 'origin/main' into jalextowle/feature/di…
jalextowle Nov 27, 2023
edd6f59
Added a test to verify that the present value is monotonic when the p…
jalextowle Nov 28, 2023
1dfc4fe
Wrote out the outline of the new `_distributeExcessIdle` calculation
jalextowle Nov 30, 2023
03b4c08
Started testing `calculateMaxShareReservesDelta`
jalextowle Dec 2, 2023
c319c24
Greatly improved the guessing methodology (it's still on the wrong side)
jalextowle Dec 3, 2023
d627729
Got some initial unit tests working for the hard part of `_distribute…
jalextowle Dec 4, 2023
4116a72
Merge remote-tracking branch 'origin/main' into jalextowle/feature/di…
jalextowle Dec 4, 2023
d27ccff
Fixed a few tests
jalextowle Dec 5, 2023
8629d75
Started the overhaul of `removeLiquidity`
jalextowle Dec 5, 2023
d37b3de
Fixed some of the `removeLiquidity` tests
jalextowle Dec 5, 2023
b945cd0
Fixed the `addLiquidity` tests
jalextowle Dec 5, 2023
7f967f2
Fixed the `redeemWithdrawalShares` test
jalextowle Dec 5, 2023
e06c338
Fixed a bug in `calculateDistributeExcessIdle`
jalextowle Dec 6, 2023
92ddccb
Fixed a bug in `YieldSpaceMath`'s `calculateMaxSellBondsIn`
jalextowle Dec 6, 2023
50f15c3
Fixed an edge case that caused the present value to underflow
jalextowle Dec 6, 2023
656ee6d
Fixed a couple more failing tests
jalextowle Dec 6, 2023
1a7f86d
Removed the LP fairness tests and started fixed the LP withdrawal tests
jalextowle Dec 7, 2023
8e08ea2
Fixed more of the LP withdrawal tests and found a negative solvency e…
jalextowle Dec 7, 2023
223af66
Fixed the remaining tests
jalextowle Dec 7, 2023
4250edc
Improved the LP withdrawal tests and added failing edge cases
jalextowle Dec 8, 2023
9e7889e
Merge remote-tracking branch 'origin/main' into jalextowle/feature/di…
jalextowle Dec 12, 2023
25175a6
Fixed some edge cases in `LpWithdrawalTest`
jalextowle Dec 12, 2023
f7cd4c0
Added another edge case to the tests
jalextowle Dec 12, 2023
2d14db8
Merge remote-tracking branch 'origin/main' into jalextowle/feature/di…
jalextowle Dec 12, 2023
e587f00
Fixed issues post-merge
jalextowle Dec 12, 2023
f1673fc
Appeased the linter in `YieldSpaceMath`
jalextowle Dec 12, 2023
e09f7ef
Renamed `LPMath` to `LpMath`
jalextowle Dec 12, 2023
508d0d0
Reverted back to using `LPMath`
jalextowle Dec 12, 2023
a8bf20b
Use `calculateUpdateLiquidity` in `_updateLiquidity`
jalextowle Dec 12, 2023
75c2600
Added an edge-case to the Zombie interest tests
jalextowle Dec 12, 2023
c71a39e
Started to polish `LPMath`
jalextowle Dec 12, 2023
14b170e
Continued polishing
jalextowle Dec 12, 2023
dcb53b6
Continued polishing
jalextowle Dec 13, 2023
ab8a163
Added a parameter for the number iterations in `calculateMaxShareRese…
jalextowle Dec 13, 2023
61a43c9
Fixed some of the broken tests
jalextowle Dec 13, 2023
142df7f
Merge remote-tracking branch 'origin/main' into jalextowle/feature/di…
jalextowle Dec 13, 2023
0f9bd85
Added short-circuiting for `calculateDistributeExcessIdleShareProceeds`
jalextowle Dec 13, 2023
52ca8b3
Optimized the sad-path of `_distributeExcessIdle`
jalextowle Dec 13, 2023
de6e176
Fixed an edge-case in `calculateNetCurveTradeSafe` when we are net long
jalextowle Dec 13, 2023
a2d03a6
Bug fix that significantly improves the convergence of `calculateMaxS…
jalextowle Dec 14, 2023
0ccd9ae
Fixed some bugs in `calculateDistributeExcessIdleShareProceeds` and i…
jalextowle Dec 14, 2023
15f9f22
More screw tightening and bounds finding
jalextowle Dec 14, 2023
62f4cab
Adds a test that shows that adding liquidity fails when PV = 0
jalextowle Dec 14, 2023
e813109
Added checks to ensure that distribute excess idle works with remove …
jalextowle Dec 14, 2023
3bbfaad
Cleaned up and added tests for the share proceeds edge case calculation
jalextowle Dec 15, 2023
9869146
Added short-circuting and an iterations constant to `calculateMaxShar…
jalextowle Dec 15, 2023
887ac65
Cleaned up the LP withdrawal tests
jalextowle Dec 15, 2023
3bcc88e
Cleaned up the `HyperdriveLP` contract and fixed a test edge case
jalextowle Dec 15, 2023
061f8c0
Removed snake case from `MockYieldSpaceMath`
jalextowle Dec 15, 2023
f9912d2
Fixed the Rust tests
jalextowle Dec 15, 2023
fc1c8af
Apply suggestions from code review
jalextowle Dec 18, 2023
7a6bb14
Addressed some review feedback from @jrhea
jalextowle Dec 18, 2023
7db99c8
Added the algorithm to the comments
jalextowle Dec 18, 2023
ee7aaae
Merge remote-tracking branch 'origin/jalextowle/feature/distribute-ex…
jalextowle Dec 18, 2023
b51a40f
Cleaned up the algorithm documentation in `calculateDistributeExcessI…
jalextowle Dec 18, 2023
adfa445
Removed some unneeded FIXMEs
jalextowle Dec 18, 2023
6e1dce4
Updated `calculateMaxBuyBondsOutDerivativeSafe` so that it rounds con…
jalextowle Dec 18, 2023
037b091
Updated `calculateSharesInGivenBondsOutDerivative` so that it rounds …
jalextowle Dec 18, 2023
37b8809
Updated `calculateSharesOutGivenBondsInDerivative` so that it rounds …
jalextowle Dec 18, 2023
4854918
Updated `calculateMaxShareReservesDeltaInitialGuess` to make the roun…
jalextowle Dec 18, 2023
48005a0
Updated `calculateDistributeExcessIdleShareProceedsNetLongEdgeCaseSaf…
jalextowle Dec 18, 2023
ede3f93
Updated `calculateDistributeExcessIdleWithdrawalSharesRedeemed` to ma…
jalextowle Dec 18, 2023
ef21785
Updated `calculateDistributeExcessIdleShareProceeds` to make the roun…
jalextowle Dec 18, 2023
5f20c87
Fixed the remaining issues related to rounding
jalextowle Dec 18, 2023
41fe4ee
Disabled the code-size-check
jalextowle Dec 18, 2023
8319231
Merge remote-tracking branch 'origin/main' into jalextowle/feature/di…
jalextowle Dec 18, 2023
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
4 changes: 2 additions & 2 deletions contracts/src/external/HyperdriveTarget0.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { HyperdriveShort } from "../internal/HyperdriveShort.sol";
import { HyperdriveStorage } from "../internal/HyperdriveStorage.sol";
import { AssetId } from "../libraries/AssetId.sol";
import { FixedPointMath } from "../libraries/FixedPointMath.sol";
import { HyperdriveMath } from "../libraries/HyperdriveMath.sol";
import { LPMath } from "../libraries/LPMath.sol";

/// @author DELV
/// @title HyperdriveTarget0
Expand Down Expand Up @@ -267,7 +267,7 @@ abstract contract HyperdriveTarget0 is
_totalSupply[AssetId._WITHDRAWAL_SHARE_ASSET_ID] -
_withdrawPool.readyToWithdraw;
uint256 presentValue = sharePrice > 0
? HyperdriveMath
? LPMath
.calculatePresentValue(_getPresentValueParams(sharePrice))
.mulDown(sharePrice)
: 0;
Expand Down
72 changes: 64 additions & 8 deletions contracts/src/internal/HyperdriveBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IHyperdrive } from "../interfaces/IHyperdrive.sol";
import { AssetId } from "../libraries/AssetId.sol";
import { FixedPointMath, ONE } from "../libraries/FixedPointMath.sol";
import { HyperdriveMath } from "../libraries/HyperdriveMath.sol";
import { LPMath } from "../libraries/LPMath.sol";
import { SafeCast } from "../libraries/SafeCast.sol";
import { HyperdriveStorage } from "./HyperdriveStorage.sol";

Expand Down Expand Up @@ -258,17 +259,49 @@ abstract contract HyperdriveBase is HyperdriveStorage {
);
}

/// @dev Gets the distribute excess idle parameters from the current state.
/// @param _sharePrice The current share price.
/// @return params The distribute excess idle parameters.
function _getDistributeExcessIdleParams(
uint256 _idle,
uint256 _withdrawalSharesTotalSupply,
uint256 _sharePrice
) internal view returns (LPMath.DistributeExcessIdleParams memory params) {
LPMath.PresentValueParams
memory presentValueParams = _getPresentValueParams(_sharePrice);
uint256 startingPresentValue = LPMath.calculatePresentValue(
presentValueParams
);
int256 netCurveTrade = int256(
presentValueParams.longsOutstanding.mulDown(
presentValueParams.longAverageTimeRemaining
)
) -
int256(
presentValueParams.shortsOutstanding.mulDown(
presentValueParams.shortAverageTimeRemaining
)
);
params = LPMath.DistributeExcessIdleParams({
presentValueParams: presentValueParams,
startingPresentValue: startingPresentValue,
activeLpTotalSupply: _totalSupply[AssetId._LP_ASSET_ID],
withdrawalSharesTotalSupply: _withdrawalSharesTotalSupply,
idle: _idle,
netCurveTrade: netCurveTrade,
originalShareReserves: presentValueParams.shareReserves,
originalShareAdjustment: presentValueParams.shareAdjustment,
originalBondReserves: presentValueParams.bondReserves
});
}

/// @dev Gets the present value parameters from the current state.
/// @param _sharePrice The current share price.
/// @return presentValue The present value parameters.
/// @return params The present value parameters.
function _getPresentValueParams(
uint256 _sharePrice
)
internal
view
returns (HyperdriveMath.PresentValueParams memory presentValue)
{
presentValue = HyperdriveMath.PresentValueParams({
) internal view returns (LPMath.PresentValueParams memory params) {
params = LPMath.PresentValueParams({
shareReserves: _marketState.shareReserves,
shareAdjustment: _marketState.shareAdjustment,
bondReserves: _marketState.bondReserves,
Expand Down Expand Up @@ -364,6 +397,8 @@ abstract contract HyperdriveBase is HyperdriveStorage {
/// @dev Calculates the number of share reserves that are not reserved by
/// open positions.
/// @param _sharePrice The current share price.
/// @return idleShares The amount of shares that are available for LPs to
/// withdraw.
jalextowle marked this conversation as resolved.
Show resolved Hide resolved
function _calculateIdleShareReserves(
uint256 _sharePrice
) internal view returns (uint256 idleShares) {
Expand All @@ -379,6 +414,26 @@ abstract contract HyperdriveBase is HyperdriveStorage {
return idleShares;
}

/// @dev Calculates the LP share price.
/// @param _sharePrice The current share price.
jalextowle marked this conversation as resolved.
Show resolved Hide resolved
/// @return lpSharePrice The LP share price.
jalextowle marked this conversation as resolved.
Show resolved Hide resolved
function _calculateLPSharePrice(
uint256 _sharePrice
) internal view returns (uint256 lpSharePrice) {
uint256 presentValue = _sharePrice > 0
? LPMath
.calculatePresentValue(_getPresentValueParams(_sharePrice))
.mulDown(_sharePrice)
: 0;
uint256 lpTotalSupply = _totalSupply[AssetId._LP_ASSET_ID] +
_totalSupply[AssetId._WITHDRAWAL_SHARE_ASSET_ID] -
_withdrawPool.readyToWithdraw;
lpSharePrice = lpTotalSupply == 0
? 0
: presentValue.divDown(lpTotalSupply);
return lpSharePrice;
}

/// @dev Calculates the fees that go to the LPs and governance.
/// @param _shareAmount The amount of shares exchanged for bonds.
/// @param _spotPrice The price without slippage of bonds in terms of base
Expand Down Expand Up @@ -496,7 +551,8 @@ abstract contract HyperdriveBase is HyperdriveStorage {
flatFee.mulDown(_governanceFee);
}

/// @dev Converts input to base if necessary according to what is specified in options.
/// @dev Converts input to base if necessary according to what is specified
/// in options.
/// @param _amount The amount to convert.
/// @param _sharePrice The current share price.
/// @param _options The options that configure the conversion.
Expand Down
14 changes: 2 additions & 12 deletions contracts/src/internal/HyperdriveCheckpoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IHyperdrive } from "../interfaces/IHyperdrive.sol";
import { AssetId } from "../libraries/AssetId.sol";
import { FixedPointMath } from "../libraries/FixedPointMath.sol";
import { HyperdriveMath } from "../libraries/HyperdriveMath.sol";
import { LPMath } from "../libraries/LPMath.sol";
import { SafeCast } from "../libraries/SafeCast.sol";
import { HyperdriveBase } from "./HyperdriveBase.sol";
import { HyperdriveLong } from "./HyperdriveLong.sol";
Expand Down Expand Up @@ -187,23 +188,12 @@ abstract contract HyperdriveCheckpoint is

// Emit an event about the checkpoint creation that includes the LP
// share price.
uint256 presentValue = _sharePrice > 0
? HyperdriveMath
.calculatePresentValue(_getPresentValueParams(_sharePrice))
.mulDown(_sharePrice)
: 0;
uint256 lpTotalSupply = _totalSupply[AssetId._LP_ASSET_ID] +
_totalSupply[AssetId._WITHDRAWAL_SHARE_ASSET_ID] -
_withdrawPool.readyToWithdraw;
uint256 lpSharePrice = lpTotalSupply == 0
? 0
: presentValue.divDown(lpTotalSupply);
emit CreateCheckpoint(
_checkpointTime,
_sharePrice,
maturedShortsAmount,
maturedLongsAmount,
lpSharePrice
_calculateLPSharePrice(_sharePrice)
);

return _sharePrice;
Expand Down
Loading
Loading