mirror of
https://github.com/status-im/EIPs.git
synced 2025-02-22 19:58:06 +00:00
Automatically merged updates to draft EIP(s) 2535 (#3173)
Hi, I'm a bot! This change was automatically merged because: - It only modifies existing Draft or Last Call EIP(s) - The PR was approved or written by at least one author of each modified EIP - The build is passing
This commit is contained in:
parent
b06f3538fc
commit
52beedfb70
@ -107,16 +107,13 @@ A diamond can be upgraded if it has a diamondCut external function or other func
|
||||
|
||||
A facet defines external functions and can define or use internal functions, libraries, and state variables.
|
||||
|
||||
A facet can declare and use state variables but it does it in a way that is safer, easier and more flexible for diamonds.
|
||||
|
||||
Facet state variables are declared in structs. Each struct is given a specific position in contract storage. This technique is called **Diamond Storage**.
|
||||
A facet can declare state variables in structs. Each struct is given a specific position in contract storage. This technique is called **Diamond Storage**.
|
||||
|
||||
Here is a simple example that shows diamond storage and its use in a facet:
|
||||
|
||||
```Solidity
|
||||
// A contract that implements diamond storage.
|
||||
// This contract is inherited and used by facets.
|
||||
contract DiamondStorageContract {
|
||||
library LibA {
|
||||
|
||||
// This struct contains state variables we care about.
|
||||
struct DiamondStorage {
|
||||
@ -128,23 +125,23 @@ contract DiamondStorageContract {
|
||||
// ds is short for DiamondStorage
|
||||
function diamondStorage() internal pure returns(DiamondStorage storage ds) {
|
||||
// Specifies a random position from a hash of a string
|
||||
bytes32 storagePosition = keccak256("diamond.storage")
|
||||
bytes32 storagePosition = keccak256("diamond.storage.LibA")
|
||||
// Set the position of our struct in contract storage
|
||||
assembly {ds.slot := storagePosition}
|
||||
}
|
||||
}
|
||||
|
||||
// Our facet inherits and uses the diamond storage defined above.
|
||||
contract FacetA is DiamondStorageContract {
|
||||
// Our facet uses the diamond storage defined above.
|
||||
contract FacetA {
|
||||
|
||||
function setDataA(bytes32 _dataA) external {
|
||||
DiamondStorage storage ds = diamondStorage();
|
||||
LibA.DiamondStorage storage ds = LibA.diamondStorage();
|
||||
require(ds.owner == msg.sender, "Must be owner.");
|
||||
ds.dataA = _dataA
|
||||
}
|
||||
|
||||
function getDataA() external view returns (bytes32) {
|
||||
return diamondStorage().dataA
|
||||
return LibDiamond.diamondStorage().dataA
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -155,8 +152,6 @@ By using diamond storage facets can declare their own state variables that do no
|
||||
|
||||
By using diamond storage facets can be developed independently, without connection or concern for other facets.
|
||||
|
||||
Solidity libraries can also use diamond storage.
|
||||
|
||||
## Diamonds Can Use Other Contract Storage Strategies
|
||||
|
||||
Diamonds and facets don't have to use diamond storage. They can use other contract storage strategies such as contract inheritance.
|
||||
|
Loading…
x
Reference in New Issue
Block a user