|
2 | 2 |
|
3 | 3 | # hardhat-etherscan
|
4 | 4 |
|
5 |
| -[Hardhat](https://hardhat.org) plugin for integration with [Etherscan](https://etherscan.io)'s contract verification service. |
| 5 | +The `@nomiclabs/hardhat-etherscan` plugin is deprecated in favor of our new [`@nomicfoundation/hardhat-verify`](/packages/hardhat-verify) plugin. |
6 | 6 |
|
7 |
| -## What |
| 7 | +## Migrating to `hardhat-verify` |
8 | 8 |
|
9 |
| -This plugin helps you verify the source code for your Solidity contracts on [Etherscan](https://etherscan.io). |
| 9 | +`hardhat-verify` is a drop-in replacement of `hardhat-etherscan`. To migrate to it: |
10 | 10 |
|
11 |
| -It's smart and it tries to do as much as possible to facilitate the process: |
12 |
| - |
13 |
| -- Just provide the deployment address and constructor arguments, and the plugin will detect locally which contract to verify. |
14 |
| -- If your contract uses Solidity libraries, the plugin will detect them and deal with them automatically. You don't need to do anything about them. |
15 |
| -- A simulation of the verification process will run locally, allowing the plugin to detect and communicate any mistakes during the process. |
16 |
| -- Once the simulation is successful the contract will be verified using the Etherscan API. |
17 |
| - |
18 |
| -## Installation |
19 |
| - |
20 |
| -```bash |
21 |
| -npm install --save-dev @nomiclabs/hardhat-etherscan |
22 |
| -``` |
23 |
| - |
24 |
| -And add the following statement to your `hardhat.config.js`: |
25 |
| - |
26 |
| -```js |
27 |
| -require("@nomiclabs/hardhat-etherscan"); |
28 |
| -``` |
29 |
| - |
30 |
| -Or, if you are using TypeScript, add this to your `hardhat.config.ts`: |
31 |
| - |
32 |
| -```js |
33 |
| -import "@nomiclabs/hardhat-etherscan"; |
34 |
| -``` |
35 |
| - |
36 |
| -## Tasks |
37 |
| - |
38 |
| -This plugin provides the `verify` task, which allows you to verify contracts through Etherscan's service. |
39 |
| - |
40 |
| -## Environment extensions |
41 |
| - |
42 |
| -This plugin does not extend the environment. |
43 |
| - |
44 |
| -## Usage |
45 |
| - |
46 |
| -You need to add the following Etherscan config to your `hardhat.config.js` file: |
47 |
| - |
48 |
| -```js |
49 |
| -module.exports = { |
50 |
| - networks: { |
51 |
| - mainnet: { ... } |
52 |
| - }, |
53 |
| - etherscan: { |
54 |
| - // Your API key for Etherscan |
55 |
| - // Obtain one at https://etherscan.io/ |
56 |
| - apiKey: "YOUR_ETHERSCAN_API_KEY" |
57 |
| - } |
58 |
| -}; |
59 |
| -``` |
60 |
| - |
61 |
| -Alternatively you can specify more than one block explorer API key, by passing an object under the `apiKey` property, see [`Multiple API keys and alternative block explorers`](#multiple-api-keys-and-alternative-block-explorers). |
62 |
| - |
63 |
| -Lastly, run the `verify` task, passing the address of the contract, the network where it's deployed, and the constructor arguments that were used to deploy it (if any): |
64 |
| - |
65 |
| -```bash |
66 |
| -npx hardhat verify --network mainnet DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1" |
67 |
| -``` |
68 |
| - |
69 |
| -### Complex arguments |
70 |
| - |
71 |
| -When the constructor has a complex argument list, you'll need to write a javascript module that exports the argument list. The expected format is the same as a constructor list for an [ethers contract](https://docs.ethers.io/v5/api/contract/). For example, if you have a contract like this: |
72 |
| - |
73 |
| -```solidity |
74 |
| -struct Point { |
75 |
| - uint x; |
76 |
| - uint y; |
77 |
| -} |
78 |
| -
|
79 |
| -contract Foo { |
80 |
| - constructor (uint x, string s, Point memory point, bytes b) { ... } |
81 |
| -} |
82 |
| -``` |
83 |
| - |
84 |
| -then you can use an `arguments.js` file like this: |
85 |
| - |
86 |
| -```js |
87 |
| -module.exports = [ |
88 |
| - 50, |
89 |
| - "a string argument", |
90 |
| - { |
91 |
| - x: 10, |
92 |
| - y: 5, |
93 |
| - }, |
94 |
| - // bytes have to be 0x-prefixed |
95 |
| - "0xabcdef", |
96 |
| -]; |
97 |
| -``` |
98 |
| - |
99 |
| -Where the third argument represents the value for the `point` parameter. |
100 |
| - |
101 |
| -The module can then be loaded by the `verify` task when invoked like this: |
102 |
| - |
103 |
| -```bash |
104 |
| -npx hardhat verify --constructor-args arguments.js DEPLOYED_CONTRACT_ADDRESS |
105 |
| -``` |
106 |
| - |
107 |
| -### Libraries with undetectable addresses |
108 |
| - |
109 |
| -Some library addresses are undetectable. If your contract uses a library only in the constructor, then its address cannot be found in the deployed bytecode. |
110 |
| - |
111 |
| -To supply these missing addresses, you can create a javascript module that exports a library dictionary and pass it through the `--libraries` parameter: |
112 |
| - |
113 |
| -```bash |
114 |
| -hardhat verify --libraries libraries.js OTHER_ARGS |
115 |
| -``` |
116 |
| - |
117 |
| -where `libraries.js` looks like this: |
118 |
| - |
119 |
| -```js |
120 |
| -module.exports = { |
121 |
| - SomeLibrary: "0x...", |
122 |
| -}; |
123 |
| -``` |
124 |
| - |
125 |
| -### Multiple API keys and alternative block explorers |
126 |
| - |
127 |
| -If your project targets multiple EVM-compatible networks that have different explorers, you'll need to set multiple API keys. |
128 |
| - |
129 |
| -To configure the API keys for the chains you are using, provide an object under `etherscan/apiKey` with the identifier of each chain as the key. **This is not necessarily the same name that you are using to define the network**. For example, if you are going to verify contracts in Ethereum mainnet, Optimism and Arbitrum, your config would look like this: |
130 |
| - |
131 |
| -```js |
132 |
| -module.exports = { |
133 |
| - networks: { |
134 |
| - mainnet: { ... }, |
135 |
| - testnet: { ... } |
136 |
| - }, |
137 |
| - etherscan: { |
138 |
| - apiKey: { |
139 |
| - mainnet: "YOUR_ETHERSCAN_API_KEY", |
140 |
| - optimisticEthereum: "YOUR_OPTIMISTIC_ETHERSCAN_API_KEY", |
141 |
| - arbitrumOne: "YOUR_ARBISCAN_API_KEY", |
142 |
| - } |
143 |
| - } |
144 |
| -}; |
145 |
| -``` |
146 |
| - |
147 |
| -To see the full list of supported networks, run `npx hardhat verify --list-networks`. The identifiers shown there are the ones that should be used as keys in the `apiKey` object. |
148 |
| - |
149 |
| -### Adding support for other networks |
150 |
| - |
151 |
| -If the chain you are using is not in the list, you can manually add the necessary information to verify your contracts on it. For this you need three things: the chain id of the network, the URL of the verification endpoint, and the URL of the explorer. |
152 |
| - |
153 |
| -For example, if Goerli wasn't supported, you could add it like this: |
154 |
| - |
155 |
| -``` |
156 |
| -etherscan: { |
157 |
| - apiKey: { |
158 |
| - goerli: "<goerli-api-key>" |
159 |
| - }, |
160 |
| - customChains: [ |
161 |
| - { |
162 |
| - network: "goerli", |
163 |
| - chainId: 5, |
164 |
| - urls: { |
165 |
| - apiURL: "https://api-goerli.etherscan.io/api", |
166 |
| - browserURL: "https://goerli.etherscan.io" |
167 |
| - } |
168 |
| - } |
169 |
| - ] |
170 |
| -} |
171 |
| -``` |
172 |
| - |
173 |
| -Keep in mind that the name you are giving to the network in `customChains` is the same one that has to be used in the `apiKey` object. |
174 |
| - |
175 |
| -To see which custom chains are supported, run `npx hardhat verify --list-networks`. |
176 |
| - |
177 |
| -### Using programmatically |
178 |
| - |
179 |
| -To call the verification task from within a Hardhat task or script, use the `"verify:verify"` subtask. Assuming the same contract as [above](#complex-arguments), you can run the subtask like this: |
180 |
| - |
181 |
| -```js |
182 |
| -await hre.run("verify:verify", { |
183 |
| - address: contractAddress, |
184 |
| - constructorArguments: [ |
185 |
| - 50, |
186 |
| - "a string argument", |
187 |
| - { |
188 |
| - x: 10, |
189 |
| - y: 5, |
190 |
| - }, |
191 |
| - "0xabcdef", |
192 |
| - ], |
193 |
| -}); |
194 |
| -``` |
195 |
| - |
196 |
| -If the verification is not successful, an error will be thrown. |
197 |
| - |
198 |
| -#### Providing libraries from a script or task |
199 |
| - |
200 |
| -If your contract has libraries with undetectable addresses, you may pass the libraries parameter with a dictionary specifying them: |
201 |
| - |
202 |
| -```js |
203 |
| -hre.run("verify:verify", { |
204 |
| - // other args |
205 |
| - libraries: { |
206 |
| - SomeLibrary: "0x...", |
207 |
| - } |
208 |
| -} |
209 |
| -``` |
210 |
| -
|
211 |
| -## How it works |
212 |
| -
|
213 |
| -The plugin works by fetching the bytecode in the given address and using it to check which contract in your project corresponds to it. Besides that, some sanity checks are performed locally to make sure that the verification won't fail. |
214 |
| -
|
215 |
| -## Known limitations |
216 |
| -
|
217 |
| -- Adding, removing, moving or renaming new contracts to the hardhat project or reorganizing the directory structure of contracts after deployment may alter the resulting bytecode in some solc versions. See this [Solidity issue](https://github.com/ethereum/solidity/issues/9573) for further information. |
| 11 | +1. Uninstall the `@nomiclabs/hardhat-etherscan` package |
| 12 | +2. Install the `@nomicfoundation/hardhat-verify` package |
| 13 | +3. Update your Hardhat config to import `@nomicfoundation/hardhat-verify` instead of `@nomiclabs/hardhat-etherscan` |
0 commit comments