Skip to main content

Contracts

Verify the contract through hardhat verify

The official recommendation of Hardhat is to use hardhat-verify along with hardhat-toolbox for custom browser API configurations and contract verification.

tip

Please use hardhat-verify for contract verification , hardhat-etherscan is not recommended.

https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify

An example of Hardhat Project Configuration and Contract Verification

The package.json file needs to add the following dependencies:

tip

Please use hardhat-verify for contract verification, hardhat-etherscan is not recommended.

// Adjust the version according to needs
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
"@nomicfoundation/hardhat-verify": "^2.0.5",
"hardhat": "^2.19.4"

Execute the npm install command to install dependencies.

The configuration in the hardhat.config.js file is as follows:

require("@nomicfoundation/hardhat-toolbox"); // The toolbox library supports customChains configuration.
require("@nomicfoundation/hardhat-verify");

module.exports = {
solidity: {
version: "0.8.23", // Configure the solidity version for your own project, noting that the solidity version for deploying and verifying contracts should be the same.
settings: {
// Configure this part according to needs.
optimizer:{
enabled: true,
runs: 200,
}
}
},
networks: {
bitlayertestnet: {
url: 'https://testnet-rpc.bitlayer.org',
chainId: 200810,
accounts: ["private key of your account"]
},
bitlayer: {
url: 'https://rpc.bitlayer.org',
chainId: 200901,
accounts: ["private key of your account"]
},
},
etherscan: {
apiKey: {
// An API key needs to be written as the hardhat-verify plugin will require it, and the verification will fail if it is not provided.
// The current bitlayer browser has not yet enabled API key verification, so you can write any random string for now.
bitlayertestnet: "1234",
bitlayer: "1234"
},
customChains: [
{
network: "bitlayertestnet",
chainId: 200810,
urls: {
apiURL: "https://api-testnet.btrscan.com/scan/api",
browserURL: "https://testnet.btrscan.com/"
}
},
{
network: "bitlayer",
chainId: 200901,
urls: {
apiURL: "https://api.btrscan.com/scan/api",
browserURL: "https://www.btrscan.com/"
}
}
]
}
};

Steps to Verify a Contract:

  1. Contract compilation requires compiling the contract according to the configuration parameters used during deployment (such as the Solidity version, whether the optimizer is enabled, etc.), otherwise the compiled bytecode will not match the bytecode of the contract on the blockchain, and verification will not be possible.
  2. When verifying a contract, you need to specify the network, contract path, contract name, etc. If the contract constructor has parameters, you need to include the constructor arguments that were passed in during the deployment of the contract. Provide as many as there are, and if there are no parameters, you don't need to write "constructorArguments". Here's an example:
npx hardhat verify --network bitlayer --contract contracts/proxy/ERC1967/ERC1967Proxy.sol:ERC1967Proxy ${contract_address} constructorArguments1 constructorArguments2 constructorArguments3...
  1. Regarding passing constructor arguments, if the constructor arguments are of complex types, such as address[] or custom structs, it can be inconvenient to pass them through the command line. Instead, you can use --constructor-args arguments.js, where arguments.js exports the parameters in order.
  • Example of an arguments.js file
module.exports = [
"arg0",
"arg1"
];
  • Example of an arguments.js file for complex types

For example, the contract is defined with the following constructor:

struct Point {
uint x;
uint y;
}

contract Foo {
constructor (uint x, string s, Point memory point, bytes b) { ... }
}

Then the arguments.js file can be written like this:

module.exports = [
50,
"a string argument",
{
x: 10,
y: 5,
},
// bytes have to be 0x-prefixed
"0xabcdef",
];

An example of using arguments.js file to pass arguments and execute the verify command is as follows:

npx hardhat verify --constructor-args arguments.js --contract contracts/path/path/SimpleContract.sol:SimpleContract DEPLOYED_CONTRACT_ADDRESS

Get Contract ABI for Verified Contract Source Codes

Returns the Contract Application Binary Interface ( ABI ) of a verified smart contract.

https://api.btrscan.com/scan/api
?module=contract
&action=getabi
&address=0xc9121e476155ebf0b794b7b351808af3787e727d

Try this endpoint in your browser 🔗

Query Parameters

ParameterDescription
addressthe contract address that has a verified source code
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider());
var version = web3.version.api;

$.getJSON('https://api.btrscan.com/scan/api?module=contract&action=getabi&address=0xc9121e476155ebf0b794b7b351808af3787e727d', function (data) {
var contractABI = "";
  contractABI = JSON.parse(data.result);
if (contractABI != ''){
  var MyContract = web3.eth.contract(contractABI);
  var myContractInstance = MyContract.at("0xc9121e476155ebf0b794b7b351808af3787e727d");
  var result = myContractInstance.memberId("0xfe8ad7dd2f564a877cc23feea6c0a9cc2e783715");
  console.log("result1 : " + result);
  var result = myContractInstance.members(1);
  console.log("result2 : " + result);
} else {
  console.log("Error");
}
});

