Using Uniswap SDK gives me this error: resolver or addr is not configured for ENS name
Using Uniswap SDK gives me this error: resolver or addr is not configured for ENS name
我正在尝试使用 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
提供您的帐户/钱包地址,目标代币将被发送到该地址。
对于那些面临 INVALID ARGUMENT 错误的人,这是对我有用的方法(使用 React):
- 导入 router02 :
import UniswapV2Router02 from '@uniswap/v2-periphery/build/UniswapV2Router02.json';
- 十六进制函数:
const toHex = (currencyAmount) => `0x${currencyAmount.raw.toString(16)}`;
const amountOutMin = toHex(trade.minimumAmountOut(slippageTolerance));
const value = toHex(trade.inputAmount);
- 连接到区块链
const provider = ethers.getDefaultProvider('mainnet', {
infura: 'JUST_INFURA_NUMBER eg. xxxxxxxxxx'
});
- 获取合同和方法:
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}
);
就我而言,我直接从 uniswap 文档中复制粘贴了路由器地址。所以我的变量看起来像这样:
const routerAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ";
由于字符串末尾有一个 space,ethers.js 库将其混淆为 ENS 名称而不是地址。所以我将其更正为:
const routerAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";
猜想这是一个愚蠢的错误,但请注意它,以防万一无效!
我正在尝试使用 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
提供您的帐户/钱包地址,目标代币将被发送到该地址。
对于那些面临 INVALID ARGUMENT 错误的人,这是对我有用的方法(使用 React):
- 导入 router02 :
import UniswapV2Router02 from '@uniswap/v2-periphery/build/UniswapV2Router02.json';
- 十六进制函数:
const toHex = (currencyAmount) => `0x${currencyAmount.raw.toString(16)}`;
const amountOutMin = toHex(trade.minimumAmountOut(slippageTolerance));
const value = toHex(trade.inputAmount);
- 连接到区块链
const provider = ethers.getDefaultProvider('mainnet', {
infura: 'JUST_INFURA_NUMBER eg. xxxxxxxxxx'
});
- 获取合同和方法:
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}
);
就我而言,我直接从 uniswap 文档中复制粘贴了路由器地址。所以我的变量看起来像这样:
const routerAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ";
由于字符串末尾有一个 space,ethers.js 库将其混淆为 ENS 名称而不是地址。所以我将其更正为:
const routerAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";
猜想这是一个愚蠢的错误,但请注意它,以防万一无效!