Smart contract deployment and execution costs depend on the blockchain’s gas fees, which vary based on network congestion and contract complexity.
Key takeaways:
Hardhat simplifies deploying and interacting with Ethereum smart contracts.
Hardhat’s
npx hardhat node
starts a local Ethereum node for testing.The
deploy.js
script deploys the contract to a specified network.The Hardhat console allows interacting with the deployed contract.
Commands like
setVariable
andgetVariable
can be used to update and retrieve data from the smart contract.
Blockchain technology has changed the way we do things online. Ethereum, a platform on this blockchain, introduced smart contracts—contracts that run automatically when certain conditions are met. These smart contracts make transactions transparent, secure, and efficient. Now, deploying a smart contract on Ethereum might sound complex, but it’s made easier with tools like Hardhat.
Hardhat is a framework, specifically an Ethereum development environment, enabling developers to build and test their smart contracts. In this Answer, we’ll walk through the basic steps of using Hardhat to deploy a smart contract on Ethereum and interact with the smart contract via terminal, making it simpler for developers to bring their ideas to life on the blockchain.
When we create a Hardhat project, we get the following file structure:
simple-hardhat-project/│├── contracts/│ └── MyContract.sol│├── scripts/│ └── deploy.js│├── test/│ └── MyContract.js│├── artifacts/│ └── MyContract.json│├── cache/│├── node_modules/│├── .gitignore├── hardhat.config.js├── package.json├── README.md
/simple_hard_hat_project/hardhat.config.js
: This file configures the Hardhat development environment. It specifies the Solidity compiler version, customizes artifact output paths, and defines a local Ethereum network with a specific URL and chain ID for development and testing purposes./simple_hard_hat_project/contracts/SimpleSmartContract.sol
: This Solidity file represents a simple, smart contract named SimpleSmartContract
. It includes a state variable myVariable
, a function setVariable
to set its value, and a function getVariable
to retrieve its value. The contract is written in Solidity version 0.8.0 and is open-source under the MIT license./simple_hard_hat_project/scripts/deploy.js
:
This deployment script file utilizes Hardhat to deploy the smart contract, i.e., SimpleSmartContract
. It obtains the You can follow the steps given below to run the environment set up for you and learn along!
require("@nomicfoundation/hardhat-toolbox"); /** * @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.4", paths: { artifacts: './src/artifacts', }, networks: { customLocal: { url: "http://0.0.0.0:3000", // Use the HTTPS URL for your local Ethereum client chainId: 31337, // Replace with your desired chain ID }, } };
cd /usercode/simple_hard_hat_project/ && npx hardhat node --hostname 0.0.0.0 --port 3000
The preceding command does the following:
/usercode/simple_hard_hat_project/
.cd /usercode/simple_hard_hat_project/ && npx hardhat run scripts/deploy.js --network customLocal
The preceding command does the following:
/usercode/simple_hard_hat_project/
.deploy.js
using Hardhat, targeting the “customLocal” network, and it will deploy the contract on the local Ethereim Hardhat node initiated back in step 1.npx hardhat console --network customLocal
const contractAddress = "0x5fbdb2315678afecb367f032d93f642f64180aa3";
const contract = await ethers.getContractAt("SimpleSmartContract", contractAddress);
setVariable
function in the smart contract to set a variable’s value to 42:await contract.setVariable(42);
const currentValue = await contract.getVariable();
console.log("Current value:", currentValue.toString());
Ignore the “undefined” message in the terminal when running the step 5 commands
Hardhat provides a streamlined environment for deploying and interacting with Ethereum smart contracts, making it easier for developers to test and deploy their projects locally before moving to the main network. With its simple setup and useful tools like local nodes and deployment scripts, Hardhat enables efficient development workflows, allowing seamless interaction with smart contracts through the console for real-time updates and management.
Haven’t found what you were looking for? Contact Us
Free Resources