簡體   English   中英

Ropsten TestNet 上與 Uniswap 的交易失敗

[英]Transaction to Uniswap failing on Ropsten TestNet

我正在嘗試以編程方式在 Uniswap 上創建交易,流程 nd 代碼似乎在那里,但無論出於何種原因,Ropsten 上的交易都因"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);
}

錯誤是正確的。 您的交易不應該成功。 Go 到 uniswap ropsten 路由器地址。 https://ropsten.etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d#readContract

有一個 function 可以查看 ropsten 的 WETH 地址。 這就是它返回的內容

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

代替

const wethErc20Address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";

您要求 uniswap 路由器使用您列出的 WETH 地址,而不是它使用的實際 WETH 地址。 這樣想吧。 為什么在 uniswap 上流動性為 0 的地址會在 Uniswap 上運行?

最好先寫一些 function 來檢查流動性,然后再假設它存在。 不,我不會告訴你怎么做。

該行:

const tx = await exchangeContract.swapExactTokensForTokens(

應該:

const tx = await exchangeContract.methods.swapExactTokensForTokens(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM