Skip to content

Commit 01735dc

Browse files
committed
fix destTokenPos for root vertical branch in ex02 encoding
1 parent 5dca844 commit 01735dc

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

src/executor/Executor02BytecodeBuilder.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
ZEROS_4_BYTES,
2929
DEFAULT_RETURN_AMOUNT_POS,
3030
} from './constants';
31+
import * as constants from 'constants';
3132

3233
const {
3334
utils: { hexlify, hexDataLength, hexConcat, hexZeroPad, solidityPack },
@@ -475,6 +476,7 @@ export class Executor02BytecodeBuilder extends ExecutorBytecodeBuilder<
475476
swap: OptimalSwap,
476477
swapCallData: string,
477478
flag: Flag,
479+
isRoot = false,
478480
) {
479481
const data = this.packVerticalBranchingData(swapCallData);
480482

@@ -486,16 +488,34 @@ export class Executor02BytecodeBuilder extends ExecutorBytecodeBuilder<
486488
let destTokenPos: number;
487489

488490
if (isEthDest) {
489-
anyDexOnSwapNeedsWrapNative = this.anyDexOnSwapNeedsWrapNative(
490-
priceRoute,
491-
swap,
492-
exchangeParams,
493-
);
494-
anyDexOnSwapDoesntNeedWrapNative = this.anyDexOnSwapDoesntNeedWrapNative(
495-
priceRoute,
496-
swap,
497-
exchangeParams,
498-
);
491+
if (!isRoot) {
492+
anyDexOnSwapNeedsWrapNative = this.anyDexOnSwapNeedsWrapNative(
493+
priceRoute,
494+
swap,
495+
exchangeParams,
496+
);
497+
anyDexOnSwapDoesntNeedWrapNative =
498+
this.anyDexOnSwapDoesntNeedWrapNative(
499+
priceRoute,
500+
swap,
501+
exchangeParams,
502+
);
503+
} else {
504+
anyDexOnSwapNeedsWrapNative = priceRoute.bestRoute.some(route =>
505+
this.anyDexOnSwapNeedsWrapNative(
506+
priceRoute,
507+
route.swaps[route.swaps.length - 1],
508+
exchangeParams,
509+
),
510+
);
511+
anyDexOnSwapDoesntNeedWrapNative = priceRoute.bestRoute.some(route =>
512+
this.anyDexOnSwapDoesntNeedWrapNative(
513+
priceRoute,
514+
route.swaps[route.swaps.length - 1],
515+
exchangeParams,
516+
),
517+
);
518+
}
499519
}
500520

501521
if (
@@ -1313,6 +1333,7 @@ export class Executor02BytecodeBuilder extends ExecutorBytecodeBuilder<
13131333
needWrapEth
13141334
? Flag.DONT_INSERT_FROM_AMOUNT_DONT_CHECK_BALANCE_AFTER_SWAP // 0
13151335
: Flag.DONT_INSERT_FROM_AMOUNT_CHECK_SRC_TOKEN_BALANCE_AFTER_SWAP, // 8
1336+
true, // isRoot branch
13161337
);
13171338
}
13181339

src/executor/executor02-bytecode-builder-e2e.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,36 @@ describe('Executor02ByteCodeBuilder e2e tests', () => {
14971497
);
14981498
});
14991499
});
1500+
1501+
describe('USDCe -> MATIC', () => {
1502+
const dexKeys = ['CurveV2', 'UniswapV3', 'SushiSwapV3', 'SwaapV2'];
1503+
1504+
const tokenASymbol: string = 'USDCe';
1505+
const tokenBSymbol: string = 'MATIC';
1506+
const tokenAAmount: string = '1978798814';
1507+
1508+
const side = SwapSide.SELL;
1509+
1510+
it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
1511+
await testE2E(
1512+
tokens[tokenASymbol],
1513+
tokens[tokenBSymbol],
1514+
holders[tokenASymbol],
1515+
tokenAAmount,
1516+
side,
1517+
dexKeys,
1518+
contractMethod,
1519+
network,
1520+
provider,
1521+
undefined,
1522+
undefined,
1523+
undefined,
1524+
100,
1525+
2000,
1526+
false,
1527+
);
1528+
});
1529+
});
15001530
});
15011531
});
15021532

0 commit comments

Comments
 (0)