diff --git a/package.json b/package.json index e73eafb06..44655e19c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@paraswap/dex-lib", - "version": "2.30.0", + "version": "2.30.1", "main": "build/index.js", "types": "build/index.d.ts", "repository": "https://github.com/paraswap/paraswap-dex-lib", diff --git a/src/dex/balancer-v2/balancer-v2.ts b/src/dex/balancer-v2/balancer-v2.ts index f71274414..ac1c3f443 100644 --- a/src/dex/balancer-v2/balancer-v2.ts +++ b/src/dex/balancer-v2/balancer-v2.ts @@ -826,8 +826,6 @@ export class BalancerV2 prices: resOut.prices, data: { poolId: pool.id, - tokenIn: _from.address.toLowerCase(), - tokenOut: _to.address.toLowerCase(), }, poolAddresses: [poolAddress], exchange: this.dexKey, diff --git a/src/dex/balancer-v2/optimizer.ts b/src/dex/balancer-v2/optimizer.ts index 201b7a19f..818c2f7f8 100644 --- a/src/dex/balancer-v2/optimizer.ts +++ b/src/dex/balancer-v2/optimizer.ts @@ -1,83 +1,70 @@ -import _ from 'lodash'; -import { UnoptimizedRate } from '../../types'; +import { UnoptimizedRate, OptimalSwapExchange } from '../../types'; +import { BalancerSwapV2 } from './types'; import { SwapSide } from '../../constants'; import { BalancerConfig } from './config'; -import { OptimalSwap } from '@paraswap/core'; + +const MAX_UINT256 = + '115792089237316195423570985008687907853269984665640564039457584007913129639935'; + +const AllBalancerV2Forks = Object.keys(BalancerConfig); export function balancerV2Merge(or: UnoptimizedRate): UnoptimizedRate { - const balancerForksList = Object.keys(BalancerConfig).map(b => - b.toLowerCase(), - ); - const fixSwap = (rawRate: OptimalSwap[], side: SwapSide): OptimalSwap[] => { - let lastExchange: false | OptimalSwap = false; - let optimizedRate = new Array(); - rawRate.forEach((s: OptimalSwap) => { - if ( - s.swapExchanges.length !== 1 || - !balancerForksList.includes(s.swapExchanges[0].exchange.toLowerCase()) - ) { - lastExchange = false; - optimizedRate.push(s); - } else if ( - lastExchange && - lastExchange.swapExchanges[0].exchange.toLowerCase() === - s.swapExchanges[0].exchange.toLowerCase() && - _.last( - lastExchange.swapExchanges[0].data.swaps, - )!.tokenOut.toLowerCase() === - s.swapExchanges[0].data.tokenIn.toLowerCase() - ) { - const [lastExchangeSwap] = lastExchange.swapExchanges; - const [currentSwap] = s.swapExchanges; - lastExchangeSwap.srcAmount = ( - BigInt(lastExchangeSwap.srcAmount) + BigInt(currentSwap.srcAmount) + const fixSwap = ( + rawSwap: OptimalSwapExchange[], + side: SwapSide, + ): OptimalSwapExchange[] => { + const newBalancers: { [key: string]: OptimalSwapExchange } = {}; + let optimizedSwap = new Array>(); + rawSwap.forEach((s: OptimalSwapExchange) => { + const exchangeKey = s.exchange.toLowerCase(); + if (AllBalancerV2Forks.some(d => d.toLowerCase() === exchangeKey)) { + if (!(exchangeKey in newBalancers)) { + newBalancers[exchangeKey] = { + exchange: s.exchange, + srcAmount: '0', + destAmount: '0', + percent: 0, + poolAddresses: [], + data: { + swaps: new Array(), + gasUSD: '0', + }, + }; + } + newBalancers[exchangeKey].srcAmount = ( + BigInt(newBalancers[exchangeKey].srcAmount) + BigInt(s.srcAmount) ).toString(); - lastExchangeSwap.destAmount = ( - BigInt(lastExchangeSwap.destAmount) + BigInt(currentSwap.destAmount) + newBalancers[exchangeKey].destAmount = ( + BigInt(newBalancers[exchangeKey].destAmount) + BigInt(s.destAmount) ).toString(); - lastExchangeSwap.percent += currentSwap.percent; - lastExchangeSwap.data.gasUSD = ( - parseFloat(lastExchangeSwap.data.gasUSD) + - parseFloat(currentSwap.data.gasUSD) + newBalancers[exchangeKey].percent += s.percent; + newBalancers[exchangeKey].data.exchangeProxy = s.data.exchangeProxy; + newBalancers[exchangeKey].data.gasUSD = ( + parseFloat(newBalancers[exchangeKey].data.gasUSD) + + parseFloat(s.data.gasUSD) ).toFixed(6); - lastExchangeSwap.data.swaps.push({ - poolId: currentSwap.data.poolId, - amount: - side === SwapSide.SELL - ? currentSwap.srcAmount - : currentSwap.destAmount, - tokenIn: currentSwap.data.tokenIn, - tokenOut: currentSwap.data.tokenOut, + newBalancers[exchangeKey].data.swaps.push({ + poolId: s.data.poolId, + amount: side === SwapSide.SELL ? s.srcAmount : s.destAmount, }); - lastExchangeSwap.poolAddresses!.push(currentSwap.poolAddresses![0]); + newBalancers[exchangeKey].poolAddresses!.push(s.poolAddresses![0]); } else { - lastExchange = _.cloneDeep(s); - lastExchange.swapExchanges[0].data = {}; - lastExchange.swapExchanges[0].data.gasUSD = - s.swapExchanges[0].data.gasUSD; - lastExchange.swapExchanges[0].data.swaps = [ - { - poolId: s.swapExchanges[0].data.poolId, - amount: - side === SwapSide.SELL - ? s.swapExchanges[0].srcAmount - : s.swapExchanges[0].destAmount, - tokenIn: s.swapExchanges[0].data.tokenIn, - tokenOut: s.swapExchanges[0].data.tokenOut, - }, - ]; - optimizedRate.push(lastExchange); + optimizedSwap.push(s); } }); - return optimizedRate; + optimizedSwap = optimizedSwap.concat(Object.values(newBalancers)); + return optimizedSwap; }; or.bestRoute = or.bestRoute.map(r => ({ ...r, - swaps: fixSwap(r.swaps, or.side), + swaps: r.swaps.map(s => ({ + ...s, + swapExchanges: fixSwap(s.swapExchanges, or.side), + })), })); return or; } diff --git a/src/dex/balancer-v2/types.ts b/src/dex/balancer-v2/types.ts index 33dd35bfe..f1ef3194c 100644 --- a/src/dex/balancer-v2/types.ts +++ b/src/dex/balancer-v2/types.ts @@ -82,8 +82,6 @@ export interface SubgraphPoolBase { export type BalancerSwapV2 = { poolId: string; amount: string; - tokenIn: string; - tokenOut: string; }; export type OptimizedBalancerV2Data = { @@ -141,8 +139,6 @@ export type BalancerV2DirectParam = [ export type BalancerV2Data = { poolId: string; - tokenIn: string; - tokenOut: string; }; export type DexParams = { diff --git a/src/dex/curve-v1-factory/price-handlers/functions/calc_token_amount.ts b/src/dex/curve-v1-factory/price-handlers/functions/calc_token_amount.ts index cb83a1dfe..d8c37134c 100644 --- a/src/dex/curve-v1-factory/price-handlers/functions/calc_token_amount.ts +++ b/src/dex/curve-v1-factory/price-handlers/functions/calc_token_amount.ts @@ -35,38 +35,6 @@ const customPlain3CoinThree: calc_token_amount = ( return (diff * token_amount) / D0; }; -const factoryPlain2Basic: calc_token_amount = ( - self: IPoolContext, - state: PoolState, - amounts: bigint[], - is_deposit: boolean, -) => { - const { N_COINS } = self.constants; - const amp = state.A; - const balances = [...state.balances]; - const D0 = self.get_D(self, balances, amp); - for (const i of _.range(N_COINS)) { - if (is_deposit) balances[i] += amounts[i]; - else balances[i] -= amounts[i]; - } - const D1 = self.get_D(self, balances, amp); - - if (state.totalSupply === undefined) { - throw new Error( - `${self.IMPLEMENTATION_NAME} customPlain3CoinThree: totalSupply is not provided`, - ); - } - - const token_amount = state.totalSupply; - let diff = 0n; - if (is_deposit) { - diff = D1 - D0; - } else { - diff = D0 - D1; - } - return (diff * token_amount) / D0; -}; - const customAvalanche3CoinLending: calc_token_amount = ( self: IPoolContext, state: PoolState, @@ -146,7 +114,7 @@ const implementations: Record = { [ImplementationNames.FACTORY_META_USD_BALANCES_FRAX_USDC]: notImplemented, [ImplementationNames.FACTORY_PLAIN_2_BALANCES]: notImplemented, - [ImplementationNames.FACTORY_PLAIN_2_BASIC]: factoryPlain2Basic, + [ImplementationNames.FACTORY_PLAIN_2_BASIC]: notImplemented, [ImplementationNames.FACTORY_PLAIN_2_ETH]: notImplemented, [ImplementationNames.FACTORY_PLAIN_2_OPTIMIZED]: notImplemented, diff --git a/src/dex/uniswap-v3/uniswap-v3.ts b/src/dex/uniswap-v3/uniswap-v3.ts index 1e9be24da..8a6ae661c 100644 --- a/src/dex/uniswap-v3/uniswap-v3.ts +++ b/src/dex/uniswap-v3/uniswap-v3.ts @@ -120,7 +120,7 @@ export class UniswapV3 this.stateMultiContract = new this.dexHelper.web3Provider.eth.Contract( this.config.stateMultiCallAbi !== undefined ? this.config.stateMultiCallAbi - : UniswapV3StateMulticallABI as AbiItem[], + : (UniswapV3StateMulticallABI as AbiItem[]), this.config.stateMulticall, ); @@ -279,7 +279,7 @@ export class UniswapV3 e, ); } else { - // on unknown error mark as failed and increase retryCount for retry init strategy + // on unkown error mark as failed and increase retryCount for retry init strategy // note: state would be null by default which allows to fallback this.logger.warn( `${this.dexKey}: Can not generate pool state for srcAddress=${srcAddress}, destAddress=${destAddress}, fee=${fee} pool fallback to rpc and retry every ${this.config.initRetryFrequency} times, initRetryAttemptCount=${pool.initRetryAttemptCount}`, @@ -1059,7 +1059,8 @@ export class UniswapV3 initHash: this.config.initHash, subgraphURL: this.config.subgraphURL, stateMultiCallAbi: this.config.stateMultiCallAbi, - decodeStateMultiCallResultWithRelativeBitmaps: this.config.decodeStateMultiCallResultWithRelativeBitmaps, + decodeStateMultiCallResultWithRelativeBitmaps: + this.config.decodeStateMultiCallResultWithRelativeBitmaps, }; return newConfig; }