Javascript

錯誤:未擷取噸是peError:C一個notread_執行任務_____是′ContractN一米和′的_和__ef我的__噸是p和和rr這r:C一種nn這噸r和一種dpr這p和r噸是′C這n噸r一種C噸ñ一種米和′這F在nd和F一世n和d…

  • December 4, 2019

我正在嘗試獲取開源驗證契約的來源並寫入本地文件夾中的文件。我只能寫一個文件(第一個地址),但其餘的我面臨標題錯誤。請注意,我已經下載了經過驗證的開源合約地址列表並載入到地址中

$$ $$. 我還驗證了數組(addresses[])具有有效地址,並且我可以通過使用這些地址(單獨)訪問原始碼,之後我可以將該原始碼寫入我的文件/文件夾。但是,如果在循環中執行,相同的過程會出錯。 這是我的程式碼;

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)

}

這是我的錯誤;

{
 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)

更新:輸出 console.log("%o", adData)如下;

   {
     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
 ]
}

的輸出 console.log(Object.keys(adData.result))[ '0' ]

問題在這裡:

data = adData.result[index]

adData.result只有一個元素,而index可能很容易大於零,因為它是地址的索引是地址列表。如果您只需要為每個智能合約保存一個源文件,則將這一行更改如下:

data = adData.result[0]

如果您需要保存所有源文件,請執行以下操作:

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 
 });
});

引用自:https://ethereum.stackexchange.com/questions/78047