Skip to main content

Using Hardhat to Deploy Smart Contracts

This tutorial will guide you through the process of deploying a smart contract on Status Network's Ethereum Layer 2 using Hardhat and TypeScript.


Prerequisitesโ€‹

Before you begin, ensure you have the following:

  • Node.js and npm: Download and install from the official Node.js website.
  • Ethereum Wallet: A private key for the Status Network Testnet that has testnet ETH.
    • Obtain testnet ETH from the Status Network Testnet Faucet.
    • Important: Use a new wallet without real funds to ensure security in case the private key gets compromised.
  • Basic Knowledge: Familiarity with Solidity, Hardhat, and the command line is helpful but not mandatory.

What You'll Accomplishโ€‹

  • Initialize a TypeScript-based Hardhat project.
  • Write a basic Ethereum smart contract.
  • Configure Hardhat for Status Network Testnet deployment.
  • Deploy your smart contract on Status Network.

Stepsโ€‹

1. Initialize a Hardhat TypeScript Projectโ€‹

Open your terminal and create a new directory for your project, then navigate into it:

mkdir my-hardhat-project && cd my-hardhat-project

Initialize an npm project:

npm init -y

Install the necessary packages for Hardhat and TypeScript:

npm install --save-dev hardhat ts-node typescript @nomiclabs/hardhat-ethers ethers @typechain/ethers-v5 @typechain/hardhat typechain

Start a new Hardhat project with TypeScript:

npx hardhat

When prompted, make the following selections:

  • Choose "Create a TypeScript project".
  • For the .gitignore prompt, select "Yes".
  • For installing the project's dependencies, select "Yes".

Example interaction:

888    888                      888 888               888
888 888 888 888 888
888 888 888 888 888
8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888
888 888 88b 888P d88 888 888 88b 88b 888
888 888 .d888888 888 888 888 888 888 .d888888 888
888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.
888 888 Y888888 888 Y88888 888 888 Y888888 Y888

๐Ÿ‘ท Welcome to Hardhat v2.18.2 ๐Ÿ‘ทโ€

โœ” What do you want to do? ยท Create a TypeScript project
โœ” Hardhat project root: ยท /Users/username/my-hardhat-project
โœ” Do you want to add a .gitignore? (Y/n) ยท y
โœ” Do you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)? (Y/n) ยท y

2. Writing the Smart Contractโ€‹

In the contracts directory, delete the sample smart contract Lock.sol and create a new file named HelloWorld.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract HelloWorld {
string public greet = "Hello, Status Network!";

function setGreet(string memory _greet) public {
greet = _greet;
}

function getGreet() public view returns (string memory) {
return greet;
}
}

3. Configuring Hardhat for Status Networkโ€‹

Edit the hardhat.config.ts file to include Status Network Testnet settings:

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

const config: HardhatUserConfig = {
networks: {
statusTestnet: {
url: "https://testnet.status.network",
chainId: 2020,
accounts: ["YOUR_PRIVATE_KEY_HERE"], // BE VERY CAREFUL, DO NOT SHARE THIS
},
},
solidity: "0.8.24",
};

export default config;
  • Replace YOUR_PRIVATE_KEY_HERE with your Status Network Testnet private key (without quotes).

Important: Do not push your hardhat.config.ts file to any public repository or share your private key with anyone. To prevent accidental exposure, ensure your .gitignore file includes hardhat.config.ts.


4. Compilationโ€‹

Compile the smart contract:

npx hardhat compile

You should see output indicating that the compilation was successful.


5. Deploymentโ€‹

In the scripts directory, create a new file named deploy.ts:

import { ethers } from "hardhat";

async function main() {
const HelloWorld = await ethers.getContractFactory("HelloWorld");

const helloWorld = await HelloWorld.deploy();

await helloWorld.deployed();

console.log("HelloWorld deployed to:", helloWorld.address);
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

Note: If you encounter gas-related issues, you can specify gasPrice and gasLimit when deploying:

const gasPrice = ethers.utils.parseUnits('10', 'gwei'); // Adjust as needed
const gasLimit = 500000; // Adjust based on your contract

const helloWorld = await HelloWorld.deploy({ gasPrice, gasLimit });

Deploy the smart contract to the Status Network Testnet:โ€‹

npx hardhat run scripts/deploy.ts --network statusTestnet

Upon successful deployment, you should see an output similar to:

HelloWorld deployed to: 0xYourContractAddressHere

6. Verify Your Contract on the Block Explorerโ€‹

Visit the Status Network Testnet Explorer and search for your contract address to view its details.


7. Interacting with Your Smart Contractโ€‹

You can interact with your deployed contract using Hardhat scripts, tests, or via a frontend application.

Example: Interacting via a Scriptโ€‹

Create a new script interact.ts in the scripts directory:

import { ethers } from "hardhat";

async function main() {
const contractAddress = "0xYourContractAddressHere"; // Replace with your contract address

const HelloWorld = await ethers.getContractFactory("HelloWorld");
const helloWorld = HelloWorld.attach(contractAddress);

// Read the current greeting
const currentGreet = await helloWorld.getGreet();
console.log("Current Greet:", currentGreet);

// Update the greeting
const tx = await helloWorld.setGreet("Hello from Hardhat!");
await tx.wait();

// Read the updated greeting
const newGreet = await helloWorld.getGreet();
console.log("Updated Greet:", newGreet);
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

Run the script:

npx hardhat run scripts/interact.ts --network statusTestnet

Conclusionโ€‹

Congratulations! You've successfully deployed a smart contract on the Status Network Testnet using Hardhat and TypeScript.


Next Stepsโ€‹

  • Explore More Tutorials:

  • Deploy to Mainnet:

    • Once you're comfortable with the testnet, consider deploying to the Status Network Mainnet. Update your hardhat.config.ts with the mainnet RPC URL and Chain ID.
  • Secure Your Private Keys:

    • Use environment variables to store your private keys securely.

    • Install dotenv package:

      npm install dotenv
    • Update your hardhat.config.ts:

      import { HardhatUserConfig } from "hardhat/config";
      import "@nomicfoundation/hardhat-toolbox";
      import * as dotenv from "dotenv";

      dotenv.config();

      const config: HardhatUserConfig = {
      networks: {
      statusTestnet: {
      url: "https://testnet.status.network",
      chainId: 2020,
      accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
      },
      },
      solidity: "0.8.24",
      };

      export default config;
    • Create a .env file in the root of your project:

      PRIVATE_KEY=your_private_key_without_quotes
    • Update your .gitignore to include .env.


Resourcesโ€‹


Need Help?

If you encounter any issues or have questions, feel free to reach out:


Happy Coding!