Solidity

如何從 Solidity 函式返回儲存數組?

  • July 23, 2021

我寫了一個非常簡單的智能合約,我想創建一個外部函式,它作為輸出返回一個包含以下項目的儲存數組

$$ 0,2,4,6,8,10,12,14,16,18 $$:

pragma solidity ^0.8.0;

contract MyContract {

   uint[] myArray;

   function myFunc() external returns(uint[] memory){

       for(uint i=0; i<10; i++){
           myArray[i] = i*2;
       }
   
       return myArray;
   }

}

然而,一旦我通過 truffle ( truffle migrate) 將它部署在 Ganache 區塊鏈上,我打開 truffle 控制台 ( truffle console) 並輸入以下程式碼行:

MyContract.deployed().then(function(i) { contract=i; })

contract.myFunc()

我收到此錯誤:

Uncaught Error: Returned error: VM Exception while processing transaction: revert
   at evalmachine.<anonymous>:0:10
   at sigintHandlersWrap (vm.js:273:12)
   at Script.runInContext (vm.js:140:14)
   at runScript (C:\Users\feder\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:270:1)
   at Console.interpret (C:\Users\feder\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:285:1)
   at bound (domain.js:413:15)
   at REPLServer.runBound [as eval] (domain.js:424:12)
   at REPLServer.onLine (repl.js:817:10)
   at REPLServer.emit (events.js:315:20)
   at REPLServer.EventEmitter.emit (domain.js:467:12)
   at REPLServer.Interface._onLine (readline.js:337:10)
   at REPLServer.Interface._line (readline.js:666:8)
   at REPLServer.Interface._ttyWrite (readline.js:1010:14)
   at REPLServer.self._ttyWrite (repl.js:907:9) {
 data: {
   '0x9aa71a7a511b486ff55a74ab556e9c22eb420ea4991a206087051cd9ae08ca94': {
     error: 'revert',
     program_counter: 163,
     return: '0x4e487b710000000000000000000000000000000000000000000000000000000000000032'
   },
   stack: 'RuntimeError: VM Exception while processing transaction: revert\n' +
     '    at Function.RuntimeError.fromResults (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\utils\\runtimeerror.js:94:13)\n' +
     '    at BlockchainDouble.processBlock (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\blockchain_double.js:627:24)\n' +
     '    at processTicksAndRejections (internal/process/task_queues.js:93:5)',
   name: 'RuntimeError'
 },
 hijackedStack: 'Error: Returned error: VM Exception while processing transaction: revert\n' +
   '    at Object.ErrorResponse (C:\\Users\\feder\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-helpers\\lib\\errors.js:28:1)\n' +     
   '    at C:\\Users\\feder\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3\\node_modules\\web3-core-requestmanager\\lib\\index.js:303:1\n' + 
   '    at C:\\Users\\feder\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\packages\\provider\\wrapper.js:107:1\n' +
   '    at XMLHttpRequest.request.onreadystatechange (C:\\Users\\feder\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3\\node_modules\\web3-providers-http\\lib\\index.js:98:1)\n' +
   '    at XMLHttpRequestEventTarget.dispatchEvent (C:\\Users\\feder\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\xhr2-cookies\\dist\\xml-http-request-event-target.js:34:1)\n' +
   '    at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._setReadyState (C:\\Users\\feder\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\xhr2-cookies\\dist\\xml-http-request.js:208:1)\n' +
   '    at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._onHttpResponseEnd (C:\\Users\\feder\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\xhr2-cookies\\dist\\xml-http-request.js:318:1)\n' +
   '    at IncomingMessage.<anonymous> (C:\\Users\\feder\\AppData\\Roaming\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\xhr2-cookies\\dist\\xml-http-request.js:289:47)\n' +
   '    at IncomingMessage.emit (events.js:327:22)\n' +
   '    at IncomingMessage.EventEmitter.emit (domain.js:529:15)\n' +
   '    at endReadableNT (internal/streams/readable.js:1327:12)\n' +
   '    at processTicksAndRejections (internal/process/task_queues.js:80:21)'
}

有誰知道我做錯了什麼?我怎樣才能解決這個問題並得到我想要的結果?

將項目推送到數組上使用 .push() 在數組上,這樣它就可以工作了:

myArray.push(i*2);

;)

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