Write Your First Smart Contract

The First of Three in A Tutorial Series On How To Write Smart Contracts.


I wrote these tutorials as a follow up to my video “How do blockchains & smart contracts work w/ MadHive case study”.

In these tutorials, you will learn how to write 3 different smart contracts of increasing complexity. In Part 1, we will build a basic “Hello World” style contract. In the Part 2, we will create a “token style” contract. In Part 3, we will create an “escrow style” contract.


Prerequisites:

A basic understanding of any programming language.

Goal:

By the end of these tutorials you will develop a better understanding of how Solidity works and be able to create your own smart contracts and tokens on the Ethereum platform using Remix, the Solidity IDE.


As you progress through this tutorial Vitalik will grow stronger, just like your mastery of this topic!

Vitalik Buterin — Co-Founder of Ethereum and all around good guy.

PART 1: Hello World

First we are going to build a basic Hello World smart contract. Are you excited? You should be! You are on your way to becoming a smart contract ninja!

Step 1: Remix & Creating Your File

Go to the Remix IDE website. Remix is an integrated development environment that will help you to write your smart contracts. It’s available online and makes it extremely easy to code, debug and deploy smart contracts. When you reach the site you may see a sample smart contract already open like this:

If you see a sample contract like the one above, close the tab. We will be writing ours from scratch.

Your screen should look like this:

Next, click on the + symbol in the top left corner to create a new contract and name it Hello.sol. Your screen should look like this.

Step 2: Version Pragma

It is important that we stipulate what version of compiler our contract is compatible with. We can do this by stipulating which minimum version to use. For our example, we will stipulate a minimum version of 0.4.0 and also compatibility with any version up to 0.5.0. We do this by adding the ^ symbol prior to our minimum version as illustrated below.

This declaration ensures that your code will compile properly.

Step 3: Declare your Contract

We have to declare our contract. We do so much like we would declare a class in other languages such as Java. Instead of class, we use the keyword contract followed by the contract name, which should match the name of the .sol file. In our case, we will name the contract Hello.

We have declared our contract and also identified Solidity version compatibility.

Step 4: Declare Variables

Our next step is to declare our variables. Unlike languages like JavaScript, we must specify a data type for our variables. In this contract, we will be using two variables. The first variable will be of type address. Address basically means an account on Ethereum. When you’re referencing an address you are referring to either a user account or a smart contract account. The second variable will be of type string. String is a ubiquitous data type so we won’t bother defining it here.

We will name our variables creator and greeting. The creator variable will store the address of the account that deployed the smart contract and the greeting variable will hold a string that we can return or manipulate in the contracts state.

Step 5: The Constructor

Next, we will write the constructor. This function will be called once upon contract creation. The constructor must share the same name as the contract. We will also initialize the variables using the constructor. The string variable will be set to an argument passed into the constructor.

Notice that we initialize the creator variable with msg.sender, msg is a globally available object that provides information about the transaction that is invoking the contract or function.

Adding the constructor means you’re one step closer to completing your first contract!

Can you feel the Gains?!!! Vitalik is Proud of You!

The more you learn, the stronger you both get!

Step 6: Writing A Constant Function

A constant function is a smart contract function which does not alter the state of the contract. It is read-only. It does not cost any gas to call this type of function. In the future, the constant keyword will be replaced by the view keyword.

We will add a constant function named greet(). This function will return the variable greeting of type string.

We’ve added our greet function. You’re doing amazing!

Step 7: Changing the state of our contract using setGreeting()

Can you believe you are almost done with your first contract? We only have one last function to add before we can deploy it!

We want to add a function that lets you change the greeting to anything you want. To do this we will write a function named setGreeting() which will take in a string as an argument. The function will assign our greeting variable to whatever the argument is.

Notice in this function we don’t use the return or constant keywords. We don’t use constant because this function alters state and writes to the blockchain so it will cost gas when it is called.

Our contract is done! Now to deploy! 🚀

Step 8: Deploying The Contract To The Sandbox

Now that our contract is complete we will deploy it to the built in sandbox blockchain to see it in action. Here is what your code should look like:

Hello.sol

To deploy your contract you will:

  1. Click on the Run tab on the right hand side of your editor. Choose JavaScript VM from the first drop down menu. This will result in your second drop down menu being populated with some test Ethereum accounts each possessing 100 eth.
  2. Enter in the string “Hello World” into the field next to the Create button.

Your screen should look like this:

3. Click on the Create button to deploy. Once deployed you will have information regarding the contract in the grey terminal area on bottom of your IDE. If you expand that section you will see something similar to below:

The status displays the deployment status of the contract.

The contract address is the ethereum address by which you can refer to your contract.

The from address is the address of the account that deployed the contract.

To, in this case, refers to the constructor function that was called to create the contract.

Gas was the amount payed in wei to deploy the contract.

The input is the binary that was compiled from your code and is what lives on the ethereum platform inside the EVM (Ethereum Virtual Machine).

Step 9: Interacting with the contract.

Once deployed, you will see inputs to interact with your contract on the right hand side. There will be a drop down box which will contain the name of the contract as well as its address. Below that you will see a button labeled greet. When you click that button it will call the greet function and return the variable greeting’s value.

As you can see in the decoded output the greet method returns our string variable’s value.

To alter the variable greeting, we will use our setGreeting() function. To do this we can type whatever string we want in quotes in the input box next to the setGreeting button. Let’s type in “Hola Mundo” and then press the greet button again. It should return our new string.

Type in your new string in the input field. In our case, “Hola Mundo”.
Once you run greet() again, you will see that our variable is now changed to “Hola Mundo”!

🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉

Yay!!!! You’ve Written and Deployed Your First Smart Contract!

🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉

Vitalik is proud of you!

Conclusion

I hope you enjoyed this first of three tutorials on smart contracts. This technology isn’t only bleeding edge but also a lot of fun! I like to keep my instruction light-hearted and I really hope you get some benefit from it. If you have any questions, comments or concerns feel free to reach out to me here or via twitter. I’m always happy to help😃.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.