One Code Many Storages

When you deploy a compiled smart contract to an Ethereum-like blockchain like RSK, your transaction includes the compiled bytecode, including the constructor code.

An example, if you want to deploy Counter.sol:

your transaction will contain:

Deploy Transaction for Counter.sol

The data field is huge, for such small smart contract. Imagine its size when you deploy a more complicated DeFi-related contract. The size of the payload and the size of the runtime bytecodes in the contract HAVE an associated cost in gas. This is because you must pay for the work every node in the network will execute to deploy this contract.

Some times, you need to deploy the same contract many times. IE, a liquidity pool in a DeFi project, or a surrogate contract for representing a user without balance using metatransactions (see my project https://github.com/ajlopez/EthMeta)

For those cases, I have a proposal and associated code. Instead of sending the smart contract code EVERY TIME you need a new instance, you can say:

“I need a new instance BUT WITH THE same code another instance has”.

Then, this new instance will have its own storage, but the code is shared with another account.

The way to specify this intention is SENDING THE another instance ADDRESS as part of the data field. My proposal: send a data field with 32 bytes, including the address padded to the right:

In this way, the cost could be lower, and it could be significant impact, for example, in metatrasactions implementations that support thousands of surrogate contracts representing users without balance.

I coded a first implementation in my personal BlockchainJ project, see commit https://github.com/ajlopez/BlockchainJ/commit/206c1d57a47608b5ead619ca493c3e444f02aee2

The price to pay: you lost the constructor. But it is a common pattern now, with upgradable contracts, to have an initialized() function to be called immediately after the creation of the new instance.

Usually this kind of deploy are executed using scripts, so there is no change in the wallet implementations.

Angel “Java” Lopez
@ajlopez

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store