LogoLogo
  • Overview
  • Basics
    • Architecture
  • Key Concepts
    • EVM Ledger
    • UTXO Chain
      • Confidential transfers
        • Overview
        • Concepts
        • Technical Specifications
    • Prism++ Transfer
    • Triple Masking
      • Overview
      • SDK Installation
      • Bar to Abar
      • Abar to Bar
      • Abar Transfer
    • Staking
      • Overview
      • EVM Staking
        • UTXO Staking and EVM Staking
        • EVM Staking Portal User Guide
      • Consensus
      • Rewards
      • Penalties
  • Developers
    • Acquire Testnet FRA
    • EVM Tools & Tutorials
      • Contract Deployment
        • Ganache
        • Hardhat
        • Remix
        • Truffle
        • Waffle
      • The Graph
      • Band Protocol
      • Mint NFTs
        • Deploy NFT Contract (1 of 3)
        • Mint NFT (2 of 3)
        • View NFT (3 of 3)
    • Developer SDKs
      • UTXO Native Chain SDK
        • UTXO Native Chain SDK Installation
        • Developer Tools
          • Findora CLI Tool
        • UTXO API Reference
          • Account
          • Keypair
          • Network
          • Asset
          • Staking
          • Transaction
          • Helpers
      • ZkRouting SDK
      • zkDID SDK
        • zkDID SDK Installation
        • zkDID API Reference
        • zkDID Example Code
      • Prism++ SDK
    • EVM References
      • Metamask
      • Local Development Network Setup
      • EVM API Reference
      • Precompiled Contracts
    • Resources
      • Bug Bounties
      • Events
        • 🏗️ETH San Francisco 2022
          • zkDID and Credentials
        • 🏗️ETH Denver 2023
    • Game-Specific Chains
      • GSC Highlights
      • User Guide
      • Developer Tools
  • Network Settings
    • Contract Addresses
    • Network Settings
  • General User Materials
    • Acquire FRA
    • Acquire FRA (Testnet)
    • Stake FRA
    • Use Wallets
      • MetaMask
        • Download
        • Configure (Auto)
        • Configure (Manual)
      • Findora Wallet
        • Download
        • New Wallet
        • Transfer
        • Prism++
        • Adding Assets to the Findora Wallet
        • Manage Assets
      • Ledger Hardware Wallet
    • Bridging Tokens to Findora
    • Use Block Explorers
    • Explore Testnet
      • Triple Masking
        • Triple Masking Demo
      • EVM Staking
      • Prism++
        • Prism++ Testing Campaign
          • 💰Testnet Wallet Setup/Funding
          • 👨‍🌾👨🌾 Peasant: Prism++ Campaign Signup
          • 🧒Pupil: FRC20 Token Transfers
          • 🧑‍🎨🧑🎨 Pilgrim: FRC 721 Token Transfers
          • 🧑‍🎓🧑🎓 Prodigy: FRC1155 Token Transfers
          • 🧑‍🚀🧑🚀 Pioneer: Mainnet Transfers
          • Prism++ Testnet Campaign FAQs
    • Gaxle Events
      • Game Chain Challenges
        • Getting Started
        • The Campaigns
        • The Reward Raffles
  • Validator Materials
    • Findora's University Program
    • Validator Setup Guides
      • System Requirements
      • Acquire a Server
      • Validator Toolbox Setup
        • New Build
        • Existing Build
        • Additional Info
      • Manual Setup
      • Automated Setup (Deprecated)
    • Upgrade Guides
      • Node Upgrade (Toolbox)
      • Node Upgrade (Manual)
      • fn CLI Upgrade (Wallet)
    • Operational Guides
      • Emergency Recovery
      • Data Backup
      • CLI Staking
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Developers
  2. EVM Tools & Tutorials
  3. Contract Deployment

Hardhat

PreviousGanacheNextRemix

Last updated 2 years ago

Was this helpful?

Overview

Hardhat is an Ethereum development environment that helps developers manage and automate repetitive tasks for smart contract and DApp development, and can be used in the truffle project.

Prerequisites

This tutorial requires Node.js to be installed. You can download it through or run the following code to complete the installation. You can verify the correct installation by requesting the version of each installation package:

   node -v
   npm -v

Create Hardhat project

Here we use the truffle project (please refer to the truffle document to create a project module using Truffle to complete the 1, 2, and 3 steps), then configure the package.json file, and add dependencies in devDependencies:

"devDependencies": {
   "@nomiclabs/hardhat-ethers": "^2.0.2",
   "@nomiclabs/hardhat-etherscan": "^2.1.0",
   "hardhat": "^2.0.3",
   "hardhat-gas-reporter": "^1.0.1",
   "ts-node": "^9.0.0",
   "typescript": "^4.0.5"
}

Need to reinstall dependencies

npm install

This will create a Hardhat config file (hardhat.config.js) in our project directory, and create a .secret in the project root directory to store the wallet address private key we need to use when operating the contract.

import '@nomiclabs/hardhat-ethers';
import '@nomiclabs/hardhat-etherscan';
import 'hardhat-gas-reporter';
import * as fs from 'fs';

const mnemonic = fs.readFileSync('.secret').toString().trim();

module.exports = {
  defaultNetwork: "hardhat",
  networks: {
    hardhat: {
    },
    findora: {
      url: "https://prod-testnet.prod.findora.org:8545",
      chainId: 2153,
      accounts: [mnemonic]
    }
  },
  solidity: {
    version: "0.6.12",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: './build/cache',
    artifacts: './build/artifacts',
  },
  mocha: {
    timeout: 20000
  },
  gasReporter: {
    currency: 'USD',
    enabled: true,
  },
  etherscan: {
    // Your API key for Etherscan
    // Obtain one at https://etherscan.io/
    apiKey: '**',
  }
}

Create a script directory at the same level as contract to store Hardhat script files (example: deploy.ts), and then use ethers to write deployment scripts. First, create a local instance of the contract through the getContractFactory() method. Next, use the deploy() method included in the instance to initiate a smart contract. Finally, use deployed() to wait for the deployment to complete. After the contract is deployed, the contract address can be obtained in the MyContract instance. The script is a simplified version used in this tutorial.

import { network, ethers} from 'hardhat';

// scripts/deploy.ts
async function main() {
   // We get the contract to deploy
   const MyContract = await ethers.getContractFactory('MyContract');
   console.log('Deploying MyContract...');

   // Instantiating a new MyContract smart contract
   const myContract = await MyContract.deploy();

   // Waiting for the deployment to resolve
   await myContract.deployed();
   console.log('MyContract deployed to:', myContract.address);
}

main()
   .then(() => process.exit(0))
   .catch((error) => {
      console.error(error);
      process.exit(1);
   });

Use the run command to deploy the MyContract contract to Findora devnet

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

The contract can be deployed in a few seconds, and then you can see the contract address printed out on the terminal:

Create a local instance of the MyContract.sol contract, and then enter the address obtained when deploying the contract, connect this instance to an existing instance, and interact with it after connecting to the contract. When the console command is still running, call the method in the contract and pass in the correct parameters (If the method involves ERC20 type transfer, please authorize first).

const myContract = await ethers.getContractAt('MyContract', '0x8D94133ddF3A6Cc451653Cd4B21Dc8b65c3383B0');
const tx = await myContract.connect(operator).setValue(88, override);
console.log('hash is:', tx.hash)

const value = await myContract.getValue();
console.log('value is:', value.toString())

Congratulations, you have completed the Hardhat basic operation guide!

Create Hardhat script file

Deploy contract

Interact with the contract

​
​
Node.js
​
​
​
​