Viction
  • Getting Started
  • General
    • Overview
    • Viction Blockchain
      • PoSV Consensus
      • Comparison
    • Staking
      • Staking Requirements
      • Staking Rewards
      • How to stake on Viction Wallet
    • Network Information
      • Viction Mainnet
      • Viction Testnet
    • Viction RPC API
  • Smart Contract Development
    • Solidity
      • A Simple Smart Contract
      • Solidity by Example
    • Standards & Specification
      • VRC25 Specification
      • VRC725 Specification
    • IDEs and Tools
      • Remix
      • Ethers.js
      • web3.js
      • thirdweb CLI
    • Deployment & Verification
      • Hardhat
      • Foundry
  • DApp Development
    • Integration
      • Exchange/Wallet integration
      • VRC25 Exchange/Wallet integration
      • Viction Staking Governance
      • VIC ZeroGas
      • VRRF
    • Data and analytics
    • Embedded Wallet (MPC)
    • Walkthrough: Build a Dapp on Viction
      • Setup Environment
      • Write the Smart Contract
      • Interacting with the Dapp in a browser
  • Masternode
    • Requirements
    • Run a Full Node
      • Binary
      • Create a Viction Masternode
      • Tmn
      • Docker
    • Apply Your Node
    • Slashing Mechanism
    • Chain Data Snapshots
    • Troubleshooting
  • Viction Wallet
    • User Guide
      • Authentication
      • How to create a new wallet
      • How to restore a wallet?
      • Wallet settings
      • Send & Receive Tokens
      • Add custom token
      • Manage Tokens
      • Send NFT
      • General settings
    • Developer Guide
    • Privacy Policy
    • Term and Services
  • Viction Bridge
    • Spacegate
    • Arken Bridge
    • Hyperlane
  • Viction Data Availability
    • Viction DA RPC API
    • DA Integration Use cases
      • Simple Guide for Integrating OP Stack Rollup with Viction DA Layer
  • How to
    • How to Connect to Viction Blockchain
      • Coin98 Super Wallet
      • Metamask
      • Ledger
    • How to troubleshoot when the node is up but couldn't begin to sync block
    • How to Vote for Viction Saigon Network Upgrade Proposal
    • How to issue a token via VICIssuer
    • How to verify if a contract has been issued via VICIssuer
    • How to deploy the VRC725 contract
    • How to apply ZeroGas for VRC725 contract
    • How to Migrate Dapps from Ethereum
    • How to register Token or NFT logo on Vicscan
    • How to verify a contract on Vicscan
    • How to confirm a project on Vicscan
    • How to check if a token is gas sponsored on Viction
    • How to verify gas sponsored transactions
    • How to create Telegram Mini Apps
    • How to use VictionSafe (Multisig)
  • FAQ
    • APIs
    • General
      • Viction
      • Ecosystem
      • VIC - Economics
      • Contact & Support
    • Masternodes and Voting
      • Masternodes
      • Voter
    • Products
      • VicScan (Explorer)
      • VicMaster
      • VicStats
      • VicIssuer
        • How to Verify & Publish Contract Source Code on VicScan
      • Viction Wallet
      • Viction Data Availability Network
  • Legal
    • Terms of Use
    • Privacy Policy
  • Whitepaper and Research
  • Archive
    • TOMOE
    • How to Deploy a VRC25 Token on Viction
    • How to deploy an ICO smart contract on Viction
    • How to deploy an NFT token
    • An Example of Building a Dapp on Viction
    • Migrate Ethereum Dapp to Viction
    • TomoMasterDAO
      • Introduction
      • Governance model
        • On-Chain vs Off-Chain Voting
        • Board
        • Proposals
        • Voting and Outcome
      • Tokenomics
      • How to utilize and trade tDAO
      • Proposal guidelines for TomoMasterDAO
    • Old Viction Testnet
    • Deploy on Viction
      • CLI Commands
      • Viction Private Testnet Setup
Powered by GitBook
On this page
  • Prerequisites​
  • Create a Hardhat project​
  • Configure hardhat with Viction
  • Install Hardhat toolbox
  • Load environment variables
  • Compile the smart contract​
  • Deploy the smart contract​
  • Verify contract on VicScan
  • Tips to verify contracts
  1. Smart Contract Development
  2. Deployment & Verification

Hardhat

This section will guide you through deploying a smart contract on the Viction using Hardhat.

PreviousDeployment & VerificationNextFoundry

Last updated 1 year ago

Prerequisites

Before you begin, ensure you've:

  • Download .

  • An ethereum wallet.

  • Funded your wallet for caring gas fee of transactions.

Create a Hardhat project

To create an empty Hardhat project, run the following commands:

mkdir hardhat-tomo-tutorial
cd hardhat-tomo-tutorial
npm init
npm install --save-dev hardhat
npx hardhat

Select Create a TypeScript project then press enter to confirm the project root.

Select y for both adding a .gitignore and loading the sample project. It will take a moment for the project setup process to complete.

Configure hardhat with Viction

In order to deploy smart contracts to the Viction, you will need to configure your Hardhat project and add the Viction network.

To configure Hardhat to use Viction, add Viction as a network to your project's hardhat.config.ts file:

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

