Blockchain Programming: Creating Your First Smart Contract with Solidity

In this article we discuss how to create your first smart contract with Solidity with the Remix IDE. In particular, how to configure the compiler, deploy the smart contract, and interact with it in the browser.

14 days ago   •   4 min read

By Peter Foy

In this article, we're going to discuss how to write your first smart contract with Solidity.

To do so, we'll assume you already have a MetaMask wallet setup with some test ETH to play around with, if you haven't done so you can check out this tutorial to learn more.

This article is based on notes from this Blockchain Developer course and is organized as follows:

  • What is a smart contract
  • Setting up the Remix IDE
  • Hello World with Solidity
  • Configure the compiler
  • Deploy the smart contract
  • Interacting with the smart contract

What is a smart contract?

Before we get into writing our first smart contract, let's briefly discuss what they actually are.

The term "smart contract" was coined by computer scientist and cryptographer Nick Szabo in 1994.

In simple terms, a smart contract is a piece of code running on a blockchain, which:

  • Is a state machine
  • Needs transactions to change the state
  • Can also perform logic operations

State changes of a smart contract happen through mining and transactions.

A smart contract is also turning complete, which means in theory it can solve any computational problem.

Smart contract programming languages

There are several programming languages for smart contracts designed for Ethereum Virtual Machines (EVMs) including Solidity, Vyper which is more Pythonic, and LLL which is the original Ethereum smart contract programming language.

Regardless of which programming language is used, they are all compiled and what is sent to the blockchain in EVM Bytecode.

Every Ethereum node in the network thus executes the same code since every node has a copy of the chain.

Below we'll look at how to create our first smart contract with Solidity as it's one of the most popular and well-maintained programming languages.

Setting up the Remix IDE

In order to create our first smart contract we'll be using the Remix Ethereum IDE, which allows us to compile, run, and deploy smart contracts with Solidity. From the Ethereum Blockchain Developer site, Remix is...

...a cloud based integrated development environment tailored to Solidity programming. It has a plug-in based architecture and, by default, there are two plug-ins already activated, which are essential to blockchain developers like us: the compiler and a way to simulate an execution environment to test your smart contracts.

In order to make use of the Remix IDE we first need to enable several plugins, namely the Solidity Compiler and the Deploy & Run Transactions plugin.

We can enable these by going to the Plugin Manager, search for these plugins and activate them.

Hello World with Solidity

The next step is to create a new Solidity file by going to the File Manager, right click on the contracts folder, and name our contract MyContract.sol.

Every Solidity file starts with a pragma line which defines the version the Solidity file is written for. In our case we will create the contract for version Solidity 0.8.14: pragma solidity 0.8.14;

We will then start the contract with the keyword contract, name it and write our hello world statement with a public string as follows:

pragma solidity 0.8.14;

contract MyContract {
    string public myString = "Hello World!";

Configure the compiler

Next, we want to go to the Solidity Compiler tab and select "auto-compile" or we can manually compile the contract by clicking "Compile MyContract.sol".

Deploy the smart contract

Let's now deploy this contract on a test blockchain. To do so, we:

  • Go to the Deploy & Run Transactions tab
  • Select the Remix VM (London) environment
  • Select MyContract.sol from the Contract downtown
  • Click Deploy and you'll see a contract instance at the bottom of Remix

Interacting with the smart contract

We can interact with the smart contract by uncollapsing it under Deployed Contracts:

If we hit the "myString" button it shows us the Hello World! string from our contract:

This will also log another transaction in the logging section, which is called call:

One thing to note is that we can't update a smart contract that has already been deployed, instead we need to deploy a new instance.

In Remix, if we reload the page the contract instances are removed and we can update and redeploy our contract again.

Structure of a Solidity smart contract

As we saw, all Solidity smart contracts start with a pragma line.

Following that, below is a high-level overview of a Solidity smart contract's structure:

  • It starts with a class-like structure
  • It contains functions
  • It contains control structures like if / else
  • It contains loops like for / while
  • It contains DataTypes including (U)Int, booleans, arrays, struct, mapping, and addresses
  • Note there are no floats in Solidity smart contracts
  • There is inheritance
  • There are special structures like modifiers
  • There are imports

Summary: Creating your first smart contract with Solidity

In this article we introduced how to create your first smart contract with Solidity with the Remix IDE.

We looked at what smart contracts are, how to install the necessary plugins, configure the compiler, deploy the smart contract, how to interact with the smart contract in the browser, and a high-level overview of the structure of a Solidity smart contract.

Now that we have the fundamentals of getting started with Solidity, in the next articles we'll look at how to create more useful smart contracts.

Spread the word

Keep reading