How to deploy a smart contract using Hardhat

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 and getVariable 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.

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
  1. /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.
  2. /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.
  3. /simple_hard_hat_project/scripts/deploy.js: This deployment script file utilizes Hardhat to deploy the smart contract, i.e., SimpleSmartContract. It obtains the contract factoryA 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. , 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.

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
  },
}
};
Smart Contract Deployment and Testing Code
  1. Click the “Run” button; this will open up a terminal.
  2. 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
  1. 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.js using Hardhat, targeting the “customLocal” network, and it will deploy the contract on the local Ethereim Hardhat node initiated back in step 1.
  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
    
  2. Enter the following commands to interact with the deployed smart contract:
    1. Set the Ethereum smart contract address to which the subsequent commands will connect:
      const contractAddress = "0x5fbdb2315678afecb367f032d93f642f64180aa3";
      
    2. Retrieve an instance of the smart contract named “SimpleSmartContract” at the specified address:
      const contract = await ethers.getContractAt("SimpleSmartContract", contractAddress);
      
    3. Invoke the setVariable function in the smart contract to set a variable’s value to 42:
      await contract.setVariable(42);
      
    4. Fetch the current value of the variable from the smart contract:
      const currentValue = await contract.getVariable();
      
    5. Log the retrieved current value to the console after converting it to a string:
      console.log("Current value:", currentValue.toString());
      

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?

Smart contract deployment and execution costs depend on the blockchain’s gas fees, which vary based on network congestion and contract complexity.


How do you trigger a smart contract?

You trigger a smart contract by sending a transaction to its address that calls one of its functions, either through a wallet or a dApp interface.


How do I deploy a smart contract on Etherscan?

To deploy a smart contract on Etherscan, first deploy the contract on the Ethereum network, then verify and publish the contract’s source code through Etherscan’s contract verification tool.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved