Skip to content

Commit

Permalink
Update verification guide to use ignition (#4833)
Browse files Browse the repository at this point in the history
* update verification guide to use ignition

* docs: add lang to code sections in verify

* docs: update the verification guide to use vars

* docs: fix linting issue

---------

Co-authored-by: John Kane <[email protected]>
  • Loading branch information
zoeyTM and kanej committed Mar 11, 2024
1 parent 5051bdb commit 8c5b790
Showing 1 changed file with 64 additions and 27 deletions.
91 changes: 64 additions & 27 deletions docs/src/content/hardhat-runner/docs/guides/verifying.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,48 @@

Once your contract is ready, the next step is to deploy it to a live network and verify its source code.

:::tip
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.

Looking for a way to verify your contracts deployed via a Hardhat Ignition module? Check out the Hardhat Ignition [guide to verifying contracts](/ignition/guides/verify).
In this guide we'll explain how to do this in the [Etherscan](https://etherscan.io/) explorer.

:::
:::tip

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.
If you'd like to verify on Sourcify instead of Etherscan, or if you wish to verify a contract deployed outside of Hardhat Ignition, you can use the [hardhat-verify plugin](/hardhat-runner/plugins/nomicfoundation-hardhat-verify).

In this guide we'll explain how to do this in the [Etherscan](https://etherscan.io/) explorer, but there are other ways to verify a contract, for example with [Sourcify](https://sourcify.dev/).
:::

## Getting an API key from Etherscan

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.

Open your Hardhat config and add the API key you just created:
Store the API key as the configuration variable `ETHERSCAN_API_KEY`:

```sh
$ npx hardhat vars set ETHERSCAN_API_KEY
✔ Enter value: ********************************
```

:::tip

Learn more about setting and using configuration variable in [our guide](/guides/configuration-variables).

:::

Open your Hardhat config and set the Etherscan API key as the stored configuration variable `ETHERSCAN_API_KEY`:

::::tabsgroup{options=TypeScript,JavaScript}

:::tab{value=TypeScript}

```ts
import { vars } from "hardhat/config";

const ETHERSCAN_API_KEY = vars.get("ETHERSCAN_API_KEY");

export default {
// ...rest of the config...
etherscan: {
apiKey: "ABCDE12345ABCDE12345ABCDE123456789",
apiKey: ETHERSCAN_API_KEY,
},
};
```
Expand All @@ -36,10 +53,14 @@ export default {
:::tab{value=JavaScript}

```js
const { vars } = require("hardhat/config");

const ETHERSCAN_API_KEY = vars.get("ETHERSCAN_API_KEY");

module.exports = {
// ...rest of the config...
etherscan: {
apiKey: "ABCDE12345ABCDE12345ABCDE123456789",
apiKey: ETHERSCAN_API_KEY,
},
};
```
Expand All @@ -58,16 +79,19 @@ We are going to use the [Sepolia testnet](https://ethereum.org/en/developers/doc

```js
// Go to https://infura.io, sign up, create a new API key
// in its dashboard, and replace "KEY" with it
const INFURA_API_KEY = "KEY";
// in its dashboard, and store it as the "INFURA_API_KEY"
// configuration variable
const INFURA_API_KEY = vars.get("INFURA_API_KEY");

// Replace this private key with your Sepolia 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
// Store the private key as the "SEPOLIA_PRIVATE_KEY" configuration
// variable.
// Beware: NEVER put real Ether into testing accounts
const SEPOLIA_PRIVATE_KEY = "YOUR SEPOLIA PRIVATE KEY";
const SEPOLIA_PRIVATE_KEY = vars.get("SEPOLIA_PRIVATE_KEY");

module.exports = {
// ...rest of your config...
Expand All @@ -86,16 +110,19 @@ module.exports = {

```js
// Go to https://alchemy.com, sign up, create a new App in
// its dashboard, and replace "KEY" with its key
const ALCHEMY_API_KEY = "KEY";
// its dashboard, and store it as the "ALCHEMY_API_KEY"
// configuration variable
const ALCHEMY_API_KEY = vars.get("ALCHEMY_API_KEY");

// Replace this private key with your Sepolia 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
// Store the private key as the "SEPOLIA_PRIVATE_KEY" configuration
// variable.
// Beware: NEVER put real Ether into testing accounts
const SEPOLIA_PRIVATE_KEY = "YOUR SEPOLIA PRIVATE KEY";
const SEPOLIA_PRIVATE_KEY = vars.get("SEPOLIA_PRIVATE_KEY");

module.exports = {
// ...rest of your config...
Expand Down Expand Up @@ -125,32 +152,44 @@ Open your contract and add a comment with something unique, like your GitHub's u
contract Lock {
```

You can now run the deploy script using the newly added Sepolia network:
To run the deployment we will leverage the Ignition module `LockModule` that we created in the [Deploying your contracts](./deploying.md) guide. Run the deployment using Hardhat Ignition and the newly added Sepolia network:

::::tabsgroup{options=TypeScript,JavaScript}
::::tabsgroup{options="TypeScript,JavaScript"}

:::tab{value=TypeScript}
:::tab{value="TypeScript"}

```
npx hardhat run scripts/deploy.ts --network sepolia
```shell
npx hardhat ignition deploy ignition/modules/LockModule.ts --network sepolia --deployment-id sepolia-deployment
```

:::

:::tab{value=JavaScript}
:::tab{value="JavaScript"}

```
npx hardhat run scripts/deploy.js --network sepolia
```shell
npx hardhat ignition deploy ignition/modules/LockModule.js --network sepolia --deployment-id sepolia-deployment
```

:::

::::

Take note of the address and the unlock time and run the `verify` task with them:
:::tip

The `--deployment-id` flag is optional, but it allows you to give a custom name to your deployment. This makes it easier to refer to later, for instance when you want to verify it.

:::

Lastly, to verify the deployed contract, you can run the `ignition verify` task and pass the deployment Id:

```sh
npx hardhat ignition verify sepolia-deployment
```
npx hardhat verify --network sepolia <address> <unlock time>

Alternatively, you can combine deployment and verification into one step, by invoking the `deploy` task with the `--verify` flag:

```sh
npx hardhat ignition deploy ignition/modules/LockModule.js --network sepolia --deployment-id sepolia-deployment --verify
```

:::tip
Expand All @@ -159,6 +198,4 @@ If you get an error saying that the address does not have bytecode, it probably

:::

After the task is successfully executed, you'll see a link to the publicly verified code of your contract.

To learn more about verifying, read the [hardhat-verify](/hardhat-runner/plugins/nomicfoundation-hardhat-verify) documentation.
After the `ignition verify` task is successfully executed, you'll see a link to the publicly verified code of your contract.

0 comments on commit 8c5b790

Please sign in to comment.