Skip to content

Commit

Permalink
Fix rounding of computeBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
joaobrunoah committed Dec 19, 2024
1 parent 2445e2c commit 1d90eaf
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pkg/pool-gyro/contracts/Gyro2CLPPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,24 @@ contract Gyro2CLPPool is IGyro2CLPPool, BalancerPoolToken {
// New invariant
invariant = invariant.mulUp(invariantRatio);
uint256 squareNewInv = invariant * invariant;
// L / sqrt(beta)
uint256 a = invariant.divDown(sqrtBeta);
// L * sqrt(alpha)
uint256 b = invariant.mulDown(sqrtAlpha);

if (tokenInIndex == 0) {
// if newBalance = newX

// L / sqrt(beta), rounded down to minimize newBalance.
uint256 a = invariant.divDown(sqrtBeta);
// L * sqrt(alpha), rounded up to minimize newBalance (b is in the denominator).
uint256 b = invariant.mulUp(sqrtAlpha);

newBalance = squareNewInv.divUpRaw(b + balancesLiveScaled18[1]) - a;
} else {
// if newBalance = newY

// L / sqrt(beta), rounded up to minimize newBalance (a is in the denominator).
uint256 a = invariant.divUp(sqrtBeta);
// L * sqrt(alpha), rounded down to minimize newBalance.
uint256 b = invariant.mulDown(sqrtAlpha);

newBalance = squareNewInv.divUpRaw(a + balancesLiveScaled18[0]) - b;
}
}
Expand Down

0 comments on commit 1d90eaf

Please sign in to comment.