0
0
Blockchain / Solidityprogramming~5 mins

Checks-Effects-Interactions pattern in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

This pattern helps keep blockchain programs safe by avoiding problems when multiple actions happen together. It stops bad things like losing money or breaking the program.

When writing smart contracts that send money to others.
When updating balances or states before calling other contracts.
When you want to avoid attacks that trick your contract into doing wrong things.
When handling user requests that change data and then interact with other contracts.
When you want your contract to be reliable and secure.
Syntax
Blockchain / Solidity
function example() public {
    // 1. Checks: Verify conditions
    require(condition, "Check failed");

    // 2. Effects: Update contract state
    stateVariable = newValue;

    // 3. Interactions: Call external contracts or send money
    externalContract.call{value: amount}();
}

Always do checks first to make sure everything is okay.

Update your own data before talking to others.

Examples
This example shows a simple withdraw function that uses the pattern to avoid problems.
Blockchain / Solidity
function withdraw(uint amount) public {
    // Checks
    require(balances[msg.sender] >= amount, "Not enough balance");

    // Effects
    balances[msg.sender] -= amount;

    // Interactions
    payable(msg.sender).transfer(amount);
}
This example shows buying an item safely by checking, updating, then paying.
Blockchain / Solidity
function buyItem(uint itemId) public payable {
    // Checks
    require(items[itemId].available, "Item not available");
    require(msg.value >= items[itemId].price, "Not enough payment");

    // Effects
    items[itemId].available = false;
    ownerBalances[owner] += msg.value;

    // Interactions
    seller.call{value: msg.value}();
}
Sample Program

This smart contract lets users deposit and withdraw money safely. The withdraw function uses the Checks-Effects-Interactions pattern to avoid problems like reentrancy attacks.

Blockchain / Solidity
pragma solidity ^0.8.0;

contract SimpleBank {
    mapping(address => uint) public balances;

    // Deposit money to your balance
    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }

    // Withdraw money safely using Checks-Effects-Interactions
    function withdraw(uint amount) public {
        // Checks
        require(balances[msg.sender] >= amount, "Insufficient balance");

        // Effects
        balances[msg.sender] -= amount;

        // Interactions
        payable(msg.sender).transfer(amount);
    }
}
OutputSuccess
Important Notes

Never call external contracts before updating your own data.

Use require() to stop bad actions early.

This pattern helps prevent common security bugs in blockchain programs.

Summary

Checks-Effects-Interactions means: check conditions, update your data, then interact with others.

It keeps your smart contracts safe and reliable.

Always follow this order when writing functions that send money or call other contracts.