Welcome to this comprehensive guide on deploying your first smart contract on an EVM-compatible blockchain network. A smart contract is a self-executing piece of code stored on the blockchain, where the contract’s terms are directly written into code. This means that when the predefined conditions are met, the contract automatically executes without the need for intermediaries, ensuring secure, transparent, and trustless interactions.
In this tutorial, you’ll learn how to craft a smart contract using the Solidity
language and deploy using Remix IDE or Hardhat. By understanding the inner workings of these
contracts and how they operate on a decentralized network, you’ll be well-equipped to build robust and efficient applications. By the end of this guide,
you’ll have developed a fully functional smart contract, ready for deployment on the peaq network.
The most simple way to write a smart contract is through the web-based IDE (Integrated Development Environment) called Remix. The software has been specifically tailored to the writing, development, and deployment of Solidity contracts. It contains certain features such as smart contract compilation and interaction with previously deployed contracts. Remix is the perfect platform for quick plug and play to get started with blockchain development.
Head over to the Remix webpage.
In the left-hand file explorer panel, click on the New File icon to generate a new file where the smart contract code will be written. Name the file SimpleStorage.sol
(.sol
being the standard extension for Solidity code).
Here we will create a simple contract that stores and retrieves a number. Please copy and paste the following code in the file that you just created.
1. File License Declaration: // SPDX-License-Identifier: MIT
2. Pragma Directive: pragma solidity ^0.8.0;
^
symbol indicates compatibility with versions 0.8.0 and above (up to but not including 0.9.0).3. Contract Declaration: contract SimpleStorage { … }
4. State Variable: uint256 private data;
uint256
represents the variable type. In this particular case it is an unsigned
integer that can hold large non-negative numbers. The identifier private
restricts the access of the variable within the contract only.5. Set Function function set(uint256 _data) public { … }
set
function here allows users to update the value of data
. The keyword public
means that the function is
accessible to everyone (any address can call it). The keyword parameter _data
is used to represent the data a user sent to the contact. By convention these variables start with an underscore.6. Get Function function get() public view returns (uint256) { … }
data
that is stored on the blockchain. The keyword view
is used to restrict the function to be read-only so it does not modify the
state of the chain. returns
specifies what type of value the function will return to the user.With that example, we now have a simple contract that stores an integer on a blockchain. For more complex examples and explanations what you can do with the Solidity programming language please look at their documentation.
The following two sections will show you how to setup a local JavaScript and Python environment to write smart contracts. The deployment and interaction of the written contract is done in the subsequent sections.
Remix is not the only tool you can use to deploy your smart contracts. A popular tool used in local Ethereum development is Hardhat. It allows for the development, compilation, and interaction of smart contracts on a local IDE. Similar to Remix in many ways, Hardhat does allow for more flexibility and configurability. If you would like to learn more please read about Hardhat.
.env
File.env
file to store sensitive data..env
file:rpc_url
with the RPC URL of the peaq/agung test network.wallet_private_key
with your wallet’s private key.hardhat.config.js
file to include the network configurationcontracts/
folder, create a file named SimpleStorage.sol
.You have successfully created 2 separate environments that were used to write a smart contract. Please follow the next pages to see how to deploy and interact with the contract written on this page.