Solidity programming: The Checks-Effects-Interactions Pattern

Sam Vishwas
3 min readMar 1, 2023

--

When designing a smart contract in Solidity programming, it is important to consider security risks such as re-entrancy attacks. One of the best practices for developing secure smart contracts is to use the checks-effects-interactions pattern. This pattern involves separating the validation of inputs (checks), the changes to the contract state (effects), and the interactions with external contracts or entities (interactions). In this article, we will explore the checks-effects-interactions pattern in more detail and provide an example Solidity contract that implements the pattern.

The Checks-Effects-Interactions Pattern

The checks-effects-interactions pattern is a best practice for developing smart contracts that helps to reduce the risk of security vulnerabilities in the smart contract code. The pattern involves separating the validation of inputs (checks), the changes to the contract state (effects), and the interactions with external contracts or entities (interactions).

Checks

The first step in the checks-effects-interactions pattern is to validate any inputs to the smart contract, such as parameters to a function call. The goal of this step is to ensure that the inputs are valid and that the contract is in a state where it can safely proceed with the requested action. For example, if a contract is transferring funds to an external account, the checks might include verifying that the contract has sufficient funds to complete the transfer.

Effects

The second step in the checks-effects-interactions pattern is to update the state of the smart contract in response to the validated inputs. For example, if the contract is transferring funds to an external account, this step would involve subtracting the amount to be transferred from the contract’s balance.

Interactions

The third and final step in the checks-effects-interactions pattern is to perform any interactions with external contracts or entities, such as initiating a transfer to an external account. By performing this step last, the contract can ensure that it has all the information it needs to safely interact with the external entity. Additionally, this step can be performed in a separate transaction, which can help reduce the risk of re-entrancy attacks and other security vulnerabilities.

An Example Solidity Contract

Here is an example Solidity contract that implements the checks-effects-interactions pattern. This contract allows users to deposit and withdraw Ether from the contract balance:

contract WithdrawalPatternExample {
mapping(address => uint) public balances;
    function deposit() external payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint amount) external {
require(balances[msg.sender] >= amount, "Insufficient balance");

// Check: Ensure sufficient balance before withdrawing
balances[msg.sender] -= amount;
// Effect: Update contract state by subtracting amount from sender's balance
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Withdrawal failed");
// Interaction: Transfer Ether to the sender's account
}
}

In this contract, the deposit() function allows users to deposit Ether into the contract by calling the function and sending Ether along with the transaction. The withdraw() function allows users to withdraw Ether from their balance in the contract, up to the amount specified in the function parameter.

The checks-effects-interactions pattern is implemented in the withdraw() function as follows:

  1. Checks: The function checks that the sender has a balance greater than or equal to the specified amount using the require() statement. If the check fails, the function reverts and any state changes are undone.
  2. Effects: If the check succeeds, the function updates the contract state by subtracting the specified amount from the sender’s balance.
  3. Interactions: Finally, the function transfers

To check up up to date information on Solidity Security Considerations please click here…

This article was generated with the help of ChatGPT, an AI language model developed by OpenAI. The article provides an overview of the checks-effects-interactions pattern in Solidity programming, along with an example Solidity contract that implements the pattern.

--

--

Sam Vishwas

Experienced software architect available for work. 25+ years of design & development experience. Blockchain enthusiast skilled in multiple languages.