You should have completed part 1 creating and deploying a smart contract, and part 2 interacting with your smart contract prior to starting part 3 below.
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!
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 .env
file should look like this:
API_URL = "https://eth-ropsten.alchemyapi.io/v2/your-api-key"PUBLIC_KEY = "your-public-account-address"PRIVATE_KEY = "your-private-account-address"ETHERSCAN_API_KEY = "your-etherscan-key"
If you were following the Truffle tutorial, your .env file should look like this:
API_URL = "https://eth-ropsten.alchemyapi.io/v2/your-api-key"MNEMONIC = "your-metamask-seed-reference"PUBLIC_KEY = "your-public-account-address"PRIVATE_KEY = "your-private-account-address"ETHERSCAN_API_KEY = "your-etherscan-key"
This is where our steps diverge for HardHat and Truffle deployed smart contracts, as they require different plugins. Skip to the Step 2: Truffle-deployed smart contracts section if you deployed your contract using Truffle.
hardhat-etherscan
pluginPublishing your contract to Etherescan 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 project directory run:
npm install --save-dev @nomiclabs/hardhat-etherscan
Once installed, include the following statement at the top of your hardhat.config.js
:
require("@nomiclabs/hardhat-etherscan");
Next, add the following Etherscan config to your hardhat.config.js
file:
module.exports = {networks: {mainnet: { ... }},etherscan: {// Your API key for Etherscan// Obtain one at https://etherscan.io/apiKey: process.env.ETHERSCAN_API_KEY}};
Finally, run the verify
task, passing the address of the contract, and the network where it's deployed:
npx hardhat verify --network ropsten DEPLOYED_CONTRACT_ADDRESS "Hello World!"
If all goes well, you should see the following message in your terminal:
Successfully submitted source code for contractcontracts/HelloWorld.sol:HelloWorld at 0xdeployed-contract-addressfor verification on Etherscan. Waiting for verification result...Successfully verified contract HelloWorld on Etherscan.https://ropsten.etherscan.io/address/<contract-address>#contracts
Congrats! Your smart contract code should be on Etherescan! Check out Step 4 to see how to view your smart contract code!
Skip this section if you deployed your smart contract with HardHat.
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:
npm install -g truffle-plugin-verify
Once installed, add the plugin to your truffle-config.js
file. Your file should look similar to this.
const HDWalletProvider = require("@truffle/hdwallet-provider");require('dotenv').config()const { API_URL, MNEMONIC } = process.env;module.exports = {networks: {development: {host: "localhost",port: 8545,network_id: "*", // Match any network idgas: 5000000},ropsten: {provider: function() {return new HDWalletProvider(MNEMONIC, API_URL)},network_id: 3}},compilers: {solc: {settings: {optimizer: {enabled: true, // Default: falseruns: 200 // Default: 200},}}},plugins: ['truffle-plugin-verify'] //PLUGIN ADDED HERE};
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:
const HDWalletProvider = require("@truffle/hdwallet-provider");require('dotenv').config()const { API_URL, MNEMONIC } = process.env;module.exports = {networks: {development: {host: "localhost",port: 8545,network_id: "*", // Match any network idgas: 5000000},ropsten: {provider: function() {return new HDWalletProvider(MNEMONIC, API_URL)},network_id: 3}},compilers: {solc: {settings: {optimizer: {enabled: true, // Default: falseruns: 200 // Default: 200},}}},plugins: ['truffle-plugin-verify'],api_keys: {etherscan: process.env.ETHERSCAN_API_KEY}};
Last but not least, run the following command in your terminal:
truffle run verify HelloWorld --network ropsten
If all goes well, you should see the following message in your terminal.
Verifying HelloWorldPass - Verified: https://ropsten.etherscan.io/address/<contract-address>#contractsSuccessfully verified 1 contract(s).
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!🎉