2

How do I know when transactions are either successfully mined or still sitting in the mempool? I don't want to just overbid on gas to get my transaction to get mined faster / more instantly from the mempool and added to a block.

It's always uncertain to me when my transaction goes through successfully and I just have to keep checking back. Is there a way to do this, maybe via webhooks or something?

2 Answers 2

2

Unfortunately notifications don't exist in blockchain.

However, you can use either Alchemy's Notify API or custom webhooks -- basically prebuilt or customized webhooks that enable you to choose events you want to receive notifications about as soon as they happen. Both are free to use along with your free Alchemy account.

For your use case, I'd use the Notify API b/c it's already set up for your use case -- it has built-in notifications for mined transactions, dropped transactions, and other smart contract events like NFT transfers and ERC20 token transfers.

More information about the Notify API here.

2

It depends on the tool/library you are using.

If you are using web3js, you can leverage the "receipt" or "confirmation" event emitter.

"receipt": Fired when the transaction receipt is available.

"confirmation": Fired for every confirmation up to the 12th confirmation. Receives the confirmation number as the first and the receipt as the second argument. Fired from confirmation 0 on, which is the block where it’s mined.

Below is a sample code with sendTransaction method:

// using the event emitter
web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
    value: '1000000000000000'
})
.on('transactionHash', function(hash){
    ...
})
.on('receipt', function(receipt){
    ...
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.on('error', console.error); // If a out of gas error, the second parameter is the receipt.

For Ethers.js transaction.wait is part of the transaction response, it is resolved when the transaction is confirmed.

Resolves to the TransactionReceipt once the transaction has been included in the chain for confirms blocks. If confirms is 0, and the transaction has not been mined, null is returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.