Deploying via create2
When deploying contracts, you may want to deploy them to a deterministic address. This can be useful for a variety of reasons, such as deploying a contract to the same address on multiple networks. Hardhat Ignition makes this easy by allowing you to deploy your existing Ignition modules via a create2 deployment utilizing the CreateX factory.
Deploying on the Sepolia testnet
Section titled “Deploying on the Sepolia testnet”We are going to use the Sepolia testnet to deploy our Ignition module, so you need to add this network in your Hardhat config. Here we are using Alchemy to connect to the network.
import { configVariable } from "hardhat/config";
// Go to https://alchemy.com, sign up, create a new App in// its dashboard, and set the Hardhat configuration variable// ALCHEMY_RPC_URL to the full RPC URL, including the API keyconst ALCHEMY_RPC_URL = configVariable("ALCHEMY_RPC_URL");
// Replace this private key with your Sepolia test account private key// To export your private key from Coinbase Wallet, go to// Settings > Developer Settings > Show private key// To export your private key from Metamask, open Metamask and// go to Account Details > Export Private Key// Beware: NEVER put real Ether into testing accountsconst SEPOLIA_PRIVATE_KEY = configVariable("SEPOLIA_PRIVATE_KEY");
export default { // ...rest of your config... networks: { sepolia: { url: ALCHEMY_RPC_URL, accounts: [SEPOLIA_PRIVATE_KEY], }, },};To deploy on Sepolia you need to send some Sepolia ether to the address that’s going to be making the deployment. You can get testnet ether from a faucet, a service that distributes testing-ETH for free. Here is one for Sepolia:
Our last step before deploying is to add a salt for the deployment. This step is required to avoid any potential collisions with other deployments. The salt must be a 32 byte hex encoded string. You can add the salt via your Hardhat config:
export default { // ...rest of your config... ignition: { strategyConfig: { create2: { // To learn more about salts, see the CreateX documentation salt: "0x0000000000000000000000000000000000000000000000000000000000000000", }, }, },};You can now run the deployment with create2 using the newly added Sepolia network:
npx hardhat ignition deploy ignition/modules/Apollo.ts --network sepolia --strategy create2pnpm hardhat ignition deploy ignition/modules/Apollo.ts --network sepolia --strategy create2yarn hardhat ignition deploy ignition/modules/Apollo.ts --network sepolia --strategy create2The --strategy create2 flag tells Ignition to deploy the module using create2. You should see output similar to the following:
Compiled 1 Solidity file successfully (evm target: paris).Hardhat Ignition 🚀
Deploying [ Apollo ] with strategy create2
Batch #1 Executed Apollo#Rocket
Batch #2 Executed Apollo#Rocket.launch
[ Apollo ] successfully deployed 🚀
Deployed Addresses
Apollo#Rocket - 0x10D3deBd2F40F8EaB28a1d3b472a1269a12E9855Deploying this module using the create2 strategy will deploy the contract to this same address, regardless of what network you’re deploying to.
To read more about create2 and the CreateX factory, please see the CreateX documentation.