0

我正在尝试以编程方式在 Uniswap 上创建交易,流程 nd 代码似乎在那里,但无论出于何种原因,交易在 Ropsten for "Warning! Error encountered during contract execution [Reverted]". 我使用 javascript 和 Nodejs 作为我的服务器。关于为什么失败的任何建议?下面的代码:

const { ethers } = require("ethers");

const walletAddress = "My_own_address";
const wethErc20Address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
const uniErc20Address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";

const uniswapRouterAbi = [
  "function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)",
];

async function buyListedTokenWithEth(
  amountEthFloat,
  uniswapRouterAddress,
  provider
) {
  const wallet = new ethers.Wallet(Buffer.from(privateKey, "hex"));

  const signer = wallet.connect(provider); //provider is Infura https ROPSTEN url

  const exchangeContract = new ethers.Contract(
    uniswapRouterAddress,
    uniswapRouterAbi,
    signer
  );
  const ethAmount = ethers.utils.parseEther("0.1");

  const tx = await exchangeContract.swapExactTokensForTokens(
    ethAmount,
    0,
    [wethErc20Address, uniErc20Address],
    walletAddress, 
    createDeadline(), // Math.floor(Date.now() / 1000) + 20
    createGasOverrides() // { gasLimit: ethers.utils.hexlify(300000), gasPrice: gasPriceWei }
  );
  console.log("https://ropsten.etherscan.io/tx/" + tx.hash);
}
4

2 回答 2

0

该行:

const tx = await exchangeContract.swapExactTokensForTokens(

应该:

const tx = await exchangeContract.methods.swapExactTokensForTokens(

于 2021-04-27T11:17:47.917 回答
-1

错误是正确的。您的交易不应该成功。转到 uniswap ropsten 路由器地址。https://ropsten.etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d#readContract

有一个功能可以查看 ropsten 的 WETH 地址。这就是它返回的内容

0xc778417e063141139fce010982780140aa0cd5ab //Uniswap-router-factory-weth address

代替

const wethErc20Address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";

您要求 uniswap 路由器使用您列出的 WETH 地址,而不是它使用的实际 WETH 地址。这样想吧。为什么在 uniswap 上流动性为 0 的地址会在 Uniswap 上运行?

最好在假设它存在之前编写一些函数来检查流动性。不,我不会告诉你怎么做。

于 2021-03-21T08:09:31.870 回答