0

I want to download all contracts (open source code) from etherscan.io using Web3js. Etherscan gives only the list of such verified open source contract with txHash, name, and their addresses. Is there any way to write a program that takes address (from mentioned list) and then download its source code and save to local folder. If, yes, then is there any way to download only the source code of contract having version ^0.5. ?

I am trying to follow this code from EtherScan, and intend to give the address field data run-time/dynamically, but I'm not sure how to proceed in web3.js, node.js or with other APIs. Also, I am not sure how to call $.getJSON? Where it is defined?

    var Web3 = require('web3');
    var web3 = new Web3(new Web3.providers.HttpProvider());
    var version = web3.version.api;

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

NOTE: I have read all questions regarding this topic on this forum, but no one is providing a solution.

  • The source code is not available on etherscan unless it is verified on etherscan. – goodvibration Nov 30 '19 at 7:23
  • yes, i also want to download that verified open source contract....and etherscan.io is saying that "A total of 3,034 Verified Contracts Source Code found (OpenSource)" .. just see here ... etherscan.io/contractsVerified?filter=opensourcelicense – Amir Ali Nov 30 '19 at 7:42
0

$ refers to jQuery, and the Etherscan link you shared also notes that. Check out their docs here.

I would recommend downloading their list of addresses, and then iterating over it like so:

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider());
var version = web3.version.api;

let addresses = [];
// TODO: Read in CSV data

for (let i = 0; i < addresses.length; i += 1)
  $.getJSON('http://api.etherscan.io/api?module=contract&action=getabi&address=' + addresses[i], function (data) {
        // function body from etherscan
        });

For reading the CSV data, check out this question.

|improve this answer|||||
  • thanks for your answer.. i will try it and will update you... thanks again... – Amir Ali Dec 1 '19 at 12:49
  • I have done as per your direction, but when i run code, they download contract source (which i need) but also give this error 'Error: Cross origin null forbidden'. Is there any limit condition (means in 5 second you can request only five contract... ?) ... – Amir Ali Dec 2 '19 at 13:44
  • Thanks it works for me, but have problem , anyhow i have posted such problem at ethereum.stackexchange.com/questions/77982/…. If you have time, please suggest solution.. – Amir Ali Dec 2 '19 at 14:19
  • Weird, maybe this thread will help? How are you running your javascript? – Ann Kilzer Dec 3 '19 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.