|
| 1 | +# Verifying your contracts |
| 2 | + |
| 3 | +Once your module has been deployed to a live network, the next step is to verify its source code. |
| 4 | + |
| 5 | +Verifying a contract means making its source code public, along with the compiler settings you used, which allows anyone to compile it and compare the generated bytecode with the one that is deployed on-chain. Doing this is extremely important in an open platform like Ethereum. |
| 6 | + |
| 7 | +In this guide we'll explain how to do this with Hardhat Ignition in the [Etherscan](https://etherscan.io/) explorer. |
| 8 | + |
| 9 | +## Getting an API key from Etherscan |
| 10 | + |
| 11 | +The first thing you need is an API key from Etherscan. To get one, go to [their site](https://etherscan.io/login), sign in (or create an account if you don't have one) and open the "API Keys" tab. Then click the "Add" button and give a name (like "Hardhat") to the API key you are creating. After that you'll see the newly created key in the list. |
| 12 | + |
| 13 | +Open your Hardhat config and add the API key you just created: |
| 14 | + |
| 15 | +::::tabsgroup{options=TypeScript,JavaScript} |
| 16 | + |
| 17 | +:::tab{value=TypeScript} |
| 18 | + |
| 19 | +```ts |
| 20 | +export default { |
| 21 | + // ...rest of the config... |
| 22 | + etherscan: { |
| 23 | + apiKey: "ABCDE12345ABCDE12345ABCDE123456789", |
| 24 | + }, |
| 25 | +}; |
| 26 | +``` |
| 27 | + |
| 28 | +::: |
| 29 | + |
| 30 | +:::tab{value=JavaScript} |
| 31 | + |
| 32 | +```js |
| 33 | +module.exports = { |
| 34 | + // ...rest of the config... |
| 35 | + etherscan: { |
| 36 | + apiKey: "ABCDE12345ABCDE12345ABCDE123456789", |
| 37 | + }, |
| 38 | +}; |
| 39 | +``` |
| 40 | + |
| 41 | +::: |
| 42 | + |
| 43 | +:::: |
| 44 | + |
| 45 | +## Deploying and verifying a contract in the Sepolia testnet |
| 46 | + |
| 47 | +We are going to use the [Sepolia testnet](https://ethereum.org/en/developers/docs/networks/#sepolia) to deploy and verify our contract, so you need to add this network in your Hardhat config. Here we are using [Alchemy](https://alchemy.com/) to connect to the network, but you can use an alternative JSON-RPC URL like [Infura](https://infura.io/) if you want. |
| 48 | + |
| 49 | +::::tabsgroup{options=Alchemy,Infura} |
| 50 | + |
| 51 | +:::tab{value=Alchemy} |
| 52 | + |
| 53 | +```js |
| 54 | +// Go to https://alchemy.com, sign up, create a new App in |
| 55 | +// its dashboard, and replace "KEY" with its key |
| 56 | +const ALCHEMY_API_KEY = "KEY"; |
| 57 | + |
| 58 | +// Replace this private key with your Sepolia account private key |
| 59 | +// To export your private key from Coinbase Wallet, go to |
| 60 | +// Settings > Developer Settings > Show private key |
| 61 | +// To export your private key from Metamask, open Metamask and |
| 62 | +// go to Account Details > Export Private Key |
| 63 | +// Beware: NEVER put real Ether into testing accounts |
| 64 | +const SEPOLIA_PRIVATE_KEY = "YOUR SEPOLIA PRIVATE KEY"; |
| 65 | + |
| 66 | +module.exports = { |
| 67 | + // ...rest of your config... |
| 68 | + networks: { |
| 69 | + sepolia: { |
| 70 | + url: `https://eth-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`, |
| 71 | + accounts: [SEPOLIA_PRIVATE_KEY], |
| 72 | + }, |
| 73 | + }, |
| 74 | +}; |
| 75 | +``` |
| 76 | + |
| 77 | +::: |
| 78 | + |
| 79 | +:::tab{value=Infura} |
| 80 | + |
| 81 | +```js |
| 82 | +// Go to https://infura.io, sign up, create a new API key |
| 83 | +// in its dashboard, and replace "KEY" with it |
| 84 | +const INFURA_API_KEY = "KEY"; |
| 85 | + |
| 86 | +// Replace this private key with your Sepolia account private key |
| 87 | +// To export your private key from Coinbase Wallet, go to |
| 88 | +// Settings > Developer Settings > Show private key |
| 89 | +// To export your private key from Metamask, open Metamask and |
| 90 | +// go to Account Details > Export Private Key |
| 91 | +// Beware: NEVER put real Ether into testing accounts |
| 92 | +const SEPOLIA_PRIVATE_KEY = "YOUR SEPOLIA PRIVATE KEY"; |
| 93 | + |
| 94 | +module.exports = { |
| 95 | + // ...rest of your config... |
| 96 | + networks: { |
| 97 | + sepolia: { |
| 98 | + url: `https://sepolia.infura.io/v3/${INFURA_API_KEY}`, |
| 99 | + accounts: [SEPOLIA_PRIVATE_KEY], |
| 100 | + }, |
| 101 | + }, |
| 102 | +}; |
| 103 | +``` |
| 104 | + |
| 105 | +::: |
| 106 | + |
| 107 | +:::: |
| 108 | + |
| 109 | +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: |
| 110 | + |
| 111 | +- [Alchemy Sepolia Faucet](https://sepoliafaucet.com/) |
| 112 | + |
| 113 | +:::tip |
| 114 | + |
| 115 | +This guide assumes you are using the contracts and deployment module from the [quick start guide](/ignition/docs/getting-started#quick-start), but the steps are the same for any contract. |
| 116 | + |
| 117 | +::: |
| 118 | + |
| 119 | +Now you are ready to deploy your module to the testnet, but first we are going to make the source code of our contract unique. The reason we need to do this is that the sample code from the quick start guide is already verified in Sepolia, so if you try to verify it you'll get an error. If you are using your own contracts, you can likely skip this step. |
| 120 | + |
| 121 | +Open your contract and add a comment with something unique, like your GitHub's username. Keep in mind that whatever you include here will be, like the rest of the code, publicly available on Etherscan: |
| 122 | + |
| 123 | +```solidity |
| 124 | +// Author: @janedoe |
| 125 | +contract Rocket { |
| 126 | +``` |
| 127 | + |
| 128 | +You can now run the deployment using the newly added Sepolia network: |
| 129 | + |
| 130 | +::::tabsgroup{options="TypeScript,JavaScript"} |
| 131 | + |
| 132 | +:::tab{value="TypeScript"} |
| 133 | + |
| 134 | +```sh |
| 135 | +npx hardhat ignition deploy ignition/modules/Apollo.ts --network sepolia --verify |
| 136 | +``` |
| 137 | + |
| 138 | +::: |
| 139 | + |
| 140 | +:::tab{value="JavaScript"} |
| 141 | + |
| 142 | +```sh |
| 143 | +npx hardhat ignition deploy ignition/modules/Apollo.js --network sepolia --verify |
| 144 | +``` |
| 145 | + |
| 146 | +::: |
| 147 | + |
| 148 | +:::: |
| 149 | + |
| 150 | +The `--verify` flag is optional, but it tells Hardhat Ignition to verify the contracts immediately after finishing the deployment. If you have an existing deployment and want to verify it, you can also run the `verify` task directly using the deployment ID: |
| 151 | + |
| 152 | +```sh |
| 153 | +npx hardhat ignition verify chain-11155111 |
| 154 | +``` |
| 155 | + |
| 156 | +:::tip |
| 157 | + |
| 158 | +If you get an error saying that the address does not have bytecode, it probably means that Etherscan has not indexed your contract yet. In that case, wait for a minute and then try again. |
| 159 | + |
| 160 | +::: |
| 161 | + |
| 162 | +After the task is successfully executed, you'll see a link to the publicly verified code of your contract. |
| 163 | + |
| 164 | +To learn more about verifying, read the [hardhat-verify](/hardhat-runner/plugins/nomicfoundation-hardhat-verify) documentation. |
0 commit comments