Get Contract Source Code for Verified Contract Source Codes

Returns the source code of a verified smart contract.

https://api.btrscan.com/scan/api
?module=contract
&action=getsourcecode
&address=0xc9121e476155ebf0b794b7b351808af3787e727d

Try this endpoint in your browser 🔗

Query Parameters

ParameterDescription
addressthe contract address that has a verified source code

or

Get Contract Creator and Creation Tx Hash

https://api.btrscan.com/scan/api
?module=contract
&action=getcontractcreation
&contractaddresses=0xff82b0676f7bc1038dda706374ac706a59cc2163

Try this endpoint in your browser 🔗

Verify Source Code (beta)

1、Current daily limit of 100 submissions per day per user (subject to change)

2、Only supports HTTP POST due to max transfer size limitations for HTTP GET

3、Contracts that use "imports" will need to have the code concatenated into one file as we do not support "imports" in separate files

4、List ofsupported solc versions, only solc version v0.4.11 and above is supported. Ex. v0.4.25+commit.59dbf8f1

5、Upon successful submission you will receive a GUID (50 characters) as a receipt

6、You may use this GUID to track the status of your submission

7、Verified Source Codes will be displayed at the Verified Contracts page

Source Code Submission Gist (returns a guid as part of the result upon success):

//Submit Source Code for Verification
$.ajax({
 type: "POST", //Only POST supported
  url: "//api.btrscan.com/scan/api", //Set to the correct API url for Other Networks
  data: {
   apikey: $('#apikey').val(),            //A valid API-Key is required
   module: 'contract',               //Do not change
   action: 'verifysourcecode',            //Do not change
   contractaddress: $('#contractaddress').val(),   //Contract Address starts with 0x...
   sourceCode: $('#sourceCode').val(),       //Contract Source Code (Flattened if necessary)
   codeformat: $('#codeformat').val(),       //solidity-single-file (default) or solidity-standard-json-input (for std-input-json-format support
   contractname: $('#contractname').val(),     //ContractName (if codeformat=solidity-standard-json-input, then enter contractname as ex: erc20.sol:erc20)
   compilerversion: $('#compilerversion').val(),   //see https://api-testnet.bitlayer.org/scan/solcversions for list of support versions
   optimizationUsed: $('#optimizationUsed').val(), //0 = No Optimization, 1 = Optimization used (applicable when codeformat=solidity-single-file)
   runs: 200,                   //set to 200 as default unless otherwise (applicable when codeformat=solidity-single-file)
   constructorArguements: $('#constructorArguements').val(), //if applicable
   evmversion: $('#evmVersion').val(),       //leave blank for compiler default, homestead, tangerineWhistle, spuriousDragon, byzantium, constantinople, petersburg, istanbul (applicable when codeformat=solidity-single-file)
   licenseType: $('#licenseType').val(),      //Valid codes 1-12 where 1=No License .. 12=Apache 2.0, see https://api-testnet.bitlayer.org/scan/contract-license-types
  },
  success: function (result) {
   console.log(result);
   if (result.status == "1") {
    //1 = submission success, use the guid returned (result.result) to check the status of your submission.
    // Average time of processing is 30-60 seconds
    document.getElementById("postresult").innerHTML = result.status + ";" + result.message + ";" + result.result;
    // result.result is the GUID receipt for the submission, you can use this guid for checking the verification status
   } else {
    //0 = error
    document.getElementById("postresult").innerHTML = result.status + ";" + result.message + ";" + result.result;
   }
   console.log("status : " + result.status);
   console.log("result : " + result.result);
  },
  error: function (result) {
   console.log("error!");
   document.getElementById("postresult").innerHTML = "Unexpected Error"
  }
});

Check Source code verification submission status:

//Check Source Code Verification Status
$.ajax({
type: "GET",
url: "https://api-testnet.bitlayer.org/scan/api",
data: {
  apikey: $('#apikey').val(),
  guid: 'ezq878u486pzijkvvmerl6a9mzwhv6sefgvqi5tkwceejc7tvn', //Replace with your Source Code GUID receipt above
  module: "contract",
  action: "checkverifystatus"
},
success: function (result) {
  console.log("status : " + result.status); //0=pending 1=pass 2=fail
  console.log("message : " + result.message); //Pass - Verified, Fail - Unable to verify Pending in queue
  console.log("result : " + result.result); //result explanation
  $('#guidstatus').html(">> " + result.result);
},
error: function (result) {
  alert('error');
}
});