Reputation: 1
Wondering if anyone can help me with a error im getting within my solidity contract.
Im getting the errir "Stack too deep". I understand the meaning behind the error however Im having trouble fixing it.
The Error is occuring here (3rd line):
function _getValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256) {
(uint256 tTransferAmount, uint256 tFee, uint256 tLiquidity, uint256 tMarketing, uint256 tBurn) = _getTValues(tAmount);
(uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, tLiquidity, tMarketing, tBurn, _getRate());
return (rAmount, rTransferAmount, rFee, tTransferAmount, tFee, tLiquidity, tMarketing, tBurn);
}
Upvotes: 0
Views: 2789
Reputation: 9717
The reason for this error is explained by @MrFrenzoid already, so I'm skipping that part.
You can mitigate this problem by either breaking your monolithic function into multiple granular reusable functions or you can use scopes inside your function to remove unused variables from the stack to open up space.
Upvotes: 1
Reputation: 1336
As the docs mention:
The EVM is not a register machine but a stack machine, so all computations are performed on an area called the stack. It has a maximum size of 1024 elements and contains words of 256 bits.
Access to the stack is limited to the top end in the following way: It is possible to copy one of the topmost 16 elements to the top of the stack or swap the topmost element with one of the 16 elements below it.
All other operations take the topmost two (or one, or more, depending on the operation) elements from the stack and push the result onto the stack.
Of course it is possible to move stack elements to storage or memory, but it is not possible to just access arbitrary elements deeper in the stack without first removing the top of the stack.
The number 16 and this last part is important.
It basically means that the ETH machine has a "no more than 16 local variables in use" limit.
Also, the 16 is also not a strict limit, it can be smaller, it depends on what you do with the variables, also the limit is imposed by the fact that the EVM can only reach a specific depth into the stack and the fact that local variables are stored on the stack in Solidity.
I would recommend refactoring your code and splitting it into two functions, and have sequential queries from your caller for each chunk of data, or shrink the variables into structs.
Upvotes: 2