How to deploy your smart contract to Rinkeby
Published
Table of contents
In this article I'll explain how to deploy a smart contract built with Hardhat to the Rinkeby Ethereum testnet using Alchemy
Alchemy provides an API that we can use to deploy our smart contracts to Ethermeum, Arbitrum, Polygon and Optimism and target the the main network or any of the multiple test nets.
You can create an account for free here and get \$100 in credits if you decide to upgrade your account.
Deploy to Rinkeby step-by-step
Create app in Alchemy
Once you've created your account on Alchemy, click on the +Create App button and you'll see the following form:
Enter your application name and description. I like to think of Rinkeby as a Staging environment so that's what I select. Then choose Ethereum as chain and Rinkeby as the Network.
Once your app is created, click on View Key to get the HTTP endpoint that we can use to deploy the smart contract. But before being able to deploy it, we first need a wallet with some ETH in the Rinkeby network to "pay" (with fake ETH) for it.
How to receive Rinkeby Eth tokens
Create a new account in Metamask (you can install it here) that you can use just to deploy contracts to Rinkeby.
To get some fake Eth, you can use any of the following faucets:
- https://ethily.io/rinkeby-faucet/
- https://app.mycrypto.com/faucet
- https://faucets.chain.link/rinkeby - provides both ETH and LINK tokens
- https://www.rinkeby.io/#faucet - social media faucet. Requires you to post on twitter/facebook to get ETH.
Configure Hardhat network
Once we have enough Eth in our wallet to deploy our contract, we need to configure the network we want to deploy to. In the hardhat.config.js
file, there is a section in which we can include multiple networks. For Rinkeby we'll configure it like this:
require('@nomiclabs/hardhat-waffle');
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: '0.8.4',
networks: {
hardhat: {
chainId: 1337,
},
rinkeby: {
url: process.env.DEPLOY_KEY_RINKEBY,
accounts: [process.env.DEPLOY_ACC_RINKEBY],
},
},
};
Notice that I've added the Rinkeby network, for which we need to provide a url and a list of accounts (or just one in this case) that will be used to deploy the contract. The URL is the HTTP endpoint that Alchemy provided for our project and the account is the private key of the Metamask account in which we have the ETH.
To extract the private key of the metamask account, you need to click on the three dots button next to the account icon, click "Account Details" and then "Export private key"
As you can see, I've decided to use environment variables for this values as it's important not to share them (specially our account private key!) or upload them to our code repository.
Configure Hardhat to use environment variables
In order to load environment variables and use them in the hardhat.config.js
, we need to install the dontenv NPM package running npm i dotenv
. Once installed we can create a .env
file in the root of the project and include some variables. In my case it would be like this:
DEPLOY_KEY_RINKEBY=https://eth-rinkeby.alchemyapi.io/v2/ThisIsMyAPIKey_FromAlchemy
DEPLOY_ACC_RINKEBY=123456abcdef12345xxxx12345fedcabxxxx0
Once we've created the .env
file with the correct values, we need to indicate hardhat to load it. For that, we'll modify the hardhat.config.js
file to require the dotenv package and run the config function, which will automatically pick up our .env
file:
require('@nomiclabs/hardhat-waffle');
// ➡️ Load env file
require('dotenv').config();
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task('accounts', 'Prints the list of accounts', async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: '0.8.4',
networks: {
hardhat: {
chainId: 1337,
},
rinkeby: {
url: process.env.DEPLOY_KEY_RINKEBY,
accounts: [process.env.DEPLOY_ACC_RINKEBY],
},
},
};
Next step is to create a deployment script.
Create and run deploy script
With all the configuration done, we just need to create a deployment script that will take our smart contract, compile it and deploy it to the target network (in this case Rinkeby) using the account we provided.
In the scripts
folder generated by hardhat, create a deploy.js
file or modify the existing one. To deploy a smart contract, we need to use the getContractFactory()
provided by Ethers and the deploy()
method.
For this example, I'll assume the smart contract file name is LuckyNumber.sol
:
const main = async () => {
// gets info of the account used to deploy
const [deployer] = await hre.ethers.getSigners();
const accountBalance = await deployer.getBalance();
console.log('Deploying contract with account: ', deployer.address);
console.log('Account balance: ', accountBalance.toString());
// read contract file
const luckyContractFactory = await hre.ethers.getContractFactory(
'LuckyNumber'
);
// triggers deployment
const luckyContract = await luckyContractFactory.deploy({});
// wait for deployment to finish
await luckyContract.deployed();
console.log('LuckyNumber contract address: ', luckyContract.address);
};
const runMain = async () => {
try {
await main();
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
};
runMain();
To execute this script, we'll run npx hardhat run ./scripts/deploy.js --network rinkeby
. Make sure the path to the deploy.js file is correct and that the network name matches with what we included in the hardhat.config.js
file previously. If everything goes right, you'll see something like this:
> npx hardhat run ./solidity/scripts/deploy.js --network rinkeby ✔
Deploying contracts with account: 0xA454e378Df323082FEC4BD5A74251fa742405333
Account balance: 64404957750264327
LuckyNumber contract address: 0x656E6B45bf63b1AA77b733Dc5B353615de64F6d1
To confirm that everything went as expected, you can visit the Etherscan Rinkeby website and search using the contract address:
Conclusion
You can follow these steps to deploy your smart contracts to any ethereum network. You just need to change the hardhat configuration file, get an endpoint from Alchemy, and make sure you have enough ETH to cover the deployment fees 🤑
TAGS