使用Uniswap SDK会给我这个错误:未为ENS名称配置resolver或addr

问题描述

我正在尝试使用UniSwap SDK和javascript将ETH交换为DAI令牌,但是在运行脚本时遇到以下错误

(node:10096) UnhandledPromiseRejectionWarning: Error: resolver or addr is not configured for ENS name (argument="name",value="",code=INVALID_ARGUMENT,version=contracts/5.0.5)

我已将错误缩小到uniswap.swapExactETHForTokens函数,但我仍然不知道如何解决

完整代码:(出于明显原因,私钥隐藏在代码中)

const { ChainId,Fetcher,WETH,Route,Trade,TokenAmount,TradeType,Percent } = require('@uniswap/sdk');
const ethers = require('ethers');

const chainId = ChainId.MAINNET;
const tokenAddress = '0x6B175474E89094C44Da98b954EedeAC495271d0F';

const init = async () => {
  const dai = await Fetcher.fetchTokenData(chainId,tokenAddress);
  const weth = WETH[chainId];
  const pair = await Fetcher.fetchPairData(dai,weth);
  const route = new Route([pair],weth);
  const Trade = new Trade(route,new TokenAmount(weth,'1000000000000'),TradeType.EXACT_INPUT);
  
  const slippagetolerance = new Percent('50','10000');
  const amountOutMin = Trade.minimumAmountOut(slippagetolerance).raw;

  const path = [weth.address,dai.address];
  const to = '';
  const deadline = Math.floor(Date.Now() / 1000) + 60 * 20;
  const value = Trade.inputAmount.raw;

  const provider = ethers.getDefaultProvider('mainnet',{
    infura: 'https://mainnet.infura.io/v3/ba14d1b3cfe5405088ee3c65ebd1d4' 
  });

  const signer = new ethers.Wallet(PRIVATE_KEY);
  const account = signer.connect(provider);

  const uniswap = new ethers.Contract(
    '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',['function swapExactETHForTokens(uint amountOutMin,address[] calldata path,address to,uint deadline) external payable returns (uint[] memory amounts)'],account
  );
 
  const tx = await uniswap.swapExactETHForTokens(
    amountOutMin,path,to,deadline,{ value,gasPrice: 20e9 }
  );
  console.log(`Transaction hash: ${tx.hash}`);

  const receipt = await tx.wait();
  console.log(`Transaction was mined in block ${receipt.blockNumber}`);
}

init();

解决方法

我猜你可以替换

const to = ''

作者:

const to = process.env.ACCOUNT 

提供您的账户/钱包地址,targetTokens 将被发送到该地址。

,

对于那些面临 INVALID ARGUMENT 错误的人,以下是对我(使用 React)有用的方法:

  1. 导入 router02:
import UniswapV2Router02 from '@uniswap/v2-periphery/build/UniswapV2Router02.json';
  1. 十六进制函数:
const toHex = (currencyAmount) => `0x${currencyAmount.raw.toString(16)}`;
const amountOutMin = toHex(trade.minimumAmountOut(slippageTolerance));
const value = toHex(trade.inputAmount);
  1. 连接到区块链
const provider = ethers.getDefaultProvider('mainnet',{
        infura: 'JUST_INFURA_NUMBER eg. xxxxxxxxxx'
      }); 
  1. 获取合约和方法:
      const abi = UniswapV2Router02['abi'];
      const uniswapRouter = new ethers.Contract(
         '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',abi,account); //if this doesnt work,try using provider/signer instead of account
      console.log("uniswap contract: ",uniswapRouter);
      const tx = await uniswapRouter.swapExactETHForTokens(
        amountOutMin,path,to,deadline,{value,gasPrice: 20e9,gasLimit: 250000}
      );

教程代码:https://www.youtube.com/watch?v=0Im5iaYoz1Y