Contract Structure

Contract structure

State and local variables

State variables are variables declared at the beginning of the contract, outside the scope of local variables declared in function.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;

contract SimpleStorage {
    uint public storedData; // State variable
    // ...

    function updateStoredData(uint newData) public {
        uint formattedData = newData * 2; // formattedData is local variable
        storedData = formattedData;
    }

    function getFormattedData() public view returns (uint) {
        return formattedData; // failed can't compile because formattedData is local scope to the other function
    }

    function getStoredData() public view returns (uint) {
        return storedData; // can compile because global variable
    }
}

Functions

Function are functions declared to perform calculations, change the value of variables, etc. A sample function is given below.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;

contract SimpleAuction {
    function bid() public payable { // Function
        // ...
    }
}

// Helper function defined outside of a contract
function helper(uint x) pure returns (uint) {
    return x * 2;
}

Function modifiers

Function modifier are declarations for function to create conditions for running actions of that function.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;

contract Purchase {
    address public seller;

    modifier onlySeller() { // Modifier
        require(
            msg.sender == seller,
            "Only seller can call this."
        );
        _;
    }

    function abort() public view onlySeller { // Modifier usage
        // ...
    }
}

Events

event is a feature for recording smart contract activities. event is often used in building interactive UI with smart contracts.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.22;

event HighestBidIncreased(address bidder, uint amount); // Event

contract SimpleAuction {
    function bid() public payable {
        // ...
        emit HighestBidIncreased(msg.sender, msg.value); // Triggering event
    }
}

Errors

error is used to inform the user why the action failed, and error has a lower gas cost than returning string.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

/// Not enough funds for transfer. Requested `requested`,
/// but only `available` available.
error NotEnoughFunds(uint requested, uint available);

contract Token {
    mapping(address => uint) balances;
    function transfer(address to, uint amount) public {
        uint balance = balances[msg.sender];
        if (balance < amount)
            revert NotEnoughFunds(amount, balance);
        balances[msg.sender] -= amount;
        balances[to] += amount;
        // ...
    }
}

Struct types

struct is used to declare a type of object.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;

contract Ballot {
    struct Voter { // Struct
        uint weight;
        bool voted;
        address delegate;
        uint vote;
    }
}

Enum types

enum is used to declare a type whose values ​​are constant.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;

contract Purchase {
    enum State { Created, Locked, Inactive } // Enum
}

Last updated