Deploy Your First Smart Contract on Ethereum
A Guide for Newcomers into the Blockchain Technology
--
Are you curious about how smart contracts work on the Ethereum network? Although smart contracts are a fundamental concept on the blockchain, fully comprehending their potential may be difficult without practical experience.
Here we will explore how to deploy our first smart contract on the Ethereum network using Remix IDE. As the saying goes, “Practice makes perfect,” you will gain a better understanding of how actual smart contracts exist on the blockchain.
Introduction
What is Smart Contract?
The term “smart contract” refers to a computerized protocol that can negotiate, verify and automate the performance of a contract. Smart contracts can work as computer programs to help people make and keep promises with each other without cheating. Theoretically.
Smart contracts are self-executing agreements operating on the blockchain and used for a wide range of applications:
- Token Sales (e.g., ETH, NFT art)
- Insurance
- Loans and Mortgages
- Supply Chain Management
- Crowdfunding
What is Remix IDE?
Remix IDE is a free web-based integrated development environment (IDE) for writing, testing, and deploying smart contracts on the Ethereum. Remix IDE makes it easy to write and test Solidity code online. It also includes a built-in compiler and a JavaScript virtual machine, allowing to test smart contracts directly in the browser.
What is Solidity?
Solidity is a high-level programming language designed for developing smart contracts on the Ethereum. Solidity is similar to other C-like programming languages, such as C++, Java, and JavaScript. Solidity code is compiled into bytecode and executed on the Ethereum Virtual Machine (EVM).
Creating Your First Smart Contract
1. Setting up Your Environment
Visit the website of Remix IDE and load the software.
2. Write a Smart Contract in Remix IDE
Choose “File explorer” from the menu on the left-hand side to display the files and folders in your project.
Click on “Create new file” and name the file “CityPopulation.sol”.
3. Write a Smart Contract in the Editor
Copy the code below and paste it into the Code Editor. This example contract is utilized to store and manage population data of a city on the Ethereum blockchain.
Caution - It’s recommended to use well-audited and trusted libraries and frameworks for smart contract development. Do NOT copy and paste codes from unreliable sources to avoid scams.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Information on the population of a city all people can share
contract CityPopulation{
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() view public returns (uint) {
return storedData;
}
function increment (uint n) public {
storedData = storedData + n;
return;
}
function decrement (uint n) public {
storedData = storedData - n;
return;
}
}
4. Compile the Code to Check for Errors
Go to the “Solidity Compiler” menu and click the “Compile” button to compile the “CityPopulation.sol” file.
5. Deploying the Contract on the Ethereum Network
Next, navigate to the “Deploy & Run Transactions” menu.
Click on the orange “Deploy” button.
Verify whether a message similar to the one shown in a green box area.
6. Review the Transaction
Click on “>” symbol to see a balance on a deployed contract.
Set the value to “100,000” and click on “set”, then click on “get” to demonstrate that a particular city has a population of 100,000.
Then, suppose the population increases by 10. Input “10” into “increment” and click on “increment” button. Then click on “get” to receive the calculated value. Perform the same steps for the decrement function as well.
7. Functions
Getter and setter are important to control variables within smart contracts. The contract provides four fundamental functions:
- “Set” function allows anyone to set the value of “storedData” by providing a value as a parameter
- “Get” function returns the current value of “storedData”
- “Increment” function increases the value of “storedData” by taking a uint value as a parameter
- “Decrement” function decreases the value of “storedData” by taking a uint value as a parameter
8. (Optional) Contracts with the Same Address
Click on “copy” button to copy the address of the smart contract.
Paste the address into “At Address” box. Click on “At Address” to deploy a new smart contract which have the same address number.
Check the two smart contracts at the same address exist. Confirm that the same balance value can be obtained by clicking “get” button regardless of which contract is called as long as the address remains the same. Here smart contracts can be executed on all the full nodes and visible to all participants on the chain.
Implementing a simple smart contract allows continuous monitoring in transactions. It could be analogous to how one’s account balance is tracked.
Understanding the fundamentals of smart contracts is beneficial also for non-technical persons. Smart contracts provide more efficient and secure ways of conducting transactions in various industries.
Also read: Create Your First DAO Using Aragon in 10 Minutes | Medium
Author
Miyoko Shimura has over 8 years of engineering experience in the technology industry bringing together analytical perspectives.
DLT Mentor at German think-tank Frankfurt School Blockchain Center and Technical Editor at Coinmonks. For further updates please follow me on LinkedIn or Twitter 🌏
Portfolio https://linktr.ee/miyokoshimura