require('dotenv').config();

const config: HardhatUserConfig = {
  solidity: {
    version: '0.8.17',
  },
  networks: {
    // for mainnet
    'tomo-mainnet': {
      url: 'https://rpc.viction.xyz',
      accounts: [process.env.PRIVATE_KEY as string],
    },
    // for testnet
    'tomo-testnet': {
      url: 'https://rpc-testnet.viction.xyz',
      accounts: [process.env.PRIVATE_KEY as string],
    },
  },
  defaultNetwork: 'hardhat',
};

export default config;

Install Hardhat toolbox

The above configuration uses the @nomicfoundation/hardhat-toolbox plugin to bundle all the commonly used packages and Hardhat plugins recommended to start developing with Hardhat.

To install @nomicfoundation/hardhat-toolbox, run:

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

Load environment variables

To install dotenv, run:

npm install --save-dev dotenv

Once you have dotenv installed, you can create a .env file with the following content:

PRIVATE_KEY=<YOUR_PRIVATE_KEY>

Substitute <YOUR_PRIVATE_KEY> with the private key for your wallet.

Below is a simple token contract (ERC20) written in the Solidity programming language:

pragma solidity 0.8.17;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
    constructor() ERC20("My Token", "MYT") {}

    function mint(address recipient, uint256 amount)
        external
        returns (uint256)
    {
        _mint(recipient, amount);
        return amount;
    }
}

To add the OpenZeppelin Contracts library to your project, run:

npm install --save @openzeppelin/contracts@4.9.3

In your project, delete the contracts/Lock.sol contract that was generated with the project and add the above code in a new file called contracts/MyToken.sol.

To compile the contract using Hardhat, run:

npx hardhat compile

Once your contract has been successfully compiled, you can deploy the contract to the Viction networks.

To deploy the contract to the Viction testnet, you'll need to modify the scripts/deploy.ts in your project:

import { ethers } from 'hardhat';

async function main() {
  const gasLimit = 100_000_000;
  const myToken = await ethers.deployContract('MyToken', { gasLimit });

  await myToken.waitForDeployment();

  console.log('Token Contract Deployed at ' + myToken.target);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Note

Gas limit is required when deploying a smart contract using Hardhat in Viction

Finally, ensure your wallet has enough fund to cover gas fee and run script with command:

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

Verify contract on VicScan

VicScan now support contract verification via Hardhat API, you will need to change hardhat.config.ts with the following configuration:

Mainnet

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

/** @type import('hardhat/config').HardhatUserConfig */
const config: HardhatUserConfig = {
  networks: {
    Viction: {
      url: "https://rpc.viction.xyz", // for mainnet
      accounts:  ['']
    }
  },

  etherscan: {
    apiKey: {
      goerli: "",
      Viction: "tomoscan2023",
    },
    customChains: [
      {
        network: "Viction",
        chainId: 88, // for mainnet
        urls: {
          apiURL: "https://www.vicscan.xyz/api/contract/hardhat/verify", // for mainnet
          browserURL: "https://vicscan.xyz", // for mainnet

        }
      }
    ]
  }
};

Testnet

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

/** @type import('hardhat/config').HardhatUserConfig */
const config: HardhatUserConfig = {
  networks: {
    Viction: {
      url: "https://rpc-testnet.viction.xyz", // for testnet
      accounts:  ['']
    }
  },

  etherscan: {
    apiKey: {
      goerli: "",
      Viction: "tomoscan2023",
    },
    customChains: [
      {
        network: "victiontestnet",
        chainId: 89, // for testnet
        urls: {
          apiURL: "https://scan-api-testnet.viction.xyz/api/contract/hardhat/verify", // for testnet
          browserURL: "https://www.testnet.vicscan.xyz", // for testnet

        }
      }
    ]
  }
};

Tips to verify contracts

It is recommended that the contract be deployed and verified using several files rather than a single file to let the verification process go more smoothly using the hardhat plugin.

Because verifying contracts requires compiling the source code to bytecode and comparing it to the bytecode on onchain, occasionally the source code might be a large file size, causing the compilation to take longer than usual.

It is strongly advised that those source code files be flattened into numerous files with less than 1MB each file to ensure performance and stability.

In the event the contract has previously been deployed in the single file format, but the verification procedure has failed. It is recommended that you re-deploy the contract with different file formats and continue the verification procedure.

If you are still unable to verify the contract after several attempts, please upload your contract source code along with the compliation configuration to Github and contact us for assistance.

The above configuration also uses to load the PRIVATE_KEY environment variable from a .env file to process.env.PRIVATE_KEY. You should use a similar method to avoid hardcoding your private keys within your source code.

Compile the smart contract

The Solidity code above defines a smart contract named ERC20. The code uses the ERC20 interface provided by the to create a token smart contract. OpenZeppelin allows developers to leverage battle-tested smart contract implementations that adhere to official ERC standards.

Deploy the smart contract

The contract will be deployed on the Viction testnet. You can view the deployment status and contract by using and searching for the address returned by your deploy script. If you've deployed an exact copy of the token contract above, it will be verified, and you'll be able to read and write to the contract using the web interface.

​
Node v16+
​
dotenv
​
OpenZeppelin Contracts library
​
VicScan