Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Transaction confirmation handling in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Transaction confirmation handling
Send Transaction
Wait for Mining
Check Confirmation Count
Confirm
This flow shows sending a transaction, waiting for it to be mined, then checking if it has enough confirmations to be considered final.
Execution Sample
Blockchain / Solidity
tx_hash = send_transaction(tx)
confirmations = 0
while confirmations < 3:
    confirmations = get_confirmations(tx_hash)
    sleep(10)
print(f"Transaction confirmed with {confirmations} blocks")
This code sends a transaction and waits until it has at least 3 confirmations before proceeding.
Execution Table
StepActionConfirmationsCondition (confirmations < 3)Next Step
1Send transaction, get tx_hash0TrueWait and check confirmations
2Check confirmations1TrueWait more blocks
3Check confirmations2TrueWait more blocks
4Check confirmations3FalsePrint confirmation message
5Print confirmation message3N/AEnd
💡 Confirmations reached 3, condition is false, loop ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
tx_hashNone0xabc1230xabc1230xabc1230xabc1230xabc123
confirmations001233
Key Moments - 3 Insights
Why do we check confirmations in a loop instead of once?
Because the transaction needs multiple blocks to be mined on top to be secure. The loop (see execution_table rows 2-4) waits until confirmations reach the required number.
What happens if confirmations never increase?
The loop keeps waiting (rows 2-4). In real code, you would add a timeout or error handling to avoid waiting forever.
Why do we print confirmation after the loop ends?
Because the loop ends only when confirmations >= 3 (row 4), so printing then means the transaction is safely confirmed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of confirmations at Step 3?
A0
B2
C1
D3
💡 Hint
Check the 'Confirmations' column at Step 3 in the execution_table.
At which step does the condition 'confirmations < 3' become false?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Condition' column in the execution_table to find when it changes to False.
If we change the required confirmations to 2, at which step would the loop end?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Refer to the 'Confirmations' and 'Condition' columns in the execution_table to see when condition becomes false for 2 confirmations.
Concept Snapshot
Transaction confirmation handling:
- Send transaction and get tx_hash
- Loop: check confirmations count
- Wait until confirmations >= required number
- Confirmations mean blocks mined on top
- Print or proceed after enough confirmations
Full Transcript
This visual execution shows how a blockchain transaction is confirmed. First, the transaction is sent and a transaction hash is received. Then, the program checks how many confirmations the transaction has by counting blocks mined on top. It loops, waiting and checking repeatedly until the confirmations reach the required number (3 in this example). Once confirmed, it prints a message. Variables like tx_hash and confirmations update step-by-step. Key points include why we wait for multiple confirmations for security, and that the loop ends only when confirmations are enough. The quiz questions help check understanding of confirmation counts and loop behavior.

Practice

(1/5)
1. What does transaction confirmation mean in blockchain?
easy
A. It means the transaction is deleted from the blockchain.
B. It means the transaction is reversed by the user.
C. It means the transaction is pending and not yet sent.
D. It means the transaction is safely recorded on the blockchain.

Solution

  1. Step 1: Understand transaction confirmation meaning

    Transaction confirmation means the blockchain network has recorded the transaction securely.
  2. 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.
  3. Final Answer:

    It means the transaction is safely recorded on the blockchain. -> Option D
  4. Quick 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
A. await transaction.confirm();
B. transaction.wait();
C. await transaction.wait();
D. transaction.confirm();

Solution

  1. Step 1: Identify correct async syntax

    To wait for a promise in JavaScript, use await before the async function call.
  2. Step 2: Match function name for confirmation

    The standard method to wait for transaction confirmation is wait(), not confirm().
  3. Final Answer:

    await transaction.wait(); -> Option C
  4. Quick 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
A. 3
B. Promise {<pending>}
C. undefined
D. Error: wait is not a function

Solution

  1. Step 1: Understand async function behavior

    The function confirmTx awaits tx.wait() which resolves to an object with confirmations: 3.
  2. Step 2: Return and log confirmations

    The function returns receipt.confirmations which is 3, and then(console.log) prints 3.
  3. Final Answer:

    3 -> Option A
  4. Quick 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
A. Missing await before tx.wait()
B. Incorrect property name confirmations
C. Function should not be async
D. console.log should be outside the function

Solution

  1. Step 1: Check async call usage

    The function calls tx.wait() which returns a Promise, but does not use await.
  2. Step 2: Understand consequences of missing await

    Without await, receipt is a Promise object, so receipt.confirmations is undefined.
  3. Final Answer:

    Missing await before tx.wait() -> Option A
  4. Quick 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
A. const receipt = await tx.wait(5); if(receipt.confirmations < 5) { proceed(); }
B. const receipt = await tx.wait(5); if(receipt.confirmations >= 5) { proceed(); }
C. const receipt = await tx.wait(3); if(receipt.confirmations >= 5) { proceed(); }
D. const receipt = await tx.wait(); if(receipt.confirmations == 5) { proceed(); }

Solution

  1. Step 1: Use wait() with confirmation count

    Calling tx.wait(5) waits until at least 5 confirmations are reached.
  2. Step 2: Check confirmations before proceeding

    Check if receipt.confirmations >= 5 to ensure safe confirmation before calling proceed().
  3. Final Answer:

    const receipt = await tx.wait(5); if(receipt.confirmations >= 5) { proceed(); } -> Option B
  4. Quick 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