typescriptethereumweb3jserc20uniswap

Uniswap V3 SwapRouter.swapCallParameters Could not parse fraction


I am trying to preform a basic trade following this guide (code)

I have edited some of the functions in the code to take in parameters for things like :

The following is my createTrade function :

export async function createTrade(
  tin: Token,
  tout: Token,
  amountIn: number,
): Promise<TokenTrade> {
  const poolInfo = await getPoolInfo(tin, tout);

  const pool = new Pool(
    tin, // tin
    tout, //tout
    FeeAmount.MEDIUM,
    poolInfo.sqrtPriceX96.toString(),
    poolInfo.liquidity.toString(),
    poolInfo.tick,
  );

  const swapRoute = new Route(
    [pool],
    tin, // tin
    tout, //tout
  );

  const amountOut = await getOutputQuote(swapRoute, amountIn, tin);

  const uncheckedTrade = Trade.createUncheckedTrade({
    route: swapRoute,
    inputAmount: CurrencyAmount.fromRawAmount(
      tin, //tin
      fromReadableAmount(
        amountIn, // amount in
        tin.decimals, // tin.decimals
      ).toString(),
    ),
    outputAmount: CurrencyAmount.fromRawAmount(
      tout, //tout
      JSBI.toNumber(JSBI.BigInt(amountOut)),
    ),
    tradeType: TradeType.EXACT_INPUT,
  });

  return uncheckedTrade;
}

And this is my executeTrade function :

export async function executeTrade(
  trade: TokenTrade,
  tin: Token,
): Promise<TransactionState> {
  const walletAddress = getWalletAddress();
  const provider = getProvider();

  if (!walletAddress || !provider) {
    throw new Error('Cannot execute a trade without a connected wallet');
  }

  // Give approval to the router to spend the token
  const tokenApproval = await getTokenTransferApproval(tin); //tin

  // Fail if transfer approvals do not go through
  if (tokenApproval !== TransactionState.Sent) {
    return TransactionState.Failed;
  }

  const options: SwapOptions = {
    slippageTolerance: new Percent(50, 10_000), // 50 bips, or 0.50%
    deadline: Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time
    recipient: walletAddress,
  };

  const methodParameters = SwapRouter.swapCallParameters([trade], options);

  const tx = {
    data: methodParameters.calldata,
    to: SWAP_ROUTER_ADDRESS,
    value: methodParameters.value,
    from: walletAddress,
    maxFeePerGas: MAX_FEE_PER_GAS,
    maxPriorityFeePerGas: MAX_PRIORITY_FEE_PER_GAS,
  };

  const res = await sendTransaction(tx);

  return res;
}

And finally, here is how i am calling it :

import { createTrade, executeTrade } from './services/trade';
import { SUPPORTED_CHAINS, Token } from '@uniswap/sdk-core';
import { getCurrencyBalance, wrapETH } from './services/wallet';
import { getProvider, getWalletAddress } from './services/providers';

const run = async function () {
  const USDC_TOKEN = new Token(
    SUPPORTED_CHAINS[1],
    '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
    6,
    'USDC',
    'USD//C',
  );

  const WETH_TOKEN = new Token(
    SUPPORTED_CHAINS[1],
    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
    18,
    'WETH',
    'Wrapped Ether',
  );

  console.log(
    await getCurrencyBalance(getProvider(), getWalletAddress(), WETH_TOKEN),
  );
  console.log(await wrapETH(1));
  console.log(
    await getCurrencyBalance(getProvider(), getWalletAddress(), WETH_TOKEN),
  );
  //buy WETH with ETH

  const trade = await createTrade(WETH_TOKEN, USDC_TOKEN, 1);

  const execute = await executeTrade(trade, WETH_TOKEN);

  console.log(execute);

  console.log(
    await getCurrencyBalance(getProvider(), getWalletAddress(), USDC_TOKEN),
  );
};

run();

However, I encounter the following problem when this line gets executed

const methodParameters = SwapRouter.swapCallParameters([trade], options);

What am I doing wrong ?

Error: Could not parse fraction
    at Function.tryParseFraction (c:\Users\DixSon\Desktop\Frog\periscope\node_modules\.pnpm\@uniswap+sdk-core@4.0.6\node_modules\@uniswap\sdk-core\dist\sdk-core.cjs.development.js:317:11)
    at Percent2.lessThan (c:\Users\DixSon\Desktop\Frog\periscope\node_modules\.pnpm\@uniswap+sdk-core@4.0.6\node_modules\@uniswap\sdk-core\dist\sdk-core.cjs.development.js:348:32)
    at Trade2.minimumAmountOut (c:\Users\DixSon\Desktop\Frog\periscope\node_modules\.pnpm\@uniswap+v3-sdk@3.10.0_hardhat@2.17.0\node_modules\@uniswap\v3-sdk\dist\v3-sdk.cjs.development.js:3028:25)
    at <anonymous> (c:\Users\DixSon\Desktop\Frog\periscope\node_modules\.pnpm\@uniswap+v3-sdk@3.10.0_hardhat@2.17.0\node_modules\@uniswap\v3-sdk\dist\v3-sdk.cjs.development.js:3978:28)
    at Array.reduce (<anonymous>)
    at Function.swapCallParameters (c:\Users\DixSon\Desktop\Frog\periscope\node_modules\.pnpm\@uniswap+v3-sdk@3.10.0_hardhat@2.17.0\node_modules\@uniswap\v3-sdk\dist\v3-sdk.cjs.development.js:3977:33)
    at executeTrade (c:\Users\DixSon\Desktop\Frog\periscope\src\features\transaction\services\trade.ts:110:39)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at run (c:\Users\DixSon\Desktop\Frog\periscope\src\features\transaction\run.ts:35:19)

enter image description here


Solution

  • I fixed it by downgrading JSBI to version 3.2.5

    "jsbi" :"3.2.5"