[Ethereum] Large contract (bytecode >= 45K) causes out-of-gas on deploy


I need some advice about handling a large contract. At a certain size it causes an out-of-gas error when I deploy to a local testrpc node. I experience this when the bytecode reaches approximately 45K. I have temporarily solved the issue by factoring out as much logic as possible into a library that is deployed separately. However, I'm running out of things that I can take out. The main contract has about 30 functions, many of which are now just forwarding to the library functions. Soon I will be unable to add even small features or improvements to the codebase, not to mention the undesirable effect of inducing refactoring for optimization reasons not architectural reasons.

How do you avoid the out-of-gas error for large contracts?

Best Answer

I am also running into this issue as I have started to test my new contract which is using 79k in byte code so the Ethereum test-net will not allow the contract to be deployed.

My contract gas usage to be deployed is 7058868, which is much higher then the network block size limits.

I can recommend the following tips:

  • Continue to refactor into libraries. This is the #1 way to reduce the bytecode size of the main contract.

  • Use shorter types for struct elements and sort them such that short types are grouped together. Started doing this and it makes a massive difference even if the code is not as readable or clean looking.

  • Move duplicate functionality into generic functions or function modifiers (even if it means more function parameters).

  • Instead of creating multiple getters (or multiple public members), create bundled getters that return multiple values at once.

  • Use local variables to reference storage array elements.

  • Remove some constant functions that are not critical to the contract.

I am using the Metamask chrome plugin for test deployment which gives more information on deployment failure—such as contract gas usage.

The official Ethereum wallet software just displays a window after deployment failure that says - 'Intrinsic gas is too low'.

Related Topic