struct FacetAddressAndSelectorPosition {
address facetAddress;
uint16 selectorPosition;
}
struct DiamondStorage {
// function selector => facet address and selector position in selectors array
mapping(bytes4 => FacetAddressAndSelectorPosition) facetAddressAndSelectorPosition;
bytes4[] selectors;
// selectors => selectorAddress, selectorPostion(=selectorCount)
mapping(bytes4 => bool) supportedInterfaces;
// owner of the contract
address contractOwner;
}
Main
// add the mapping value [selector => facetAdd, selectorCount]
ds.facetAdedressAndSelectorPosition[selector]
= FacetAddressAndSelectorPosition(_facetAddress, selectorCount);
ds.selectors.**push**(selector); // bytes4[] selectors;
// add the count of the selector
selectorCount++;
change the address
ds.facetAddressAndSelectorPosition[selector].facetAddress = _facetAddress;
// Input
(address _facetAddress, bytes4[] memory _functionSelectors)
// Example
(0xED98485593D5865E892f823e1f66FB99fE64F639, [0x12345678,0x456789ab])
// Methods
// _functionSelectors => selectorIndex
bytes4 selector = _functionSelectors[selectorIndex];
// get the target selector and the its position and address
FacetAddressAndSelectorPosition memory **oldFacetAddressAndSelectorPosition**
= ds.facetAddressAndSelectorPosition[selector];
// the target position to the last selector!
if (oldFacetAddressAndSelectorPosition.selectorPosition != selectorCount) {
bytes4 lastSelector = ds.selectors[selectorCount];
ds.selectors[oldFacetAddressAndSelectorPosition.selectorPosition] = lastSelector;
ds.facetAddressAndSelectorPosition[lastSelector].selectorPosition = oldFacetAddressAndSelectorPosition.selectorPosition;
}
// delete
ds.selectors.pop(); // delete item of array
delete ds.facetAddressAndSelectorPosition[selector]; // delete item of mapping
https://piedao.notion.site/Upgradeable-Contracts-with-OZ-e1657f19c569475098a4ebf2a08a5d2b#41adefcce7a84882bd5bde01d8d79ae8