smart-order-router icon indicating copy to clipboard operation
smart-order-router copied to clipboard

how to swap native token with erc20 using universal router

Open s-pcode opened this issue 1 year ago • 2 comments

  • I'm submitting a ... [ ] question about how to use this project

  • Summary I want to bulid a swap dapp using uniswap universal router. I need to get the quotes for the token pairs to be rendered in the UI. But the quote is null for native->erc20 pair.

    const baseToken = Ether.onChain(1) as Currency; - this Ether.onChain() is not working for other chain native tokens.

    For example if the chain is Binance smart chain with chainId as 56, then it throws the below error : Error: Invariant failed: WRAPPED. I guess the WETH9 function is failing internally.

    I have also tried the nativeOnChain(chainId) function, but yet the quote returned from the alpha router is null.

  • Other information (e.g. detailed explanation, stack traces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)

I'm able to get the quote for an erc20->erc20 pair using this sdk but not for a native token -> erc20. Below is my code implementation, please let me know if I have messed up somewhere.


 const router = new AlphaRouter({
      chainId,
      provider,
    });

    try {
      console.log(WETH9[chainId], "WETH9[chainId].address");
      const tokenA =
        tokenIn.token_address.toLowerCase() === getChainWiseNativeToken(chainId)
          ? (Ether.onChain(chainId) as Currency)
          : new Token(
              chainId,
              tokenIn.token_address,
              +tokenIn.decimals,
              tokenIn.symbol,
              tokenIn.symbol
            );
      const tokenB = new Token(
        chainId,
        tokenOut.address,
        tokenOut.decimals,
        tokenOut.symbol,
        tokenOut.name
      );

      const amountIn = CurrencyAmount.fromRawAmount(
        tokenA,
        JSBI.BigInt(ethers.utils.parseUnits(amount.toString(), tokenA.decimals))
      );

      console.log(amountIn.toExact(), "amountIn");

      let swapOptions: SwapOptions = {
        type: SwapType.UNIVERSAL_ROUTER,
        recipient: userWalletAddress,
        slippageTolerance: new Percent(5, 100),
        deadlineOrPreviousBlockhash: parseDeadline(360),
      };

      const partialRoutingConfig: Partial<AlphaRouterConfig> = {
        protocols: [Protocol.V2, Protocol.V3, Protocol.MIXED],
      };

      if (permitSig?.signature) {
        swapOptions = {
          ...swapOptions,
          inputTokenPermit: {
            ...permitSig?.permit,
            signature: permitSig.signature,
          },
        };
      }

      const quote = await router.route(
        amountIn,
        tokenB,
        TradeType.EXACT_INPUT,
        swapOptions,
        partialRoutingConfig
      );

      console.log(
        quote?.methodParameters?.calldata,
        "quote?.methodParameters?.calldata"
      );
      if (!quote) return;
      return quote;
    } catch (err: any) {
      console.log(err);
    }

s-pcode avatar Nov 24 '23 20:11 s-pcode

hey @s-pcode did you find the solution or issue with this?

harshit-bitpack avatar Jul 07 '24 22:07 harshit-bitpack

hey @harshit-bitpack , have documented the steps here

s-pcode avatar Jul 08 '24 10:07 s-pcode