Skip to main content

Verifying Smart Contracts with Truffle Verify

This tutorial includes items from the truffle quickstart docs

Inspired by truffle verify docs

Create a project

Make sure you have truffle installed:

npm install -g truffle

Create a new directory for your Truffle project:

mkdir MetaCoin
cd MetaCoin

Download ("unbox") the MetaCoin box:

truffle unbox metacoin

Once this operation is completed, you'll now have a project structure with the following items:

  • contracts/: Directory for Solidity contracts
  • migrations/: Directory for scriptable deployment files
  • test/: Directory for test files for testing your application and contracts
  • truffle.js: Truffle configuration file

Compiling

Before we compile our smart contract, we must set up our environment

Run the following commands:

npm init -y 
yarn add @truffle/hdwallet-provider yarn add -D truffle-plugin-verify 

Create a .env.json file in your project's root directory:

{
"mnemonic": "your-wallet-seed-phrase",
"snowtraceApiKey": "your-snowtrace-api-key"
}

Get your snowtrace API key here

Configure your truffle-config.js file to the appropriate settings:

/**
* Use this file to configure your truffle project. It's seeded with some
* common settings for different networks and features like migrations,
* compilation and testing. Uncomment the ones you need or modify
* them to suit your project as necessary.
*
* More information about configuration can be found at:
*
* trufflesuite.com/docs/advanced/configuration
*
* To deploy via Infura you'll need a wallet provider (like @truffle/hdwallet-provider)
* to sign your transactions before they're sent to a remote public node. Infura accounts
* are available for free at: infura.io/register.
*
* You'll also need a mnemonic - the twelve word phrase the wallet uses to generate
* public/private key pairs. If you're publishing your code to GitHub make sure you load this
* phrase from a file you've .gitignored so it doesn't accidentally become public.
*
*/

const HDWalletProvider = require("@truffle/hdwallet-provider");

//
const { snowtraceApiKey, mnemonic } = require("./.env.json");

module.exports = {
/**
* Networks define how you connect to your ethereum client and let you set the
* defaults web3 uses to send transactions. If you don't specify one truffle
* will spin up a development blockchain for you on port 9545 when you
* run `develop` or `test`. You can ask a truffle command to use a specific
* network from the command line, e.g
*
* $ truffle test --network <network-name>
*/

plugins: [
'truffle-plugin-verify'
],
api_keys: {
snowtrace: snowtraceApiKey
},
networks: {

fuji: {
provider: () => new HDWalletProvider(mnemonic, `https://api.avax-test.network/ext/bc/C/rpc`),
network_id: 1,
timeoutBlocks: 200,
confirmations: 5
}
}
};

Network can be configured for mainnet deployment(see Alternatives)

Run the following command:

truffle compile

Once this operation is completed, your ./build/contracts folder should contain the following items:

  • ConvertLib.json
  • MetaCoin.json
  • Migrations.json

Migrate

Run the following command:

npx truffle migrate --network fuji

You should see the txn activity in your terminal truffle-verify-txn1

truffle-verify-txn2

truffle-verify-txn3

Truffle verify

Truffle verify allows users to verify contracts from the CLI

Fuji Testnet

Take a look at the Fuji Testnet Explorer here and read more about truffle verify here

If you have issues, contact us on Discord

  1. Run the following command:
npx truffle run verify ConvertLib MetaCoin --network fuji
  1. Wait for the verification message from the CLI truffle-verify-message1
  1. View the verified contract truffle-verify-view-contract

Mainnet

Configure your truffle-config.js file to the appropriate settings:

module.exports = {
...
plugins: [
'truffle-plugin-verify'
],
api_keys: {
snowtrace: snowtraceApiKey
},
networks: {

mainnet: {
provider: () => new HDWalletProvider(mnemonic, `https://api.avax.network/ext/bc/C/rpc`),
network_id: 1,
timeoutBlocks: 200,
confirmations: 5
}
}
};

Run the following commands:

truffle migrate --network mainnet
truffle verify CovertLib MetaCoin --network mainnet

Thanks for reading 🔺