Shardeum Documentation

Using Hardhat

Overview

This guide is a step-by-step reference to create and deploy a SimpleStorage contract on Shardeum using Hardhat.

Deployment Guide

Prerequisites

Step 1: Initialize a Hardhat Project

mkdir shardeum-hardhat
cd shardeum-hardhat
npm init -y
npm install --save-dev hardhat
npx hardhat

When prompted, select:Create an empty hardhat.config.js

Create project folders:

mkdir contracts scripts

Step 2: Install Dependencies

npm install --save-dev @nomicfoundation/hardhat-ethers ethers dotenv

Step 3: Add Environment Variables

Create a .env file in the project root:

SHARDEUM_RPC=YOUR_SHARDEUM_RPC_URL
PRIVATE_KEY=YOUR_WALLET_PRIVATE_KEY

Security note:

  • Never commit .env
  • Add .env to .gitignore

Step 4: Write the SimpleStorage Contract

Create contracts/SimpleStorage.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
 
contract SimpleStorage {
    uint256 public value;
 
    event ValueUpdated(uint256 newValue);
 
    constructor(uint256 _value) {
        value = _value;
        emit ValueUpdated(_value);
    }
 
    function setValue(uint256 _value) external {
        value = _value;
        emit ValueUpdated(_value);
    }
}

Step 5: Configure Hardhat

Replace hardhat.config.js with:

require("dotenv").config();
require("@nomicfoundation/hardhat-ethers");
 
module.exports = {
  solidity: "0.8.20",
  networks: {
    shardeum: {
      url: process.env.SHARDEUM_RPC,
      accounts: [process.env.PRIVATE_KEY],
      // chainId: <YOUR_CHAIN_ID>
    },
  },
};

Chain IDs differ across Shardeum networks (testnet vs mainnet). Use the chain ID for the specific network you’re deploying to

Step 6: Compile

npx hardhat compile

Step 7: Create a Deploy Script

Create scripts/deploy.js:

const hre = require("hardhat");
 
async function main() {
  const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage");
 
  // constructor arg for SimpleStorage(uint256 _value)
  const contract = await SimpleStorage.deploy(1);
 
  await contract.waitForDeployment();
 
  console.log("SimpleStorage deployed to:", await contract.getAddress());
}
 
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Step 8: Deploy to Shardeum

npx hardhat run scripts/deploy.js --network shardeum

Copy the deployed contract address and view it on Shardeum explorers

(Optional) Interact Quickly

You can interact via Hardhat console:

npx hardhat console --network shardeum

Then:

const c = await ethers.getContractAt("SimpleStorage", "<CONTRACT_ADDRESS>");
await c.value();
await c.setValue(42);
await c.value();

On this page