Write Your First Solidity Smart Contract in the Next 10 Minutes
Are you up for the challenge?
This article is a continuation of my last article (which was featured on Hashnode!) showcasing what I'm learning in 30 Days of Web3 by @WomenBuildWeb3.
What is a Smart Contract?
A smart contract is a computer program that allows us to interact with the blockchain.
For example, it allows us to swap one cryptocurrency into another - or to mint an NFT (non-fungible token).
What is Solidity?
Solidity is a programming language that we can use to build Ethereum smart contracts.
In Solidity, a smart contract is a collection of code made up of a constructor, variables, functions, etc.
Getting Started
The smart contract that we're going to make with Solidity will do a simple calculation - multiplying two numbers.
Where do I write my code?
We're going to be using an online Ethereum IDE called Remix. Click here to open Remix in your browser.
The Basics of Solidity
Feel free to skip this section if you just want to move on to writing your first smart contract!
a) Defining a State Variable
We define a state variable in Solidity like this:
In the above code, we are creating a variable called myBook
that is publicly available (i.e public
) and will always contain a value that will be of string
data type.
b) Setter and Getter Functions
The majority of Solidity functions will be 'getter' or 'setter' functions.
A 'Setter' Function:
A setter function always updates/changes the state of a smart contract (i.e. updating/changing the values of any variables).
function setBookName(string memory bookName) public {
myBook = bookName;
}
In the above code, we are creating a function called setBookName
that accepts one parameter called bookName
that will always be of string
data type.
The function is publicly available (i.e. public
) and will update the value of myBook
to the new value that we are accepting, which is bookName
.
A 'Getter' Function:
A getter function is used for reading the state of the smart contract.
function getBookName() public view returns (string memory) {
return myBook;
}
In the above code, we are creating a function called getBookName
.
This function is publicly available (i.e. public
) and promises not to change the state of the blockchain (i.e. view
).
It always returns a string
data type, i.e. myBook
.
c) The Anatomy of a Smart Contract Function
function functionName(uint x, uint y) public view returns (uint) {
// write function body here
}
Begins with keyword
function
followed by the name of the function -functionName
.Has parameters which are the values that you are going to pass to the function when you are calling it. ('uint' is an abbreviation for 'unsigned integer' and signifies a number value)
public
- specifies the visibility of the function. In this case, it means that the function is accessible to other contracts.view
- the function promises not to modify the state of the blockchain.returns
- the function will return a value and also define the data type of the output.
LET'S GO!
1) Open Remix and create a new file in 'contracts'
You will see 'contracts' to the left of your browser window. Right click it and add a new file. Name this file 'multiply.sol'.
2) Add the license identifier
The license identifier should be your first line of code. It is needed so that developers can specify the license the contract uses. If it is not included, the compiler will show an error.
// SPDX-License-Identifier: GPL-3.0
3) Add the pragma version
This should be your second line of code.
The pragma directive tells us which version of the Solidity compiler we should use to compile this code.
pragma solidity >=0.7.0 <0.9.0;
4) Define a variable of type 'uint' and set it to zero
As mentioned earlier in this article,uint
is an abbreviation for unsigned integer. This is a number value that has no '-' or '+'.
First, we need to create a new smart contract by using the keyword contract
.
We will name it multiplyNumbers
.
We can then create a variable called total
that is of uint
data type.
We can also add the public
keyword to signify that the variable is publicly available.
contract multiplyNumbers {
uint total;
}
5) Write the 'getter and 'setter' functions to multiply the numbers
Create the getter and setter functions and place them in the contract as well.
Setter Function
function multiplyNums(uint x, uint y) public {
total = x * y;
}
Getter Function
function getTotal() public view returns (uint) {
return total;
}
6) Final Result!
Your code should now look like this:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract multiplyNumbers {
uint total;
function multiplyNums(uint x, uint y) public {
total = x * y;
}
function getTotal() public view returns (uint) {
return total;
}
}
7) Compile!
Click the third icon (rotating arrows) on the left side of your browser window. When you hover over it, it should say 'Solidity compiler'.
Then, click the blue compile button:
8) Deploy!
Click the 4th icon down the left side of your browser (under the rotating arrows).
Choose the Remix VM environment. This will give you some fake Ether to be able to deploy and test your smart contract.
Click the orange button that says deploy. This will create an instance of your contract that we can then test to see if it is working.
9) Test!
Go to Deployed Contracts in the bottom left corner of your browser window. Click the arrow on the left of it to open it.
Write two numbers into the multiplyNums box. Choose any numbers you wish! These could be 5 and 2.
Then, click the orange 'multiplyNums' button.
(An alternative to clicking the 'multiplyNums' button is to click the dropdown arrow on the right of the box where you wrote '5, 2'. This will bring up an orange button that says 'transact' that you can then click.)
This adds the two numbers, but doesn't return the total that we want. For this, we have to call our 'getter function'.
To do this this, click the blue button - getTotal.
You will see that a new log will appear in the bottom right of your screen with a blue debug button next to it.
Click the dropdown arrow to the right of the blue debug button to get more details about it.
Find where it says decoded output.
If we had just pushed the multiplyNums button, the decoded output would show as empty. But since we have now called the getter function, it should say 10!
If it does say 10... congratulations! 🥳
You've just created, compiled, deployed and tested your first Solidity smart contract in 10 minutes flat!
Comment 'Done!' on this post if you were able to do it 🥳
Follow me on Twitter for more updates about my progress with 30 Days of Web3!