I am using the method: swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
.
How to determine the path router of a swap in address[] calldata path
Someone's path is 3 addresses, another is 2 addresses
1 Answer
if i got your question correctly, you would like to determine when have 2 address or 3 address in your path.
There are instances where you would like to trade your tokenA and tokenB through WETH. That is from tokenA to WETH then from WETH to tokenB. The reason being, some times it results in a better price. In such cases your path will have 3 Addresses [tokenA, WETH, tokenB].
If you want a direct swap or one of your token pools has WETH already then you can have two addresses. Find below snippet on how you can check and choose between 3 addresses and 2 address.
address[] memory path;
if (tokenA == WETH || tokenB == WETH) {
path = new address[](2);
path[0] = tokenA;
path[1] = tokenB;
} else {
path = new address[](3);
path[0] = tokenA;
path[1] = WETH;
path[2] = tokenB;
}
-
Thank for you answer. I take the example of FLURRY. It has 3 pairs of liquidity
FLURRY/WBNB FLURRY/BUSD FLURRY/USDT
. I use swap coin98 link here. Case 1: SwapBUSD => FLURRY
, its path isBUSD > USDC > FLURRY
. Case 2: SwapUSDT => FLURRY
, its pathUSDT > UST > USDC > FLURRY
. Both Case1 and Case2 confuse me– BaoNXCommented Dec 13, 2021 at 3:55