Deploy Your First Smart Contract on Ethereum (2024)

Deploy Your First Smart Contract on Ethereum (2024)

Are you curious about how smart contracts work on the Ethereum network? While 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.

RemixIDE

https://remix.ethereum.org/ Captured on November 03, 2024

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

Step 1: Setting up Your Environment

Visit the website of Remix IDE and load the software.

Step 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. RemixIDE

Click on “Create new file” and name the file “CityPopulation.sol”. RemixIDE

Step 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.20; 

// 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;
    }   
}

RemixIDE

Step 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. RemixIDE

Step 5: Deploy the Contract on the Ethereum Network

Next, navigate to the “Deploy & Run Transactions” menu. RemixIDE

Click on the orange “Deploy” button. Then, verify whether a message similar to the one shown in a green box area. RemixIDE

Step 6: Review the Transaction

Click on “>” symbol to see a balance on a deployed contract. RemixIDE

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. RemixIDE

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. RemixIDE

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

(Optional) Load Contracts from the Address

Click on “copy” button to copy the address of the smart contract. RemixIDE

Paste the address into “At Address” box. Click on “At Address” to deploy a new smart contract which have the same address number. RemixIDE RemixIDE

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.


Conclusion

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: Interviwew 3 Pillars of Bitcoin: Science, Future, and Ethics