Rewrite of clones-with-immutable-args in Huff.
Note This implementation now lives in Huffmate. All future updates will happen there.
Enables creating clone contracts with immutable arguments.
The immutable arguments are stored in the code region of the created proxy contract, and whenever the proxy is called, it reads the arguments into memory, and then appends them to the calldata of the delegate call to the implementation contract. The implementation contract can thus read the arguments straight from calldata.
By doing so, the gas cost of creating parametrizable clones is reduced, since there's no need to store the parameters in storage, which you need to do with EIP-1167. The cost of using such clones is also reduced, since storage loads are replaced with calldata reading, which is far cheaper.
In other words, if you know you are not gonna need parametrization and just want exact copies, then you can keep using EIP-1167, otherwise, clones-with-immutables is cheaper.
Clone factory contracts should use the HuffCloneLib
library. CLONE
is the main macro for creating clones.
Contracts intended to be cloned should include HuffClone
to get access to the helper macros for reading immutable args.
To see an example usage of the library, check out ExampleClone
and ExampleCloneFactory
.
To install with Foundry:
forge install clabby/huff-clones
This project uses Foundry as the development framework.
forge update
forge build
forge test