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.
A basic understanding of any programming language.
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!
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:
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.
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.
Step 4: Declare Variables
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.
Can you feel the Gains?!!! Vitalik is Proud of You!
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.
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.
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:
To deploy your contract you will:
- 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.
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.
Yay!!!! You’ve Written and Deployed Your First Smart Contract!
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😃.