Write Your First Solidity Smart Contract Using Remix IDE

To use smart contracts written in Solidity our code must be compiled to bytecode and deployed to a network that uses the EVM (Ethereum Virtual Machine) for computation. This can be off putting for Solidity newcomers as there may be an assumption that we need to deploy our contract to something like the Ethereum network, which can be costly, irreversible and daunting to newcomers.

Remix can help newcomers overcome the above misconceptions. Remix is a web based IDE that allows us to compile and deploy our code in the browser. By default it achieves this by deploying our code to an isolated version of Ethereum that is ran on a single node in the browser. This is not the Ethereum mainnet, and is only used by you from your open Remix IDE. This is therefore a free and fast way to test our code without the financial commitment that comes with deploying a smart contract to the Ethereum mainnet (Note: we can also deploy smart contracts to official Ethereum testnets and other EVM blockchain networks, how to do this will be covered in subsequent tutorials).

Writing Your First Contract

Creating a New Solidity File in Remix

To create a new Solidity file we open the Remix IDE in our browser:

To create a new Solidity file in our contracts folder right click the contracts folder and select new file.

How to create a new Solidity file in Remix.
How to create a new Solidity file in Remix.

Next we are prompted to name our contract. The naming convention for Solidity files is UpperCamelCase.

Naming your new Solidity file.
Naming your new Solidity file.

Our file will then open in a new Remix tab, giving us a place to write our Solidity code.

This is where we shall write our Solidity code.
This is where we shall write our Solidity code.

A Smart Contract for Writing Data to the Blockchain

Smart contracts can be used to store data (such as integers, strings and bools) on a blockchain.

To create a smart contract that does this copy the code snippets below into your new file as you follow along (Note: a full snippet of the code is available at the end of this tutorial).

We add the below line of code to the top of our file. This explicitly states the license type for our code. Large amounts of money can be stored in smart contracts, therefore we want to maximise trust in them, one way to ensure this is to share the source code of your smart contract. Sharing your source code can lead to legal questions around copyright, the below is a machine readable SPDX license identifier, this makes your codes license type explicit to avoid any legal ambiguity.

// SPDX-License-Identifier: MIT

Next we use the pragma keyword to specify the earliest version of the Solidity compiler we wish to use. This ensures our code will compile as intended. The below snippet means our code will not compile with a compiler earlier than version 0.8.0.

pragma solidity ^0.8.0;

Now we can start writing our contract. To do this we use the contract keyword followed by its name (UpperCamelCase) and curly braces (where the body of the contract will live).

contract DataStorage {
    
}

In the body of the contract we can write code that runs once, when the compiled code is deployed. Here we shall declare a variable with the identifier firstName. Notice that lowerCamelCase is the naming convention used for variables in Solidity, and that since Solidity is statically typed, we have to explicitly define our variable type, which is a string. The word public is a visibility quantifier, other visibility quantifiers are internal and private. Public means that our variable can be used externally, and that Solidity will create a getter function for this variable. This means we can query this variables value after the contract has been deployed.

contract DataStorage {
    string public firstName;
}

Our file is now ready to be compiled. To do this we navigate to the Solidity Compiler using the compiler tab on the left hand side (this is the Solidity logo with a green tick in the below screenshot). Then all we need to do is click the blue compile button. If you have copied any of the code incorrectly it may fail to compile, however the compiler will produce helpful errors to help you identify the mistake.

We can click the blue Compile button to compile our Solidity code to bytecode.
We can click the blue Compile button to compile our Solidity code to bytecode.

Now we have compiled our code it can be deployed to the Ethereum network. The default place to deploy a contract in Remix is to the Javascript VM, this is an isolated version of Ethereum ran on only one node, in your browser. This is what we shall deploy our contract to. To do this we navigate to the Deploy and Run Transactions tab and click the Deploy button (as seen below).

Once compiled we can then deploy our contract.
Once compiled we can then deploy our contract.

After deploying our contract we will see it in a list below the Deploy button. We can view any public functions by expanding the deployed contract. We can see the value of a public variable by clicking on its getter function. We can test this by clicking the firstName getter function. This returns only the variable type (string) and no value, as we only declared the variable, we didn’t initialize (assign a value to) it.

We can see any public functions by expanded our deployed contract.
We can see any public functions by expanded our deployed contract.

We can initialise our variable on a new line (Note: string literals are denoted with double quotes in Solidity, not single quotes).

contract DataStorage {
    string public firstName;
    firstName = "John";
}

Or we can initialise our variable in the same line we use to declare it.

contract DataStorage {
    string public firstName = "John";
}

Now we have initialised our variable, we can save our file, compile our code again, and deploy our contract again. If we do this and then expand our deployed contract, and use the firstName getter function, we will notice that our variable has now been assigned a value.

We have stored a value on a copy of the Ethereum Network using our smart contract.
We have stored a value on a copy of the Ethereum Network using our smart contract.

Storing Multiple Values of Different Types

We now know how to store one value (a string) on a blockchain using a smart contract. The below code snippet is a contract that stores multiple values of different types. Here we introduce 2 new types, uint8 and bool. uint8 is an 8 bit unsigned integer, meaning any integer from 0 to 255. This is an appropriate type for age as no human has a negative age or is older than 255 (for now). bool is a type that only has 2 possible values, the keywords true or false. This is an appropriate type for likesPizza, as you either like pizza or you don’t.

contract DataStorage {
    string public firstName = "John";
    string public lastName = "Smith";
    uint8 public age = 30;
    bool public likesPizza = true;
}

If we update our Solidity file in Remix (save it, compile it and deploy it) we can expand our deployed contract to find a getter function for all 4 of the above variables.

Expanding our deployed contracts shows the getter function of each public variable.
Expanding our deployed contracts shows the getter function of each public variable.

Updating The Values of Our Variables

The above code allows us to store several values on chain, however, it doesn’t leave us with the option to update or change these values in the future. This may be something we would like to do, particularly to the age variable. To give ourselves this option we can create a function.

A function is a piece of code that we can reuse again and again, and when made public, can be used after our contract has been deployed. To create a function we add the following code to the body of our contract.

function updateAge() public {

}

We begin with a keyword, function, and write the name of our function in lowerCamelCase, followed by parenthesis and the public visibility quantifier (so we can call our function after the contract has been deployed).

For this function to be able to update our age variable we need to declare a parameter, which can be passed an integer when we want to update age. We do this inside the functions parenthesis.

function updateAge(uint8 newAge) public {

}

Finally we need to instruct our function to assign the value of newAge to the age variable.

function updateAge(uint8 newAge) public {
    age = newAge;
}

If you’ve been following along your Solidity file should now contain the following code.

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

contract DataStorage {
    string public firstName = "John";
    string public lastName = "Smith";
    uint8 public age = 30;
    bool public likesPizza = true;

    function updateAge(uint8 newAge) public {
        age = newAge;
    }
}

If we save, compile and deploy the above code we can expand our deployed contract and then test our function in 3 steps.

Step 1: Use the age getter function to check our age value. This will be 30 as it was set to that when the contract was deployed.

When the contract is deployed age is set to 30.
When the contract is deployed age is set to 30.

Step 2: Enter a new age, here we have used 31, into the updateAge function to assign the age variable a new value.

We can assign a new value to age using the updateAge function.
We can assign a new value to age using the updateAge function.

Step 3: Check that age has updated by using the age getter function again.

We can see age has successfully updated by calling its value again.
We can see age has successfully updated by calling its value again.

You’ve now successfully created a smart contract, that stores and updates values, using the Remix IDE. This is only the very beginning of smart contract development, if you’d like to learn more a good next step is to deploy a smart contract to an Ethereum testnet using Hardhat, Brownie or Foundry.

Subscribe to Mike Porter
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.