From 01735dcc5b1b8ec6dbc13e2703ca69bd0c79b674 Mon Sep 17 00:00:00 2001 From: Alexander Burkut Date: Tue, 24 Dec 2024 15:54:49 +0300 Subject: [PATCH] fix destTokenPos for root vertical branch in ex02 encoding --- src/executor/Executor02BytecodeBuilder.ts | 41 ++++++++++++++----- .../executor02-bytecode-builder-e2e.test.ts | 30 ++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/executor/Executor02BytecodeBuilder.ts b/src/executor/Executor02BytecodeBuilder.ts index 90cf76be8..f7096df67 100644 --- a/src/executor/Executor02BytecodeBuilder.ts +++ b/src/executor/Executor02BytecodeBuilder.ts @@ -28,6 +28,7 @@ import { ZEROS_4_BYTES, DEFAULT_RETURN_AMOUNT_POS, } from './constants'; +import * as constants from 'constants'; const { utils: { hexlify, hexDataLength, hexConcat, hexZeroPad, solidityPack }, @@ -475,6 +476,7 @@ export class Executor02BytecodeBuilder extends ExecutorBytecodeBuilder< swap: OptimalSwap, swapCallData: string, flag: Flag, + isRoot = false, ) { const data = this.packVerticalBranchingData(swapCallData); @@ -486,16 +488,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 ( @@ -1313,6 +1333,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 ); } diff --git a/src/executor/executor02-bytecode-builder-e2e.test.ts b/src/executor/executor02-bytecode-builder-e2e.test.ts index 514fc60e2..04b47edf7 100644 --- a/src/executor/executor02-bytecode-builder-e2e.test.ts +++ b/src/executor/executor02-bytecode-builder-e2e.test.ts @@ -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, + ); + }); + }); }); });