【问题标题】:Transaction to Uniswap failing on Ropsten TestNetRopsten TestNet 上与 Uniswap 的交易失败
【发布时间】:2021-05-30 10:32:59
【问题描述】:

我正在尝试以编程方式在 Uniswap 上创建交易,流程 nd 代码似乎在那里,但无论出于何种原因,"Warning! Error encountered during contract execution [Reverted]" 的交易在 Ropsten 上失败。我使用 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);
}

【问题讨论】:

  • WETH 代币合约中的 Uniswap 路由器地址是否已批准?
  • @BrvarG,我有,审批交易成功。
  • 请查看应该显示交易哈希的错误。在 etherscan 上查找并在此处链接。这将非常有帮助。
  • 你能分享整个代码吗?我是新手,需要实现相同的

标签: javascript solidity web3 erc20 ether


【解决方案1】:

行:

const tx = await exchangeContract.swapExactTokensForTokens(

应该是:

const tx = await exchangeContract.methods.swapExactTokensForTokens(

【讨论】:

【解决方案2】:

错误是正确的。您的交易不应该成功。 转到 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-04-15
    • 2022-11-04
    • 2021-05-11
    • 2018-07-28
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多