forked from paraswap/paraswap-dex-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
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#475 from paraswap/fix/revert-b2-buy-multihop
Fix/revert b2 buy multihop
- Loading branch information
Showing
6 changed files
with
55 additions
and
105 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,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<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) | ||
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) | ||
).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; | ||
} |
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