Transaction confirmation handling helps you know when a blockchain transaction is fully accepted and safe to trust.
0
0
Transaction confirmation handling in Blockchain / Solidity
Introduction
You want to show a user that their payment went through.
You need to wait before giving access to a service after a transaction.
You want to avoid double spending by confirming the transaction is final.
You want to update your app only after the transaction is securely recorded.
You want to notify users about the status of their blockchain transaction.
Syntax
Blockchain / Solidity
waitForTransaction(txHash, confirmations)
.then(receipt => {
// transaction is confirmed
})
.catch(error => {
// handle error
})txHash is the unique ID of the transaction.
confirmations is how many blocks after the transaction you wait for.
Examples
Waits for 1 confirmation before continuing.
Blockchain / Solidity
waitForTransaction('0xabc123...', 1) .then(receipt => console.log('1 confirmation reached'))
Waits for 6 confirmations, which is safer for big transactions.
Blockchain / Solidity
waitForTransaction('0xabc123...', 6) .then(receipt => console.log('6 confirmations reached'))
Sample Program
This program waits for 3 confirmations of a transaction and then prints a confirmation message.
Blockchain / Solidity
async function confirmTransaction(txHash, confirmations) { try { const receipt = await waitForTransaction(txHash, confirmations); console.log(`Transaction ${txHash} confirmed with ${confirmations} confirmations.`); } catch (error) { console.error('Error confirming transaction:', error); } } // Example usage confirmTransaction('0xabc123def456', 3);
OutputSuccess
Important Notes
More confirmations mean more security but longer wait time.
Always handle errors in case the transaction fails or is dropped.
Use transaction hash to track the specific transaction you want to confirm.
Summary
Transaction confirmation handling tells you when a blockchain transaction is safely recorded.
Waiting for more confirmations increases trust but takes more time.
Use async functions and error handling to manage confirmations smoothly.