DEV Community

loading...

AppStorage Pattern for State Variables in Solidity

mudgen profile image Nick Mudge ・2 min read

I have written about the diamond storage pattern for organizing contract state variables in proxy contracts and diamonds.

As a quick refresher, a state variable or storage layout organizational pattern is needed when writing proxy contracts or diamonds in Solidity because Solidity's builtin storage layout system doesn't work for them.

Diamond storage is particularly good for isolating or compartmenting state variables to specific facets or functionality. This is great for creating modular facets that can be understood as their own units and be added to diamonds. A diamond with a lot of functionality is well organized and understandable if each of its facets can be understood in isolation. Diamond storage helps make that possible.

However, I have found that when building applications with diamonds I often want to share state variables specific to my application with facets that are specific to my application. It gets tedious to call a diamondStorage() function in every function that I want to access state variables.

I have successfully utilized a new pattern in contracts called AppStorage. It is a nicer and more convenient way to access application specific state variables that are shared among facets.

The pattern works in the following way:

  1. Define a struct called AppStorage that contains all the state variables specific to your application and that you plan to share with different facets. Store AppStorage in a file. Any of your facets can now import this file to access the state variables.

  2. In a facet that imports the AppStorage struct declare an AppStorage state variable called s. This should be the only state variable declared in the facet. Here's an example:

import "./AppStorage.sol"

contract StakingFacet {
  AppStorage internal s;
  ...
Enter fullscreen mode Exit fullscreen mode

Now in your facet you can access all the state variables in AppStorage by prepending state variables with s.. Here's a simple example:

function myFacetFunction() external {
  s.lastVar = s.firstVar + s.secondVar;
}
Enter fullscreen mode Exit fullscreen mode

This is a very nice convention because it makes state variables concise, easy to access, and it distinguishes state variables from local variables and prevents name clashes/shadowing with local variables and function names. It helps identify and make explicit state variables in a convenient and concise way. It is also a little more gas efficient than diamond storage access.

Since AppStorage s is the first and only state variable declared in facets its position in contract storage is 0. This fact can be used to access AppStorage in Solidity libraries using diamond storage access. Here's an example of that:


library MyLib {
  function diamondStorage() 
    internal 
    pure 
    returns (DiamondStorage storage ds) 
  {
    bytes32 position = DIAMOND_STORAGE_POSITION;
    assembly {
      ds.slot := 0
    }
  }

  function someFunction() external {
    AppStorage storage s = diamondStorage();
    ... do stuff
  }
}
Enter fullscreen mode Exit fullscreen mode

AppStorage s can be declared as the one and only state variable in facets or it can be declared in a contract that facets inherit.

One downside is that state variables can't be declared public in structs so getter functions can't automatically be created this way. But I think it is better anyway to make your own getter functions for state variables because its more explicit.

The GHSTStakingDiamond is a good example of a diamond that uses the AppStorage pattern.

Discussion (1)

pic
Editor guide
Collapse
amxx profile image
Hadrien Croubois

Regarding the use of an AppStorage struct, I always achieved similar result by creating an abstract AppStorage contract, that all my module/facets inherit from (first inheritance).

Do you have any strong opinion/argument against storage through inheritance ?