Skip to content

Instantly share code, notes, and snippets.

@mudgen
Created August 17, 2021 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mudgen/7698ea7c85332e880e6bc45d4205d4ac to your computer and use it in GitHub Desktop.
Save mudgen/7698ea7c85332e880e6bc45d4205d4ac to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// A contract that implements Diamond Storage.
library LibA {
// This struct contains state variables we care about.
struct DiamondStorage {
address owner;
bytes32 dataA;
}
// Returns the struct from a specified position in contract storage
// ds is short for DiamondStorage
function diamondStorage() internal pure returns(DiamondStorage storage ds) {
// Specifies a random position in contract storage
// This can be done with a kaccak256 hash of a unique string as is
// done here or other schemes can be used such as this:
// bytes32 storagePosition = keccak256(abi.encodePacked(ERC1155.interfaceId, ERC1155.name, address(this)));
bytes32 storagePosition = keccak256("diamond.storage.LibA");
// Set the position of our struct in contract storage
assembly {ds.slot := storagePosition}
}
}
// Our facet uses the Diamond Storage defined above.
contract FacetA {
function setDataA(bytes32 _dataA) external {
LibA.DiamondStorage storage ds = LibA.diamondStorage();
require(ds.owner == msg.sender, "Must be owner.");
ds.dataA = _dataA;
}
function getDataA() external view returns (bytes32) {
return LibA.diamondStorage().dataA;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment