-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathStorage.sol
164 lines (148 loc) · 4.25 KB
/
Storage.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;
import "../number/types/UFixed18.sol";
/// @dev Stored boolean slot
type BoolStorage is bytes32;
using BoolStorageLib for BoolStorage global;
/// @dev Stored uint256 slot
type Uint256Storage is bytes32;
using Uint256StorageLib for Uint256Storage global;
/// @dev Stored int256 slot
type Int256Storage is bytes32;
using Int256StorageLib for Int256Storage global;
/// @dev Stored address slot
type AddressStorage is bytes32;
using AddressStorageLib for AddressStorage global;
/// @dev Stored bytes32 slot
type Bytes32Storage is bytes32;
using Bytes32StorageLib for Bytes32Storage global;
/**
* @title BoolStorageLib
* @notice Library to manage storage and retrieval of a boolean at a fixed storage slot
*/
library BoolStorageLib {
/**
* @notice Retrieves the stored value
* @param self Storage slot
* @return value Stored bool value
*/
function read(BoolStorage self) internal view returns (bool value) {
assembly ("memory-safe") {
value := sload(self)
}
}
/**
* @notice Stores the value at the specific slot
* @param self Storage slot
* @param value boolean value to store
*/
function store(BoolStorage self, bool value) internal {
assembly ("memory-safe") {
sstore(self, value)
}
}
}
/**
* @title Uint256StorageLib
* @notice Library to manage storage and retrieval of an uint256 at a fixed storage slot
*/
library Uint256StorageLib {
/**
* @notice Retrieves the stored value
* @param self Storage slot
* @return value Stored uint256 value
*/
function read(Uint256Storage self) internal view returns (uint256 value) {
assembly ("memory-safe") {
value := sload(self)
}
}
/**
* @notice Stores the value at the specific slot
* @param self Storage slot
* @param value uint256 value to store
*/
function store(Uint256Storage self, uint256 value) internal {
assembly ("memory-safe") {
sstore(self, value)
}
}
}
/**
* @title Int256StorageLib
* @notice Library to manage storage and retrieval of an int256 at a fixed storage slot
*/
library Int256StorageLib {
/**
* @notice Retrieves the stored value
* @param self Storage slot
* @return value Stored int256 value
*/
function read(Int256Storage self) internal view returns (int256 value) {
assembly ("memory-safe") {
value := sload(self)
}
}
/**
* @notice Stores the value at the specific slot
* @param self Storage slot
* @param value int256 value to store
*/
function store(Int256Storage self, int256 value) internal {
assembly ("memory-safe") {
sstore(self, value)
}
}
}
/**
* @title AddressStorageLib
* @notice Library to manage storage and retrieval of an address at a fixed storage slot
*/
library AddressStorageLib {
/**
* @notice Retrieves the stored value
* @param self Storage slot
* @return value Stored address value
*/
function read(AddressStorage self) internal view returns (address value) {
assembly ("memory-safe") {
value := sload(self)
}
}
/**
* @notice Stores the value at the specific slot
* @param self Storage slot
* @param value address value to store
*/
function store(AddressStorage self, address value) internal {
assembly ("memory-safe") {
sstore(self, value)
}
}
}
/**
* @title Bytes32StorageLib
* @notice Library to manage storage and retrieval of a bytes32 at a fixed storage slot
*/
library Bytes32StorageLib {
/**
* @notice Retrieves the stored value
* @param self Storage slot
* @return value Stored bytes32 value
*/
function read(Bytes32Storage self) internal view returns (bytes32 value) {
assembly ("memory-safe") {
value := sload(self)
}
}
/**
* @notice Stores the value at the specific slot
* @param self Storage slot
* @param value bytes32 value to store
*/
function store(Bytes32Storage self, bytes32 value) internal {
assembly ("memory-safe") {
sstore(self, value)
}
}
}