Skip to content

Instantly share code, notes, and snippets.

@timothycarambat
Created May 26, 2022 01:38
Show Gist options
  • Save timothycarambat/77981d8e661569f9855e1d10d80a7166 to your computer and use it in GitHub Desktop.
Save timothycarambat/77981d8e661569f9855e1d10d80a7166 to your computer and use it in GitHub Desktop.
How to verify contract on Etherscan, PolygonScan, Optimism Etherscan, Arbiscan, or Snowtrace programmatically with Node.js. This is mostly because the API documentation is poorly done and does not showcase a way to actually do this. I figured it out so i figure others would like to know if using the API
const EXPLORER = {
ethereum: {
api: 'https://api.etherscan.io/api',
key: '<API_KEY_HERE>', // Etherescan API key works for ethereum and rinkeby, will need to do this for each API
},
rinkeby: {
api: 'https://api-rinkeby.etherscan.io/api',
key: '',
},
polygon: {
api: 'https://api.polygonscan.com/api',
key: '',
},
mumbai: {
api: 'https://api-testnet.polygonscan.com/api',
key: '',
},
// Optimism Does not support contract validation from API
optimism: {
api: null,
key: '',
},
kovanOptimism: {
api: null,
key: '',
},
arbitrum: {
api: 'https://api.arbiscan.io/api',
key: '',
},
rinkebyArbitrum: {
api: 'https://api-testnet.arbiscan.io/api',
key: '',
},
avalanche: {
api: 'https://api.snowtrace.io/api',
key: '',
},
testnetAvalanche: {
api: 'https://api-testnet.snowtrace.io/api',
key: '',
},
};
async function verifyContract(network) {
const explorer = EXPLORER[network];
const contractSourceCode = 'pragma .......'
const contractAddress = '0xAnsfb212n12...'
if (!explorer.api) {
return {
status: false,
msg: `Contract validation not available for ${network}`,
};
}
var formBody = [];
// In order for this to work EVERY key asked for in the API must be included - even if using default.
const data = {
apikey: explorer.key,
module: 'contract',
action: 'verifysourcecode',
contractaddress: contractAddress,
sourceCode: contractSourceCode,
codeformat: 'solidity-single-file',
contractname: `MyContract`, // should match the `contract` name in source code
optimizationUsed: 1,
compilerversion: 'v0.8.9+commit.e5eed63a', // pick yours from the list of available compilers found in docs for API
constructorArguements: '',
runs: 200,
evmversion: 'default',
licenseType: 1, // Choose whichever option you want - refer to docs for API
};
for (var property in data) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(data[property]);
formBody.push(encodedKey + '=' + encodedValue);
}
formBody = formBody.join('&');
const response = await fetch(explorer.api, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formBody,
})
.then((res) => res.json())
.then((res) => {
if (+res.status === 1) {
return {
status: true,
msg: `Contract verified!`,
more: res,
};
}
return {
status: false,
msg: `Contract failed to verify!`,
more: res,
};
})
.catch((e) => {
console.error(e);
return {
status: false,
msg: `Contract failed to verify!`,
more: e,
};
});
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment