Skip to main content
A smart contract is code stored on the blockchain. When the conditions written into it are met, it runs without an intermediary. This guide shows you how to write a contract in Solidity and set it up in Remix IDE, Hardhat, or Foundry. The next two guides cover deploying the contract to peaq and interacting with it.

Prerequisites

Remix

Remix is a web-based IDE built for Solidity. It compiles contracts, deploys them, and lets you call functions on contracts that are already deployed, all in the browser. It is the fastest way to get started.

1. Open Remix IDE

Head over to the Remix webpage.

2. Create a new File

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

3. Write the Code

Here we will create a simple contract that stores and retrieves a number. Copy the following code into the file you just created.

Understanding the code

1. File License Declaration: // SPDX-License-Identifier: MIT
  • Specifies the license under which the code is distributed, in this case MIT. It is important for open-source compliance and avoids compilation warnings.
2. Pragma Directive: pragma solidity ^0.8.0;
  • Specifies the version of solidity that is required for the code. The ^ symbol indicates compatibility with versions 0.8.0 and above (up to but not including 0.9.0).
3. Contract Declaration: contract SimpleStorage { … }
  • A contract is similar to a class in typical object oriented programming paradigms. It encapsulates state variables and functions. The contract name SimpleStorage is defined by the user and should represent its purpose.
4. State Variable: uint256 private data;
  • State variables are stored directly on the blockchain, allowing for persistence and global access. 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 { … }
  • Functions are used in Solidity to interact with the contract. The 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 contract. By convention these variables start with an underscore.
6. Get Function function get() public view returns (uint256) { … }
  • Getter function that returns the value of the variable 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, you have a contract that stores an integer on a blockchain. For more complex examples of what the Solidity programming language can do, read the Solidity documentation. The next two sections set up the same contract in a local Hardhat or Foundry project. Deployment and interaction are covered in the guides that follow.

Hardhat

Hardhat is a local development environment for Ethereum. It compiles, tests, deploys, and interacts with contracts from your own machine, and gives you more configurability than Remix. To learn more, read the Hardhat 2 documentation.

Install & Initialize Hardhat

1. Install Hardhat

  • Create a New Repository and Install Hardhat:
    • Open your terminal and navigate to your project directory.
    • Run the following command to install Hardhat:

2. Initialize Hardhat

  • Initialize a new Hardhat project using:
  • Select Create an empty hardhat.config.js when prompted.

3. Install Hardhat Toolbox

  • Install the toolbox dependency for testing, contract interaction, and deployment:

4. Set Up Project Structure

  • Organize your project with the following standard folder structure:

Configure .env and Hardhat

1. Create .env File

  • In the root directory, create a .env file to store sensitive data.
  • Install dotenv to enable environment variable support:
  • Add the following variables to the .env file:
  • Replace rpc_url with the RPC URL of the peaq/agung test network.
  • Replace 0x... with your wallet’s private key, including the 0x prefix.

2. Update hardhat.config.js

  • Update the hardhat.config.js file to include the network configuration
  • Networks added here are used in the deployment script so it knows what chain to send the bytecode to. The example below shows the test network agung.

Create the SimpleStorage contract

Inside the contracts/ folder, create a file named SimpleStorage.sol and paste in the same contract you wrote in the Remix section above.

Foundry

Foundry is a Solidity toolchain that compiles, tests, and deploys from the command line. These steps set it up for peaq and agung.

1. Install Foundry

2. Initialize a project

3. Configure foundry.toml

Create or update foundry.toml in the project root:
london is a conservative target that always works. peaq’s EVM runs the Cancun configuration, so cancun compiles and deploys fine too.

4. Add the example contract

Create src/SimpleStorage.sol and paste the same contract used above:

5. Compile

forge init also generates an example contract with its own script and test. Delete those so forge build only compiles SimpleStorage.sol. You now have the contract written in every environment you need. The next guide shows you how to deploy it.