0

I am trying to get source of open source verified contracts and write into a file at my local folder. I could write only one file (of first address) but for the rest i am facing captioned error. Please note that I have download list of verified open source contract addresses's list and loaded into addresses[]. I had also verified that array (addresses[]) has valid addresses and i can access source code by using these addressess (individually) , after that i can write that sourcecode into my file/folder. But the same process gives error, if executed in loop.

Here is my code;

var data = fs.readFileSync('./osContracts/list.csv', 'utf8'); 
myJData = csvJSON(data);
var addresses = [];

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

for(i=0; i<addresses.length; i++){
  const index = i;
  setTimeout(function() {
    $.getJSON('https://api.etherscan.io/api?module=contract&action=getsourcecode&address='+addresses[index]+'&apikey=QTUXRX1........', function (adData) {

    console.log("%o", adData) // this line shows output given below
    data = adData.result[index];

    contractName = data.ContractName; // for first address its OK, if adresses are given one by one then still its OK, but for loop its not OK...

    compVersion = data.CompilerVersion;
    sourceCode = data.SourceCode;

      fs.writeFile('./openSourceContracts/'+contractName+'.sol', sourceCode, function (err) {
        if (err) throw err;
        console.log('Saved! contract'); // shows output Saved! contract... for the first contract only 
        });

    });
  }, 1500 * index)

}

Here is my error;

{
  status: '1',
  message: 'OK',
  result: [
    {
     ...........................
        },
    [length]: 1
  ]
}
Saved! contract
{
  status: '1',
  message: 'OK',
  result: [
    {.......................
       },
    [length]: 1
  ]
}
Error: Uncaught [TypeError: Cannot read property 'ContractName' of undefined]
    at reportException (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\helpers\runtime-script-errors.js:62:24)
    at innerInvokeEventListeners (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:332:9)
    at invokeEventListeners (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:267:3)
    at XMLHttpRequestEventTargetImpl._dispatch (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:214:9)
    at fireAnEvent (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\helpers\events.js:17:36)
    at Request.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:936:7)
    at Request.emit (events.js:215:7)
    at Gunzip.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:299:28)
    at Gunzip.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1184:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) TypeError: Cannot read property 'ContractName' of undefined
    at Object.success (C:\Users\aa\Desktop\temp\deploy - 2Copy.js:35:25)
    at fire (C:\Users\aa\Desktop\temp\node_modules\jquery\dist\jquery.js:3291:31)
    at Object.fireWith [as resolveWith] (C:\Users\aa\Desktop\temp\node_modules\jquery\dist\jquery.js:3421:7)
    at done (C:\Users\aa\Desktop\temp\node_modules\jquery\dist\jquery.js:9533:14)
    at XMLHttpRequest.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\jquery\dist\jquery.js:9785:9)
    at XMLHttpRequest.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\helpers\create-event-accessor.js:33:32)
    at innerInvokeEventListeners (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:316:27)
    at invokeEventListeners (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:267:3)
    at XMLHttpRequestEventTargetImpl._dispatch (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:214:9)
    at fireAnEvent (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\helpers\events.js:17:36)
    at Request.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:936:7)
    at Request.emit (events.js:215:7)
    at Gunzip.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:299:28)
    at Gunzip.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1184:12)
{
  status: '1',
  message: 'OK',
  result: [
    {
      ............ },
    [length]: 1
  ]
}
Error: Uncaught [TypeError: Cannot read property 'ContractName' of undefined]
    at reportException (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\helpers\runtime-script-errors.js:62:24)
    at innerInvokeEventListeners (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:332:9)
    at invokeEventListeners (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:267:3)
    at XMLHttpRequestEventTargetImpl._dispatch (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:214:9)
    at fireAnEvent (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\helpers\events.js:17:36)
    at Request.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:936:7)
    at Request.emit (events.js:215:7)
    at Gunzip.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:299:28)
    at Gunzip.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1184:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) TypeError: Cannot read property 'ContractName' of undefined
    at Object.success (C:\Users\aa\Desktop\temp\deploy - 2Copy.js:35:25)
    at fire (C:\Users\aa\Desktop\temp\node_modules\jquery\dist\jquery.js:3291:31)
    at Object.fireWith [as resolveWith] (C:\Users\aa\Desktop\temp\node_modules\jquery\dist\jquery.js:3421:7)
    at done (C:\Users\aa\Desktop\temp\node_modules\jquery\dist\jquery.js:9533:14)
    at XMLHttpRequest.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\jquery\dist\jquery.js:9785:9)
    at XMLHttpRequest.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\helpers\create-event-accessor.js:33:32)
    at innerInvokeEventListeners (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:316:27)
    at invokeEventListeners (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:267:3)
    at XMLHttpRequestEventTargetImpl._dispatch (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:214:9)
    at fireAnEvent (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\helpers\events.js:17:36)
    at Request.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:936:7)
    at Request.emit (events.js:215:7)
    at Gunzip.<anonymous> (C:\Users\aa\Desktop\temp\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:299:28)
    at Gunzip.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1184:12)

UPDATE: the output of console.log("%o", adData) is here below;

    {
      status: '1',
      message: 'OK',
      result: [
        {
          SourceCode: 'pragma solidity ^0.4.21;\r\n' +
            '
            '   }\r\n' +
            '}',
          ABI: '[.........']',
          ContractName: 'StandardToken',
          CompilerVersion: 'v0.4.21+commit.dfe3193c',
          OptimizationUsed: '0',
          Runs: '200',
          ConstructorArguments: '',
          Library: '',
          LicenseType: 'MIT',
          SwarmSource: 'bzzr://733f48b35626656299ada06fbcf14c46b5bd2c9d03fd05b8ab9767016fcb02d2'
        },
    [length]: 1
  ]
}

output of console.log(Object.keys(adData.result)) is [ '0' ]

  • Your error implies that data == undefined, which implies that adData.result[index] == undefined, which implies that the adData.result object has no key whose name is the value of index. You can print Object.keys(adData.result) and see what keys ARE viable in this object. – goodvibration Dec 4 '19 at 9:55
  • please see my updated question... – Amir Ali Dec 4 '19 at 10:11
0

The problem is here:

data = adData.result[index]

adData.result has only one element, while index may easily be greater than zero, as it is index of the address is address list. If you need to save only one source file per smart contract, then change this line like this:

data = adData.result[0]

If you need to save all the source files, do something like this:

adData.result.forEach (data => {
  contractName = data.ContractName; // for first address its OK, if adresses are given one by one then still its OK, but for loop its not OK...

  compVersion = data.CompilerVersion;
  sourceCode = data.SourceCode;

  fs.writeFile('./openSourceContracts/'+contractName+'.sol', sourceCode, function (err) {
    if (err) throw err;
    console.log('Saved! contract'); // shows output Saved! contract... for the first contract only 
  });
});
  • You are perfect... thanks a lot .... again... – Amir Ali Dec 4 '19 at 10:20

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.