Proxy — Eternal Storage

Eszymi
Coinmonks
2 min readOct 5, 2022

--

The introduction to the proxy you will find here.

The problem with the inherit storage proxy pattern is the growing cost of using it with the next versions of Logic. The reason of it is necessary to inherit in the new the old one’s storage, even if we won’t use some of the states from there.

Therefor, there is another type of storage — eternal storage. This time the proxy, and the Logic inherit from contract look like this

Of course, you noticed in this contract there are only mappings. The keys are always bytes32. Why that?

The main concept of the eternal storage pattern is using only the mapping from eternal storage. No other variable. So how should we work with this type of storage?

Let’s assume our Logic contract would like to define uint256 type variable costOfToken and then set it to 5. So if our variable is uint256, then we should use uintStorage mapping from our eternalStorage contract, where 5 will be the value. But what will be the key? We need a function that will give as always 32 bytes, without the difference of the input. This type of function is keccak256.

So the answer to our question, how to define a new variable and set its value is command look like

It’s look more difficult than just

but it gives us more flexibility.

Because we use dynamic arrays to save our variables we are able to define so many different variables of every type, that we need. We don’t have to worry about overwriting the slot in the storage. Also, when we upgrade our Logic, it needs only inherit the storage contract, so only the few mapping, not all variables like in inherited storage.

This solution has two big drawback. The first, reading the Logic contract won’t tell us about all defined variables, so we have to remember all used by us keys. The second, we are not able to use any other type than that declare during the creation of the eternalStorage.

Summary

Advantage

  • It’s cheaper solution than inherited storage
  • We can use so many variables, that we need
  • The Logic contract is not tightly coupled to specific proxy contracts. It’s to give us more flexibility.

Disadvantage

  • No explicite write all defined variables
  • Write and read the value is more tedious
  • We could use types only declared when the eternalStorage was written

I hope you find this post useful. If you have any idea, how could I make my posts better, let my know. I am always ready to learn. You can connect with me on LinkedIn and Telegram.

If you would like to talk with me about this or any other topic I wrote, feel free. I’m open to conversation.

Happy learning!

New to trading? Try crypto trading bots or copy trading

--

--