问题描述
我正在尝试使用 js 脚本交换令牌,该脚本可用于将以太坊交换为任何令牌。问题是我尝试和交换的一些令牌会提供错误“UnhandledPromiseRejectionWarning: InsufficientInputAmountError”。但是,如果我尝试换一个不同的令牌,它会正常工作。我知道引发错误的令牌与 uniswap 兼容,因为我通过他们的网站购买了一些并且没有收到任何错误。
const {ChainId,Fetcher,WETH,Route,Trade,TokenAmount,TradeType,Percent,Token} = require('@uniswap/sdk');
const {ethers} = require("ethers");
let Web3 = require('web3');
let web3 = new Web3(new Web3.providers.HttpProvider("INFURA_KEY"));
function toHex(Amount) {
return `0x${Amount.raw.toString(16)}`;
}
const chainId = ChainId.MAINNET;
const tokenAddress = '0x094F00Cb5e31Ab6164E3CAcb654e8D6c2b3b471C';
const provider = new ethers.providers.EtherscanProvider('homestead','ETHERSCAN_KEYY');
const init = async () => {
const gas = await web3.eth.getGasPrice();
const token = await Fetcher.fetchTokenData(chainId,tokenAddress,provider);
const weth = WETH[token.chainId];
const pair = await Fetcher.fetchPairData(token,weth,provider);
const amountIn = '10000000000000000';
const route = new Route([pair],weth);
const Trade = new Trade(route,new TokenAmount(weth,amountIn),TradeType.EXACT_INPUT);
const slippagetolerance = new Percent('1','100');
const amountOutMin = toHex(Trade.minimumAmountOut(slippagetolerance));
const path = [weth.address,token.address];
const to = 'MY_KEY';
const deadline = Math.floor(Date.Now()/1000) + 60*20;
const value = toHex(Trade.inputAmount);
const signer = new ethers.Wallet('MY_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: gas}
);
console.log(tx);
}
init();
功能代币地址:0x6b175474e89094c44da98b954eedeac495271d0f 非功能地址:0x094F00Cb5e31Ab6164E3CAcb654e8D6c2b3b471C
问题似乎出在定义 const 交易时,因为脚本不会超出此范围。我已经浏览过,但不知道是什么原因导致大多数令牌似乎有效,但有一些无效(即使它们在 uniswap 网站上有效)。我对 JS 还很陌生,正在使用 ethers/uniswap,因此不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)