【问题标题】:How to get a transaction receipt once minting is done铸币完成后如何获得交易收据
【发布时间】:2022-07-09 05:46:41
【问题描述】:

我正在使用 React 构建我的 NFT Minting DApp 的前端。

我试图在创建事务后在控制台中打印 etherscan/hash 的 URL,但我在事务开始时得到了日志,因此它在 etherscan 中不可用。

我检查了其他网站,但没有一个足够确定。

铸币过程完成后如何获得交易收据?

try {
      ethereum
        .request({
          method: "eth_sendTransaction",
          params: [tx],
        })
        .then(
          
          async (result) => 
          {
          let nftTxn = await nftContract.safeMint;
          console.log("Minting... please wait");
          web3.eth.getTransactionReceipt(result)
          .then(console.log(`Mined, see transaction: https://ropsten.etherscan.io/tx/${result}`));
          }
        )

【问题讨论】:

  • 我的所有代码都在:github

标签: reactjs blockchain ethereum web3js


【解决方案1】:

最后,我做到了。我决定使用间隔。来源:here

if (result!=null){
            const interval = setInterval(()=>{
              console.log("Attempting to get transaction receipt...");
              web3.eth.getTransactionReceipt(result, function(err, rec){
                if (rec) {
                  console.log(`See transaciton in https://ropsten.etherscan.io/tx/${rec.transactionHash}`);
                  clearInterval(interval);
                } else {
                  console.log(err);
                }
              });
            }, 1000); 
          }

【讨论】:

    【解决方案2】:

    没有可以订阅的监听器吗?

    web3.eth.subscribe("alchemy_fullPendingTransactions")
    

    【讨论】:

      【解决方案3】:

      我在 web3js 中这样做的方式是这样的,transactionHash 将很快注销然后收据就会到达。

      myContract.methods
        .myMethod(123)
        .send({ from: "0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe" })
        .on("transactionHash", function (hash) {
          console.log("transactionHash", hash);
        })
        .on("confirmation", function (confirmationNumber, receipt) {
          console.log("confirmationNumber", confirmationNumber);
          console.log("receipt", receipt);
        })
        .on("receipt", function (receipt) {
          // receipt example
          console.log(receipt);
        })
        .on("error", function (error, receipt) {
          // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt.
          console.log("error", error);
          console.log("receipt", receipt);
        });
      

      https://web3js.readthedocs.io/en/v1.7.0/web3-eth-contract.html#id37

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-13
        • 2012-08-03
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        • 2017-03-11
        • 2013-04-26
        • 1970-01-01
        相关资源
        最近更新 更多