Environment variables are essential for managing sensitive information, such as API keys and private keys, when deploying smart contracts. Using environment variables helps you keep your credentials secure and separate from your codebase. In this guide, we will go through the steps to set up environment variables for deployment in a Hardhat project.
1. Install dotenv Package
The first step is to install the dotenv
package, which allows you to load environment variables from a .env file into your Node.js application. Run the following command in your project directory:
npm install dotenv
2. Create a .env File
Next, create a file named .env
in the root of your Hardhat project. This file will hold all your environment variables. Here’s an example of what your .env
file might look like:
INFURA_PROJECT_ID=
INFURA_PROJECT_SECRET=
ALCHEMY_API_KEY=
PRIVATE_KEY=
Make sure to replace the placeholders with your actual API keys and private key. Note that the private key should be kept confidential and should never be shared or committed to version control.
3. Update hardhat.config.js
Now that you have your environment variables set up, you need to modify your hardhat.config.js
file to use these variables. Here’s how you can do that:
require("@nomiclabs/hardhat-waffle");
require("dotenv").config();
module.exports = {
solidity: "0.8.0",
networks: {
mainnet: {
url: `https://mainnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
accounts: [`0x${process.env.PRIVATE_KEY}`]
},
rinkeby: {
url: `https://rinkeby.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
accounts: [`0x${process.env.PRIVATE_KEY}`]
},
goerli: {
url: `https://goerli.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
accounts: [`0x${process.env.PRIVATE_KEY}`]
}
}
};
In this configuration, we are using the dotenv
package to load the environment variables from the .env
file. The variables are then accessed using process.env.VARIABLE_NAME
.
4. Using Environment Variables in Your Scripts
You can also use environment variables in your deployment scripts. For example, if you want to deploy a contract using the private key stored in your environment variables, you can do so as follows:
npm install dotenv
0
5. Running Your Deployment
Once everything is set up, you can run your deployment script using the following command:
npm install dotenv
1
This command will deploy your contract to the Rinkeby testnet using the credentials stored in your .env
file.
6. Security Considerations
When using environment variables, keep the following security considerations in mind:
- Do not commit your .env file: Make sure to add your
.env
file to yournpm install dotenv
4 file to prevent it from being committed to your version control system. - Use secure storage: For production environments, consider using secure storage solutions for your API keys and private keys, such as AWS Secrets Manager or Azure Key Vault.
Conclusion
Setting up environment variables for deployment in Hardhat is a straightforward process that enhances the security