0
0
Blockchain / Solidityprogramming~10 mins

Withdrawal patterns in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Withdrawal patterns
User requests withdrawal
Check user balance
Is balance sufficient?
NoReject withdrawal
Yes
Lock funds
Process withdrawal transaction
Confirm transaction success?
NoUnlock funds, notify failure
Yes
Update user balance
Notify user of success
END
This flow shows how a withdrawal request is handled step-by-step, checking balance, locking funds, processing, and confirming success.
Execution Sample
Blockchain / Solidity
def withdraw(user, amount):
    if user.balance < amount:
        return "Insufficient funds"
    user.lock_funds(amount)
    success = blockchain.send_transaction(user.address, amount)
    if not success:
        user.unlock_funds(amount)
        return "Transaction failed"
    user.balance -= amount
    return "Withdrawal successful"
This code handles a withdrawal by checking balance, locking funds, sending transaction, and updating balance.
Execution Table
StepActionCondition/EvaluationResult/State ChangeOutput
1Check if user.balance < amountuser.balance=100, amount=50Condition False (100 >= 50)Continue
2Lock fundsLock 50 unitsFunds locked: 50No output
3Send transactionSimulate blockchain.send_transactionTransaction success = TrueNo output
4Check transaction successsuccess == TrueProceed to update balanceNo output
5Update user.balanceuser.balance = 100 - 50user.balance = 50No output
6Return success messageN/AN/AWithdrawal successful
7EndN/AN/AProcess complete
💡 Withdrawal completes successfully after balance check, locking funds, transaction success, and balance update.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5Final
user.balance1001001001005050
amount505050505050
funds_locked0050505050
transaction_successN/AN/AN/ATrueTrueTrue
Key Moments - 3 Insights
Why do we lock funds before sending the transaction?
Locking funds prevents the user from spending the same amount twice while the transaction is pending, as shown in step 2 of the execution_table.
What happens if the transaction fails after locking funds?
If the transaction fails, the locked funds are unlocked to restore the user's balance, as explained in the code and implied in the flow after the transaction check.
Why do we update the user balance only after confirming transaction success?
Updating balance only after success ensures the user’s balance reflects actual funds spent, preventing errors or double spending, as seen in step 5 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the user.balance after step 5?
A0
B50
C100
D150
💡 Hint
Check the 'Result/State Change' column at step 5 in the execution_table.
At which step is the transaction success evaluated?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where 'Check transaction success' is performed in the execution_table.
If the user.balance was 40 and amount 50, what would happen at step 1?
AFunds locked anyway
BCondition False, withdrawal proceeds
CCondition True, withdrawal rejected
DTransaction sent immediately
💡 Hint
Refer to the condition check in step 1 of the execution_table and variable_tracker for balance values.
Concept Snapshot
Withdrawal Patterns in Blockchain:
- Check user balance before withdrawal
- Lock funds to prevent double spending
- Send transaction to blockchain
- Confirm transaction success
- Update user balance only after success
- Unlock funds if transaction fails
Full Transcript
This visual execution trace shows how a withdrawal request is processed in blockchain systems. First, the user's balance is checked to ensure sufficient funds. If enough, the requested amount is locked to prevent spending it elsewhere. Then, a transaction is sent to the blockchain network. If the transaction succeeds, the user's balance is updated by subtracting the withdrawn amount. If the transaction fails, the locked funds are released back to the user. This process ensures security and consistency in handling withdrawals.