0
0
Blockchain / Solidityprogramming~10 mins

Checks-Effects-Interactions pattern in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Checks-Effects-Interactions pattern
Start Function Call
Perform Checks
Update State (Effects)
Interact with External Contracts
End Function
This pattern ensures safety by first checking conditions, then updating contract state, and finally interacting with other contracts.
Execution Sample
Blockchain / Solidity
function withdraw(uint amount) public {
  require(balances[msg.sender] >= amount);
  balances[msg.sender] -= amount;
  (bool success, ) = msg.sender.call{value: amount}("");
  require(success);
}
Withdraw function that first checks balance, then updates it, and finally sends Ether to the user.
Execution Table
StepActionEvaluation/StateResult/Effect
1Check if user balance >= amountbalances[msg.sender] = 100, amount = 50True, continue
2Subtract amount from user balancebalances[msg.sender] = 100 - 50balances[msg.sender] = 50
3Send Ether to user (external call)Call msg.sender with 50 EtherEther sent successfully
4Require success of external callsuccess = trueFunction completes successfully
💡 Execution stops after successful external call and state update
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
balances[msg.sender]100100505050
amount5050505050
successundefinedundefinedundefinedtruetrue
Key Moments - 3 Insights
Why do we update the balance before sending Ether?
Updating the balance first (Step 2) prevents reentrancy attacks by ensuring the contract state is correct before external calls (Step 3). See execution_table rows 2 and 3.
What happens if the check fails?
If the check in Step 1 fails, the function stops immediately and no state changes or external calls happen, preventing unsafe operations.
Why do we require success after the external call?
Requiring success (Step 4) ensures the external call did not fail silently, so the function only completes if Ether was sent properly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the balance after Step 2?
A100
B0
C50
DUndefined
💡 Hint
Check the 'balances[msg.sender]' value in variable_tracker after Step 2
At which step does the contract interact with an external address?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for external calls
If the check in Step 1 fails, what happens next?
AState updates and external call still happen
BFunction stops immediately, no changes
COnly external call happens
DBalance is set to zero
💡 Hint
Refer to key_moments about what happens if the check fails
Concept Snapshot
Checks-Effects-Interactions pattern:
1. Checks: Verify conditions first (e.g., require statements).
2. Effects: Update contract state variables next.
3. Interactions: Finally, call external contracts.
This order prevents reentrancy and ensures safe state changes.
Full Transcript
The Checks-Effects-Interactions pattern is a safety method in blockchain programming. First, the contract checks if conditions are met, like if the user has enough balance. Then, it updates its own state, such as subtracting the withdrawn amount. Lastly, it interacts with external contracts, for example sending Ether. This order helps avoid security problems like reentrancy attacks. In the example, the function withdraw checks the balance, updates it, then sends Ether. If any check fails, the function stops immediately. This pattern is important for writing safe smart contracts.