KCC Documents
  • Quickstart
  • Overview
    • Introduction
    • Features
    • Our Goal
    • Consensus Engine
    • Marketing Guide
      • Requirements
      • KCC Official Website Resource
      • KCC Social Media Promotion
      • PR & Third-Party Marketing Support
  • Developers
    • Network Endpoints
    • Deploy Smart Contract
      • Using Hardhat
      • Using Remix
    • Verify Smart Contract
      • Using Hardhat
      • Using Foundry
      • Using Remix
    • Issue ERC20 Token
    • Deploy NFTs
      • Create A Foundry Project
      • Create Your ERC721 Contract
      • Add Metadata
    • Run a Node
      • Hardware & System Requirements
      • Install KCC
      • Run A Full Node
      • Run A Validator Node
        • Block Rewards
        • Apply For Running A Validator
        • Manager Account And Validator Account
        • Run A Validator
      • Troubleshooting
    • Explorer
    • Oracles
      • KCC Oracle
    • Bridge
    • Dev Toolkit
    • Data
      • Indexing and Querying
        • The Graph
        • Using KCC's hosted subgraph
    • Gas Revenue Program
      • Rules for Calculation of Gas Revenue
      • How to Join the KCC Gas Revenue Program
  • Individuals
    • Wallet
      • Supported Wallet
      • Tutorial on how to set up wallet
      • Mutisig Wallet
        • Create New Safe
        • Send KCS From Your Safe
        • Use A DApp With Your Safe
    • Network Configuration
      • Configure Value
      • Tutorial on how to config KCC network in Metamask
      • Tutorial on how to config KCC network in Chainlist
    • Bridge Assets
      • Bridge assets from others chains
      • KCC Bridge
      • Bridge assets form Exchange
    • Find a dApp
      • Add Your dAPP
    • KCS Token
      • Get KCS
      • Stake KCS
        • How to participate in KCS Staking
        • How to redeem staked KCS
        • How to check or claim staking rewards
  • FAQs
    • FAQs
      • General FAQs
      • KCS and Staking FAQs
      • Validator FAQs
  • Future Developments
    • Milestone
  • CONTACT US
    • Contact Us
  • Disclosure
    • Disclaimers
    • Risk Statement
    • Media Kit
Powered by GitBook
On this page
  • Overview
  • What you will do​
  • Setting up the development environment
  • Pre-requisites​
  • Creating a project
  • Checking the Contract
  • Configure Hardhat for KCC
  • Compiling the contract
  • Testing the Contract
  • Deploying on KCC
  1. Developers
  2. Deploy Smart Contract

Using Hardhat

PreviousDeploy Smart ContractNextUsing Remix

Last updated 2 years ago

Overview

This tutorial will teach you how to create, compile and deploy a smart contract on the KCC Testnet using Hardhat.

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

There are a few technical requirements before we start, as listed below:

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

npm install --save-dev hardhat

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

Note

Creating a project

In your project folder, run npx hardhat to create a sample project. You should see the following prompt:

Choose the TypeScript project and The following prompt should appear:

Checking the Contract

A sample contract in the contracts folder is Lock.sol, which consists of a simple digital lock that allows users to withdraw funds only after a certain time has passed.

// 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);
    }
}

Configure Hardhat for KCC

  • Go to hardhat.config.ts

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

Note

Use the KCC Explorer API key to verify the contract is only for KCC mainnet

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

dotenv.config();

const config: HardhatUserConfig = {
    solidity: {
        compilers:[
            {
                version: "0.8.9",
                settings: {
                    optimizer: {
                        enabled: true,
                        runs: 200,
                    },
                },
            },
            {
                version: "0.4.18",
            }
        ]
    },

    networks: {
        ropsten: {
            url: process.env.ROPSTEN_URL || "",
            accounts:
                process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
        },
        testnet: {
            url: "https://rpc-testnet.kcc.network",
            chainId: 322,
            accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],

        },
        mainnet: {
            url: "https://rpc-mainnet.kcc.network",
            chainId: 321,
            accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],

        },

        hardhat: {
            allowUnlimitedContractSize: true,
        },
    },
    etherscan: {
        apiKey: process.env.ETHERSCAN_API_KEY,
    },
};

export default config;

NOTE

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

Your deployment contract account's private key is located in the .env file.

Compiling the contract

To compile a Hardhat project, change to the root of the directory where the project is located and you first need to install Hardhat Toolbox:

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

And then run to compile:

npx hardhat compile

Testing the Contract

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

npx hardhat test

And this is an expected output:

Deploying on KCC

Run this command in the root of the project directory:

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

Note

If you want to deploy your smart contract to KCC Mainnet, at the Deploying stage, you need to use mainnet instead of testnet after --network.

If you have enough amount of KCS you will see the sample output:

Congratulations! You have successfully deployed the Lock Smart Contract. The Smart Contract is now available for interaction.

Besides, if you do not have the required amount of KCS, the output may look like this:

is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging, and deploying your smart contracts and dApps, all of which work together to create a complete development environment.

What you will do

Pre-requisites

(comes with Node)

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

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

Find more instructions on how to use DOTENV on .

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

You can go to to get more KCS for testing.

Hardhat
​
​
Node.js v10+ LTS and npm
Git
Hardhat Quickstart guide
creating an account
this page
https://scan-testnet.kcc.network/
KCC Testnet Faucet