From 464d505c7ef68200c9f96b2385ceed406b464ba3 Mon Sep 17 00:00:00 2001 From: johngrantuk Date: Thu, 26 May 2022 13:44:11 +0100 Subject: [PATCH 1/9] Added debug example with filtered pools and console logging. --- src/pools/stablePool/stableMathBigInt.ts | 7 +++++++ src/pools/stablePool/stablePool.ts | 8 ++++++++ test/lib/subgraphPoolDataService.ts | 9 ++++++++- test/testScripts/swapExample.ts | 6 +++--- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/pools/stablePool/stableMathBigInt.ts b/src/pools/stablePool/stableMathBigInt.ts index 16d1ceac..45709463 100644 --- a/src/pools/stablePool/stableMathBigInt.ts +++ b/src/pools/stablePool/stableMathBigInt.ts @@ -90,6 +90,13 @@ export function _calcOutGivenIn( amountIn: bigint, fee: bigint ): bigint { + console.log(amp); + console.log(balances.toString()); + console.log(tokenIndexIn); + console.log(tokenIndexOut); + console.log(amountIn); + console.log(fee); + // LOOKS OK amountIn = subtractFee(amountIn, fee); // Given that we need to have a greater final balance out, the invariant needs to be rounded up const invariant = _calculateInvariant(amp, balances, true); diff --git a/src/pools/stablePool/stablePool.ts b/src/pools/stablePool/stablePool.ts index 2dab3570..01b9adc4 100644 --- a/src/pools/stablePool/stablePool.ts +++ b/src/pools/stablePool/stablePool.ts @@ -194,6 +194,14 @@ export class StablePool implements PoolBase { // Using BigNumber.js decimalPlaces (dp), allows us to consider token decimal accuracy correctly, // i.e. when using token with 2decimals 0.002 should be returned as 0 // Uses ROUND_DOWN mode (1) + console.log( + `STABLE: ${amount.toString()} ${scale( + bnum(amt.toString()), + -18 + ) + .dp(poolPairData.decimalsOut, 1) + .toString()}` + ); return scale(bnum(amt.toString()), -18).dp( poolPairData.decimalsOut, 1 diff --git a/test/lib/subgraphPoolDataService.ts b/test/lib/subgraphPoolDataService.ts index 92c6c0c8..e2c55ca2 100644 --- a/test/lib/subgraphPoolDataService.ts +++ b/test/lib/subgraphPoolDataService.ts @@ -165,7 +165,14 @@ export class SubgraphPoolDataService implements PoolDataService { const { data } = await response.json(); - const pools = [...data.pool0, ...data.pool1000]; + const pools = [...data.pool0, ...data.pool1000].filter((p) => { + return ( + p.id === + '0x96646936b91d6b9d7d0c47c496afbf3d6ec7b6f8000200000000000000000019' || + p.id === + '0x06df3b2bbb68adc8b0e302443692037ed9f91b42000000000000000000000063' + ); + }); if (this.config.onchain) { return getOnChainBalances( diff --git a/test/testScripts/swapExample.ts b/test/testScripts/swapExample.ts index 7becc83b..3f9fe269 100644 --- a/test/testScripts/swapExample.ts +++ b/test/testScripts/swapExample.ts @@ -717,10 +717,10 @@ export async function simpleSwap() { const networkId = Network.MAINNET; // Pools source can be Subgraph URL or pools data set passed directly // Update pools list with most recent onchain balances - const tokenIn = ADDRESSES[networkId].DAI; - const tokenOut = ADDRESSES[networkId].WETH; + const tokenIn = ADDRESSES[networkId].USDC; + const tokenOut = ADDRESSES[networkId].DAI; const swapType = SwapTypes.SwapExactIn; - const swapAmount = parseFixed('1000000', 18); + const swapAmount = parseFixed('1829.912438', 6); const executeTrade = true; const provider = new JsonRpcProvider(PROVIDER_URLS[networkId]); From 20759f50beb7b4a213276ae29e51c1603535b530 Mon Sep 17 00:00:00 2001 From: sergioyuhjtman Date: Thu, 2 Jun 2022 18:08:20 -0300 Subject: [PATCH 2/9] fix rounding in subtractSwapFeeAmount --- .../phantomStablePool/phantomStablePool.ts | 2 +- src/pools/stablePool/stableMathBigInt.ts | 7 ------ src/pools/stablePool/stablePool.ts | 22 +++++++++++++++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/pools/phantomStablePool/phantomStablePool.ts b/src/pools/phantomStablePool/phantomStablePool.ts index b9077400..a4ed5460 100644 --- a/src/pools/phantomStablePool/phantomStablePool.ts +++ b/src/pools/phantomStablePool/phantomStablePool.ts @@ -488,7 +488,7 @@ export class PhantomStablePool implements PoolBase { subtractSwapFeeAmount(amount: BigNumber, swapFee: BigNumber): BigNumber { // https://github.com/balancer-labs/balancer-v2-monorepo/blob/c18ff2686c61a8cbad72cdcfc65e9b11476fdbc3/pkg/pool-utils/contracts/BasePool.sol#L466 - const feeAmount = amount.mul(swapFee).div(ONE); + const feeAmount = amount.mul(swapFee).add(ONE.sub(1)).div(ONE); return amount.sub(feeAmount); } diff --git a/src/pools/stablePool/stableMathBigInt.ts b/src/pools/stablePool/stableMathBigInt.ts index 45709463..16d1ceac 100644 --- a/src/pools/stablePool/stableMathBigInt.ts +++ b/src/pools/stablePool/stableMathBigInt.ts @@ -90,13 +90,6 @@ export function _calcOutGivenIn( amountIn: bigint, fee: bigint ): bigint { - console.log(amp); - console.log(balances.toString()); - console.log(tokenIndexIn); - console.log(tokenIndexOut); - console.log(amountIn); - console.log(fee); - // LOOKS OK amountIn = subtractFee(amountIn, fee); // Given that we need to have a greater final balance out, the invariant needs to be rounded up const invariant = _calculateInvariant(amp, balances, true); diff --git a/src/pools/stablePool/stablePool.ts b/src/pools/stablePool/stablePool.ts index 01b9adc4..e17461cb 100644 --- a/src/pools/stablePool/stablePool.ts +++ b/src/pools/stablePool/stablePool.ts @@ -176,9 +176,20 @@ export class StablePool implements PoolBase { ): OldBigNumber { try { if (amount.isZero()) return ZERO; + + const amtWithFeeEvm = this.subtractSwapFeeAmount( + parseFixed( + amount.dp(poolPairData.decimalsIn).toString(), + poolPairData.decimalsIn + ), + poolPairData.swapFee + ); + // All values should use 1e18 fixed point // i.e. 1USDC => 1e18 not 1e6 - const amtScaled = parseFixed(amount.dp(18).toString(), 18); + const amtScaled = amtWithFeeEvm.mul( + 10 ** (18 - poolPairData.decimalsIn) + ); const amt = _calcOutGivenIn( this.amp.toBigInt(), @@ -188,8 +199,9 @@ export class StablePool implements PoolBase { poolPairData.tokenIndexIn, poolPairData.tokenIndexOut, amtScaled.toBigInt(), - poolPairData.swapFee.toBigInt() + BigInt(0) ); + // return normalised amount // Using BigNumber.js decimalPlaces (dp), allows us to consider token decimal accuracy correctly, // i.e. when using token with 2decimals 0.002 should be returned as 0 @@ -279,4 +291,10 @@ export class StablePool implements PoolBase { poolPairData ); } + + subtractSwapFeeAmount(amount: BigNumber, swapFee: BigNumber): BigNumber { + // https://github.com/balancer-labs/balancer-v2-monorepo/blob/c18ff2686c61a8cbad72cdcfc65e9b11476fdbc3/pkg/pool-utils/contracts/BasePool.sol#L466 + const feeAmount = amount.mul(swapFee).add(ONE.sub(1)).div(ONE); + return amount.sub(feeAmount); + } } From 433490d173581fbb910ec1959e7a7b145f871daf Mon Sep 17 00:00:00 2001 From: sergioyuhjtman Date: Tue, 14 Jun 2022 15:35:15 -0300 Subject: [PATCH 3/9] fix discrepancy for stable pools swaps, swapExactOut case --- src/pools/stablePool/stablePool.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/pools/stablePool/stablePool.ts b/src/pools/stablePool/stablePool.ts index e17461cb..70e307fd 100644 --- a/src/pools/stablePool/stablePool.ts +++ b/src/pools/stablePool/stablePool.ts @@ -234,7 +234,7 @@ export class StablePool implements PoolBase { // i.e. 1USDC => 1e18 not 1e6 const amtScaled = parseFixed(amount.dp(18).toString(), 18); - const amt = _calcInGivenOut( + let amt = _calcInGivenOut( this.amp.toBigInt(), poolPairData.allBalancesScaled.map((balance) => balance.toBigInt() @@ -242,16 +242,20 @@ export class StablePool implements PoolBase { poolPairData.tokenIndexIn, poolPairData.tokenIndexOut, amtScaled.toBigInt(), - poolPairData.swapFee.toBigInt() + BigInt(0) ); - // return normalised amount - // Using BigNumber.js decimalPlaces (dp), allows us to consider token decimal accuracy correctly, - // i.e. when using token with 2decimals 0.002 should be returned as 0 - // Uses ROUND_UP mode (0) - return scale(bnum(amt.toString()), -18).dp( - poolPairData.decimalsIn, - 0 + + console.log('amt before fee: ', amt); + + // this is downscaleUp + const scaleFactor = BigInt(10 ** (18 - poolPairData.decimalsIn)); + amt = (amt + scaleFactor - BigInt(1)) / scaleFactor; + + const amtWithFee = this.addSwapFeeAmount( + BigNumber.from(amt), + poolPairData.swapFee ); + return bnum(amtWithFee.toString()); } catch (err) { console.error(`_evminGivenOut: ${err.message}`); return ZERO; @@ -297,4 +301,10 @@ export class StablePool implements PoolBase { const feeAmount = amount.mul(swapFee).add(ONE.sub(1)).div(ONE); return amount.sub(feeAmount); } + + addSwapFeeAmount(amount: BigNumber, swapFee: BigNumber): BigNumber { + // https://github.com/balancer-labs/balancer-v2-monorepo/blob/c18ff2686c61a8cbad72cdcfc65e9b11476fdbc3/pkg/pool-utils/contracts/BasePool.sol#L458 + const feeAmount = ONE.sub(swapFee); + return amount.mul(ONE).add(feeAmount.sub(1)).div(feeAmount); + } } From ca128752892b2c1b2cffe76e959139036ef50c2e Mon Sep 17 00:00:00 2001 From: sergioyuhjtman Date: Wed, 15 Jun 2022 17:51:19 -0300 Subject: [PATCH 4/9] fix bug from previous commit, adapt test cases to latest changes --- src/pools/phantomStablePool/phantomStablePool.ts | 8 ++++++-- src/pools/stablePool/stablePool.ts | 14 +++----------- test/stablePools.spec.ts | 4 ++-- test/testScripts/swapExample.ts | 13 +++++++++---- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/pools/phantomStablePool/phantomStablePool.ts b/src/pools/phantomStablePool/phantomStablePool.ts index a4ed5460..0d2d2ded 100644 --- a/src/pools/phantomStablePool/phantomStablePool.ts +++ b/src/pools/phantomStablePool/phantomStablePool.ts @@ -236,6 +236,8 @@ export class PhantomStablePool implements PoolBase { amount: OldBigNumber ): OldBigNumber { try { + // This code assumes that decimalsIn and decimalsOut is 18 + if (amount.isZero()) return ZERO; // All values should use 1e18 fixed point // i.e. 1USDC => 1e18 not 1e6 @@ -301,6 +303,8 @@ export class PhantomStablePool implements PoolBase { amount: OldBigNumber ): OldBigNumber { try { + // This code assumes that decimalsIn and decimalsOut is 18 + if (amount.isZero()) return ZERO; // All values should use 1e18 fixed point // i.e. 1USDC => 1e18 not 1e6 @@ -331,7 +335,7 @@ export class PhantomStablePool implements PoolBase { poolPairData.allBalancesScaled.map((b) => b.toBigInt()), amountsOutBigInt, poolPairData.virtualBptSupply.toBigInt(), - BigInt(0) // Fee is handled above + BigInt(0) // Fee is handled below ); } else { returnEvm = _calcInGivenOut( @@ -340,7 +344,7 @@ export class PhantomStablePool implements PoolBase { poolPairData.tokenIndexIn, poolPairData.tokenIndexOut, amountConvertedEvm.toBigInt(), - BigInt(0) // Fee is handled above + BigInt(0) // Fee is handled below ); } // In Phantom Pools every time there is a swap (token per token, bpt per token or token per bpt), we substract the fee from the amount in diff --git a/src/pools/stablePool/stablePool.ts b/src/pools/stablePool/stablePool.ts index 70e307fd..e4b1d43d 100644 --- a/src/pools/stablePool/stablePool.ts +++ b/src/pools/stablePool/stablePool.ts @@ -206,14 +206,6 @@ export class StablePool implements PoolBase { // Using BigNumber.js decimalPlaces (dp), allows us to consider token decimal accuracy correctly, // i.e. when using token with 2decimals 0.002 should be returned as 0 // Uses ROUND_DOWN mode (1) - console.log( - `STABLE: ${amount.toString()} ${scale( - bnum(amt.toString()), - -18 - ) - .dp(poolPairData.decimalsOut, 1) - .toString()}` - ); return scale(bnum(amt.toString()), -18).dp( poolPairData.decimalsOut, 1 @@ -245,8 +237,6 @@ export class StablePool implements PoolBase { BigInt(0) ); - console.log('amt before fee: ', amt); - // this is downscaleUp const scaleFactor = BigInt(10 ** (18 - poolPairData.decimalsIn)); amt = (amt + scaleFactor - BigInt(1)) / scaleFactor; @@ -255,7 +245,9 @@ export class StablePool implements PoolBase { BigNumber.from(amt), poolPairData.swapFee ); - return bnum(amtWithFee.toString()); + return bnum(amtWithFee.toString()).div( + 10 ** poolPairData.decimalsIn + ); } catch (err) { console.error(`_evminGivenOut: ${err.message}`); return ZERO; diff --git a/test/stablePools.spec.ts b/test/stablePools.spec.ts index 2bf3ac83..d327bfe7 100644 --- a/test/stablePools.spec.ts +++ b/test/stablePools.spec.ts @@ -249,7 +249,7 @@ describe(`Tests for Stable Pools.`, () => { console.log(`Return amt:`); console.log(swapInfo.returnAmount.toString()); // This value is hard coded as sanity check if things unexpectedly change. Taken from V2 test run (with extra fee logic added). - expect(swapInfo.returnAmount.toString()).eq('1000401'); + expect(swapInfo.returnAmount.toString()).eq('1000402'); expect(swapInfo.swaps.length).eq(1); expect(swapInfo.swaps[0].amount.toString()).eq(swapAmt.toString()); expect(swapInfo.swaps[0].poolId).eq(poolsFromFile.pools[0].id); @@ -336,7 +336,7 @@ describe(`Tests for Stable Pools.`, () => { ); // This value is hard coded as sanity check if things unexpectedly change. Taken from V2 test run (with extra fee logic added). - expect(swapInfo.returnAmount.toString()).eq('18089532'); + expect(swapInfo.returnAmount.toString()).eq('18089534'); expect(swapInfo.swaps.length).eq(2); expect(swapInfo.swaps[0].amount.toString()).eq(swapAmt.toString()); expect(swapInfo.swaps[0].poolId).eq( diff --git a/test/testScripts/swapExample.ts b/test/testScripts/swapExample.ts index 3f9fe269..88ec8f6f 100644 --- a/test/testScripts/swapExample.ts +++ b/test/testScripts/swapExample.ts @@ -160,6 +160,11 @@ export const ADDRESSES = { decimals: 18, symbol: 'bbausdc', }, + bbadai: { + address: '0x804cdb9116a10bb78768d3252355a1b18067bf8f', + decimals: 18, + symbol: 'bb-a-dai', + }, waDAI: { address: '0x02d60b84491589974263d922d9cc7a3152618ef6', decimals: 18, @@ -717,10 +722,10 @@ export async function simpleSwap() { const networkId = Network.MAINNET; // Pools source can be Subgraph URL or pools data set passed directly // Update pools list with most recent onchain balances - const tokenIn = ADDRESSES[networkId].USDC; - const tokenOut = ADDRESSES[networkId].DAI; - const swapType = SwapTypes.SwapExactIn; - const swapAmount = parseFixed('1829.912438', 6); + const tokenIn = ADDRESSES[networkId].DAI; + const tokenOut = ADDRESSES[networkId].USDC; + const swapType = SwapTypes.SwapExactOut; + const swapAmount = parseFixed('10.912438', 6); const executeTrade = true; const provider = new JsonRpcProvider(PROVIDER_URLS[networkId]); From 30415e4cd4b538ffbe0ddfd15a78e65cd6d8c220 Mon Sep 17 00:00:00 2001 From: sergioyuhjtman Date: Wed, 15 Jun 2022 20:17:54 -0300 Subject: [PATCH 5/9] modify metastable pool fee and scaling computation --- src/pools/metaStablePool/metaStablePool.ts | 73 +++++++++++++++++----- test/lib/subgraphPoolDataService.ts | 4 +- test/testScripts/swapExample.ts | 6 +- 3 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/pools/metaStablePool/metaStablePool.ts b/src/pools/metaStablePool/metaStablePool.ts index a8b9bdd5..6e032ecc 100644 --- a/src/pools/metaStablePool/metaStablePool.ts +++ b/src/pools/metaStablePool/metaStablePool.ts @@ -198,22 +198,31 @@ export class MetaStablePool implements PoolBase { if (amount.isZero()) return ZERO; // All values should use 1e18 fixed point // i.e. 1USDC => 1e18 not 1e6 - const amountConvertedEvm = parseFixed(amount.dp(18).toString(), 18) + + const amtWithFee = this.subtractSwapFeeAmount( + parseFixed( + amount.dp(poolPairData.decimalsIn).toString(), + poolPairData.decimalsIn + ), + poolPairData.swapFee + ); + + const amountConverted = amtWithFee .mul(poolPairData.tokenInPriceRate) .div(ONE); - const returnEvm = _calcOutGivenIn( + const returnAmt = _calcOutGivenIn( this.amp.toBigInt(), poolPairData.allBalancesScaled.map((balance) => balance.toBigInt() ), poolPairData.tokenIndexIn, poolPairData.tokenIndexOut, - amountConvertedEvm.toBigInt(), - poolPairData.swapFee.toBigInt() + amountConverted.toBigInt(), + BigInt(0) ); - const returnEvmWithRate = BigNumber.from(returnEvm) + const returnEvmWithRate = BigNumber.from(returnAmt) .mul(ONE) .div(poolPairData.tokenOutPriceRate); @@ -230,28 +239,50 @@ export class MetaStablePool implements PoolBase { ): OldBigNumber { try { if (amount.isZero()) return ZERO; + const decimalsIn = poolPairData.decimalsIn; + const decimalsOut = poolPairData.decimalsOut; + // All values should use 1e18 fixed point // i.e. 1USDC => 1e18 not 1e6 - const amountConvertedEvm = parseFixed(amount.dp(18).toString(), 18) - .mul(poolPairData.tokenOutPriceRate) - .div(ONE); + const scalingFactorIn = + poolPairData.tokenInPriceRate.toBigInt() * + BigInt(10 ** (18 - decimalsIn)); + + const scalingFactorOut = + poolPairData.tokenOutPriceRate.toBigInt() * + BigInt(10 ** (18 - decimalsOut)); + + // eslint-disable-next-line prettier/prettier + const amountBigInt = BigInt( + amount + .times(10 ** decimalsOut) + .dp(0) + .toString() + ); + const amountConverted = + (amountBigInt * scalingFactorOut) / BigInt(10 ** 18); - const returnEvm = _calcInGivenOut( + const returnAmount = _calcInGivenOut( this.amp.toBigInt(), poolPairData.allBalancesScaled.map((balance) => balance.toBigInt() ), poolPairData.tokenIndexIn, poolPairData.tokenIndexOut, - amountConvertedEvm.toBigInt(), - poolPairData.swapFee.toBigInt() + amountConverted, + BigInt(0) ); - const returnEvmWithRate = BigNumber.from(returnEvm) - .mul(ONE) - .div(poolPairData.tokenInPriceRate); + const returnAmountConverted = + (returnAmount * BigInt(10 ** 18)) / scalingFactorIn; - return bnum(formatFixed(returnEvmWithRate, 18)); + const returnAmtWithFee = this.addSwapFeeAmount( + BigNumber.from(returnAmountConverted), + poolPairData.swapFee + ); + return bnum(returnAmtWithFee.toString()).div( + 10 ** poolPairData.decimalsIn + ); } catch (err) { console.error(`_evminGivenOut: ${err.message}`); return ZERO; @@ -315,4 +346,16 @@ export class MetaStablePool implements PoolBase { .times(priceRateOut) .times(priceRateOut); } + + subtractSwapFeeAmount(amount: BigNumber, swapFee: BigNumber): BigNumber { + // https://github.com/balancer-labs/balancer-v2-monorepo/blob/c18ff2686c61a8cbad72cdcfc65e9b11476fdbc3/pkg/pool-utils/contracts/BasePool.sol#L466 + const feeAmount = amount.mul(swapFee).add(ONE.sub(1)).div(ONE); + return amount.sub(feeAmount); + } + + addSwapFeeAmount(amount: BigNumber, swapFee: BigNumber): BigNumber { + // https://github.com/balancer-labs/balancer-v2-monorepo/blob/c18ff2686c61a8cbad72cdcfc65e9b11476fdbc3/pkg/pool-utils/contracts/BasePool.sol#L458 + const feeAmount = ONE.sub(swapFee); + return amount.mul(ONE).add(feeAmount.sub(1)).div(feeAmount); + } } diff --git a/test/lib/subgraphPoolDataService.ts b/test/lib/subgraphPoolDataService.ts index e2c55ca2..908085d9 100644 --- a/test/lib/subgraphPoolDataService.ts +++ b/test/lib/subgraphPoolDataService.ts @@ -165,14 +165,14 @@ export class SubgraphPoolDataService implements PoolDataService { const { data } = await response.json(); - const pools = [...data.pool0, ...data.pool1000].filter((p) => { + const pools = [...data.pool0, ...data.pool1000]; /*.filter((p) => { return ( p.id === '0x96646936b91d6b9d7d0c47c496afbf3d6ec7b6f8000200000000000000000019' || p.id === '0x06df3b2bbb68adc8b0e302443692037ed9f91b42000000000000000000000063' ); - }); + });*/ if (this.config.onchain) { return getOnChainBalances( diff --git a/test/testScripts/swapExample.ts b/test/testScripts/swapExample.ts index 88ec8f6f..d69d7f2b 100644 --- a/test/testScripts/swapExample.ts +++ b/test/testScripts/swapExample.ts @@ -722,10 +722,10 @@ export async function simpleSwap() { const networkId = Network.MAINNET; // Pools source can be Subgraph URL or pools data set passed directly // Update pools list with most recent onchain balances - const tokenIn = ADDRESSES[networkId].DAI; - const tokenOut = ADDRESSES[networkId].USDC; + const tokenIn = ADDRESSES[networkId].wSTETH; + const tokenOut = ADDRESSES[networkId].WETH; const swapType = SwapTypes.SwapExactOut; - const swapAmount = parseFixed('10.912438', 6); + const swapAmount = parseFixed('10.912438109873074672', 18); const executeTrade = true; const provider = new JsonRpcProvider(PROVIDER_URLS[networkId]); From 569bcf2f95aa173682a3f66b0eb08182c61c3f8e Mon Sep 17 00:00:00 2001 From: sergioyuhjtman Date: Thu, 16 Jun 2022 19:32:50 -0300 Subject: [PATCH 6/9] adapt test case for metastable pools --- test/metaStablePools.spec.ts | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/test/metaStablePools.spec.ts b/test/metaStablePools.spec.ts index 494ec52c..f0c09f07 100644 --- a/test/metaStablePools.spec.ts +++ b/test/metaStablePools.spec.ts @@ -488,15 +488,22 @@ describe(`Tests for MetaStable Pools.`, () => { ); expect(swapInfoStable.tokenIn).to.deep.eq(swapInfo.tokenIn); expect(swapInfoStable.tokenOut).to.deep.eq(swapInfo.tokenOut); - expect(swapInfoStable.returnAmount.toString()).eq( - swapInfo.returnAmount.mul(tokenInPriceRate).div(ONE).toString() - ); - expect(swapInfoStable.returnAmountConsideringFees.toString()).eq( - swapInfo.returnAmountConsideringFees - .mul(tokenInPriceRate) - .div(ONE) - .toString() - ); + expect( + almostEqual( + swapInfoStable.returnAmount, + swapInfo.returnAmount.mul(tokenInPriceRate).div(ONE) + ), + 'they are not almost equal' + ).eq(true); + expect( + almostEqual( + swapInfoStable.returnAmountConsideringFees, + swapInfo.returnAmountConsideringFees + .mul(tokenInPriceRate) + .div(ONE) + ), + 'they are not almost equal' + ).eq(true); expect(swapInfoStable.swaps.length).eq(swapInfo.swaps.length); swapInfoStable.swaps.forEach((swapStable, i) => { expect(swapStable.poolId).eq(swapInfo.swaps[i].poolId); @@ -750,3 +757,8 @@ describe(`Tests for MetaStable Pools.`, () => { // }); }); }); + +function almostEqual(arg1: BigNumber, arg2: BigNumber): boolean { + const diff = arg1.sub(arg2).toBigInt(); + return diff == BigInt(0) || diff == BigInt(1) || diff == BigInt(-1); +} From 147226bdb579bad1f65c295fcbdabf8a791ba128 Mon Sep 17 00:00:00 2001 From: sergioyuhjtman Date: Fri, 17 Jun 2022 11:09:17 -0300 Subject: [PATCH 7/9] removed commented code --- test/lib/subgraphPoolDataService.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/test/lib/subgraphPoolDataService.ts b/test/lib/subgraphPoolDataService.ts index 908085d9..92c6c0c8 100644 --- a/test/lib/subgraphPoolDataService.ts +++ b/test/lib/subgraphPoolDataService.ts @@ -165,14 +165,7 @@ export class SubgraphPoolDataService implements PoolDataService { const { data } = await response.json(); - const pools = [...data.pool0, ...data.pool1000]; /*.filter((p) => { - return ( - p.id === - '0x96646936b91d6b9d7d0c47c496afbf3d6ec7b6f8000200000000000000000019' || - p.id === - '0x06df3b2bbb68adc8b0e302443692037ed9f91b42000000000000000000000063' - ); - });*/ + const pools = [...data.pool0, ...data.pool1000]; if (this.config.onchain) { return getOnChainBalances( From d7c9fff9b3574f0c2c936b78cf77d445069bd8a4 Mon Sep 17 00:00:00 2001 From: Tim Robinson Date: Mon, 20 Jun 2022 14:38:49 +1000 Subject: [PATCH 8/9] Fix path to stablePool include in metaStablePool --- src/pools/metaStablePool/metaStablePool.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pools/metaStablePool/metaStablePool.ts b/src/pools/metaStablePool/metaStablePool.ts index a8b9bdd5..bcc1f77f 100644 --- a/src/pools/metaStablePool/metaStablePool.ts +++ b/src/pools/metaStablePool/metaStablePool.ts @@ -20,7 +20,7 @@ import { _calcOutGivenIn, _calcInGivenOut, } from '../stablePool/stableMathBigInt'; -import { StablePoolPairData } from 'pools/stablePool/stablePool'; +import { StablePoolPairData } from '../stablePool/stablePool'; type MetaStablePoolToken = Pick< SubgraphToken, From 32d8a8d155247bb1d204465fcfa1288e89e0766f Mon Sep 17 00:00:00 2001 From: johngrantuk Date: Mon, 20 Jun 2022 11:23:17 +0100 Subject: [PATCH 9/9] Update package: 4.0.0-beta.9. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 07835b93..45920d90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@balancer-labs/sor", - "version": "4.0.0-beta.8", + "version": "4.0.0-beta.9", "license": "GPL-3.0-only", "main": "dist/index.js", "module": "dist/index.esm.js",