Deploying Smart Contracts
Deploy smart contracts with hardhat deploy and hardhat
Deploying Smart Contracts
In this article, you’ll learn how to deploy smart contracts to multiple Blockchain networks using Hardhat and Hardhat deploy.
Objectives
By the end of this lesson, you should be able to:
- Deploy a smart contract to the Base Sepolia Testnet with hardhat-deploy
- Deploy a smart contract to the Sepolia Testnet with hardhat-deploy
- Use BaseScan to view a deployed smart contract
Overview
Hardhat capabilities enable developers to deploy smart contracts easily to any Blockchain by simply creating tasks
or scripts
. However, due to the Hardhat architecture that enables its extension by creating plugins, you can rely on existing solutions developed by the community.
Hardhat deploy is a community-developed plugin that enables the deployment of your smart contracts in a simple way.
Setting up Hardhat deploy
To install:
- Run
npm install -D hardhat-deploy
. Then, import hardhat-deploy inhardhat.config.ts
:
-
Create a folder called deploy and inside it create a new file called
001_deploy_lock.ts
. -
Include the following:
- Modify the
tsconfig.json
file to look like:
-
Before implementing the deploy functionality, configure a deployer account in the
hardhat.config.ts
file. Hardhat deployment includes a way to name accounts in the config file. -
Run the following, which adds an alias to the account 0 of your environment:
- Implement the deploy function by including the following in the
001_deploy_lock.ts
file:
Testing your deployment
The easiest way to test your deployment is by modifying the test.
Go to Lock.ts
and include in the imports the following:
deployments
will allow you to execute the deployment files from your test.
Change the before
function to look like the following:
Notice how you execute deployments.fixture
and pass a tag that matches the one you specified in the deployment file (001_deploy_lock.ts
).
The deployment file is then executed and you can then reuse that functionality and simply consume the address of the newly-deployed contract by using:
Reuse Lock__factory
but use the connect function and pass the address of the newly-created contract plus a signer. Then, run npx hardhat test
and you should get the same result:
Deploying to a test network
Deploying to a real test network involves configuring the network parameters in the hardhat config file. You need to include parameters such as:
- The JSON RPC URL
- The account you want to use
- Real test ether or the native Blockchain token for gas costs
Include the following in the hardhat.config.ts
file:
You’ve configured 2 networks:
- base_sepolia
- sepolia
You also need to create a .env
file with the following variables:
In order to ensure the environment variables are loaded, you need to install another package called dotenv
:
Then, include the following in the hardhat.config.ts
file:
Deploy to base with the following command:
After you run the command, a deployments folder appears with a newly-created deployment for base_sepolia
:
If you want to deploy to another network, change the network name as follows:
Be aware that you must have the correct environment variables for the JSON RPC URLs. For example, for Sepolia use ALCHEMY_SEPOLIA_KEY
.
Conclusion
In this lesson, you’ve learned how to deploy smart contracts using Hardhat and Hardhat-deploy. You have configured hardhat to easily deploy to multiple networks and you created deployment files to abstract this task.
See also
Solidity Docs [Remix Project]: https://remix-project.org/ [Hardhat]: https://hardhat.org/ [Hardhat Deploy]: https://github.com/wighawag/hardhat-deploy