Programmatic EtherScan verify of OpenZeppelin SDK deployed contracts

I am programmaticlly deployment multiple contracts. I would like verify these on EtherScan. I have stued how OpenZeppelin cli does it internally. But to make sure I do it correctly I need to bridge the gap in the following bit:

I get a contract class

const TokenSwap = Contracts.getFromLocal('TokenSwap');

I deploy it

const deployed = await TokenSwap.new(...parameters, txParams);

And now the question is how do I extract all the parameters I need to pass to EtherScan verified function?

  const remote = "etherscan";
  const { compilerVersion, sourcePath } = this.localController.getContractSourcePath(contractName);
  const contractSource = await flattenSourceCode([sourcePath]);
  const contractAddress = this.networkFile.contracts[contractName].address;]
  const network = await ZWeb3.getNetworkName();

  await Verifier.verifyAndPublish(remote, {
    contractName,
    compilerVersion,
    optimizer,
    optimizerRuns,
    contractSource,
    contractAddress,
    apiKey,
    network: this.network,
  });

I need to create a LocalController - what is this? https://github.com/OpenZeppelin/openzeppelin-sdk/blob/master/packages/cli/src/models/local/LocalController.ts - Do I need to create ZWeb3 instance in some specific manner with OpenZeppelin project file to be able to access getContractSourcePath?

For the reference the full deployment script is here: https://github.com/Dawn-Protocol/dawn-erc20/blob/master/src/scripts/deployTestnet.ts

1 Like

Hi @miohtama,

I assume that the volume of contracts to be verified makes programmatic verification worthwhile (compared with doing it manually)?

You could look at the Etherscan documentation on using their API too, as this may be easier than repurposing some of the CLI code: https://etherscan.io/apis#contracts

Hi,

I have now solved this issue. I lifted EtherScan verification code from OpenZeppelin SDK. I made some changes in it, so that it has better separation of concern . No dependencies in ZWeb3 or SDK internal classes. Then I also modified it to support constructor arguments in the contracts.

Now you can verify programmatically deployed contracts with one JavaScript function call.

You can find the code here:

1 Like

Hi @miohtama,

Glad you were able to verify programmatically.