Skip to main content
Version: V1

Swap

Config dexes swap fee (id)#

1 - 30 (default) = 0.3%

2 - 25

3 - 20

4 - 15

5 - 10

6 - 5

{
all: {
pancake: 2,
'pancake-legacy': 2,
apeswap: 3,
wault: 3,
biswap: 5,
polydex: 5,
jetswap: 5,
polycat: 2,
spookyswap: 3
},
[ChainId.BSCMAINNET]: {
jetswap: 1
}
}

Config dexes type (type)#

default 0

{
all: {
curve: 2,
dmm: 3,
oneswap: 1,
ellipsis: 2,
nerve: 1,
'iron-stable': 4,
balancer: 6,
synapse: 4
}
}

Prepare swapSequences from trade.swaps array#

With each sequence, prepare an object

{data, dexOption}
  • dexOption = (dexType(8 bit format) + dexId(8 bit format)) in decimal format - Ex: Dmm dex(id=1, type=3) -> dexOption = 0000001100000001 in decimal format = 769
  • data
    - dexType = 1 || 4 -> stable type
    new ethers.utils.AbiCoder().encodeParameters(
    ['address', 'address', 'address', 'int128', 'int128', 'uint256', 'uint256', 'uint256', 'address'],
    [pool, tokenIn, tokenOut, extra?.tokenInIndex, extra?.tokenOutIndex, swapAmount, limitReturnAmount || '0', poolLength, pool]
    )
    - dexType = 2 -> Curve Type
    const usePoolUnderlying = data.extra?.underlying
    const isTriCrypto = poolType === 'curve-tricrypto'
    new ethers.utils.AbiCoder().encodeParameters(
    ['address', 'address', 'address', 'int128', 'int128', 'uint256', 'uint256', 'bool', 'bool'],
    [pool, tokenIn, tokenOut, extra?.tokenInIndex, extra?.tokenOutIndex, swapAmount, '0', usePoolUnderlying, isTriCrypto]
    )
    - dexType = 6 -> Balancer
    new ethers.utils.AbiCoder().encodeParameters(
    ['address', 'bytes32', 'address', 'address', 'uint256', 'uint256'],
    [data.extra?.vault, data.pool, data.tokenIn, data.tokenOut, data.swapAmount, data.limitReturnAmount || '0']
    )
    - else -> Uni type
    new ethers.utils.AbiCoder().encodeParameters(
    ['address', 'address', 'address', 'uint256', 'uint256'],
    [pool, tokenIn, tokenOut, swapAmount, limitReturnAmount || '0']
    )

Prepare executorData#

aggregationExecutorContract.interface.encodeFunctionData('getCallByte', [[swapSequences, tokenIn, tokenOut, amountOut, to, deadline]])
executorData = '0x' + executorData.slice(10) //remove method name

Prepare and execute swap call#

const swapDesc = [
tokenIn,
tokenOut,
aggregationExecutorAddress,
to,
amountIn,
amountOut,
etherIn ? numberToHex(0) : numberToHex(4),
'0x'
]
args = [aggregationExecutorAddress, swapDesc, executorData]
value = etherIn ? amountIn : ZERO_HEX
aggregationRouterContract["swap"](...args, gasLimit, { value, from: account })