6.4.2: `TypeError: Cannot read property '0' of undefined`
See original GitHub issueExpected Behavior
ganache-cli not crashing
Current Behavior
After upgrading to v6.4.2 (from v6.2.5), the testchain will randomly crash in the middle of our tests with
eth_blockNumber
eth_blockNumber
eth_blockNumber
TypeError: Cannot read property '0' of undefined
at e (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:5393)
at u (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:5429)
at u (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:5955)
at u (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:5770)
at /Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:4169
at /Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1861552
at /Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26124
at i (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:41179)
Steps to Reproduce
checkout branch sdk-688 here: https://github.com/makerdao/dai.js/tree/sdk-688
follow steps under Developing in Readme
run tests with yarn test
Your Environment
- Version used: 6.4.2
- NodeJS Version: v10.13.0
- Operating System and version: macOS High Sierra, version 10.13.4 Beta
- Link to your project or repro gist: https://github.com/makerdao/dai.js/tree/sdk-688
- Commit hash to use with above link for reproduction: 7a246d18b91f259bb9e3f9ec1a064fba96ab9619
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:13 (6 by maintainers)
Top Results From Across the Web
6.4.2: `TypeError: Cannot read property '0' of undefined ...
Expected Behavior ganache-cli not crashing Current Behavior After upgrading to v6.4.2 (from v6.2.5), the testchain will randomly crash in ...
Read more >[SOLVED] Cannot Read Property '0' of Undefined in JavaScript
The “cannot read property '0' of undefined” error in JavaScript will occur when you access the 0 index of a variable that is...
Read more >How to Prevent the Error: Cannot Read Property '0' of Undefined
If a property of an object is an array, it's possible to accidentally misspell the key or try to access the array through...
Read more >Explaining and Solving "Cannot read property ... - YouTube
This video aims at answering questions of 형빈 to my video.Basic Web Development Environment- VS Code with Live Server Extension- ...
Read more >TypeError: Cannot read Property '0' of Undefined in JS
The "Cannot read Property '0' of undefined" error occurs when accessing an undefined value at index 0 . To solve the error, initialize...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

David, yes, I tested it with the verbose option on. Also the contracts passed about 200 unit tests in Truffle where I never estimate gas usage and the only time the ganache-cli breaks with
TypeError: Cannot read property '0' of undefinederror is when a link to a contract function is clicked and the MetaMask transaction sign window has to pop up with the gas price and the total cost of the transaction.The estimated gas for the transaction that the MetaMask is displaying is almost the maximum I set for allowed for block gas limit. If I leave the MetaMask transaction window open and restart the ganache-cli it sends it successfully.
Also, the problem did not exist in versions lower than 6.4.2 which is another thing that led me to think is gas estimation problem related as this was one of the few changes in versions 6.4.2 and 6.4.3.
The estimated gas for the transactions that is displayed in the logs below are incorrect - it is almost the maximum allowed for the block, as mentioned above.
I did some more testing, there are functions that do not break the ganache client when MetaMask is invoked, but they are very simple short ones, on calls to more complicated functions the problem persists.
Update - More testing and information related to use cases with web3.js: After executing only the web3.js gas estimation method
contract.methods.myMethod().estimateGas()on a function call (that crashes ganache when called via MetaMask), the ganache-cli does NOT break, but it estimates super incorrect gas required for the call - more precisely 46k for a function transaction that requires 650k, meaning that the gas is very underestimated, as opposed to the MetaMask case above.However, when I tried to run the same web3.js .estimateGas() method on a ganache-cli prior to v.6.4.2 it doesn’t work at all throwing the following error:
RPC Error: Internal JSON-RPC error. Object { code: -32603, message: “Internal JSON-RPC error.” }
In summary, that might be a tricky one…
OS: Windows 10, web3.js version: 1.00-beta.52 | Node - 10.14.1 | ganache-cli version used for the logs is 6.4.3 but when I tried 6.4.2 before a few days the outcome was the same.
Log 1:
Sometimes ganache executes one or few more JSON RPC call before crashing, everything else is the same, the gas cost is again at the maximum allowed block gas limit.
Thanks @ndeshev for following up. I wasn’t able to reproduce the crash with the contract examples you provided since they are incomplete contracts. I’m hesitant to release this patch without a test to ensure we don’t regress in the future. Is there any chance you can provide us with a complete reproduction contract.
The overestimate is definitely a separate bug. @nicholasjpaterno will likely be looking into this issue when he wraps up some of his current tasks. Changing the
blockGasLimithaving an effect on estimation isn’t too surprising (probably still a bug if we are returning fluctuating gas estimates), as many contract functions do require additional gas the more gas you provide (we use theblockGasLimitas the defaultgasamount in our gas estimate calculations). See EIP-150 (specifically the EIP-114 proposal it implements) for details.There is one edge case we do know about and don’t try to handle: when a contract checks its gasLeft and behaves differently depending on this value. A contrived example being:
A function like the above would cause gas estimations to vary wildly depending on the amount of gas initially supplied to the transaction.
Just to reiterate: the amount of gas required is often greater than the amount of gas used. Any chance you could provide a minimal reproduction of a fluctuating and wildly high gas estimate contracts?