Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 47 ยท Getting Started

Your First Contract

Write and understand your first Solidity contract: license line, pragma, state variables, functions, view, plus how to deploy it in Remix and a JS twin.

Time to read and write real Solidity. The classic first contract is a counter: it stores one number, lets anyone add to it, and lets anyone read it. It is tiny, but it contains the pieces of almost every contract you will write later: a license line, a version pragma, a contract block, a state variable and a few functions.

You will first read the whole contract, then take it apart line by line, then learn how you would deploy it with Remix (as read-only steps, since the real compiler cannot run on this site). Finally you will run a JavaScript class that behaves the same way, so you can watch the state change.

Parts of a contract file

License A comment that names the license.
Pragma Which compiler versions are allowed.
Contract The block that holds everything.
State variable Data saved on-chain.

Functions then read and change that saved data.

1 / 2

The whole contract

Save this as Counter.sol. Read it once from top to bottom before we explain it.

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

contract Counter {
    uint256 public count;

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

    function add(uint256 amount) public {
        count += amount;
    }

    function decrement() public {
        require(count > 0, "counter is already zero");
        count -= 1;
    }

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

It is read-only on this page, but every part of it will be explained in the sections below.

Line by line

The license line

// SPDX-License-Identifier: MIT is a special comment. The compiler warns if it is missing, because published contracts are public and people want to know how they may reuse the code. MIT is a common permissive choice. For code you do not want to license, you can write UNLICENSED.

The pragma

pragma solidity ^0.8.24; says which compiler versions may build this file. The caret means "0.8.24 or any later 0.8.x release, but not 0.9.0". This matters because the language changes between versions. The rules you will learn in Integers, Overflow and Checked Math, for example, rely on version 0.8 or newer. When you start, use a recent 0.8.x version; production teams often pin one exact version (pragma solidity 0.8.24;) so the same compiler builds the deployed code every time.

The contract

contract Counter { ... } is similar to a class. Everything inside belongs to one contract that will live at one address once deployed. A file may hold several contracts, but one per file is the usual habit, named like the file.

A state variable

uint256 public count; declares a state variable. It lives in the contract's storage, so it persists between calls (How a Smart Contract Runs). The type uint256 is an unsigned 256-bit integer, the default number type on the EVM. You will meet all the types in Value Types.

You did not give it a starting value, and you do not need to: every variable starts at its type's default, which is 0 for numbers.

The word public makes the compiler generate a free getter function called count(). Anyone can read the number without you writing any code for it.

Functions that change state

SOLIDITY
function increment() public {
    count += 1;
}

A function has a name, parameters in parentheses, a visibility (public means callable from outside and inside) and a body. Because increment writes to storage, calling it must be a signed, paid-for transaction. add(uint256 amount) takes a parameter, whose type is always written.

require and revert

require(count > 0, "counter is already zero"); checks a condition. If it is false the whole transaction reverts: every change it made is undone and the message goes back to the caller. Here it prevents the counter from going below zero. Without that check, the subtraction would fail anyway because of Solidity 0.8's built-in overflow checks, but a clear message is friendlier. Control Flow, require, revert and Custom Errors covers errors fully.

view functions

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

view promises that the function does not modify state. Calling it from outside is free and needs no transaction. returns (uint256) declares the type of the returned value. In this contract current() is redundant because count is already public, but it is the simplest example of a function that returns something.

Note the naming habit: count, current and increment are lowerCamelCase for variables and functions, and Counter is UpperCamelCase for the contract. Solidity programmers follow the same style everywhere.

Deploying it in Remix

These steps are read-only here because they need a browser app, not code you can run on this page. In Remix (see Your Solidity Toolbox) the process is:

  1. Open remix.ethereum.org, create a file named Counter.sol in the file explorer and paste the contract.
  2. In the Solidity compiler tab choose a compiler matching your pragma and press Compile. Green means success.
  3. In the Deploy and run transactions tab choose the in-browser environment (a fake chain with funded accounts) and press Deploy.
  4. The contract appears under "Deployed Contracts". Press count (blue button, a free call) and see 0.
  5. Press increment (orange button, a transaction), then count again and see 1.
  6. Press decrement twice more. The second press fails with the message counter is already zero and the count stays at zero.

Blue buttons are calls and orange ones are transactions, the same split you learned in the last lesson. Notice that the failed decrement still produced a transaction record and spent gas, but changed nothing.

The same behavior in JavaScript

Here is a JavaScript twin of the contract. It is a stand-in: there is no compiler, no gas and no chain, but the rules are the same. State is a field, require throws and aborts, and a failed transaction leaves the old value in place.

JS Counter as a JavaScript class

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Now add a transaction log, so you can see which calls changed state and which did not. A real node would also keep gas used for every transaction.

JS Transactions and calls on the counter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The "reset-ish" transaction sets the count to zero and then fails. Because a revert undoes everything, the value stays at 10. That all-or-nothing behavior is one of the most useful guarantees of the EVM.

What is real: the Solidity listing, pragma semantics, defaults, require reverting the whole transaction, and the call versus transaction split. What is the stand-in: the JavaScript has no compiler, gas, access to a chain or visibility rules, and BigInt only mirrors uint256 without its upper limit.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does pragma solidity ^0.8.24; mean?

    The caret allows compatible upgrades within the same breaking range. Here that means 0.8.24 and later 0.8.x versions.
  2. 2. What does the public keyword on a state variable do?

    The compiler adds a getter, so count() can be called from outside without you writing a function.
  3. 3. A transaction calls decrement() while count is 0. What happens?

    require fails, so the transaction reverts and the state stays the same. The gas for the work already done is still charged.
  4. 4. What does the view keyword promise?

    view means no state changes. Called from outside as a call it needs no transaction, though inside a transaction it still uses gas like any other code.

Summary

  • A contract file starts with a license comment and a pragma that restricts compiler versions.
  • contract Name { ... } holds state variables and functions; state variables persist in storage and start at default values.
  • public state variables get an automatic getter; view functions read without changing state.
  • require(condition, message) reverts the whole transaction when the condition is false.
  • In Remix, blue buttons are free calls and orange ones are transactions.
  • A JavaScript class can mirror the behavior, but it has no gas, chain or compiler.

Next: Value Types.