How to generate multi hop swap path with ethers.js?

19 views Asked by At

everyone. I am beginner to blockchain development. Currently I have worked in a web3 project with ethers.js. Now I am going to use Uniswap's multi hop swap. I got SwapRoute instance. But the smart contract requires the bytes typed path(swapExactInputMultihop method). How can I get it from SwapRoute object?

Following is a function to generate route I referred to uniswap example projects.

async generateRoute(chainId, recipientAddress, tokenin, tokenout, amountInInt) {
    
    const router = new AlphaRouter({
      chainId: chainId,
      provider: provider(providerURL(currencyFrom.blockchain)),
    });

    const options = {
      recipient: recipientAddress,
      slippageTolerance: new Percent(100, 10_000), // 1%
      deadline: Math.floor(Date.now() / 1000 + 1800), //30min
      type: 1, // means, SwapType.SWAP_ROUTER_02
    };

    const route = await router.route(
      CurrencyAmount.fromRawAmount(
        tokenin,
        amountInInt.toString(),
      ),
      tokenout,
      0, // means TradeType.EXACT_INPUT,
      options,
    );
    if (!route || !route.methodParameters) {
      return null;
    }

    return route;
  }

I found this: ethers.utils.solidityPack([tokenin, poolFee, anothertoken, poolFee, tokenout]). But I don't know also how to get array from SwapRoute instance. Any help would be nice to me. Thank you.

0

There are 0 answers