Skip to content

Instantly share code, notes, and snippets.

@ashwinYardi
Created August 19, 2022 16:18
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 ashwinYardi/dc0319e0f884de9934c9d248b47256bc to your computer and use it in GitHub Desktop.
Save ashwinYardi/dc0319e0f884de9934c9d248b47256bc to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract FacetA {
struct DiamondStorage {
uint firstVar;
uint secondVar;
}
function getStorage() internal pure returns(DiamondStorage storage ds) {
// Specifies a random position in contract storage
// This can be done with a keccak256 hash of a unique string as is
// done here or other schemes can be used
bytes32 storagePosition = keccak256("diamond.storage.facetA");
// Set the position of our struct in contract storage
assembly {ds.slot := storagePosition}
}
function getVars() public view returns(uint, uint) {
DiamondStorage storage s = getStorage();
return (s.firstVar, s.secondVar);
}
}
contract FacetB {
struct DiamondStorage {
string thirdVar;
string fourthVar;
}
function getStorage() internal pure returns(DiamondStorage storage ds) {
bytes32 storagePosition = keccak256("diamond.storage.facetB");
assembly {ds.slot := storagePosition}
}
function setVars() public {
DiamondStorage storage s = getStorage();
s.thirdVar = "Ashwin";
s.fourthVar = "Yardi";
}
function getVars() public view returns(string memory, string memory) {
DiamondStorage storage s = getStorage();
return (s.thirdVar, s.fourthVar);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment