What is a Smart Contract?
Writing and Deploying a smart Contract using Solidity
In this article, we are going to learn about smart contracts, writing and deploying a smart contract. “Smart contracts” is one of the buzzwords spreading fast in the Web3 ecosystem, it is the soul of Web3.
This is one of the topics that got me excited during the 4week Prep-Phase of the Web3Ladies Mentorship program. If you've come across this term and you do not yet understand what it means “Fear not”, here you are going to learn about what smart contracts are, write and deploy a simple smart contract that greets you.
Let’s goooooo 🚀🚀🚀
Prerequisite
Remix IDE (Integrated Developer Environment) is an online visual IDE for composing solidity smart contracts and Dapp agents. You can also use IDEs like Atom, VSCode, etc to write solidity smart contracts.
What are Smart contracts?
Smart Contracts are small computer programs that are stored and executed on the Ethereum blockchain network. It is a piece of code that can be executed automatically in a deterministic way - This means that they behave exactly as programmed and cannot be changed.
Smart contracts digitize agreements by turning the terms of an agreement into computer code that automatically executes when the contract terms are met.
Let’s take for example when you get insurance for your car and it gets into an accident, you will have to take some documents to the insurance company and start the process of getting the insurance money on your car. This process might drag on for months or years and it can be stressful. But if the insurance was done based on a smart contract code, the smart contract reads through the conditions programmed into it and tries to match it with the information you provided, and if it corresponds it executes the code, and in a matter of minutes you would be getting that insurance money🤑
Importance Of Smart Contracts
Smart contracts are permissionless, trustless, and Immutable.
Permissionless
This means that anyone can write and deploy a smart contract. Two developer-friendly languages that can be used to write smart contracts on the Ethereum blockchain network are Solidity and Vyper.
Trustless
This means that there is no need for a third party or middleman. The conditions are both evaluated and executed by a computer code
Immutable
Once a smart contract code is deployed, it cannot be changed because it is stored on the blockchain.
Writing Our Smart Contract
Now we understand what smart contracts are and why we need a smart contract, it's time to write our smart contract. In this article, we are going to create a simple smart contract that greets the user by their name when the user inputs their name.
Step 1: Writing our Solidity code
Navigate to your Remix IDE, this will set up a default workspace that contains a contract, scripts, and tests folder.
In your contracts folder create a file Greeter.sol
Add the code below at the very top of your Greeter.sol
file
//SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
The SPDX-License-Identifier
helps define the license of our file. In this tutorial, we wil be using the MIT Opensource license.
The pragma solidity >=0.7.0 <0.9.0
line tells the compiler which version of Solidity it will use when compiling our smart contract. In this tutorial, our smart contract can be compiled with a solidity version that is greater than or equal to version 0.7.0 and less than 0.9.0
Now we define our contract using the contract
keyword. It is advised that your contract name and your file name are the same.
contract Greeter {
}
Declare the state variables inside our Greeter
contract called name
and prefix
with a type
of string
and function visibility of public
, the prefix
variable should take in a string of “Hello ”
. State variables are stored on the blockchain and they can be called multiple times in our smart contract.
string public name;
string public prefix = "Hello ";
Still, in our Greeter
contract, we write a “setter” function to set the name
variable. The function will have a name of setName
and an argument that will take in a type
of string and function visibility of public
.
function setName(string memory newName) public{
name = newName;
}
Declaring a “setter” function in solidity, we use the keyword “function”, followed by the “function name” and “function arguments.”
Finally, we will write a corresponding “getter” function with function visibility of public
, a keyword view
which specifies that the function will only 'read' data on the blockchain and returns
the value type
of string
A getter function doesn't take any arguments as it's only returning some information from the smart contract.
function getGreeting() public view returns (string memory) {
return string(abi.encodePacked(prefix, name));
}
In the code snippet above we used a function abi.encodePacked
to concatenate the two state variables that we defined - prefix
and name
.
String concatenation in Solidity is NOT straightforward as in other Object Oriented programming languages.
Our final code should look like this
//SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract Greeter {
string public prefix = "Hello ";
string public name;
function setName(string memory newName) public{
name = newName;
}
function getGreeting() public view returns (string memory) {
return string(abi.encodePacked(prefix, name));
}
}
Wow! You just wrote a smart contract all by yourself. You are crushing this
Step 2: Compiling our code
First, click on the “Solidity Compiler icon” on the left pane and ensure that the compiler version is set to a version within the version range specified in our code >=0.7.0 <0.9.0
.
Click the “Compile Greeter.sol” button to compile the code. If the compilation was successful you would see a “green” tick next to the “Solidity Compiler icon”.
Step 3: Deploy our Smart contract
Click on the “Deploy and Run Transaction icon”. We will be deploying our smart contract on a test network.
- Under the “Environment” section, select “Remix VM.
- Under the “Account” section, select the address where you want to deploy the smart contract.
- Click on Deploy Do not worry about the other options like “Gas Limit” and “Wei.” After clicking the Deploy button, scroll down to see your deployed contract. Click on it to see a dropdown of your deployed contract.
- In the
setName
field, type in the name you want to greet. - Click on
setName
- After that, if you click on the
getGreet
function it will return a string of the greeting you want - Also if you click on the name function it returns the name of the user that you set it to.
Conclusion
If you have come this far, congratulations! You’ve just written, compiled, and deployed a smart contract. Have a slice of pizza and enjoy yourself. You took up a challenge and completed it, you deserve it.Resources and Further Reading
- https://ethereum.org/en/smart-contracts/
- https://ethereum.org/en/developers/docs/smart-contracts/
- Smart Contracts: The Blockchain Technology That Will Replace Lawyers
- Smart contracts for developers
- An insurance policy that pays out automatically
- Learn to write smart-contracts
- Mastering Ethereum - What is a Smart Contract?