DEV Community

loading...

Discussion on: AppStorage Pattern for State Variables in Solidity

Collapse
mudgen profile image
Nick Mudge Author • Edited

What you describe is the inherited storage pattern. It is totally fine. AppStorage is similar and can also be placed in a storage contract that is inherited by all facets.

What I like about AppStorage is that it adds a "s." prefix to all state variables which separates them from arguments, return variables and local variables. For me this makes things more explicit, clear and prevents name shadowing with function names or other variable names.

Collapse
binzydev profile image
Binzy

The s. is pretty great. Hey have you run into any storage access issues? I cut a new facet and use a library get() function like someFunction() that returns the slot (hashed with keccak256). But it returns empty storage. It's definitely the same diamond contract but seems to get mixed up. Maybe I did something wrong?

library ArcaneBarracksStorage {
bytes32 constant namespace = keccak256("game.arcane.storage.barracks");

struct Storage {
    mapping (address => mapping (uint8 => uint256)) items;
}

function get() internal pure returns (Storage storage s) {
    bytes32 addr = namespace;
    assembly {
        s.slot := addr
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Forem Open with the Forem app