Deploy smart contracts with hardhat deploy and hardhat
In this article, you’ll learn how to deploy smart contracts to multiple Blockchain networks using Hardhat and Hardhat deploy.
By the end of this lesson, you should be able to:
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.
To install:
npm install -D hardhat-deploy
. Then, import hardhat-deploy in hardhat.config.ts
:Create a folder called deploy and inside it create a new file called 001_deploy_lock.ts
.
Include the following:
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:
001_deploy_lock.ts
file: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 real test network involves configuring the network parameters in the hardhat config file. You need to include parameters such as:
Include the following in the hardhat.config.ts
file:
You’ve configured 2 networks:
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
.
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.
Solidity Docs [Remix Project]: https://remix-project.org/ [Hardhat]: https://hardhat.org/ [Hardhat Deploy]: https://github.com/wighawag/hardhat-deploy
Deploy smart contracts with hardhat deploy and hardhat
In this article, you’ll learn how to deploy smart contracts to multiple Blockchain networks using Hardhat and Hardhat deploy.
By the end of this lesson, you should be able to:
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.
To install:
npm install -D hardhat-deploy
. Then, import hardhat-deploy in hardhat.config.ts
:Create a folder called deploy and inside it create a new file called 001_deploy_lock.ts
.
Include the following:
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:
001_deploy_lock.ts
file: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 real test network involves configuring the network parameters in the hardhat config file. You need to include parameters such as:
Include the following in the hardhat.config.ts
file:
You’ve configured 2 networks:
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
.
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.
Solidity Docs [Remix Project]: https://remix-project.org/ [Hardhat]: https://hardhat.org/ [Hardhat Deploy]: https://github.com/wighawag/hardhat-deploy