Let's write a cool smart contract on remix
image

That random-written smart contract has a faction system.
When you receive or send a token, both sender & recipient will join a random faction.
That faction will have a power unit that increase or decrease based on the transfers.

It's so cool that we left the floating pragma

pragma solidity ^0.8.4;

Now let's deploy it! For this test i'll use bsc testnet

image

Yes! now let's verify it!
https://testnet.bscscan.com/address/0xf20c5F772a27d257236E61d93B567FeE11Cf94e5

Click on "Contract"

image

Click "Verify and Publish" image

Now select the compiler version and license...
mmm.... what that was?? image

I've written and compiled this contract using Remix, so i can get the data here image

Let's go ahead and verify it! image

Paste the solidity code, select "I am not a robot" and press "verify and publish" image

OH NO image

Let's start over... what happened?

AHHHH
version 0.8.19, not 0.8.18! image

Ok now we're good to go! image

OMG krako you're doing the error thing again!
yes, so you will know how to avoid those.

We fixed the solidity version... what's missing?
image

Will it verify now?
image I mean... it's all 1:1

  • compiler version
  • optimization --> yes
  • optimization runs --> 200 (if you input that wrong will fail again) image So... we're lost?

No, simply you cannot verify that contract from the explorer.
Why?
Simple, READ THE ERROR.
It doesn't support imports like we did in this one.

So what we are gonna do now?
Lucky HardHat has a plugin that can save us, let's configure our workspace!
image add the key related to your network, you can get a list of supported network with

npx hardhat verify --list-networks

In my case, bscTestnet
Get an API key for that explorer (in my case, https://bscscan.com/myapikey same as BSCScan api key) and set it in your .env file.
Now,

  • Create a new contract in your /contracts folder with the same name, same code as the one deployed.

  • Configure the compiler & optimizer settings to be 1:1 as the deployment scenario (0.8.19, 200 opt) in your hardhat.config.js

  • Run:

npx hardhat clean (optional if you have troubles)
npx hardhat compile
npx hardhat verify 0xf20c5F772a27d257236E61d93B567FeE11Cf94e5 --network bsctest

And wait for the magic... image

image Boom!!! we're on!

Notice:

  • The --network bsctest is the network name in the key "networks" in the config
  • You want to verify your contract directly from HardHat, it's really useless to go on the explorer, i don't do that anymore
  • There has been 3/4 contracts that I've failed to verify, but i'm quite sure it was never an explorer-related bug
  • The example contract has been written in ~20mins and it's not indeed suitable for mainnet.
  • Link to the original code [https://testnet.bscscan.com/address/0xf20c5F772a27d257236E61d93B567FeE11Cf94e5#code]here
  • The test function is a test function that test the transfer function. much wow.