|
| 1 | +# Deploying Via Create2 |
| 2 | + |
| 3 | +When deploying contracts, you may want to deploy them to a specific 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](https://createx.rocks/). |
| 4 | + |
| 5 | +::: tip |
| 6 | + |
| 7 | +As is the case when using any unaudited contract system, please be aware of the [security considerations](https://github.com/pcaversaccio/createx?tab=readme-ov-file#security-considerations) of using the CreateX factory on a mainnet network. |
| 8 | + |
| 9 | +::: |
| 10 | + |
| 11 | +## Deploying on the Sepolia testnet |
| 12 | + |
| 13 | +We are going to use the [Sepolia testnet](https://ethereum.org/en/developers/docs/networks/#sepolia) to deploy our Ignition module, so you need to add this network in your Hardhat config. Here we are using [Alchemy](https://alchemy.com/) to connect to the network. |
| 14 | + |
| 15 | +:::tip |
| 16 | + |
| 17 | +For more information on `vars` and configuration variables, please see our [configuration variables guide](../../../hardhat-runner/docs/guides/configuration-variables.md). |
| 18 | + |
| 19 | +::: |
| 20 | + |
| 21 | +::::tabsgroup{options=TypeScript,JavaScript} |
| 22 | + |
| 23 | +:::tab{value=TypeScript} |
| 24 | + |
| 25 | +```ts |
| 26 | +// Go to https://alchemy.com, sign up, create a new App in |
| 27 | +// its dashboard, and set the Hardhat configuration variable |
| 28 | +// ALCHEMY_API_KEY to the key |
| 29 | +const ALCHEMY_API_KEY = vars.get("ALCHEMY_API_KEY"); |
| 30 | + |
| 31 | +// Replace this private key with your Sepolia test account private key |
| 32 | +// To export your private key from Coinbase Wallet, go to |
| 33 | +// Settings > Developer Settings > Show private key |
| 34 | +// To export your private key from Metamask, open Metamask and |
| 35 | +// go to Account Details > Export Private Key |
| 36 | +// Beware: NEVER put real Ether into testing accounts |
| 37 | +const SEPOLIA_PRIVATE_KEY = vars.get("SEPOLIA_PRIVATE_KEY"); |
| 38 | + |
| 39 | +export default { |
| 40 | + // ...rest of your config... |
| 41 | + networks: { |
| 42 | + sepolia: { |
| 43 | + url: `https://eth-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`, |
| 44 | + accounts: [SEPOLIA_PRIVATE_KEY], |
| 45 | + }, |
| 46 | + }, |
| 47 | +}; |
| 48 | +``` |
| 49 | + |
| 50 | +::: |
| 51 | + |
| 52 | +:::tab{value=JavaScript} |
| 53 | + |
| 54 | +```js |
| 55 | +// Go to https://alchemy.com, sign up, create a new App in |
| 56 | +// its dashboard, and set the Hardhat configuration variable |
| 57 | +// ALCHEMY_API_KEY to the key |
| 58 | +const ALCHEMY_API_KEY = vars.get("ALCHEMY_API_KEY"); |
| 59 | + |
| 60 | +// Replace this private key with your Sepolia test account private key |
| 61 | +// To export your private key from Coinbase Wallet, go to |
| 62 | +// Settings > Developer Settings > Show private key |
| 63 | +// To export your private key from Metamask, open Metamask and |
| 64 | +// go to Account Details > Export Private Key |
| 65 | +// Beware: NEVER put real Ether into testing accounts |
| 66 | +const SEPOLIA_PRIVATE_KEY = vars.get("SEPOLIA_PRIVATE_KEY"); |
| 67 | + |
| 68 | +module.exports = { |
| 69 | + // ...rest of your config... |
| 70 | + networks: { |
| 71 | + sepolia: { |
| 72 | + url: `https://eth-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`, |
| 73 | + accounts: [SEPOLIA_PRIVATE_KEY], |
| 74 | + }, |
| 75 | + }, |
| 76 | +}; |
| 77 | +``` |
| 78 | + |
| 79 | +::: |
| 80 | + |
| 81 | +:::: |
| 82 | + |
| 83 | +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: |
| 84 | + |
| 85 | +- [Alchemy Sepolia Faucet](https://sepoliafaucet.com/) |
| 86 | + |
| 87 | +:::tip |
| 88 | + |
| 89 | +This guide assumes you are using the contracts and Ignition module from the [quick start guide](/ignition/docs/getting-started#quick-start), but the steps are the same for any deployment. |
| 90 | + |
| 91 | +::: |
| 92 | + |
| 93 | +You can now run the deployment with `create2` using the newly added Sepolia network: |
| 94 | + |
| 95 | +::::tabsgroup{options="TypeScript,JavaScript"} |
| 96 | + |
| 97 | +:::tab{value="TypeScript"} |
| 98 | + |
| 99 | +```sh |
| 100 | +npx hardhat ignition deploy ignition/modules/Apollo.ts --network sepolia --strategy create2 |
| 101 | +``` |
| 102 | + |
| 103 | +::: |
| 104 | + |
| 105 | +:::tab{value="JavaScript"} |
| 106 | + |
| 107 | +```sh |
| 108 | +npx hardhat ignition deploy ignition/modules/Apollo.js --network sepolia --strategy create2 |
| 109 | +``` |
| 110 | + |
| 111 | +::: |
| 112 | + |
| 113 | +:::: |
| 114 | + |
| 115 | +The `--strategy create2` flag tells Ignition to deploy the module using `create2`. You should see output similar to the following: |
| 116 | + |
| 117 | +```sh |
| 118 | +Compiled 1 Solidity file successfully (evm target: paris). |
| 119 | +Hardhat Ignition 🚀 |
| 120 | + |
| 121 | +Deploying [ Apollo ] with strategy < create2 > |
| 122 | + |
| 123 | +Batch #1 |
| 124 | + Executed Apollo#Rocket |
| 125 | + |
| 126 | +Batch #2 |
| 127 | + Executed Apollo#Rocket.launch |
| 128 | + |
| 129 | +[ Apollo ] successfully deployed 🚀 |
| 130 | + |
| 131 | +Deployed Addresses |
| 132 | + |
| 133 | +Apollo#Rocket - 0x8A818FCD6dcdFCF59A1aeebB241644f256Bd19c3 |
| 134 | +``` |
| 135 | + |
| 136 | +Deploying this module using the `create2` strategy will deploy the contract to this same address, regardless of what network you're deploying to. |
| 137 | + |
| 138 | +To read more about `create2` and the CreateX factory, please see the [CreateX documentation](https://github.com/pcaversaccio/createx). |
0 commit comments