diff --git a/contracts/src/libraries/LPMath.sol b/contracts/src/libraries/LPMath.sol index 104b04d42..d9fa0b067 100644 --- a/contracts/src/libraries/LPMath.sol +++ b/contracts/src/libraries/LPMath.sol @@ -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); } @@ -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 @@ -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; ) {