Smart contract deployment and execution costs depend on the blockchain’s gas fees, which vary based on network congestion and contract complexity.
How to deploy a smart contract using Hardhat
Key takeaways:
Hardhat simplifies deploying and interacting with Ethereum smart contracts.
Hardhat’s
npx hardhat nodestarts a local Ethereum node for testing.The
deploy.jsscript deploys the contract to a specified network.The Hardhat console allows interacting with the deployed contract.
Commands like
setVariableandgetVariablecan 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.
Hardhat projects structure
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 namedSimpleSmartContract. It includes a state variablemyVariable, a functionsetVariableto set its value, and a functiongetVariableto 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 , deploys the contract, and logs the contract address upon successful deployment. In case of an error, it logs the error message and exits the process with an error code.contract factory A contract factory is like a blueprint for creating identical contracts on the Ethereum blockchain. It’s a special contract designed to deploy and create new instances of another contract. In simpler terms, it’s a tool that makes it easy to set up multiple copies of the same smart contract.
Code to deploy smart contract
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
},
}
};- Click the “Run” button; this will open up a terminal.
- Enter the following command in the terminal:
cd /usercode/simple_hard_hat_project/ && npx hardhat node --hostname 0.0.0.0 --port 3000
The preceding command does the following:
- Changes the current directory to
/usercode/simple_hard_hat_project/.
- Starts a local Ethereum node using Hardhat, allowing you to interact with smart contracts on IP 0.0.0.0 and port 3000
- Open up a new terminal by clicking the “+” button and enter the following command:
cd /usercode/simple_hard_hat_project/ && npx hardhat run scripts/deploy.js --network customLocal
The preceding command does the following:
- Changes the current directory to
/usercode/simple_hard_hat_project/. - Executes the deployment script, i.e.,
deploy.jsusing Hardhat, targeting the “customLocal” network, and it will deploy the contract on the local Ethereim Hardhat node initiated back in step 1.
- Enter the following command to open up the hardhat console in order to interact with the smart contract in the previous step:
npx hardhat console --network customLocal - Enter the following commands to interact with the deployed smart contract:
- Set the Ethereum smart contract address to which the subsequent commands will connect:
const contractAddress = "0x5fbdb2315678afecb367f032d93f642f64180aa3"; - Retrieve an instance of the smart contract named “SimpleSmartContract” at the specified address:
const contract = await ethers.getContractAt("SimpleSmartContract", contractAddress); - Invoke the
setVariablefunction in the smart contract to set a variable’s value to 42:await contract.setVariable(42); - Fetch the current value of the variable from the smart contract:
const currentValue = await contract.getVariable(); - Log the retrieved current value to the console after converting it to a string:
console.log("Current value:", currentValue.toString());
- Set the Ethereum smart contract address to which the subsequent commands will connect:
Ignore the “undefined” message in the terminal when running the step 5 commands
Conclusion
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.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
How much do smart contracts cost?
How do you trigger a smart contract?
How do I deploy a smart contract on Etherscan?
Free Resources