Skip to content

Commit

Permalink
Merge pull request paraswap#475 from paraswap/fix/revert-b2-buy-multihop
Browse files Browse the repository at this point in the history
Fix/revert b2 buy multihop
  • Loading branch information
mwamedacen authored Aug 21, 2023
2 parents 8f18094 + 882b7b1 commit a1a4270
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 105 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 0 additions & 2 deletions src/dex/balancer-v2/balancer-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
111 changes: 49 additions & 62 deletions src/dex/balancer-v2/optimizer.ts
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;
}
4 changes: 0 additions & 4 deletions src/dex/balancer-v2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ export interface SubgraphPoolBase {
export type BalancerSwapV2 = {
poolId: string;
amount: string;
tokenIn: string;
tokenOut: string;
};

export type OptimizedBalancerV2Data = {
Expand Down Expand Up @@ -141,8 +139,6 @@ export type BalancerV2DirectParam = [

export type BalancerV2Data = {
poolId: string;
tokenIn: string;
tokenOut: string;
};

export type DexParams = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -146,7 +114,7 @@ const implementations: Record<ImplementationNames, calc_token_amount> = {
[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,

Expand Down
7 changes: 4 additions & 3 deletions src/dex/uniswap-v3/uniswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down Expand Up @@ -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}`,
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit a1a4270

Please sign in to comment.