【问题标题】:Using Uniswap SDK gives me this error: resolver or addr is not configured for ENS name使用 Uniswap SDK 时出现此错误:未为 ENS 名称配置解析器或地址
【发布时间】:2021-02-14 00:33:06
【问题描述】:

我正在尝试使用 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();

【问题讨论】:

标签: javascript ethereum smartcontracts


【解决方案1】:

就我而言,我直接从 uniswap 文档中复制粘贴了路由器地址。所以我的变量看起来像这样:

const routerAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ";

由于字符串末尾有一个空格,ethers.js 库将其混淆为 ENS 名称而不是地址。所以我把它更正了:

const routerAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";

猜这是一个愚蠢的错误,但要小心它,以防万一没有任何效果!

【讨论】:

    【解决方案2】:

    对于那些面临 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

    【讨论】:

      【解决方案3】:

      我猜你可以替换

      const to = ''
      

      作者:

      const to = process.env.ACCOUNT 
      

      提供您的目标代币将被发送到的帐户/钱包地址。

      【讨论】:

        猜你喜欢
        • 2022-06-28
        • 1970-01-01
        • 2022-08-20
        • 1970-01-01
        • 2012-12-09
        • 1970-01-01
        • 2019-11-13
        • 2010-09-10
        • 1970-01-01
        相关资源
        最近更新 更多