diff --git a/package.json b/package.json index fdf43010..3ad929ad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@balancer-labs/sor", - "version": "4.1.1-beta.0", + "version": "4.1.1-beta.1", "license": "GPL-3.0-only", "main": "dist/index.js", "module": "dist/index.esm.js", diff --git a/src/pools/linearPool/linearPool.ts b/src/pools/linearPool/linearPool.ts index 76058bef..9823fa4e 100644 --- a/src/pools/linearPool/linearPool.ts +++ b/src/pools/linearPool/linearPool.ts @@ -785,154 +785,162 @@ export class LinearPool implements PoolBase { poolPairData: LinearPoolPairData, amount: OldBigNumber ): OldBigNumber { - const bigintAmount = parseFixed( - amount.dp(18).toString(), - 18 - ).toBigInt(); - const mainBalance = poolPairData.mainBalanceScaled.toBigInt(); - const wrappedBalance = poolPairData.wrappedBalanceScaled.toBigInt(); - const bptSupply = poolPairData.virtualBptSupply.toBigInt(); - const params = { - fee: poolPairData.swapFee.toBigInt(), - lowerTarget: poolPairData.lowerTarget.toBigInt(), - upperTarget: poolPairData.upperTarget.toBigInt(), - rate: poolPairData.rate.toBigInt(), - }; - let result: bigint; - if (poolPairData.pairType === PairTypes.MainTokenToBpt) { - result = _spotPriceAfterSwapBptOutPerMainIn( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if (poolPairData.pairType === PairTypes.BptToMainToken) { - result = _spotPriceAfterSwapMainOutPerBptIn( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if (poolPairData.pairType === PairTypes.WrappedTokenToBpt) { - result = _spotPriceAfterSwapBptOutPerWrappedIn( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if (poolPairData.pairType === PairTypes.BptToWrappedToken) { - result = _spotPriceAfterSwapWrappedOutPerBptIn( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if ( - poolPairData.pairType === PairTypes.MainTokenToWrappedToken - ) { - result = _spotPriceAfterSwapWrappedOutPerMainIn( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if ( - poolPairData.pairType === PairTypes.WrappedTokenToMainToken - ) { - result = _spotPriceAfterSwapMainOutPerWrappedIn( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params + try { + const bigintAmount = parseFixed( + amount.dp(18).toString(), + 18 + ).toBigInt(); + const mainBalance = poolPairData.mainBalanceScaled.toBigInt(); + const wrappedBalance = poolPairData.wrappedBalanceScaled.toBigInt(); + const bptSupply = poolPairData.virtualBptSupply.toBigInt(); + const params = { + fee: poolPairData.swapFee.toBigInt(), + lowerTarget: poolPairData.lowerTarget.toBigInt(), + upperTarget: poolPairData.upperTarget.toBigInt(), + rate: poolPairData.rate.toBigInt(), + }; + let result: bigint; + if (poolPairData.pairType === PairTypes.MainTokenToBpt) { + result = _spotPriceAfterSwapBptOutPerMainIn( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if (poolPairData.pairType === PairTypes.BptToMainToken) { + result = _spotPriceAfterSwapMainOutPerBptIn( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if (poolPairData.pairType === PairTypes.WrappedTokenToBpt) { + result = _spotPriceAfterSwapBptOutPerWrappedIn( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if (poolPairData.pairType === PairTypes.BptToWrappedToken) { + result = _spotPriceAfterSwapWrappedOutPerBptIn( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if ( + poolPairData.pairType === PairTypes.MainTokenToWrappedToken + ) { + result = _spotPriceAfterSwapWrappedOutPerMainIn( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if ( + poolPairData.pairType === PairTypes.WrappedTokenToMainToken + ) { + result = _spotPriceAfterSwapMainOutPerWrappedIn( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else return bnum(0); + return scale(bnum(result.toString()), -18).dp( + poolPairData.decimalsOut, + 0 ); - } else return bnum(0); - return scale(bnum(result.toString()), -18).dp( - poolPairData.decimalsOut, - 0 - ); + } catch (err) { + return bnum(0); + } } _spotPriceAfterSwapTokenInForExactTokenOut( poolPairData: LinearPoolPairData, amount: OldBigNumber ): OldBigNumber { - const bigintAmount = parseFixed( - amount.dp(18).toString(), - 18 - ).toBigInt(); - const mainBalance = poolPairData.mainBalanceScaled.toBigInt(); - const wrappedBalance = poolPairData.wrappedBalanceScaled.toBigInt(); - const bptSupply = poolPairData.virtualBptSupply.toBigInt(); - const params = { - fee: poolPairData.swapFee.toBigInt(), - lowerTarget: poolPairData.lowerTarget.toBigInt(), - upperTarget: poolPairData.upperTarget.toBigInt(), - rate: poolPairData.rate.toBigInt(), - }; - let result: bigint; - if (poolPairData.pairType === PairTypes.MainTokenToBpt) { - result = _spotPriceAfterSwapMainInPerBptOut( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if (poolPairData.pairType === PairTypes.BptToMainToken) { - result = _spotPriceAfterSwapBptInPerMainOut( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if (poolPairData.pairType === PairTypes.WrappedTokenToBpt) { - result = _spotPriceAfterSwapWrappedInPerBptOut( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if (poolPairData.pairType === PairTypes.BptToWrappedToken) { - result = _spotPriceAfterSwapBptInPerWrappedOut( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if ( - poolPairData.pairType === PairTypes.MainTokenToWrappedToken - ) { - result = _spotPriceAfterSwapMainInPerWrappedOut( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params - ); - } else if ( - poolPairData.pairType === PairTypes.WrappedTokenToMainToken - ) { - result = _spotPriceAfterSwapWrappedInPerMainOut( - bigintAmount, - mainBalance, - wrappedBalance, - bptSupply, - params + try { + const bigintAmount = parseFixed( + amount.dp(18).toString(), + 18 + ).toBigInt(); + const mainBalance = poolPairData.mainBalanceScaled.toBigInt(); + const wrappedBalance = poolPairData.wrappedBalanceScaled.toBigInt(); + const bptSupply = poolPairData.virtualBptSupply.toBigInt(); + const params = { + fee: poolPairData.swapFee.toBigInt(), + lowerTarget: poolPairData.lowerTarget.toBigInt(), + upperTarget: poolPairData.upperTarget.toBigInt(), + rate: poolPairData.rate.toBigInt(), + }; + let result: bigint; + if (poolPairData.pairType === PairTypes.MainTokenToBpt) { + result = _spotPriceAfterSwapMainInPerBptOut( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if (poolPairData.pairType === PairTypes.BptToMainToken) { + result = _spotPriceAfterSwapBptInPerMainOut( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if (poolPairData.pairType === PairTypes.WrappedTokenToBpt) { + result = _spotPriceAfterSwapWrappedInPerBptOut( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if (poolPairData.pairType === PairTypes.BptToWrappedToken) { + result = _spotPriceAfterSwapBptInPerWrappedOut( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if ( + poolPairData.pairType === PairTypes.MainTokenToWrappedToken + ) { + result = _spotPriceAfterSwapMainInPerWrappedOut( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else if ( + poolPairData.pairType === PairTypes.WrappedTokenToMainToken + ) { + result = _spotPriceAfterSwapWrappedInPerMainOut( + bigintAmount, + mainBalance, + wrappedBalance, + bptSupply, + params + ); + } else return bnum(0); + return scale(bnum(result.toString()), -18).dp( + poolPairData.decimalsOut, + 0 ); - } else return bnum(0); - return scale(bnum(result.toString()), -18).dp( - poolPairData.decimalsOut, - 0 - ); + } catch (err) { + return bnum(0); + } } _derivativeSpotPriceAfterSwapExactTokenInForTokenOut( diff --git a/src/types.ts b/src/types.ts index 71564943..7f3802bf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -187,6 +187,7 @@ export enum PoolFilter { Gyro2 = 'Gyro2', Gyro3 = 'Gyro3', GyroE = 'GyroE', + EulerLinear = 'EulerLinear', } export interface PoolBase { diff --git a/test/testScripts/swapExample.ts b/test/testScripts/swapExample.ts index 7a3b0c40..5aa5ae5d 100644 --- a/test/testScripts/swapExample.ts +++ b/test/testScripts/swapExample.ts @@ -58,7 +58,7 @@ function setUp(networkId: Network, provider: JsonRpcProvider): SOR { } export async function swap(): Promise { - const networkId = Network.POLYGON; + const networkId = Network.MAINNET; const provider = new JsonRpcProvider(PROVIDER_URLS[networkId]); // gasPrice is used by SOR as a factor to determine how many pools to swap against. // i.e. higher cost means more costly to trade against lots of different pools. @@ -66,7 +66,7 @@ export async function swap(): Promise { // This determines the max no of pools the SOR will use to swap. const maxPools = 4; const tokenIn = ADDRESSES[networkId].USDC; - const tokenOut = ADDRESSES[networkId].brz; + const tokenOut = ADDRESSES[networkId].DAI; const swapType: SwapTypes = SwapTypes.SwapExactIn; const swapAmount = parseFixed('200', 6);