September 5, 2026

Tips to avoid “Stack Too Deep” in Solidity contracts

Tips to avoid “Stack Too Deep” in Solidity contracts

“Stack too Deep” is an error you get when you try to access to the deep part of the stack in the Ethereum Virtual Machine. The reason why this error happens is described in

In this article I will show you how to fix the “Stack too Deep” error which shows up in the contract bellow.

In this contract, two parts are causing stack too deep.

1. Taking too much arguments in a function.

function zombieeFactory has 17 arguments. Each arguments will be set into the slot of the stack and the access to the satck is limited to the 16th slot, so theoretically the maximum arguments you can take is 16.

solution

Encode the arguments beforehand and decode it in the function.

Use the abi.encoder in web3.js or ethers.js.
https://docs.ethers.io/ethers.js/html/api-advanced.html?highlight=defaultabicoder#static-properties

and then put the bytes in to the functions argument.

you can get the entire struct when you decode it in the function.

2. Taking too big struct in a public array

When you are making an array of structs, the maximum amount of properties you can take in a struct is 12.

solution

change the visibility of the array to private.

that’s it ! when you want to get a specific struct in the array use a function like this.

The whole contract will be like this.

Thank you for reading! I hope this article will help you write solidity contracts. If you have any comments or question, leave a message below or feel free to contact me!

Twitter: https://twitter.com/tomo_aki25
Twitter(Japanese): https://twitter.com/tapioca_pudd

Published at Sat, 21 Dec 2019 15:53:37 +0000

{flickr|100|campaign}

Previous Article

Bitcoin Wars: If Blockchain History Were the Original Star Wars Trilogy

Next Article

Of Communication and Cryptocurrencies – Varun GS