FNCY Chain
FNCY 2.0.2 (ENG)
FNCY 2.0.2 (ENG)
  • Introduction
  • Getting Started
  • Design
    • Structure
    • Three Main Sets of Participants
    • PoSA Consensus
    • Governance
    • Staking
    • Mining Reward
    • Gas Fee
    • Runtime Upgrade
    • Cross-Chain Bridge
    • Security
  • Build On FNCY Chain
    • RPC Endpoints
    • Testnet Faucet
    • Block Explorer
      • Token Listing
    • BEP20 Token
    • NFT
    • Validator Requirements
    • NFT Market
      • FNCY Marketplace
  • For Developers
    • JSON-RPC
    • FNCY Chain API
      • Wallet API
      • Transaction API
      • Blockchain API
    • Wallet
      • FNCY Wallet
      • Use MetaMask for FNCY Chain
      • SDK
        • Android
          • Install
          • Guide
            • Initialize
            • Create/Restore Wallet
            • Wallet Search
            • Execute SendCoin/Contract
            • Transaction Search
            • Other Features
          • Methods
          • Domain
          • ETC
        • iOS
          • Install
          • Guide
            • Initialize
            • Create/Restore Wallet
            • Wallet Search
            • Execute SendCoin/Contract
            • Transaction Search
            • Other Features
          • Methods
          • Domain
          • ETC
    • Smart Contract
      • Deployment
        • Truffle
        • Hardhat
        • Remix IDE
      • Verify Your Contract on FncyScan
    • Gasless Transaction
    • with FNCY
      • FNCY Login
      • GAME AUTH Login
  • Tokenomics
  • Ecosystem Partner
  • FNCY Governance Partner
Powered by GitBook
On this page
  • Deploying Smart Contract Using Hardhat
  • Preparation
  • Project Setting
  • Writing Smart Contract
  • Deploying Smart Contract
  • Wrapping Up
  1. For Developers
  2. Smart Contract
  3. Deployment

Hardhat

Deploying Smart Contract Using Hardhat

In this article, we'll learn how to build a smart contract with Hardhat.

Preparation

Before you start, make sure that the following prerequisites are installed.

  • Node.js and npm

  • Hardhat

Project Setting

  1. Create a new directory for your project and navigate to it.

  2. Run the following command to initialize a new npm project.

npm init -y
  1. Run the following command to install Hardhat.

npm install --save-dev hardhat
  1. Run the following command and follow the prompts to initialize Hardhat.

npx hardhat

Writing Smart Contract

  1. In thecontractsdirectory, create a solidity file named MyContract.sol.

  2. Create a file as shown below.

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

contract MyContract {
    // Contract code goes here...
}

Deploying Smart Contract

  1. Create a new file called deploy.js in the scripts directory.

  2. Write a deployment script using Hardhat's deployment API.

const { ethers } = require("hardhat");

async function main() {
    const MyContract = await ethers.getContractFactory("MyContract");
    const myContract = await MyContract.deploy();

    await myContract.deployed();

    console.log("MyContract deployed to:", myContract.address);
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });
  1. Run the following command to run the deployment script.

Set the network name to deploy to.

npx hardhat run scripts/deploy.js --network <network-name>
  1. This script deploys a smart contract and writes the contract address to the console.

Wrapping Up

You've successfully deployed a smart contract using Hardhat. Now you can call the deployed contract using its address.

PreviousTruffleNextRemix IDE

For more information, visit .

Hardhat's official website