Hardhat

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

Prerequisites

Before you begin, ensure you've:

  • Download Node v16+.

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

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:

Load environment variables

The above configuration also uses dotenv 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.

To install dotenv, run:

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

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

Compile the smart contract

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

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

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

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:

Deploy the smart contract

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:

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

The contract will be deployed on the Viction testnet. You can view the deployment status and contract by using VicScan 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.

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

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.

Last updated