1

I have a contract that all of the functions either change the state or return data from storage. I have tried the techniques to reduce the size and it helped a bit. However, I need to add a couple more functions but I have reached the limit. I am using remix and I have not enabled the optimizer.

  • If I enable optimizer does it increase the cost of calling the functions too much or is it not too much of a difference since my current size is also very close to max limit.
  • Is it possible to split the contract? I am a bit confused about splitting contracts. I am using mappings and updating the data in these functions or directly returning data from these mappings and arrays, is it possible to put some functions to another contract and reach the storage from that contract?

2 Answers 2

1

It's a bit difficult to suggest anything specific, but the bottom line is that you have to split your contract. If you're now at the limit, you will be even more above it after you add more stuff. And you don't want to be close to the limit anyway - what if you complete the contract, audit it and they find something you still need to add?

A contract has its own storage. It can be accessed by other contracts if the contract exposes it - either implicitly (by declaring a variable public which creates a public getter for it) or explicitly (by writing functions which return certain data).

Basically just consider which are the logical pieces which should stay together and which are (at least partially) part of another logical block, and separate those to different contracts/libraries.

1
  1. The optimizer generally increases the contract size but decreases the cost of calling the functions. However I would try enabling it with a low number of runs, if it works it's the best solution. I would also try using the via-ir option (I don't expect it to work but you never know). *

  2. You can put some logic in another contract (called "linked" or "external" library). Then the main contract can "delegatecall" into the library to use this logic, without problems with storage or whatever. The problem is that it's more complicated to set up and the execution gas cost will be slightly higher. There's a tutorial here for example.

Linked libraries aren't common in my experience. Instead, you can try simply separating the contract in two parts if they operate on different parts of storage.


* Using remix, before compiling, click on "Advanced Configurations" and "Use configuration file", then change the "compiler_config.json" adding this line:

    "settings": {
        "viaIR": true,
        ...
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.