Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers manage and automate the recurring tasks inherent to the process of building smart contracts and dApps and easily introduces more functionality around this workflow. This means compiling, running and testing smart contracts at the very core.
In the same directory where you installed Hardhat run:
npx hardhat
In the terminal output, select "Create a basic sample project":
888 888 888 888 888888 888 888 888 888888 888 888 888 8888888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888888 888 "88b 888P" d88" 888 888 "88b "88b 888888 888 .d888888 888 888 888 888 888 .d888888 888888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.888 888 "Y888888 888 "Y88888 888 888 "Y888888 "Y888👷 Welcome to Hardhat v2.9.3 👷? What do you want to do? …❯ Create a basic sample project Create an advanced sample project Create an advanced sample project that uses TypeScript Create an empty hardhat.config.js Quit
Once this operation is complete, you'll now have a project structure with the following items:
1. contracts/: Directory for Solidity contracts2. scripts/: Directory for scriptable deployment files3. test/: Directory for test files for testing your application and contracts4. hardhat-config.js:Hardhat configuration file5. artifacts(visible after compile): Compiled Solidity contracts with bytecode and ABI
require("@nomiclabs/hardhat-waffle");require("@nomiclabs/hardhat-ethers");const { privateKeys } = require("./secrets.json");// This is a sample Hardhat task. To learn how to create your own go to// https://hardhat.org/guides/create-task.htmltask("accounts", "Prints the list of accounts", async () => { const accounts = await ethers.getSigners(); for (const account of accounts) { console.log(account.address); }});// You need to export an object to set up your config// Go to https://hardhat.org/config/ to learn more/** * @type import('hardhat/config').HardhatUserConfig */module.exports = { defaultNetwork: "sphinx", networks: { hardhat: {}, sphinx: { url: "https://sphinx.shardeum.org/", chainId: 8082, accounts: [privateKeys], }, }, solidity: { //configure solidity version for compilation version: "0.8.0", settings: { optimizer: { enabled: true, }, }, }, paths: { sources: "./contracts", tests: "./test", cache: "./cache", artifacts: "./artifacts", }, mocha: { timeout: 20000, },};
Note: Make sure to add your mnemonic or private key and add it to a separate file named "secrets.json" (make sure never to upload this file to GitHub or GitLab).
Create a file in scripts folder named "deploy.js".
// Runtime Environment's members available in the global scope.const hre = require("hardhat");async function main() { // Hardhat always runs the compile task when running scripts with its command // line interface. // // If this script is run directly using `node` you may want to call compile // manually to make sure everything is compiled // await hre.run('compile'); // We get the contract to deploy const TestToken = await hre.ethers.getContractFactory("testToken"); const testToken = await TestToken.deploy("100000000000000000000"); await testToken.deployed(); console.log("testToken deployed to:", testToken.address);}// We recommend this pattern to be able to use async/await everywhere// and properly handle errors.main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });