First Smart Contract

Beginners Guide to Solidity Development - Episode 2

First Smart Contract

In this episode, we will be creating our first simple smart contract and we will publish this contract to a locally running virtual copy of the Ethereum block chain network.


To do all this we will be using a tool called Remix. The Remix is a very nice easy way to get started writing some simple smart contracts because it runs in your browser and you can also interact with your contracts through the interface that Remix provides. The drawback of Remix is that it doesn't support some advanced features that we will be using in some later tutorials, but for now it's a very easy way to quickly get started.

You can find Remix at . So, let's go there now.

This is what it looks like when you open that page for the first time. Now, instead of using this 'Ballot'-program which is prefilled for us, we will click the cross over here to remove it because we will be creating our own contract from scratch.

Writing our first lines of code

At the top left you can create a new file, name it FirstContract.sol and click OK to create this file. Now type along with what I am going to type here, and I will explain afterward what exactly this does.

pragma solidity ^0.4.24;

contract FirstContract {

Then we have some curly braces and inside here we actually type the content of the contract. You can have multiple contracts in the same file.

pragma solidity ^0.4.24;

contract FirstContract {
    string public message = "I like cookies";

of course you can type in whatever message (instead of "I like cookies") you want.

Alright, what we've now done is created a very simple contract. although we haven't actually published it yet.

Explanation of the Contract

What the first line does is tell the Solidity compiler whenever you run it that you write this contract and expect the compiler to be at least version 0.4.24. If you try to compile it using the older version you will get an error. And that is really nice because we will be using some newer features in coming tutorials and in this way you are sure that your code works as intended because sometimes the behavior of certain types of commands changes on certain newer compiler versions in relation to previous the compiler versions.

Now, so what happens here is that we created this contract, or we started specifying the information of this contract, we have a curly bracket here and everything in between is the body of this contract.

Inside this contract, we create one data field which is called message. Inside this field, we can store string information which is text and finally we write some default value in here and if we wouldn't provide this then the default value for a string would be an empty string.

And, then what we do is add the public keyword here and that means that Solidity will automatically create a very simple function for us that reads whatever is stored in this data field and returns to us. And this function will also be known as a message and this way we can actually read what's inside here which is nice because we would like to use the contract in some way. Otherwise, we would only be able to see that the contract exist but not what is stored inside.

Publishing the Contract

Alright, over here there's button Start to compile, let's press that now.

You see at the top that static analysis raised 1 warning and this warning gas requirement is very high, is a quirk that current versions of Remix currently have and you can ignore that one.

Now, after you clicked the Compile button you can go over to Run and then Deploy. Make sure that your environment is the JavaScript Virtual Machine which is probably preselected already unless you already installed some of the more advanced tools that we will be using in the later tutorials.

After you click Deploy then you will see some information here about the how Remix communicates with a locally virtual version of the block chain. Because it's virtual it goes very quickly and in a real environment, it might take a couple of seconds and then over here you see some information about this contract. And it now is deployed somewhere on the block chain and you have the button here message. And you can click on it to execute the function we defined. And this function will actually read the field and return the string I like cookies.


So, there you go. We created a very simple smart contract, we compiled it and then published it to a locally running version of a block chain currently it's inside a browser virtual machine of the Ethereum block chain. And then we interacted with it using the message command which we defined over here and in this way we were able to see what string is stored inside.

Now, in the future tutorials, we will be expanding on this very simple logic and introduce some more advanced features.,