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.5 and the project initiated with npx hardhat init.
Setup Hardhat
Install
To add VeChain support, install the plugin:
npminstall--save-dev@vechain/sdk-hardhat-plugin
Configure
Import or require the plugin in your hardhat.config.ts file:
import'@vechain/sdk-hardhat-plugin'
Add a network for TestNet or MainNet:
The Hardhat plugin requires vechain in the network's name.
Remote accounts are unsupported; therefore, provide account information.
vechain_testnet: { url:"https://testnet.vechain.org", accounts,// optionally use fee delegation to let someone else pay the gas fees// visit vechain.energy for a public fee delegation service delegator: { delegatorUrl:"https://sponsor-testnet.vechain.energy/by/90" }, enableDelegation:true,},vechain_mainnet: { url:"https://mainnet.vechain.org", accounts},
Example
A fully functional example configuration:
import"@nomicfoundation/hardhat-toolbox"import"@vechain/sdk-hardhat-plugin"constaccounts= ['0x2422eb37a0046d42e3c8d05c7d972de7fe1bb805e90b3a0dbc7d12b4d444c634']/** @type import('hardhat/config').HardhatUserConfig */module.exports= { solidity:"0.8.24", networks: { vechain_testnet: { url:"https://testnet.vechain.org", accounts,// optionally use fee delegation to let someone else pay the gas fees// visit vechain.energy for a public fee delegation service delegator: { delegatorUrl:"https://sponsor-testnet.vechain.energy/by/90" }, enableDelegation:true, }, vechain_mainnet: { url:"https://mainnet.vechain.org", accounts }, }};
OpenZeppelin Contracts
Bootstrap your smart contract creation with OpenZeppelin contracts. A compilation of widely used standards and patterns can significantly speed up the development process.
import { HardhatRuntimeEnvironment } from"hardhat/types";importtype { DeployFunction } from"hardhat-deploy/types";importtype { MyToken } from"../typechain-types";constfunc:DeployFunction=asyncfunction (hre:HardhatRuntimeEnvironment) {const [deployer] =awaithre.getUnnamedAccounts()awaithre.deployments.deploy("MyToken", { contract:"MyToken", log:true, from: deployer, proxy: { proxyContract:"UUPS", execute: { init: { methodName:"initialize", args: [deployer], }, }, }, libraries: {}, });// read data from contractconstcontract= (awaithre.ethers.getContract("MyToken", deployer )) asMyToken;// get role identifierconstugpraderRole=awaitcontract.DEFAULT_ADMIN_ROLE();// check roleif (!(awaitcontract.hasRole(ugpraderRole, deployer))) {console.log("Granting DEFAULT_ADMIN_ROLE");// execute a function of the deployed contract// .wait() waits for the receipts and throws if it revertsawait (awaitcontract.grantRole(ugpraderRole, deployer)).wait(); } else {console.log("Already has DEFAULT_ADMIN_ROLE"); }// access deployed addressconstMyToken=awaithre.deployments.get("MyToken");console.log("MyToken is available at",MyToken.address);};func.id ="mytoken-upgradeable"; // name your deploymentfunc.tags = ["mytoken"]; // tag your deployment, to run certain tags onlyfunc.dependencies = []; // build a dependency tree based on tags, to run deployments in a certain orderexportdefault func;
All scripts in deploy will be run with:
npxhardhatdeploy--networkvechain_testnet
If a contract changes, deployment will automatically take place.
The status of deployments is stored in deployments/<network name>. Check out hardhat-deploy to learn more about its features and best practices.