From b90dc16c0577886ae4eb6d6aa3204c725f06bc08 Mon Sep 17 00:00:00 2001 From: Zviad Metreveli Date: Sat, 15 Jul 2023 14:20:01 +0000 Subject: [PATCH] fix mobius math too --- src/pairs/stableswap.ts | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/pairs/stableswap.ts b/src/pairs/stableswap.ts index daf5c1a..74e6158 100644 --- a/src/pairs/stableswap.ts +++ b/src/pairs/stableswap.ts @@ -16,6 +16,8 @@ interface PairStableSwapSnapshot extends Snapshot { preciseA: BigNumberString } +const SWAP_FEE_DENOM = new BigNumber(10).pow(10) + export class PairStableSwap extends Pair { allowRepeats = false private swapPool: ISwap @@ -82,7 +84,7 @@ export class PairStableSwap extends Pair { } this.paused = paused this.balancesWithAdjustedPrecision = balances.map((b, idx) => this.tokenPrecisionMultipliers[idx].multipliedBy(b)) - this.swapFee = new BigNumber(swapFee).div(new BigNumber(10).pow(10)) + this.swapFee = new BigNumber(swapFee) this.preciseA = new BigNumber(preciseA) } @@ -101,8 +103,8 @@ export class PairStableSwap extends Pair { this.balancesWithAdjustedPrecision, this.preciseA) const outputAmountWithFee = this.balancesWithAdjustedPrecision[tokenIndexTo].minus(y).minus(1) - const fee = outputAmountWithFee.multipliedBy(this.swapFee) - const outputAmount = outputAmountWithFee.minus(fee).div(this.tokenPrecisionMultipliers[tokenIndexTo]).integerValue() + const fee = outputAmountWithFee.multipliedBy(this.swapFee).idiv(SWAP_FEE_DENOM) + const outputAmount = outputAmountWithFee.minus(fee).idiv(this.tokenPrecisionMultipliers[tokenIndexTo]) return outputAmount } @@ -114,19 +116,15 @@ export class PairStableSwap extends Pair { const s = x const c = d - .multipliedBy(d).div(x.multipliedBy(nTokens)) - .integerValue() - .multipliedBy(d).multipliedBy(PairStableSwap.A_PRECISION).div(nA.multipliedBy(nTokens)) - .integerValue() - const b = s.plus(d.multipliedBy(PairStableSwap.A_PRECISION).div(nA)).integerValue() + .multipliedBy(d).idiv(x.multipliedBy(nTokens)) + .multipliedBy(d).multipliedBy(PairStableSwap.A_PRECISION).idiv(nA.multipliedBy(nTokens)) + const b = s.plus(d.multipliedBy(PairStableSwap.A_PRECISION).idiv(nA)) let yPrev let y = d for (let i = 0; i < 256; i++) { yPrev = y - y = y.multipliedBy(y).plus(c).div( - y.multipliedBy(2).plus(b).minus(d)) - .integerValue() + y = y.multipliedBy(y).plus(c).idiv(y.multipliedBy(2).plus(b).minus(d)) if (y.minus(yPrev).abs().lte(1)) { return y } @@ -149,14 +147,14 @@ export class PairStableSwap extends Pair { for (let i = 0; i < 256; i++) { let dP = d xp.forEach((x) => { - dP = dP.multipliedBy(d).div(x.multipliedBy(nTokens)).integerValue() + dP = dP.multipliedBy(d).idiv(x.multipliedBy(nTokens)) }) prevD = d - d = nA.multipliedBy(s).div(PairStableSwap.A_PRECISION).plus(dP.multipliedBy(nTokens)).multipliedBy(d).div( - nA.minus(PairStableSwap.A_PRECISION).multipliedBy(d).div(PairStableSwap.A_PRECISION).plus( + d = nA.multipliedBy(s).idiv(PairStableSwap.A_PRECISION).plus(dP.multipliedBy(nTokens)).multipliedBy(d).idiv( + nA.minus(PairStableSwap.A_PRECISION).multipliedBy(d).idiv(PairStableSwap.A_PRECISION).plus( new BigNumber(nTokens).plus(1).multipliedBy(dP) ) - ).integerValue() + ) if (d.minus(prevD).abs().lte(1)) { return d }