TExcelTAD
Home › Solidity › Your First Contract

Your First Contract

Let's write a real smart contract: a counter that anyone can increase and read. It is tiny, but it already contains the three ingredients of every contract: state that is stored on the blockchain, functions that change it, and functions that read it.

The Counter contract

solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract Counter {
    uint256 public count;          // stored permanently in the contract's storage

    function increment() external {
        count += 1;                // changes state: costs gas, needs a transaction
    }

    function current() external view returns (uint256) {
        return count;              // only reads state: free when called off-chain
    }
}

Line by line:

Line Meaning
// SPDX-License-Identifier: MIT A machine-readable license comment. The compiler warns if it is missing
pragma solidity ^0.8.24; Which compiler versions may compile this file (0.8.24 up to, not including, 0.9.0)
contract Counter { … } Declares a contract, similar to a class
uint256 public count; A state variable: an unsigned 256-bit integer, starting at 0
function increment() external A function that other accounts can call
view returns (uint256) Promises not to modify state and returns one number

Deploying and calling it

On a real network you would compile the contract, send a deployment transaction, and then send transactions to call increment(). In the playground, a contract called Main does that job for us: it deploys Counter with new and calls it like any other object.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Counter {
    uint256 public count;

    function increment() external {
        count += 1;
    }

    function current() external view returns (uint256) {
        return count;
    }
}

contract Main {
    function main() external {
        Counter counter = new Counter();
        console.log("Deployed Counter at", address(counter));
        console.log("Start:", counter.current());

        counter.increment();
        counter.increment();
        counter.increment();
        console.log("After 3 increments:", counter.current());
    }
}
Deployed Counter at 0xa16E02E87b7454126E5E10d957A927A7F5B5d2be
Start: 0
After 3 increments: 3

new Counter() creates a brand-new contract account with its own address and its own storage. The variable counter holds a reference to it, and counter.increment() sends a message call to that address.

The free getter

Because count is declared public, the compiler automatically generates a function count() that returns its value. So the current() function above is redundant. We can call the getter directly:

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Counter {
    uint256 public count;

    function increment() external {
        count += 1;
    }

    function reset() external {
        count = 0;
    }
}

contract Main {
    function main() external returns (uint256) {
        Counter counter = new Counter();
        for (uint256 i = 0; i < 5; i++) {
            counter.increment();
        }
        console.log("count() =", counter.count());
        counter.reset();
        return counter.count();
    }
}
count() = 5
main() returned: 0

State lives on

State variables keep their value between calls; that is what "stored on the blockchain" means. Two separate deployments have completely separate storage:

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Counter {
    uint256 public count;

    function increment() external {
        count += 1;
    }
}

contract Main {
    function main() external {
        Counter a = new Counter();
        Counter b = new Counter();
        a.increment();
        a.increment();
        b.increment();
        console.log("a.count:", a.count());
        console.log("b.count:", b.count());
    }
}
a.count: 2
b.count: 1

A note on the compile step

Behind the scenes, solc turns your source into two things:

  • Bytecode: the EVM instructions that get stored at the contract's address.
  • ABI (Application Binary Interface): a JSON description of the functions and events, which tools like ethers.js use to call the contract.
json
[
  { "type": "function", "name": "count", "inputs": [], "outputs": [{ "name": "", "type": "uint256" }], "stateMutability": "view" },
  { "type": "function", "name": "increment", "inputs": [], "outputs": [], "stateMutability": "nonpayable" }
]

Tip: Try breaking the code: remove a semicolon or misspell uint256 and press Run. The playground shows the compiler's error message with the line and column, exactly as solc reports it.

Test yourself

What does declaring uint256 public count; give you for free?

  1. A setter function setCount
  2. A getter function count()
  3. An event when it changes
  4. Nothing, it only changes visibility

What does new Counter() do inside another contract?

  1. Copies Counter's code into the current contract
  2. Deploys a new Counter contract with its own address and storage
  3. Calls Counter's constructor on the existing instance
  4. Imports Counter from a library

What does pragma solidity ^0.8.24; allow?

  1. Exactly version 0.8.24
  2. 0.8.24 and any later 0.8.x version
  3. Any version from 0.8.24 to 1.0
  4. Any version below 0.8.24