Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They run on blockchain platforms like Ethereum.
This enables decentralized applications (DApps) to function without intermediaries. Solidity is the programming language used to write these contracts.
Here, explore how to build smart contract using Solidity.
Before starting, ensure to have a few essential tools and a basic understanding of programming concepts. Here’s a list of the prerequisites that an individual needs:
a. Basic Programming Knowledge: Familiarity with fundamental programming concepts such as variables, functions, and control structures is essential.
b. Node.js and npm: These tools are necessary for managing the dependencies needed for smart contract development.
c. MetaMask: A browser extension that allows to interact with the Ethereum blockchain.
d. Remix IDE: An online integrated development environment specifically designed for writing, compiling, and deploying smart contracts.
The first step in smart contract development is to set up the environment. Here’s how one can do it:
Node.js is a JavaScript runtime that enables developers to run JavaScript code outside of a browser, while npm is a package manager that helps in managing project dependencies.
a. Visit the official Node.js website at nodejs.org.
b. Download and install the latest version of Node.js. npm. It is bundled with Node.js, so that one don’t need to install it separately.
c. To verify the installation, open the terminal or command prompt and type:
node -v
npm -v
MetaMask is a popular browser extension that allows you to interact with the Ethereum blockchain. Use MetaMask to deploy and manage your smart contract.
a. Go to the official MetaMask website at metamask.io.
b. Download the extension for the browser and follow the setup instructions to create a new wallet.
c. After setting up your wallet, ensure that it is connected to the Ethereum network. This is required for deploying smart contract.
The smart contract will have two main functions, including one for storing a value and another for retrieving it.
Remix IDE is an online tool designed specifically for Solidity development.
It’s an all-in-one platform for writing, compiling, and deploying smart contracts.
a. Open the browser and visit remix.ethereum.org.
b. Remix IDE has a file explorer, a Solidity compiler, and a deployment manager. It’s a user-friendly environment for smart contract development.
In the file explorer panel, create a new file for the smart contract and name the file as SimpleStorage.sol.
Now, it’s time to write the Solidity code for the smart contract.
In the file created, paste the following code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private data;
function set(uint256 _data) public {
data = _data;
}
function get() public view returns (uint256) {
return data;
}
}
Let’s break down the code:
a. The contract is named SimpleStorage.
b. It contains a single state variable data, which is of type uint256 (an unsigned integer).
c. The set function allows users to store a value in data.
d. The get function is a view function, meaning it only reads the data and doesn’t modify the blockchain state. It returns the value stored in data.
This is a very simple smart contract, but it lays the foundation for more complex contracts.
Once the contract is written, the next step is to compile it to ensure there are no syntax errors.
In the Remix IDE, navigate to the “Solidity Compiler” tab located on the left side of the interface.
An individual will need to choose the appropriate compiler version that matches the code. In this case, select version 0.8.0.
Click the “Compile SimpleStorage.sol” button. If the code is correct, one can see a success message indicating that the contract compiled without errors.
If there are any errors, Remix will display them, and an individual can correct the issues before trying to compile again.
Now that the smart contract is written and compiled, it’s time to deploy it on the Ethereum blockchain.
Go to the “Deploy & Run Transactions” tab in Remix IDE.
Here, one will find several options for deploying the contract.
Select “Injected Web3” as the environment, which will connect Remix to the MetaMask account.
Once the environment is set, click the “Deploy” button. MetaMask will open a popup asking one to confirm the transaction.
Confirm it, and the smart contract will be deployed to the Ethereum network.
An individual will be able to see the transaction details and contract address in MetaMask once the deployment is successful.
After deploying the smart contract, one can start interacting with it through the Remix IDE interface.
In the “Deployed Contracts” section of Remix, one see the deployed contract. Expand the interface to view the available functions.
To store a value in the contract, use the set function. Enter a value in the input field and click the “transact” button.
MetaMask will again ask an individual to confirm the transaction. Once confirmed, the value will be stored in the smart contract.
To retrieve the stored value, use the get function. Click the “call” button, and Remix will display the value currently stored in the contract.
Building the first smart contract using Solidity is a significant step toward understanding blockchain technology and decentralized applications.
This guide will help the user learn how to set up their environment, write, compile, deploy, and interact with a simple smart contract.
An individual can also explore more advanced features and create complex DApps that use the power of blockchain.