[Ethereum] Contract code size (and how to work around it?)

bytecodeerc-20solidity

I'm writing some rather substantial Solidity smart-contracts, and believe I'm hitting the EIP170 24k bytecode size limit. I've already

(a) factored my code into libraries

(b) today split my contract into two parts.

Nothing seems to be helping to get under the limit. Heck, splitting into two contracts -increased- -severely- the code of one of the two resulting contracts — much bigger than the original all-in-one contract.

Are there tools to help figure out where the code-bloat is coming from? Other methods for splitting-up in order to reduce size?

Best Answer

Most probably the bytecode bloat comes from the new statements in your code. They're often the trouble maker because they include the code of the contract to be instantiated. You can create contract factories which you can deploy in front. This way you can reduce the bytecode size of your main contract.

E.g.

contract X {}

contract XFactory {
    function createX() returns (X) {
        return new X();
    }
}

contract Main {
    XFactory xFactory;
    ...
    Main(XFactory _xFactory) {
        xFactory = _xFactory;
    }
    ...
    function someMethod() {
        X x = xFactory.create();
    }
    ...
}
Related Topic