简体   繁体   English

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

[英]How to get a transaction receipt once minting is done

I'm building the front end of my NFT Minting DApp using React.我正在使用 React 构建我的 NFT Minting DApp 的前端。

I'm trying to print in the console the URL to etherscan/hash once the transaction has been minted, but rather I got the log when the transaction has started, so, it isn't already available in etherscan.我试图在创建事务后在控制台中打印到 etherscan/hash 的 URL,但是当事务开始时我得到了日志,所以它在 etherscan 中不可用。

I've checked other sites but no one is conclusive enough.我检查了其他网站,但没有一个足够确凿。

How to get the transaction receipt once the minting process has been done?铸币过程完成后如何获得交易收据?

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}`));
          }
        )

Finally, I did it.最后,我做到了。 I decided to use interval.我决定使用间隔。 Source: 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); 
          }

没有可以订阅的听众吗?

web3.eth.subscribe("alchemy_fullPendingTransactions")

The way I do that in web3js is like this, transactionHash will be logged out pretty quickly then the receipt will arrive.我在 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 https://web3js.readthedocs.io/en/v1.7.0/web3-eth-contract.html#id37

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM