Transaction confirmation handling helps you know when a blockchain transaction is fully accepted and safe to trust.
Transaction confirmation handling in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
Blockchain / Solidity
waitForTransaction('0xabc123...', 1) .then(receipt => console.log('1 confirmation reached'))
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);
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.
Practice
1. What does
transaction confirmation mean in blockchain?easy
Solution
Step 1: Understand transaction confirmation meaning
Transaction confirmation means the blockchain network has recorded the transaction securely.Step 2: Compare options with definition
Only It means the transaction is safely recorded on the blockchain. correctly states that confirmation means safe recording on the blockchain.Final Answer:
It means the transaction is safely recorded on the blockchain. -> Option DQuick Check:
Transaction confirmation = safe recording [OK]
Hint: Confirmation means transaction is securely recorded [OK]
Common Mistakes:
- Confusing confirmation with transaction pending state
- Thinking confirmation means deletion or reversal
- Assuming confirmation means user approval
2. Which of the following is the correct way to wait for a transaction confirmation in JavaScript using async/await?
easy
Solution
Step 1: Identify correct async syntax
To wait for a promise in JavaScript, useawaitbefore the async function call.Step 2: Match function name for confirmation
The standard method to wait for transaction confirmation iswait(), notconfirm().Final Answer:
await transaction.wait(); -> Option CQuick Check:
Use await with wait() to confirm transaction [OK]
Hint: Use await with wait() method to confirm transaction [OK]
Common Mistakes:
- Forgetting to use await causing unresolved promises
- Using wrong method name like confirm()
- Calling wait() without await leading to no pause
3. What will be the output of this JavaScript code snippet?
async function confirmTx(tx) {
const receipt = await tx.wait();
return receipt.confirmations;
}
const fakeTx = {
wait: () => Promise.resolve({ confirmations: 3 })
};
confirmTx(fakeTx).then(console.log);medium
Solution
Step 1: Understand async function behavior
The functionconfirmTxawaitstx.wait()which resolves to an object withconfirmations: 3.Step 2: Return and log confirmations
The function returnsreceipt.confirmationswhich is 3, andthen(console.log)prints 3.Final Answer:
3 -> Option AQuick Check:
Await wait() returns confirmations = 3 [OK]
Hint: Await returns resolved value, then log confirmations [OK]
Common Mistakes:
- Expecting Promise instead of resolved value
- Confusing property name confirmations
- Missing await causing Promise output
4. Identify the error in this code snippet for waiting transaction confirmation:
async function waitForConfirmation(tx) {
const receipt = tx.wait();
console.log(receipt.confirmations);
}
waitForConfirmation(transaction);medium
Solution
Step 1: Check async call usage
The function callstx.wait()which returns a Promise, but does not useawait.Step 2: Understand consequences of missing await
Withoutawait,receiptis a Promise object, soreceipt.confirmationsis undefined.Final Answer:
Missing await before tx.wait() -> Option AQuick Check:
Always await async calls to get resolved value [OK]
Hint: Always await async calls to get actual result [OK]
Common Mistakes:
- Not using await with async functions
- Assuming Promise has properties directly
- Misplacing console.log inside async function
5. You want to wait for at least 5 confirmations before proceeding with a transaction. Which code snippet correctly implements this logic?
hard
Solution
Step 1: Use wait() with confirmation count
Callingtx.wait(5)waits until at least 5 confirmations are reached.Step 2: Check confirmations before proceeding
Check ifreceipt.confirmations >= 5to ensure safe confirmation before callingproceed().Final Answer:
const receipt = await tx.wait(5); if(receipt.confirmations >= 5) { proceed(); } -> Option BQuick Check:
wait(5) ensures 5 confirmations before proceed [OK]
Hint: Use wait(5) and check confirmations >= 5 before proceed [OK]
Common Mistakes:
- Waiting fewer confirmations than needed
- Checking for exact 5 instead of >= 5
- Proceeding when confirmations are less than required
