ClonesWithImmutableArgs

Jun 6
10
min of reading

There is a famous library which allows to deploy minimal proxy contracts.

However, unlike traditional proxy contractsthat are designed for upgradeability, minimal proxy contracts are fixed to oneimplementation.

This characteristic makes them acost-effective strategy for deploying numerous contracts that share the samelogic but may operate with different state data.

A great advancement in this area is theintroduction of the ClonesWithImmutableArgs library. This library enhances theutility of minimal proxy contracts by allowing the inclusion of immutablevariables during the deployment process. These variables are then accessible ineach delegate call made to the implementation contract.

The advantage of theClonesWithImmutableArgs mechanism is the innovative approach to passingimmutable arguments. First of all, we need to understand that the proxycontract is deployed with immutable arguments.

When now a call is made to a proxycontract, the immutable arguments are appended to the end of the calldata forthe delegate call.

Once extracted, these arguments can be usedjust like any other variables within the business logic of the contract.

The ability to deploy proxy contracts withimmutable arguments has several notable benefits:

Efficiency: It significantly reduces thegas costs associated with deploying multiple instances of a contract, as thecore logic resides in a single implementation contract. The only variationamong the clones are the immutable arguments, which are efficiently passed viacalldata.

Flexibility: Developers can create contractinstances that behave differently based on the immutable parameters provided atdeployment. This allows for a broad range of applications without sacrificingthe benefits of code reuse and minimal deployment costs.

Simplicity: By utilizing theClonesWithImmutableArgs library, developers can streamline their contractarchitecture. It simplifies the management of multiple contract instances withsimilar logic but different initialization parameters.

A nasty bug can be implemented when usingthis concept:

If implementations expose a function thatexecutes a delegatecall to an address which is based on the immutable args, amalicious user can directly call the implementation by padding any arbitrarycalldata, including such an address with a self-destruct function.

This can then be abused to selfdestruct theimplementation. Oh wait, there was something with selfdestruct a few days ago …

Read the original article

Related articles