简体   繁体   中英

How to get a transaction receipt once minting is done

I'm building the front end of my NFT Minting DApp using React.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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