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.
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"
const accounts = ['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";
import type { DeployFunction } from "hardhat-deploy/types";
import type { MyToken } from "../typechain-types";
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const [deployer] = await hre.getUnnamedAccounts()
await hre.deployments.deploy("MyToken", {
contract: "MyToken",
log: true,
from: deployer,
proxy: {
proxyContract: "UUPS",
execute: {
init: {
methodName: "initialize",
args: [deployer],
},
},
},
libraries: {},
});
// read data from contract
const contract = (await hre.ethers.getContract(
"MyToken",
deployer
)) as MyToken;
// get role identifier
const ugpraderRole = await contract.DEFAULT_ADMIN_ROLE();
// check role
if (!(await contract.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 reverts
await (await contract.grantRole(ugpraderRole, deployer)).wait();
} else {
console.log("Already has DEFAULT_ADMIN_ROLE");
}
// access deployed address
const MyToken = await hre.deployments.get("MyToken");
console.log("MyToken is available at", MyToken.address);
};
func.id = "mytoken-upgradeable"; // name your deployment
func.tags = ["mytoken"]; // tag your deployment, to run certain tags only
func.dependencies = []; // build a dependency tree based on tags, to run deployments in a certain order
export default func;
All scripts in deploy will be run with:
npx hardhat deploy --network vechain_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.