forked from paraswap/paraswap-dex-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request paraswap#466 from paraswap/fix/1222-multihop-buy-b…
…alancer BACK-1222: fix BalancerV2 buy multihop
- Loading branch information
Showing
6 changed files
with
103 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,83 @@ | ||
import { UnoptimizedRate, OptimalSwapExchange } from '../../types'; | ||
import { BalancerSwapV2 } from './types'; | ||
import _ from 'lodash'; | ||
import { UnoptimizedRate } from '../../types'; | ||
import { SwapSide } from '../../constants'; | ||
import { BalancerConfig } from './config'; | ||
|
||
const MAX_UINT256 = | ||
'115792089237316195423570985008687907853269984665640564039457584007913129639935'; | ||
|
||
const AllBalancerV2Forks = Object.keys(BalancerConfig); | ||
import { OptimalSwap } from '@paraswap/core'; | ||
|
||
export function balancerV2Merge(or: UnoptimizedRate): UnoptimizedRate { | ||
const fixSwap = ( | ||
rawSwap: OptimalSwapExchange<any>[], | ||
side: SwapSide, | ||
): OptimalSwapExchange<any>[] => { | ||
const newBalancers: { [key: string]: OptimalSwapExchange<any> } = {}; | ||
let optimizedSwap = new Array<OptimalSwapExchange<any>>(); | ||
rawSwap.forEach((s: OptimalSwapExchange<any>) => { | ||
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<BalancerSwapV2>(), | ||
gasUSD: '0', | ||
}, | ||
}; | ||
} | ||
newBalancers[exchangeKey].srcAmount = ( | ||
BigInt(newBalancers[exchangeKey].srcAmount) + BigInt(s.srcAmount) | ||
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<OptimalSwap>(); | ||
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( | ||
<any[]>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) | ||
).toString(); | ||
|
||
newBalancers[exchangeKey].destAmount = ( | ||
BigInt(newBalancers[exchangeKey].destAmount) + BigInt(s.destAmount) | ||
lastExchangeSwap.destAmount = ( | ||
BigInt(lastExchangeSwap.destAmount) + BigInt(currentSwap.destAmount) | ||
).toString(); | ||
|
||
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) | ||
lastExchangeSwap.percent += currentSwap.percent; | ||
lastExchangeSwap.data.gasUSD = ( | ||
parseFloat(lastExchangeSwap.data.gasUSD) + | ||
parseFloat(currentSwap.data.gasUSD) | ||
).toFixed(6); | ||
|
||
newBalancers[exchangeKey].data.swaps.push({ | ||
poolId: s.data.poolId, | ||
amount: side === SwapSide.SELL ? s.srcAmount : s.destAmount, | ||
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].poolAddresses!.push(s.poolAddresses![0]); | ||
lastExchangeSwap.poolAddresses!.push(currentSwap.poolAddresses![0]); | ||
} else { | ||
optimizedSwap.push(s); | ||
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 = optimizedSwap.concat(Object.values(newBalancers)); | ||
return optimizedSwap; | ||
return optimizedRate; | ||
}; | ||
|
||
or.bestRoute = or.bestRoute.map(r => ({ | ||
...r, | ||
swaps: r.swaps.map(s => ({ | ||
...s, | ||
swapExchanges: fixSwap(s.swapExchanges, or.side), | ||
})), | ||
swaps: fixSwap(r.swaps, or.side), | ||
})); | ||
return or; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters