Now Reading
Episode 1: Let’s Open This Little Smartie Up!

Episode 1: Let’s Open This Little Smartie Up!

Hello again!
As promised, in the first lesson, we will discuss the anatomy of a smart contract together. We will create a simple smart contract on the Remix IDE. We will then compile and deploy the smart contract on an Ethereum testnet.
Let’s get rights to it then!

Let’s Start With Remix

As mentioned in the prologue, we will be using the Remix IDE. You can easily use Remix by going to its website (the web app) or downloading the desktop version if you prefer it that way.
When you open Remix for the first time, you will see this screen.

On the left side, we have the menu common in most IDEs. You would be able to see your workplace and the smart contracts created there.

On the main vertical menu and just below the search icon, we have the Compile and Run sections respectively.
Check out the following short video and try to explore the options one by one yourself.

We will see them in action in the next section. But first, we need to write our first smart contract!

The Anatomy Of A Smart Contract

Just like a Class in an object-oriented programming language like Java, a smart contract has its own anatomy. In other words, there are basic definitions that need to be there for the contract to be functional.
Here, you can see the basic structure of a smart contract:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;

contract HelloWorld {
/* write your code here / 
} 

Now, let’s discuss these components in detail.

// SPDX-License-Identifier: GPL-3.0

The first line is a license that allows you to share your code. This is required as we deploy smart contracts on a public blockchain.

pragma solidity >=0.8.2 <0.9.0; 

With this line, we are defining the version of the compiler.

This information is useful for others to understand what was the version of the compiler we used when we were writing, testing, and deploying this smart contract.

The line above tells that the compiler 0.8.2 and any other version between this and 0.9.0 (not 0.9.0 itself) are compatible with this smart contract. Let’s check some other examples to ensure that we get the idea:

// Only this version works
pragma solidity 0.8.2;

// This version and any version before this will work
pragma solidity <=0.8.2;

// Only versions after this will work
pragma solidity >0.8.2; 

And finally, we have our smart contract which is defined like a class.

Local variables and functions including the constructor will be placed between these two brackets.

contract HelloWorld {
/* write your code here */
}

Now, let’s create our first smart contract!

The Hello World Smart Contract

Before we create the .sol file, you should know that your compiler is always set to the latest available version. You can use that version in your smart contract.

After including the license and specifying the compiler’s version, define your smart contract by the word “contract” and add the name of your contract afterward. Here, I have selected the name “HelloWorld”.

Now, we define the constructor. Similar to a class constructor in OOP languages, the constructor will initiate the smart contract. Your constructor can have an input or be without one. Here, our constructor doesn’t have an input as it is simply going to print a message.

constructor () {}

To be able to print something in the console, we need to import a library. Just like OOP languages, the import will happen outside the smart contract. So just before the smart contract starts, include the following import code:

import "hardhat/console.sol";

Now, you’ll be able to print your message in the console using the “log” command.

console.log("Your Message");

That’s it. Our simple “HelloWorld” contract is finished. Now, let’s compile and run it and check the output together.

The Hello World Smart Contract: Compile and Run

Now, from the left menu choose “Solidity compiler”. Make sure that the compiler version matches the one you’ve defined in the smart contract and click on compile.

Now, you can simply deploy the compiled file and run your smart contract on the Ethereum blockchain. Select “Remix VM (London)”, ensure that the right smart contract is selected, and click on “Deploy”.

Now, check out the console and try to find your message.

Coming Up: Variables!

As you can see, our first smart contract was so simple. It just printed a message in the console in its constructor.
Let’s spice things up a bit in the next episode and add some variables to our smart contract!

What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0