Hardhat is a development environment to compile, deploy, test, and debug your EVM-compatible applications. It helps developers manage and automate the recurring tasks inherent to the process of building smart contracts, as well as easily integrating with various plugins to extend its functionality. With Hardhat, you can write and run tests, script deployments, and interact with your contracts.
VeChain's SDK provides a plugin to instantly enable connectivity.
The example project is relying on hardhat version 2.22.15 and the project initiated with npx hardhat init.
Setup Hardhat
Setup
To bootstrap a hardhat project:
npm install --save-dev hardhat
npx hardhat init
Install Hardhat Plugin
To add VeChain support, install the hardhat plugin:
Create contracts/GLDToken.sol and save this example:
// contracts/GLDToken.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
// Importing the ERC20 contract from OpenZeppelin
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
// Defining the GLDToken contract which inherits from ERC20
contract GLDToken is ERC20 {
// Constructor to initialize the token with an initial supply
// `initialSupply` is the amount of tokens that will be created at deployment
constructor(uint256 initialSupply) ERC20("Gold", "GLD") {
// Mint the initial supply of tokens and assign them to the contract deployer
_mint(msg.sender, initialSupply);
}
}
Run the following command to compile your contract:
npx hardhat compile
Deployment Contract
Create a deployment script in deploy/MyToken.ts:
import { ethers } from 'hardhat';
async function main(): Promise<void> {
const erc20Contract = await ethers.deployContract('GLDToken', [100000]);
await erc20Contract.waitForDeployment();
const address = await erc20Contract.getAddress();
console.log(`Gold contract deployed with address: ${address}`);
}
// 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;
});
To execute the deploy script, specifying the network to deploy to:
Bootstrap your smart contract creation with contracts. A compilation of widely used standards and patterns can significantly speed up the development process.
provides an easy-to-use web interface for generating basic smart contracts.
With , deployments can be managed automatically without tracking the addresses of deployed contracts or manually upgrading contracts.
The status of deployments is stored in deployments/<network name>. Check out to learn more about its features and best practices.
The above steps are available in an example project with the SDK repository: