Implement UUPS proxy (ERC1822) (#2542)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
@ -96,4 +96,6 @@ Note that, in all cases, accounts simply _declare_ their interfaces, but they ar
|
||||
|
||||
{{Strings}}
|
||||
|
||||
{{StorageSlot}}
|
||||
|
||||
{{Multicall}}
|
||||
|
||||
83
contracts/utils/StorageSlot.sol
Normal file
83
contracts/utils/StorageSlot.sol
Normal file
@ -0,0 +1,83 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
/**
|
||||
* @dev Library for reading and writing primitive types to specific storage slots.
|
||||
*
|
||||
* Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
|
||||
* This library helps with reading and writing to such slots without the need for inline assembly.
|
||||
*
|
||||
* The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
|
||||
*
|
||||
* Example usage to set ERC1967 implementation slot:
|
||||
* ```
|
||||
* contract ERC1967 {
|
||||
* bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
|
||||
*
|
||||
* function _getImplementation() internal view returns (address) {
|
||||
* return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
|
||||
* }
|
||||
*
|
||||
* function _setImplementation(address newImplementation) internal {
|
||||
* require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
|
||||
* StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* Available since v4.1.0 for `address`, `bool`, `bytes32`, and `uint256`.
|
||||
*/
|
||||
library StorageSlot {
|
||||
struct AddressSlot {
|
||||
address value;
|
||||
}
|
||||
|
||||
struct BooleanSlot {
|
||||
bool value;
|
||||
}
|
||||
|
||||
struct Bytes32Slot {
|
||||
bytes32 value;
|
||||
}
|
||||
|
||||
struct Uint256Slot {
|
||||
uint256 value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns an `AddressSlot` with member `value` located at `slot`.
|
||||
*/
|
||||
function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
|
||||
assembly {
|
||||
r.slot := slot
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns an `BooleanSlot` with member `value` located at `slot`.
|
||||
*/
|
||||
function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
|
||||
assembly {
|
||||
r.slot := slot
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
|
||||
*/
|
||||
function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
|
||||
assembly {
|
||||
r.slot := slot
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns an `Uint256Slot` with member `value` located at `slot`.
|
||||
*/
|
||||
function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
|
||||
assembly {
|
||||
r.slot := slot
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user