Akinleye Oyinbayode Peters
Oyinbayode

Follow

Oyinbayode

Follow
How to build your first smart contract with Solidity

Photo by Emmanuel Edward on Unsplash

How to build your first smart contract with Solidity

Learn how to build and deploy your first smart contract

Akinleye Oyinbayode Peters's photo
Akinleye Oyinbayode Peters
ยทOct 31, 2022ยท

3 min read

Play this article

Table of contents

Smart contracts are programs that run on the blockchain when they meet predefined conditions. Since they run on the blockchain, they are immutable once deployed and cannot change. In this article, you will learn how to build your first smart contract and deploy it to a local network.

What is Solidity?

Solidity is an Object-oriented, high-level language for developing smart contracts. It is statically typed, supports inheritance, and targets the Ethereum Virtual Machine (EVM) by design.

Prerequisites

To follow this tutorial, you will need;

  • Basic understanding of a programming language
  • Remix IDE

Remix is an online Integrated Development Environment (IDE) for prototyping, developing, and deploying smart contracts.

What you will build

In this tutorial, you will build a simple storage contract where you can write and store a number e.g. 50000. This is to help you get acquainted with Solidity and its syntax.

Setting up the development environment

  1. Head over to Remix Remix.png

  2. Right-click on all folders and files to delete them. a.png After deleting, you should have a blank workspace as shown above

  3. Click on the file icon and create a new file named MyStorage.sol Remix1.png

Building the contract

Declaring contract structure

After you have created the file named MyStorage.sol, copy the following code into the file

In the above code;

  • Line 1 declares the license permission for the contract. This means that the code is open-source and MIT-licensed.
  • Line 3 declares the version of Solidity you are using (v0.8.7). It is important to note that the latest version of Solidity is v0.8.17
  • In line 5, you declare the contract with the name MyStorage

Contract logic

Here, you want;

  • To store input in a variable called Number
  • A function that writes the input to the contract memory
  • A function to get the input written to contract memory

In your Remix IDE, your code should be similar to the below aa.png

Compile the contract

To compile the contract you have just created;

  1. Click on the icon below the search icon Remix2.png
  2. Click on the compile MyStorage.sol button then click on the last icon on the left side of your screen Remix3.png

Deploying the contract to Remix VM

In your current screen, leave the parameters as default and click on the yellow deploy button. This is where your contract gets deployed to the local Virtual Machine of Remix IDE. Remix4.png Once your contract deploys, you will be able to interact with the contract in the Deployed Contracts tab.

Interacting with the deployed contract

  • Enter a number you want to store in the contract and then click on the yellow storeNumber button
  • Click on the getNumber button to retrieve the value you just stored

Remix5.png Your contract works fine if you can store and retrieve the values correctly.

Closing thoughts

It is important to note that when interacting with a local network like the Remix VM, the accounts automatically come with test Ethers (100 ETH) to perform transactions and deploy your contracts. When working with a testnet like Goerli, you will need to approve transactions with test Ethers which you get from faucets.

Conclusion

Congratulations on getting this far. You learned how to set up Remix for Smart contract development and build your smart contract. In addition, you learned how to deploy your contract and interact with it to ensure that it works as expected.

Iโ€™m sure that this article provided guidance for you and helped to build a smart contract.

Cheers ๐Ÿฅ‚

ย 
Share this