Let’s see how you can verify your smart contract using a gist of your project. The process is extremely simple, but you will need a couple things:
- A working smart contract. Use your own or clone this repo.
- Some Rinkeby ETH.
- A GitHub account.
For this example I am deploying a simple ballot smart contract; you can use this method for any project you have since the important part is really after the contract is deployed, however, if you would rather follow along you can clone this repo.
The Setup
I use Truffle to write, test, and deploy my contracts, so make sure you have Truffle installed globally. Cool, the contract we are deploying is a simple ballot contract found here.
If you are following along with the example repo, simply run:
$ npm install
Otherwise, let’s get truffle ready to deploy, we are going to need the following packages:
$ npm install --save-dev dotenv
$ npm install --save-dev ethereumjs-wallet
$ npm install --save-dev truffle-wallet-provider
Great, let’s get our truffle.js file ready to deploy to Rinkeby:
require('dotenv').config()
const Web3 = require(‘web3’)
const web3 = new Web3()
const WalletProvider = require(‘truffle-wallet-provider’)
const Wallet = require(‘ethereumjs-wallet’)const rinkebyPrivateKey = new Buffer(process.env[‘RINKEBY_PRIVATE_KEY’], ‘hex’)
const rinkebyWallet = Wallet.fromPrivateKey(rinkebyPrivateKey)
const rinkebyProvider = new WalletProvider(rinkebyWallet, ‘https://rinkeby.infura.io/')module.exports = {
migrations_directory: ‘./migrations’,
networks: {
development: {
host: ‘localhost’,
port: 9545,
network_id: ‘*’ // Match any network id
},
rinkeby: {
provider: rinkebyProvider,
// You can get the current gasLimit by running
// truffle deploy — network rinkeby
// truffle(rinkeby)> web3.eth.getBlock(“pending”, (error, result)
// console.log(result.gasLimit))
gas: 6721975,
gasPrice: web3.toWei(‘50’, ‘gwei’),
network_id: ‘4’,
}
}
}
We are deploying through Infura using truffle wallet provider and ethereumjs wallet. Now let’s create a .env file in the root directory with your private key for deploying, place your private key in that file:
RINKEBY_PRIVATE_KEY=”MY_PRIVATE_KEY_GOES_HERE..."
Make sure you add this file to your .gitignore file to make sure you don’t accidentally publish your private key.
Deploying the Contract to Rinkeby
Time to deploy to rinkeby. First let’s get some test ETH from the Rinkeby Faucet. Now, let’s enter truffle develop environment:
$ truffle develop
Time to compile, migrate, and test locally to make sure we are good to go, run the following commands:
truffle(develop)> compile
...
..
.truffle(develop)> migrate
...
..
.truffle(develop)> test
...
..
.
If they all succeed with no errors, we are ready to deploy. Exit truffle develop environment and in the terminal run the following command:
$ truffle deploy --network rinkeby
You should get an output that looks something like this:
Verifying our Contract
Head over to Rinkeby Etherscan and search your newly deployed contract using the contract address from your console. You should see your contract on the network:
Cool. Now click the Code tab and click on Verify and Publish:
We are going to use the Fetch from Gist option. First we need to create a new Gist. In your GitHub account, click on your profile and select Your gists. You should see your gist profile:
In the file name text box type the name of your contract including the extension and in the gist box copy/paste your contract code from your project. Make sure the contract is exactly the same as your contract file. If you are importing any files into your source code include the code directly in your gist. Mine looks like this:
Now click Create public gist. Sweet! Time to verify. Back to Etherscan click the Fetch from gist button and copy/paste your Gist id:
Click Continue and Etherscan should load your code:
Excellent, now type your contract name in the Contract Name box, select your compiler version, if you used Optimization select it. If your constructor was deployed using arguments Etherscan should have picked up the encoded parameters automatically. Time to verify!
Click Verify and Publish and give it a little time:
After a few seconds your contract will be verified:
Done!Now your contract is officially verified and accessible to anyone with the contract address:
That’s it! Here is the repo for this example and the gist in case you want to give it a try!
Thank you very much for reading this post! If you found this post useful please clap on this article and make sure to follow me for more regular content, also check out my Github as I regularly post sample code and projects. If you have any questions feel free to reply below this post or shoot me an email.
Happy coding!