Gauss Docs
  • 🌐Getting Started
    • Welcome to Gauss
    • Whitepaper
      • Blockchain Providers Need to Do Better
        • Solving a Lack of Token Adoption
          • An Evolving Space With Untapped Potential
        • Security & Reliability
          • Curation & Vetting
            • Important Note For Brands
        • Token Creation
      • WHY BUILD WITH GAUSS?
      • Use Cases
        • Use Cases (Chart)
      • Roadmap
      • Technical Background and Blockchain Development
        • Why Another Blockchain?
        • Gauss 1.0: Built With Efficiency and a Strong Infrastructure to Launch Rapidly
        • Gauss 2.0: Added Functionality For a Better User Experiance
          • Noble Swap 2.0
          • NFTs in Gauss 2.0
          • Token Development Kit (TDK)
          • Gaming DAO
          • Omnipool
      • Token Economics
        • Gang Token Economics: Designed to Ensure Trust and Transparency
        • Token Locking Schedule
        • Reflections: Rewarding the Gauss Community
        • Charitable Allocations: Grants, Scholarships, & Financial Assistance
      • The Gauss Team
      • Important Definitions
      • DISCLAIMER
        • PURCHASER WARNING
        • PROMINENT STATEMENTS
        • FUTURE STATEMENTS
        • VALUE RISKS
        • NOT A SECURITY
    • How To Connect
      • Create Metamask Wallet
    • Links
  • ⚑Launching with Gauss
    • Benefits of Building with Gauss
      • Fostering an Environment for Success
      • Gauss Growth Grant Program
      • Gauss Liquidity Program
      • Ecosystem Integrity Fund
      • Client Referral Program
    • A Guide to Curation
      • Core Principles and Curation Guidelines
      • Curation Stages and Processing Fees
    • Building on Gauss
  • πŸ–₯️Gauss Ecosystem
    • Gauss Chain
      • Polygon-Edge Overview
      • Architecture
      • Consensus
      • Client Modules
        • Blockchain
        • Minimal
        • Networking
        • State
        • TxPool
        • JSON RPC
        • Consensus
        • Storage
        • Types
        • Syncer
        • Sealer
        • Other Modules
      • Polygon-Edge Performance Reports
      • For Developers
        • Operate a Node
          • Local Install
          • Genesis Contract
          • Server Config
          • CLI Commands
          • Key Management
        • Run a Validator
          • Validator FAQ
        • Smart Contract Deployment Permissioning
        • Deploying Contracts
          • Remix
          • Truffle
          • Hardhat
          • Replit
    • Gauss Explorer
      • Features
      • Navigating the Explorer
        • Menus
        • Blocks
        • Transactions
      • Verifying a Smart Contract
        • Hardhat Plugin
        • Sourcify Plugin
        • OpenZeppelin Plugin
      • Interacting With Smart Contracts
      • Exporting Transactions
      • FAQ
      • For Developers
        • Gauss Explorer Dependencies
        • Deployment Guide
          • Smart Contract Verification
          • Cleaning an instance from the previous deployment
          • ENV Variables
          • Testing
        • APIs
          • Requests & Limits
          • GraphQL
          • ETH RPC
    • Noble Swap
      • Liquidity Boost Program
    • Tokens
    • Gauss NFTs
      • Ferro Cards
      • F.E.R.R.E.T. NFTs
    • Contests & Giveaways
    • Gauss Faucet
      • For Developers
    • Address List
  • πŸ’‘Additional Resources
    • Partnerships & Affiliates
    • Discord Channel
    • Contact Us
    • Learning Materials
      • Web3 Glossary
    • Media Kit
Powered by GitBook
On this page
  • Overview
  • Setting up the development environment
  • Creating a project
  1. Gauss Ecosystem
  2. Gauss Chain
  3. For Developers
  4. Deploying Contracts

Hardhat

PreviousTruffleNextReplit

Last updated 2 years ago

Overview

Hardhat is an Ethereum development environment that provides an easy way to deploy smart contracts, run tests and debug Solidity code locally.

In this tutorial, you will learn how to set up Hardhat and use it to build, test and deploy a simple smart contract.

