Skip to content
Merged
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
26 changes: 13 additions & 13 deletions contracts/src/libraries/LPMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,16 @@ library LPMath {
// Step 4: Solve for the share proceeds that hold the LP share price
// invariant after all of the withdrawal shares are redeemed. If the
// calculation returns a share proceeds of zero, we can't pay out
// anything.
// anything. Similarly, if the share proceeds are greater than or equal
// to the maximum share reserves delta that was previously calculated,
// then we can't distribute excess idle since we ruled out the
// possibility of paying out the full maximum share reserves delta in
// step 3.
uint256 shareProceeds = calculateDistributeExcessIdleShareProceeds(
_params,
originalEffectiveShareReserves
);
if (shareProceeds == 0) {
if (shareProceeds == 0 || shareProceeds >= maxShareReservesDelta) {
return (0, 0);
}

Expand Down Expand Up @@ -561,16 +565,6 @@ library LPMath {
uint256 lpTotalSupply = _params.activeLpTotalSupply +
_params.withdrawalSharesTotalSupply;

// If the pool is net neutral, we can solve directly.
if (_params.netCurveTrade == 0) {
// NOTE: Round down since this is the final result.
return
_params.startingPresentValue.mulDivDown(
_params.withdrawalSharesTotalSupply,
lpTotalSupply
);
}

// NOTE: Round the initial guess down to avoid overshooting.
//
// We make an initial guess for Newton's method by assuming that the
Expand All @@ -579,12 +573,18 @@ library LPMath {
// withdrawal pool should receive more than this, but it's a good
// starting point. The calculation is:
//
// x_0 = (PV(0) / l) * w
// x_0 = w * (PV(0) / l)
uint256 shareProceeds = _params.withdrawalSharesTotalSupply.mulDivDown(
_params.startingPresentValue,
lpTotalSupply
);

// If the pool is net neutral, the initial guess is equal to the final
// result.
if (_params.netCurveTrade == 0) {
return shareProceeds;
}

// If the net curve trade is positive, the pool is net long.
if (_params.netCurveTrade > 0) {
for (uint256 i = 0; i < SHARE_PROCEEDS_MAX_ITERATIONS; ) {
Expand Down