Solidity: create your first smart contract

Solidity Smart Contract
Written by Eze

I am not going to bore you with all the statistics on how an average blockchain developer makes over $100k/year, you can learn all about Ethereum and the blockchain in our previous article.


In this article, you’ll learn how to create, compile, and deploy your first Ethereum smart contract.


Let’s get started…


First off, to follow through with the examples in this article you’ll need a browser, an internet connection, and basic programming knowledge.

Setup

There are different ways to set up Solidity for development. There are different options available for writing and compiling Solidity smart contracts in development; you could use Truffle, VS Code, or Remix.

But in this article, we’ll use Remix so, you don’t have to download any tool if you already have a web browser.


What is Remix, you asked?

According to the Remix documentation:

Remix IDE is an open source web and desktop application. It fosters a fast development cycle and has a rich set of plugins with intuitive GUIs. Remix is used for the entire journey of contract development as well as being a playground for learning and teaching Ethereum.

In plain English, it’s a browser-based IDE for writing, compiling, and deploying smart contracts. 

Finally, in case you are still wondering what a smart contract is; Smart contracts are simply programs stored on a blockchain that run when predetermined conditions are met. You can learn more about smart contracts here. Now, let’s jump into writing your first smart contract. 

What we’ll build

A simple Hello World smart contract. In our Hello World smart contract, we can set a custom message we want to output. 
In the next article, we’ll build something more realistic. For now, let’s go easy.

File extension

Solidity source file extension is .sol. So, we’ll create a helloworld.sol file in the remix.ethereum.org editor as shown below:

If you have used VS Code, Atomic editor, or any other modern IDEs you’d be familiar with this interface. So, feel free to explore it as you desire. You won’t break anything.

Define a License

For people to trust your smart contract, you’ll in some way need to open source your source code.

Therefore, it’s important you define a license for your source code, a license that defines how developers can access or distribute your source code. The way you define this license in Solidity is by adding a comment at the top of the source file. It has to be the first line of code in the source file. The license below is GPL-3.0

// SPDX-License-Identifier: GPL-3.0

Also, if your project is a closed source project, you should use the Unlicense license type as shown below: 

// SPDX-License-Identifier: Unlicense


This is not compulsory though, your code will compile without it but it’s considered a best practice to specify your software license. If you are wondering the type of Licence that will be good for your project you can check all the supported licenses on the SPX License website for more details.

Define Solidity version

After defining the type of license the next step is to define the Solidity version you want to use in the following format:

pragma solidity ^0.8.0;


It’s important you define the version as different Solidity versions most likely have breaking changes. Adding the version allows you to stay consistent.
At this point we are good. let’s define our contract.

Define a contract

You define a contract using the contract keyword as shown in the code below. It’s more like the way you define a class in Javascript.


contract HelloWorld {

}


Inside the HelloWorld contract let’s define the variable that will store the “Hello World” message.


constract HelloWorld{
  string public message;
}


Variables defined in the contract as properties are Storage variables by default and it costs gas to define storage variables because they mutate the blockchain state.

There are two types of variable persistence implementation in Solidity; Storage and Memory.  You can think of memory like RAM, the data stored in variables declared as memory are temporary while that of Storage are permanent.


As you can see we are also declaring the variable as public. This will allow access to the variable from anywhere within the contract.
We can initialize the variable with a value like so:


constract HelloWorld{
  string public message = "Hello World!";
}


However, we can’t do this:


constract HelloWorld{
  string public message;
  message = "Hello World!"
}


It’ll throw an error.


There are two ways to update the parameter. By creating a setter function or adding it in the constructor. If you update it in the constructor it will only be updated when the contract is executed. However, with the setter function, you can always change the value.


With constructor:


constract HelloWorld{ 
  string public message = "Hello World";
    constructor(){
        message="Hi World";
  }
}

With a setter function:


contract HelloWorld {
    string public message = "Hello World";
    
    constructor(){
        message="Hi";
    }
    function setMessage(string memory _message) public {
        message = _message;
    }
}


We are also setting the function as public because we want it to be accessed by other contracts. Also, in the method definition, you’ll notice that we are declaring the message as memory that’s because we want to only store the value temporarily. 


Normally, to read the value in the contract parameter message, we are supposed to create a getter function but Solidity implicitly creates a getter function for every class parameter.


So, let’s deploy the contract and run it on Remix.

Deploy and Run

Click on the Ethereum icon by the left side of the screen and click on the Deploy button as shown in the image above.


You’ll now be able to interact with the contract as shown below:

You can enter a message in the input box and click on the setMessage button the message will be set and you can hit the message getter function to show the message.


I’ll just set a message now finally.

All good.


Actually, this is not to make it sound like Solidity’s Hello World program is complicated, no, it isn’t. I just wanted to explain every bit in the process for a better first-time conceptual and practical understanding.


I hope you learned something new. Let me know if you have questions or you are looking forward to the next topic. We’ll be building something more practical.


Happy Hacking!

Leave a Comment

  • bitcoinBitcoin (BTC) $ 28,091.00 10.97%
  • ethereumEthereum (ETH) $ 1,919.12 20.58%
  • tetherTether (USDT) $ 0.994083 0.52%
  • usd-coinUSD Coin (USDC) $ 1.02 1.62%
  • binance-coinBNB (BNB) $ 253.23 18.96%
  • xrpXRP (XRP) $ 0.374699 27.2%
  • binance-usdBinance USD (BUSD) $ 1.02 0.96%
  • cardanoCardano (ADA) $ 0.445015 31.5%
  • solanaSolana (SOL) $ 44.14 31.94%
  • dogecoinDogecoin (DOGE) $ 0.076886 28.5%