Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix destTokenPos for root vertical branch in ex02 encoding #870

Merged
merged 6 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "4.0.9",
"version": "4.0.10",
"main": "build/index.js",
"types": "build/index.d.ts",
"repository": "https://github.com/paraswap/paraswap-dex-lib",
Expand Down
11 changes: 10 additions & 1 deletion src/dex/camelot/camelot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ export class Camelot
isFeeTokenInRoute: Object.values(transferFees).some(f => f !== 0),
pools: [
{
stable: pairParam.stable,
address: pairParam.exchange,
fee: parseInt(pairParam.fee),
direction: pairParam.direction,
Expand Down Expand Up @@ -843,9 +844,17 @@ export class Camelot
if (side === SwapSide.BUY) throw new Error('Buy not supported');
let exchangeDataTypes = ['bytes4', 'bytes32'];

const isStable = data.pools.some(pool => !!pool.stable);
const isStablePoolAndPoolCount = isStable
? BigNumber.from(1)
.shl(255)
.or(BigNumber.from(data.pools.length))
.toHexString()
: hexZeroPad(hexlify(data.pools.length), 32);

let exchangeDataToPack = [
hexZeroPad(hexlify(0), 4),
hexZeroPad(hexlify(data.pools.length), 32),
isStablePoolAndPoolCount,
];

const pools = encodePools(data.pools, this.feeFactor);
Expand Down
11 changes: 10 additions & 1 deletion src/dex/solidly/solidly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export class Solidly extends UniswapV2 {
isFeeTokenInRoute: Object.values(transferFees).some(f => f !== 0),
pools: [
{
stable: pairParam.stable,
address: pairParam.exchange,
fee: parseInt(pairParam.fee),
direction: pairParam.direction,
Expand Down Expand Up @@ -657,9 +658,17 @@ export class Solidly extends UniswapV2 {
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
let exchangeDataTypes = ['bytes4', 'bytes32'];

const isStable = data.pools.some(pool => !!pool.stable);
const isStablePoolAndPoolCount = isStable
? BigNumber.from(1)
.shl(255)
.or(BigNumber.from(data.pools.length))
.toHexString()
: hexZeroPad(hexlify(data.pools.length), 32);

let exchangeDataToPack = [
hexZeroPad(hexlify(0), 4),
hexZeroPad(hexlify(data.pools.length), 32),
isStablePoolAndPoolCount,
];

const pools = encodePools(data.pools, this.feeFactor);
Expand Down
7 changes: 5 additions & 2 deletions src/dex/solidly/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ export interface SolidlyPoolOrderedParams extends UniswapV2PoolOrderedParams {
stable: boolean;
}

export type SolidlyData = UniswapV2Data & { isFeeTokenInRoute: boolean };
export type SolidlyPool = UniswapPool & { stable: boolean };

export type SolidlyPool = UniswapPool;
export type SolidlyData = {
isFeeTokenInRoute: boolean;
pools: SolidlyPool[];
} & UniswapV2Data;

export interface DexParams extends Omit<UniswapV2DexParams, 'feeCode'> {
feeCode: number;
Expand Down
41 changes: 30 additions & 11 deletions src/executor/Executor02BytecodeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@paraswap/core';
import {
DexExchangeBuildParam,
DexExchangeParam,
DexExchangeParamWithBooleanNeedWrapNative,
} from '../types';
import { Executors, Flag, SpecialDex } from './types';
Expand Down Expand Up @@ -475,6 +474,7 @@ export class Executor02BytecodeBuilder extends ExecutorBytecodeBuilder<
swap: OptimalSwap,
swapCallData: string,
flag: Flag,
isRoot = false,
) {
const data = this.packVerticalBranchingData(swapCallData);

Expand All @@ -486,16 +486,34 @@ export class Executor02BytecodeBuilder extends ExecutorBytecodeBuilder<
let destTokenPos: number;

if (isEthDest) {
anyDexOnSwapNeedsWrapNative = this.anyDexOnSwapNeedsWrapNative(
priceRoute,
swap,
exchangeParams,
);
anyDexOnSwapDoesntNeedWrapNative = this.anyDexOnSwapDoesntNeedWrapNative(
priceRoute,
swap,
exchangeParams,
);
if (!isRoot) {
anyDexOnSwapNeedsWrapNative = this.anyDexOnSwapNeedsWrapNative(
priceRoute,
swap,
exchangeParams,
);
anyDexOnSwapDoesntNeedWrapNative =
this.anyDexOnSwapDoesntNeedWrapNative(
priceRoute,
swap,
exchangeParams,
);
} else {
anyDexOnSwapNeedsWrapNative = priceRoute.bestRoute.some(route =>
this.anyDexOnSwapNeedsWrapNative(
priceRoute,
route.swaps[route.swaps.length - 1],
exchangeParams,
),
);
anyDexOnSwapDoesntNeedWrapNative = priceRoute.bestRoute.some(route =>
this.anyDexOnSwapDoesntNeedWrapNative(
priceRoute,
route.swaps[route.swaps.length - 1],
exchangeParams,
),
);
}
}

if (
Expand Down Expand Up @@ -1313,6 +1331,7 @@ export class Executor02BytecodeBuilder extends ExecutorBytecodeBuilder<
needWrapEth
? Flag.DONT_INSERT_FROM_AMOUNT_DONT_CHECK_BALANCE_AFTER_SWAP // 0
: Flag.DONT_INSERT_FROM_AMOUNT_CHECK_SRC_TOKEN_BALANCE_AFTER_SWAP, // 8
true, // isRoot branch
);
}

Expand Down
30 changes: 30 additions & 0 deletions src/executor/executor02-bytecode-builder-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,36 @@ describe('Executor02ByteCodeBuilder e2e tests', () => {
);
});
});

describe('USDCe -> MATIC', () => {
const dexKeys = ['CurveV2', 'UniswapV3', 'SushiSwapV3', 'SwaapV2'];

const tokenASymbol: string = 'USDCe';
const tokenBSymbol: string = 'MATIC';
const tokenAAmount: string = '1978798814';

const side = SwapSide.SELL;

it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
await testE2E(
tokens[tokenASymbol],
tokens[tokenBSymbol],
holders[tokenASymbol],
tokenAAmount,
side,
dexKeys,
contractMethod,
network,
provider,
undefined,
undefined,
undefined,
100,
2000,
false,
);
});
});
});
});

Expand Down
Loading