Powered By GitBook
πŸ“©
Submitting your Smart Contract to Etherscan
Step by step guide on submitting your smart contract's ABI to Etherscan so anyone can look at the contract's methods and variables and understand how to interact with it.
You should have completed Part 1: Creating and Deploying a Smart Contract, and Part 2: Interacting with Your Smart Contract prior to starting this section below.

Part 3: Publish your Smart Contract to Etherscan

You did all the hard work of bringing your smart contract to life - now it's time to share it with the world!
By verifying your smart contract on Etherscan, anyone can view your source code and interact with your smart contract. Let's get started!

Step 1: Generate an API Key on your Etherscan account

An Etherscan API Key is necessary to verify that you're the owner of the smart contract that you're trying to publish.
If you don't have an Etherscan account, first sign up using this link.
Once logged in, press your username on the top right, and select the "My profile" button:
Next, navigate to the "API-KEYs" button on the left tab bar. Then press the "Add" button, name your app whatever you wish (we chose hello-world), and then select continue.
Once you've followed the steps above, you should be able to view your new API key, which we've highlighted in red below. Copy this API key to your clipboard.
Now, let's update your .env file to include your Etherscan API Key.
If you were following the Hardhat tutorial, your .envfile should look like this:
1
API_URL = "https://eth-ropsten.alchemyapi.io/v2/your-api-key"
2
API_KEY = "your-api-key"
3
PRIVATE_KEY = "your-private-account-address"
4
ETHERSCAN_API_KEY = "your-etherscan-key"
Copied!
If you were following the Truffle tutorial, your .env file should look like this:
1
API_URL = "https://eth-ropsten.alchemyapi.io/v2/your-api-key"
2
MNEMONIC = "your-metamask-seed-reference"
3
PUBLIC_KEY = "your-public-account-address"
4
PRIVATE_KEY = "your-private-account-address"
5
ETHERSCAN_API_KEY = "your-etherscan-key"
Copied!

Step 2: HardHat-deployed smart contracts

This is where our steps diverge for HardHat and Truffle deployed smart contracts, as they require different plugins. Skip to the Step 3: Truffle-deployed smart contracts section if you deployed your contract using Truffle.

Step 2.1 Install the hardhat-etherscan plugin

Publishing your contract to Etherscan with HardHat is super simple. To get started, you will first need to install the hardhat-etherscan plugin to automatically verify your smart contract's source code and ABI on Etherscan. In your hello-world project directory run:
1
npm install --save-dev @nomiclabs/hardhat-etherscan
Copied!
Once installed, include the following statement at the top of your hardhat.config.js, and add the Etherscan config options:
1
// hardhat.config.js
2
​
3
require('dotenv').config();
4
require("@nomiclabs/hardhat-ethers");
5
require("@nomiclabs/hardhat-etherscan");
6
​
7
const { API_URL, PRIVATE_KEY } = process.env;
8
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY;
9
​
10
module.exports = {
11
solidity: "0.7.3",
12
defaultNetwork: "ropsten",
13
networks: {
14
hardhat: {},
15
ropsten: {
16
url: API_URL,
17
accounts: [`0x${PRIVATE_KEY}`]
18
}
19
},
20
etherscan: {
21
// Your API key for Etherscan
22
// Obtain one at https://etherscan.io/
23
apiKey: ETHERSCAN_API_KEY
24
}
25
};
Copied!

Step 2.2 Verify your smart contract on Etherscan!

Make sure your files are properly saved (especially if you're in VSCode) and your .env variables are properly configured.
Finally, run the verify task, passing the address of the contract, and the first message argument string that we deployed it with:
1
npx hardhat verify --network ropsten DEPLOYED_CONTRACT_ADDRESS 'Hello World!'
Copied!
Make sure that DEPLOYED_CONTRACT_ADDRESS is the address of your deployed smart contract on the Ropsten test network. Also, the last argument,'Hello World!' must be the same string value that you used during the deploy step in Part 1.
If all goes well, you should see the following message in your terminal:
1
Successfully submitted source code for contract
2
contracts/HelloWorld.sol:HelloWorld at 0xdeployed-contract-address
3
for verification on Etherscan. Waiting for verification result...
4
​
5
​
6
Successfully verified contract HelloWorld on Etherscan.
7
https://ropsten.etherscan.io/address/<contract-address>#contracts
Copied!
Congrats! Your smart contract code should be on Etherescan! Check out Step 4 to see how to view your smart contract code!

Step 3: Truffle-deployed smart contracts

Skip this section if you deployed your smart contract with HardHat.

Step 3.1: Install the truffle-plugin-verify plugin

We need the truffle-plugin-verify plugin to automatically verify your truffle smart contract's source code and ABI on Etherscan. In your project directory run:
1
npm install -g truffle-plugin-verify
Copied!
Once installed, add the plugin to your truffle-config.js file. Your file should look similar to this.
1
const HDWalletProvider = require("@truffle/hdwallet-provider");
2
require('dotenv').config()
3
​
4
const { API_URL, MNEMONIC } = process.env;
5
​
6
module.exports = {
7
networks: {
8
development: {
9
host: "localhost",
10
port: 8545,
11
network_id: "*", // Match any network id
12
gas: 5000000
13
},
14
ropsten: {
15
provider: function() {
16
return new HDWalletProvider(MNEMONIC, API_URL)
17
},
18
network_id: 3
19
}
20
},
21
compilers: {
22
solc: {
23
settings: {
24
optimizer: {
25
enabled: true, // Default: false
26
runs: 200 // Default: 200
27
},
28
}
29
}
30
},
31
plugins: ['truffle-plugin-verify'] //PLUGIN ADDED HERE
32
};
Copied!
You're almost at the finish line! πŸ˜…
Let's update your truffle-config.js file to include your Etherscan API key. See the bottom of the code below for reference:
1
const HDWalletProvider = require("@truffle/hdwallet-provider");
2
require('dotenv').config()
3
​
4
const { API_URL, MNEMONIC } = process.env;
5
​
6
module.exports = {
7
networks: {
8
development: {
9
host: "localhost",
10
port: 8545,
11
network_id: "*", // Match any network id
12
gas: 5000000
13
},
14
ropsten: {
15
provider: function() {
16
return new HDWalletProvider(MNEMONIC, API_URL)
17
},
18
network_id: 3
19
}
20
},
21
compilers: {
22
solc: {
23
settings: {
24
optimizer: {
25
enabled: true, // Default: false
26
runs: 200 // Default: 200
27
},
28
}
29
}
30
},
31
plugins: ['truffle-plugin-verify'],
32
api_keys: {
33
etherscan: process.env.ETHERSCAN_API_KEY
34
}
35
​
36
};
Copied!

Step 3.2 Verify your smart contract on Etherscan!

Last but not least, run the following command in your terminal:
1
truffle run verify HelloWorld --network ropsten
Copied!
If all goes well, you should see the following message in your terminal.
1
Verifying HelloWorld
2
Pass - Verified: https://ropsten.etherscan.io/address/<contract-address>#contracts
3
Successfully verified 1 contract(s).
Copied!

Step 4: Check out your smart contract on Etherscan!

When you navigate to the link provided in your terminal, you should be able to see your smart contract code and ABI published on Etherscan!
Wahooo - you did it champ! Now anyone can call or write to your smart contract! We can't wait to see what you build next!
πŸŽ‰
​
Once you complete this tutorial, let us know how your experience was or if you have any feedback by tagging us on Twitter @alchemyplatform!
Last modified 9d ago