Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto verify smart contracts functionality while deploying #64

Closed
wants to merge 10 commits into from
Closed

auto verify smart contracts functionality while deploying #64

wants to merge 10 commits into from

Conversation

ahmedali8
Copy link
Contributor

Used packages:

  1. @nomiclabs/hardhat-etherscan
  2. child_process

A contract with simple arguments can be easily verified directly without the need to running a verify command yourself. A contract with complex arguments can be verified too using automation, will also make a PR for that as well. I have tested both but only added a single feature in this PR.
Please review the code, looking forward for your response, Thank you so much!

@pakim249CAL
Copy link

This feature should be designed to be optional imo, and should not throw an error for an empty etherscan API key.

@ahmedali8
Copy link
Contributor Author

This feature should be designed to be optional imo, and should not throw an error for an empty etherscan API key.

Thank you for this suggestion, I didn't think of it this way. Let's see what is review from Paul.

@PaulRBerg
Copy link
Owner

I'm willing to merge this, but there should be some changes before I do that.

  1. We should use the hardat-etherscan plugin programatically, not via a node.js child process.
  2. Hardhat should run even with an empty etherscan api key. Consider passing the process.env.ETHERSCAN_API_KEY var directly to the apiKey field.

@ahmedali8
Copy link
Contributor Author

I'm willing to merge this, but there should be some changes before I do that.

  1. We should use the hardat-etherscan plugin programatically, not via a node.js child process.
  2. Hardhat should run even with an empty etherscan api key. Consider passing the process.env.ETHERSCAN_API_KEY var directly to the apiKey field.

I'll surely update these mentioned things and try once again, thank you for the guidance. Should I close this PR, update the code and make another PR for review ?

@PaulRBerg
Copy link
Owner

Should I close this PR, update the code and make another PR for review ?

No. You can git push --force and replace the existing work on you branch.

@ahmedali8
Copy link
Contributor Author

Should I close this PR, update the code and make another PR for review ?

No. You can git push --force and replace the existing work on you branch.

Thank you @PaulRBerg

@ahmedali8
Copy link
Contributor Author

Hey @PaulRBerg , I refactored the code using the programmatic verify:verify subtask, please review.
Thanks!

const greeterFactory: Greeter__factory = await ethers.getContractFactory("Greeter");
const greeter: Greeter = <Greeter>await greeterFactory.deploy(taskArguments.greeting);
await greeter.deployed();
console.log("Greeter deployed to: ", greeter.address);

if (network.name !== "hardhat" && config.etherscan.apiKey) {
await run("verify:verify", {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, if we call verify on etherscan right after contract deployment it will be always failed due to etherscan need time to load the smart contract code either. Then we should delay some minutes before invoke verification.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, yes I researched about this and I'll make it wait for atleast 1-2mins after deployment so etherscan could do it's stuff in the meantime. Thank you for the correction!

@sambacha
Copy link

sambacha commented Sep 29, 2021

Edit: you can actually just install hardhat-deploy and do hardhat --network mainnet sourcify to verify your contracts that way.

for what it's worth, if you enable the metadata option in solc's default output, you can verify with Sourceify.dev automatically just by uploading it to their endpoint.

here is a script that will upload your build/contracts output provided that you have enabled metadata: https://github.com/cgewecke/metacoin-source-verify/blob/master/scripts/ipfs.js

Configuring Solc output:

export const defaultSolcOutputSelection = {
  "*": {
    "*": [
      "abi",
      "evm.bytecode",
      "evm.deployedBytecode",
      "evm.methodIdentifiers",
      "metadata",
    ],
    "": ["ast"],
  },

see discussion here: NomicFoundation/hardhat#1863 - this may become the default in hardhat soon.

@PaulRBerg
Copy link
Owner

you can actually just install hardhat-deploy and do hardhat --network mainnet sourcify to verify your contracts that way.

I find hardhat-deploy unnecessarily complicated for most use cases - and the docs/ README are hard to digest.

for what it's worth, if you enable the metadata option in solc's default output, you can verify with Sourceify.dev automatically just by uploading it to their endpoint.

Thanks for the tip! I didn't know that.

@kuzdogan
Copy link

kuzdogan commented Nov 2, 2021

Just came across this discussion by chance.

you can actually just install hardhat-deploy and do hardhat --network mainnet sourcify to verify your contracts that way

This would be the easiest way if you are using hardhat-deploy to deploy contracts

For other cases it is a little manual:

Truffle: If you are using truffle to deploy your contracts, you can provide the json file of the contract deployed at build/contracts + the source files used (i.e. all .sol files, openzeppelin libraries etc.)

Compile in Hardhat and deploy manually: We have an easier verification for hardhat build outputs coming up, but for now, you'd have to

enable the metadata option in solc's default output

as stated. Then you'd need to extract the metadata of the contract at output.contracts.<contract-path>.<contract-name>.metadata in a seperate .json file. Here you'll see the metadata for all contracts but you need to extract only the deployed main contract's metadata. Finally provide the metadata + source files in the sourcify.dev interface.

Happy to help if you have any questions regarding Sourcify!

@PaulRBerg
Copy link
Owner

Thanks for your input, @kuzdogan. There has been a length discussion in #31 which at the time led me to believe that disabling the metadata hash is the best thing to do (since the template is aimed at newbies who might be confused by the impact of the metadata hash on the bytecode).

But not supporting Sourcify out-of-the-box is not nice. I will reconsider this. Might be worth it to build a Hardhat plugin for Sourcify?

@kuzdogan
Copy link

kuzdogan commented Nov 3, 2021

As stated above hardhat-deploy has Sourcify support but a plugin would be as simple as calls to our API. I don't think we can focus on a seperate plugin for a while since hardhat-deploy exists and is quite popular. But it's welcome and would be nice to have 🙂. It can be spun off the hardhat-deploy plugin directly I guess.

+ NomicFoundation/hardhat#1863 is merged so we are expecting hardhat to output metadata by default with the next release. Then our goal is to verify a contract at a given address and chain by only the output .json in artifacts/build-info, since it contains both sources and metadatas.

@ahmedali8 ahmedali8 closed this Feb 14, 2022
@xtools-at
Copy link

But not supporting Sourcify out-of-the-box is not nice. I will reconsider this. Might be worth it to build a Hardhat plugin for Sourcify?

@PaulRBerg i've built one recently (i.e. forked and updated a defunct one) in case you're interested: @xtools-at/hardhat-sourcify

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants