0

I have download list of verified open source contract addresses's list and loaded into addresses[]. Now I want to download sourcecode/ABIs etc... of that contracts. Although, i have called setTimeOut between each API call to avoid excessive calls, but still could not be succeeded.

Here is my code; . If i download one contract, then this code has no problem

const jsdom = require("jsdom");
const { window } = new jsdom.JSDOM(`...`);
var $ = require("jquery")(window);

var data = fs.readFileSync('./osContracts/list.csv', 'utf8'); 

myJData = csvToJSON(data);

var addresses = [];

for(i=0; i<myJData.length-1; i++)
{
addresses[i] = myJData[i].ContractAddress;
}

for(i=0; i<addresses.length; i++){
  setTimeout(function() {
    $.getJSON('https://api.etherscan.io/api?module=contract&action=getsourcecode&address='+addresses[i]+'&apikey=QTUXRX1X2T.....', function (adData) {
    console.log(adData.result)          
        });
    }, 5500 * i)

}

Here is my error:

Invalid Address format
Invalid Address format
Invalid Address format..........................................

UPDATE: the value of addresses

[  
  '0x2699fc3753b1036534feb3be8704c9c5e3262606',
  '0x5a3b8896d90bf4e656d35370d6b2ab9613a854d1',
  '0xb215bf00e18825667f696833d13368092cf62e66',
  '0x213a22d873e02269ac45c094c0655a09eaa22c3a',
  '0x67d764d948408a4f01dec1623c4a42275acddb24',

...2979 more items
]

and i can get the the result with their values, but only for one contract UPDATE1. One Contract means... anyone from my addresses[].... not only one...

  • Well... Has it not occurred to you that sharing the value of addresses[i] with us might be relevant in this case??? – goodvibration Dec 4 '19 at 7:53
  • i have update my question with your requisite information, please have a look.. – Amir Ali Dec 4 '19 at 8:02
  • Tried the first address with my API key, and it's working. Maybe your API key is wrong. Maybe one of the other addresses is wrong. Can you check which address actually leads to this error? – goodvibration Dec 4 '19 at 8:14
  • Ahh, I now see your bottom line - and i can get the the result with their values, but only for one contract, and the question is: Has it not occurred to you that sharing the address of that contract with us might be relevant in this case??? – goodvibration Dec 4 '19 at 8:16
  • BTW, I was able to get it correctly for the first two contracts. – goodvibration Dec 4 '19 at 8:24
0

The problem is that i is already equal to addresses.length when your timeout callbacks are being executed. Should be:

for(i=0; i<addresses.length; i++){
  const index = i; // This will not change!
  setTimeout(function() {
    $.getJSON('https://api.etherscan.io/api?module=contract&action=getsourcecode&address='+addresses[index]+'&apikey=QTUXRX1X2T.....', function (adData) {
console.log(adData.result)          
      });
  }, 5500 * i)

}

or, how I would rewrite it:

addresses.forEach (
  (address, index) => setTimeout (
    () => $.getJSON (
      'https://api.etherscan.io/api?module=contract&action=getsourcecode&address=' +
        address +
        '&apikey=QTUXRX1X2T.....',
      adData => console.log (adData.result)),
    5500 * index));
  • thanks a lot ... always you save my head from burning.... – Amir Ali Dec 4 '19 at 8:30
  • your code works for me to print results... but when i want to write that result into a file then it gives another type of error ... Should I post into another question or update this Question? ... – Amir Ali Dec 4 '19 at 8:39
  • sorry, it works for data.result... But it does not work for if i want to access some selected component of data.result[index].ContractName ... it print the First contract name, then print undefined for the rest contracts. – Amir Ali Dec 4 '19 at 8:48
  • Just log the whole result object via console.log ("%o", adData) and you will see what is wrong. – Mikhail Vladimirov Dec 4 '19 at 8:59
  • OK now i would put all details into another question , which will be my last question on this topic.. – Amir Ali Dec 4 '19 at 9:03

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.