【问题标题】:How to cancel a payment that awaits confirmation in Metamask using web3?如何使用 web3 在 Metamask 中取消等待确认的付款?
【发布时间】:2022-06-14 12:17:02
【问题描述】:

我正在使用 ethers Web3Provider 开发一个加密支付系统

web3.eth.sendTransaction(transactionObject)

用户调用此方法后,将显示此对话框:

在应用程序中,用户可以关闭付款对话框,使其处于待处理状态。关闭应用程序内的对话框时,有没有办法使用 JS 拒绝此付款?清除所有付款也是一种解决方案,但在文档中我找不到有关此案例的任何信息。

【问题讨论】:

  • 嘿!你找到解决这个问题的方法了吗?我现在刚好遇到一模一样的情况,还需要从js中取消挂起的事务,关闭Metamask弹窗。
  • 我也很好奇
  • 不,因为没有。一位元掩码开发人员在另一个网站上评论说,这是 web3 工作方式的不可能原因。

标签: javascript reactjs metamask ethers.js


【解决方案1】:

这篇文章可能会有所帮助:https://chainstack.com/a-developers-guide-to-the-transactions-in-mempool-metamask-edition/

您基本上可以发送高额费用和与待处理交易相同的帐户通知的无 eth 交易。这将有助于删除待处理的事务。这就是元掩码“取消”交易的方式

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

要取消某些以太坊待处理交易,您需要:

  1. 创建一个新交易,您将在其中向自己发送 0 ETH。
  2. 您应该提高 gas 费用的价值
  3. 您需要使用与待处理事务相同的随机数。

使用 web3js 你可以做这样的事情:

async function main() {
   require('dotenv').config();
   const { API_URL, PRIVATE_KEY } = process.env;
   const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
   const web3 = createAlchemyWeb3(API_URL);
   const myAddress = //put your address
   const nonce = //put the nonce of pending tx
   const maxFeePerGas = //increase the gas fee value comparated to the pending tx 

   const transaction = {
      'to': myAddress,
      'value': 0,
      'gas': 21000,
      'maxFeePerGas': maxFeePerGas,
      'nonce': nonce
   };

   const signedTx = await web3.eth.accounts.signTransaction(transaction, PRIVATE_KEY);

   web3.eth.sendSignedTransaction(signedTx.rawTransaction, function(error, hash) {
      if (!error) {
         console.log("The hash of your transaction is: ", hash);
      } else {
         console.log(error)
      }
    });
}

main();

【讨论】:

    猜你喜欢
    • 2019-03-01
    • 2023-03-13
    • 2019-08-03
    • 2016-08-22
    • 1970-01-01
    • 2019-07-05
    • 2018-07-21
    • 2014-08-23
    • 1970-01-01
    相关资源
    最近更新 更多