Verifying Smart Contracts in Conflux eSpace


Hey Conflux devs!

I know that there were a lot of big news around Conflux: China’s Instagram integration and the partnership with China Telecom. I hope that Conflux will be soon in the top 10.

Not investment advice.

Thus I received a lot of messages rewarding about how to build on Conflux, verify contracts, interact with the CLI, how the two spaces can work together etc…

The Conflux devs are working really hard in the the new Conflux Developer Portal.

Take a look at the preview:

https://twitter.com/Conflux_Network/status/1623325822776901634

In the meantime I hope that with these short tutorials you can continue your development journey in Conflux, until the new dev portal is finished.

Tutorial

Ok let’s verify our contract on eSpace Testnet.

So You just deployed your contract and if you go to the conflux scan you will see these when you are trying to interact with your contract.

This means that conflux scan needs to know how your contract methods interface is and what information it holds.

You will need as followed in order to get your contract already verified:

You will need to have the same code you deployed on blockchain otherwise it will fail

  • Contract Address
  • Source code (If you contract is extended by another, you will need to flat your contract code, see below)
  • Solidity compiler version
  • If your are using optimizations how many runs. If you are using Hardhat you can search this on the config file.
  • License Type

Contract Flatten

If you are importing another libraries as OpenZeppelin or other contract. You will need to get your contract flatten since this method currently support only single-files for verifying.

Hardhat

If you are using hardhat it has a built-in method to flat our contract.

Try this on your terminar

npx hardhat flatten ./contract/MyContractName.sol` > MyContractNameFlatten.sol

Then you will get a contract with all imports in a single-file.

Most of the times it will throw you an error about multiple license identifiers, if it is the case, you just need to remove all SPDX-License-Identifier: <License-Identifier> but keeping one.

Verify contract

If you are using hardhat or truffle it will be easy to run an script and get your contract verified. In the case of hardhat you can create a file:

// scripts/verifyContract.ts

import fs from 'fs';
import axios from 'axios';


/*
   - eSpace Testnet uri
   If you need another environment see this: https://evmapi-testnet.confluxscan.net/doc
*/
const BASE_URI = "https://evmapi-testnet.confluxscan.net"; 

//- replace value with the smart contract address
const CONTRACT_ADDRESS = "0x3f55aDFBC0143DA8D0E07cB77CE29ECAfF1ef66F";

// read the solidity file
// - replace `VerifyContractFlatten.sol` with your FLATTEN contract
const contractSource = fs.readFileSync('VerifyContractFlatten.sol', 'utf8');

async function main() {
    const body = {
        module: 'contract',
        action: 'verifysourcecode',
        contractaddress: CONTRACT_ADDRESS,
        sourceCode: contractSource,
        codeformat: 'solidity-single-file',
        contractname: 'VerifyContract', //put your contract name
        compilerversion: 'v0.8.4+commit.c7e474f2',  // what compiler version you used for your contract?
        optimizationUsed: 0, // no = 0, yes =1
       // if optimization is used you need to specify your runs, (e.g. runs: 200 )
        licenseType: 3,
/*
Valid codes 1-14 where

No License (None)
The Unlicense (Unlicense)
MIT License (MIT)
GNU General Public License v2.0 (GNU GPLv2)
GNU General Public License v3.0 (GNU GPLv3)
GNU Lesser General Public License v2.1 (GNU LGPLv2.1)
GNU Lesser General Public License v3.0 (GNU LGPLv3)
BSD 2-clause 'Simplified' license (BSD-2-Clause)
BSD 3-clause 'New' Or 'Revised' license* (BSD-3-Clause)
Mozilla Public License 2.0 (MPL-2.0)
Open Software License 3.0 (OSL-3.0)
Apache 2.0 (Apache-2.0)
GNU Affero General Public License (GNU AGPLv3)
Business Source License (BSL 1.1)
*/
    }

    const response = await axios.post(`${BASE_URI}/api`, body);

    console.log(response.data);
}

main().catch((error) => {
    console.error(error.message);
    process.exitCode = 1;
});

In order to use this script you need to install axios. npm i axios -D.

So you you just need to run the script, if you are using hardhat run:

npx hardhat run ./scripts/<nameScriptFile.ts>

And if all works well you’ll get this message:

Example git repository: https://github.com/luisantoniocrag/espace-verify-contract

If you have some question or you are not available to verify your contract please send a message in #developers channel on Discord. But I hope this can help you in your Conflux dev journey.

See you soon. :grin:

I got this info on https://evmapi-testnet.confluxscan.net/doc

4 Likes