The language used in this tutorial is Solidity (the language used to build Ethereum smart contracts) and this same language can be used to deploy smart contracts on multiple chains such as Binance Smart chain, Polygon or Avalanche.
In this section, you will learn smart contract development with Solidity programming language by working on a sample project. You will also learn how to connect the various components of Solidity (variables, types, and functions) to build a complete DApp.
If you need help, check to see if your question has already been asked in #section-2-help. If you don't see it in there, post a question with any details that would make it easy for a team member to help you. We'll answer most frequently asked questions in live office hours, so keep an eye out in #announcements for those!
We will start with basic code snippets to more advanced projects in this program. For this tutorial, we will start with one beginner project. Our first project is a DApp for storing and retrieving data from the blockchain.
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; contract Pets { string public myPet; function setPetName(string memory petName) public { myPet = petName; } function getPetName() public view returns(string memory){ return myPet; } }
The first line (with the SPDX-License-Identifier) tells you that the source code is licensed under GPL-3.0.
The second line, "pragma solidity" is where we specify the version of Solidity that our smart contract is written on. We do this so that we can ensure to use the correct compiler.
A contract in Solidity is similar to class in object oriented programming languages. It is a collection of code made up of a constructor, variables, and functions. In this example, 'Pets' is the contract name. In our contract we have a state variable of type string which is public and gave it a name of 'myPet'.
We defined two functions, sometimes called 'getters' and 'setters' in programming. The first function, setPetName, sets the state, or value, of the variable myPet. The second function, 'getPetName' retrieves the value saved as the variable myPet. The vast majority of the functions you'll ever write will either be a setter or a getter.
function functionName(uint x, uint y) public view returns (uint){ // function body here }
Every function must begin with the keyword function
, followed by its name functionName
. Whatever is placed inside the brackets (parameters) are the inputs, or the values that you will pass to your function. Public view returns
states the visibility of the function. In this case, it defines that it can be accessible to the other contracts, denoted by the keyword public
. The function promises not to modify the state of the blockchain, denoted by the word view
. Finally, returns
defines that the function will return a value, and also defines the data type of that output.
Using Remix, an online IDE, create a simple smart contract which adds two numbers together and then returns the value.
You should define two functions inside your smart contract: one to do the computation based on two numbers passed in by the user, and one to return the value of that computation. We'll write one getter to retrieve the current value of the variable and one setter to add the two numbers and update the value of the variable.
In Remix, create a new file inside the contracts folder, add.sol
.
Start by adding a license identifier, followed by the version pragma:
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0;
Next, define your contract and inside, define a variable of type uint (unsigned integer) and set it to zero.
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; contract AddNumbers{ uint sum; }
Next, write the function to add two numbers passed in by the user and a function to return the current value of the sum variable. Here's what your contract should look like:
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; contract AddNums { uint sum; function addNums(uint x, uint y) public { sum = x + y; } function getSum() public view returns (uint) { return sum; } }
Now we'll compile, deploy, and test our contract. Head over to the 3rd icon from the top and hit Compile add.sol
Move to the 4th icon from the top and select the Remix VM from the dropdown in the environment
selection. This will give you some fake ether to be able to deploy and test your contract.
Note: Remix has renamed this to be Remix VM - you will no longer see the Javascript VM as an option. Select the Remix VM!
Finally, hit the Deploy
button to create an instance of your contract that we'll interact with and test that the sum function is working as expected. After a few seconds, you'll see a Deployed Contracts
panel on the bottom left.
Pass in two integers, then hit the caret button just beside addNums and the two integers you just passed in. A button 'transact' appears. Click on it and then you'll see a new log indicating the new transaction you just initiated.
The addNums function adds the two numbers, but doesn't actually return the new value. In order for us to verify that the function worked, we need to call our getter function. Hit the getSum
button. You'll notice a new log appears. Expand that log using the down arrow and scroll to the bottom to find a value called decoded output.
You'll see we get the right answer - 8! You just wrote your first smart contract :-)
π Let Twitter know you just wrote your first Solidity smart contract - hit the Share button below and paste the link to your tweet in #builders-hype so everyone can clap for you!
Writers: Cami, Fatma, Editors: Busayo, Sarah Schwartz, Deborah Emeni