Uniswap V3 SwapRouter.swapCallParameters 无法解析分数

问题描述 投票:0回答:1

我正在尝试按照本指南代码

进行基本交易

我编辑了代码中的一些函数来接收诸如以下内容的参数:

  • 代币进入
  • 代币输出
  • 金额

以下是我的

createTrade
功能:

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;
}

这是我的

executeTrade
功能:

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;
}

最后,我是这样称呼它的:

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();

但是,当执行此行时,我遇到以下问题

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

我做错了什么?

Error: Could not parse fraction
    at Function.tryParseFraction (c:\Users\DixSon\Desktop\Frog\periscope\node_modules\.pnpm\@[email protected]\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\@[email protected]\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\@[email protected][email protected]\node_modules\@uniswap\v3-sdk\dist\v3-sdk.cjs.development.js:3028:25)
    at <anonymous> (c:\Users\DixSon\Desktop\Frog\periscope\node_modules\.pnpm\@[email protected][email protected]\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\@[email protected][email protected]\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)

typescript ethereum web3js erc20 uniswap
1个回答
0
投票

我通过将 JSBI 降级到版本 3.2.5 来修复它

"jsbi" :"3.2.5"
© www.soinside.com 2019 - 2024. All rights reserved.