I wrote a very simple smart-contract where I want to create an external function which returns as an output a storage array which contains the following items [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;
}
}
However once I deploy it on the Ganache blockchain via truffle (truffle migrate
) and i open the truffle console (truffle console
) and type the following lines of code:
MyContract.deployed().then(function(i) { contract=i; })
contract.myFunc()
I get this error:
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)'
}
Does anyone know what I am doing wrong? How can I solve this issue and get the result I want?