智能合约(Contracts)

智能合约相关的 API,接口的参数说明请参考Etherscan API 约定, 文档中不单独说明。

Newly verified Contracts are synced to the API servers within 5 minutes or less

获取已经验证代码合约的ABI

Verified Contract Source Codes

  1. https://api.etherscan.io/api?module=contract&action=getabi&address=0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413&apikey=YourApiKeyToken

A simple sample for retrieving the contractABI using Web3.js and Jquery to interact with a contract

  1. var Web3 = require('web3');
  2. var web3 = new Web3(new Web3.providers.HttpProvider());
  3. var version = web3.version.api;
  4. $.getJSON('http://api.etherscan.io/api?module=contract&action=getabi&address=0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359', function (data) {
  5. var contractABI = "";
  6. contractABI = JSON.parse(data.result);
  7. if (contractABI != ''){
  8. var MyContract = web3.eth.contract(contractABI);
  9. var myContractInstance = MyContract.at("0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359");
  10. var result = myContractInstance.memberId("0xfe8ad7dd2f564a877cc23feea6c0a9cc2e783715");
  11. console.log("result1 : " + result);
  12. var result = myContractInstance.members(1);
  13. console.log("result2 : " + result);
  14. } else {
  15. console.log("Error" );
  16. }
  17. });

获取已经验证代码合约的源码

  1. https://api.etherscan.io/api?module=contract&action=getsourcecode&address=0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413&apikey=YourApiKeyToken

[BETA] 验证源代码

  1. 1. Requires a valid Etherscan APIkey, will reject if otherwise
  2. 2. Current daily limit of 100 submissions per day per user (subject to change)
  3. 3. Only supports HTTP post due to max transfer size limitations for http get
  4. 4. Supports up to 10 different library pairs
  5. 5. Contracts that use "imports" will need to have the code concatenated into one file as we do not support "imports" in separate files. You can try using the Blockcat solidity-flattener or SolidityFlattery
  6. 6. List of supported solc versions, only solc version v0.4.11 and above is supported. Ex. v0.4.25+commit.59dbf8f1
  7. 7. Upon successful submission you will receive a GUID (50 characters) as a receipt.
  8. 8. You may use this GUID to track the status of your submission
  9. 9. Verified Source Codes will be displayed at contractsVerified

See Demo Source Verification Submission Code at Source Code Verification Sample

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

  1. //Submit Source Code for Verification
  2. $.ajax({
  3. type: "POST", //Only POST supported
  4. url: "//api.etherscan.io/api", //Set to the correct API url for Other Networks
  5. data: {
  6. apikey: $('#apikey').val(), //A valid API-Key is required
  7. module: 'contract', //Do not change
  8. action: 'verifysourcecode', //Do not change
  9. contractaddress: $('#contractaddress').val(), //Contract Address starts with 0x...
  10. sourceCode: $('#sourceCode').val(), //Contract Source Code (Flattened if necessary)
  11. contractname: $('#contractname').val(), //ContractName
  12. compilerversion: $('#compilerversion').val(), // see http://etherscan.io/solcversions for list of support versions
  13. optimizationUsed: $('#optimizationUsed').val(), //0 = Optimization used, 1 = No Optimization
  14. runs: 200, //set to 200 as default unless otherwise
  15. constructorArguements: $('#constructorArguements').val(), //if applicable
  16. libraryname1: $('#libraryname1').val(), //if applicable, a matching pair with libraryaddress1 required
  17. libraryaddress1: $('#libraryaddress1').val(), //if applicable, a matching pair with libraryname1 required
  18. libraryname2: $('#libraryname2').val(), //if applicable, matching pair required
  19. libraryaddress2: $('#libraryaddress2').val(), //if applicable, matching pair required
  20. libraryname3: $('#libraryname3').val(), //if applicable, matching pair required
  21. libraryaddress3: $('#libraryaddress3').val(), //if applicable, matching pair required
  22. libraryname4: $('#libraryname4').val(), //if applicable, matching pair required
  23. libraryaddress4: $('#libraryaddress4').val(), //if applicable, matching pair required
  24. libraryname5: $('#libraryname5').val(), //if applicable, matching pair required
  25. libraryaddress5: $('#libraryaddress5').val(), //if applicable, matching pair required
  26. libraryname6: $('#libraryname6').val(), //if applicable, matching pair required
  27. libraryaddress6: $('#libraryaddress6').val(), //if applicable, matching pair required
  28. libraryname7: $('#libraryname7').val(), //if applicable, matching pair required
  29. libraryaddress7: $('#libraryaddress7').val(), //if applicable, matching pair required
  30. libraryname8: $('#libraryname8').val(), //if applicable, matching pair required
  31. libraryaddress8: $('#libraryaddress8').val(), //if applicable, matching pair required
  32. libraryname9: $('#libraryname9').val(), //if applicable, matching pair required
  33. libraryaddress9: $('#libraryaddress9').val(), //if applicable, matching pair required
  34. libraryname10: $('#libraryname10').val(), //if applicable, matching pair required
  35. libraryaddress10: $('#libraryaddress10').val() //if applicable, matching pair required
  36. },
  37. success: function (result) {
  38. console.log(result);
  39. if (result.status == "1") {
  40. //1 = submission success, use the guid returned (result.result) to check the status of your submission.
  41. // Average time of processing is 30-60 seconds
  42. document.getElementById("postresult").innerHTML = result.status + ";" + result.message + ";" + result.result;
  43. // result.result is the GUID receipt for the submission, you can use this guid for checking the verification status
  44. } else {
  45. //0 = error
  46. document.getElementById("postresult").innerHTML = result.status + ";" + result.message + ";" + result.result;
  47. }
  48. console.log("status : " + result.status);
  49. console.log("result : " + result.result);
  50. },
  51. error: function (result) {
  52. console.log("error!");
  53. document.getElementById("postresult").innerHTML = "Unexpected Error"
  54. }
  55. });

Check Source code verification submission status:

  1. //Check Source Code Verification Status
  2. $.ajax({
  3. type: "GET",
  4. url: "//api.etherscan.io/api",
  5. data: {
  6. guid: 'ezq878u486pzijkvvmerl6a9mzwhv6sefgvqi5tkwceejc7tvn', //Replace with your Source Code GUID receipt above
  7. module: "contract",
  8. action: "checkverifystatus"
  9. },
  10. success: function (result) {
  11. console.log("status : " + result.status); //0=Error, 1=Pass
  12. console.log("message : " + result.message); //OK, NOTOK
  13. console.log("result : " + result.result); //result explanation
  14. $('#guidstatus').html(">> " + result.result);
  15. },
  16. error: function (result) {
  17. alert('error');
  18. }
  19. });