What you will do

  • Set up Hardhat

  • Create a simple smart contract

  • Compile contract

  • Test contract

  • Deploy contract

Setting up the development environment

There are a few technical requirements before we start. Please install the following:

  • (comes with Node)

Once we have those installed, you need to create an npm project by going to an empty folder, running npm init, and following its instructions to install Hardhat. Once your project is ready, you should run:

npm install --save-dev hardhat

To create your Hardhat project, run npx hardhat in your project folder. Let’s create the sample project and go through these steps to try out a sample task and compile, test and deploy the sample contract.

Creating a project

To create a sample project, run npx hardhat in your project folder. You should see the following prompt

$ npx hardhat
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.9.9 πŸ‘·β€

? What do you want to do? …
❯ Create a JavaScript project
  Create a TypeScript project
  Create an empty hardhat.config.js
  Quit

Choose the JavaScript project and go through these steps to compile, test and deploy the sample contract.

Checking the contract

The contracts folder contains Lock.sol, which is a sample contract which consistis of a simple digital lock, where users could only withdraw funds after a given period of time.

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

// Import this file to use console.log
import "hardhat/console.sol";

contract Lock {
    uint public unlockTime;
    address payable public owner;

    event Withdrawal(uint amount, uint when);

    constructor(uint _unlockTime) payable {
        require(
            block.timestamp < _unlockTime,
            "Unlock time should be in the future"
        );

        unlockTime = _unlockTime;
        owner = payable(msg.sender);
    }

    function withdraw() public {
        // Uncomment this line to print a log in your terminal
        // console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

        require(block.timestamp >= unlockTime, "You can't withdraw yet");
        require(msg.sender == owner, "You aren't the owner");

        emit Withdrawal(address(this).balance, block.timestamp);

        owner.transfer(address(this).balance);
    }
}

Setting up the contract

  • Go to hardhat.config.js

  • Update the hardhat-config with matic-network-credentials

  • Create .env file in the root to store your private key

require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
require("@nomiclabs/hardhat-etherscan");

module.exports = {
  defaultNetwork: "gil-Testnet",
  networks: {
    hardhat: {
    },
    gil-Testnet: {
      url: "https://rpc.giltestnet.com",
      chainId: 1452,
      gasPrice: 20000000000,
      accounts: [process.env.PRIVATE_KEY]
    }
  },
  etherscan: {
    apiKey: {
      gil: "abc"
    },
    customChains: [
      {
        network: "gil",
        chainId: 1452,
        urls: {
          apiURL: "http://explorer.giltestnet.com/api",
          browserURL: "https://explorer.giltestnet.com/"
        }
      }
    ]
  },
  solidity: {
    version: "0.8.9",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
}

Note that the file above requires DOTENV, for managing environment variables and also ethers and etherscan. Make sure to install all those packages.

Compiling the contract

To compile the contract, you first need to install Hardhat Toolbox:

npm install --save-dev @nomicfoundation/hardhat-toolbox

Then, simply run to compile:

npx hardhat compile

Testing the Contract

To run tests with Hardhat, you just need to type the following:

npx hardhat test

Deploying on G.I.L. Testnet

Run this command in root of the project directory:

npx hardhat run scripts/deploy.js --network gil-testnet

Congratulations! You have successfully deployed Greeter Smart Contract. Now you can interact with the Smart Contract.

Quickly Verify contracts on G.I.L. Explorer

npm install --save-dev @nomiclabs/hardhat-etherscan
npx hardhat verify --network gil-testnet "CONTRACT_ADDRESS"

The sample project used here comes from the , as well as its instructions.:::

Add Polygonscan API key to .env file to verify the contract on Polygonscan. You can generate an API key by

Find more instructions on how to use DOTENV on .

The contract will be deployed on Gauss Induction Labs Testnet, and you can check the deployment status here:

Run the following commands to quickly verify your contract on the G.I.L. Explorer. This makes it easy for anyone to see the source code of your deployed contract. For contracts that have a constructor with a complex argument list, see .

πŸ–₯️
Node.js v10+ LTS and npm
Git
Hardhat Quickstart guide
creating an account
this page
https://explorer.giltestnet.com
here