@theqrl/qrl-contracts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,155 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // QRL Contracts (last updated v0.1.0) (utils/SlotDerivation.hyp)
3
+ // This file was procedurally generated from scripts/generate/templates/SlotDerivation.js.
4
+
5
+ pragma hyperion >=0.0;
6
+
7
+ /**
8
+ * @dev Library for computing storage (and transient storage) locations from namespaces and deriving slots
9
+ * corresponding to standard patterns. The derivation method for array and mapping matches the storage layout used by
10
+ * the hyperion language / compiler.
11
+ *
12
+ * See https://docs.hyperionlang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays[Solidity docs for mappings and dynamic arrays.].
13
+ *
14
+ * Example usage:
15
+ * ```hyperion
16
+ * contract Example {
17
+ * // Add the library methods
18
+ * using StorageSlot for bytes32;
19
+ * using SlotDerivation for bytes32;
20
+ *
21
+ * // Declare a namespace
22
+ * string private constant _NAMESPACE = "<namespace>"; // eg. OpenZeppelin.Slot
23
+ *
24
+ * function setValueInNamespace(uint256 key, address newValue) internal {
25
+ * _NAMESPACE.zrc7201Slot().deriveMapping(key).getAddressSlot().value = newValue;
26
+ * }
27
+ *
28
+ * function getValueInNamespace(uint256 key) internal view returns (address) {
29
+ * return _NAMESPACE.zrc7201Slot().deriveMapping(key).getAddressSlot().value;
30
+ * }
31
+ * }
32
+ * ```
33
+ *
34
+ * TIP: Consider using this library along with {StorageSlot}.
35
+ *
36
+ * NOTE: This library provides a way to manipulate storage locations in a non-standard way. Tooling for checking
37
+ * upgrade safety will ignore the slots accessed through this library.
38
+ *
39
+ * _Available since v5.1._
40
+ */
41
+ library SlotDerivation {
42
+ /**
43
+ * @dev Derive an ZRC-7201 slot from a string (namespace).
44
+ */
45
+ function zrc7201Slot(string memory namespace) internal pure returns (bytes32 slot) {
46
+ assembly ("memory-safe") {
47
+ mstore(0x00, sub(keccak256(add(namespace, 0x20), mload(namespace)), 1))
48
+ slot := and(keccak256(0x00, 0x20), not(0xff))
49
+ }
50
+ }
51
+
52
+ /**
53
+ * @dev Add an offset to a slot to get the n-th element of a structure or an array.
54
+ */
55
+ function offset(bytes32 slot, uint256 pos) internal pure returns (bytes32 result) {
56
+ unchecked {
57
+ return bytes32(uint256(slot) + pos);
58
+ }
59
+ }
60
+
61
+ /**
62
+ * @dev Derive the location of the first element in an array from the slot where the length is stored.
63
+ */
64
+ function deriveArray(bytes32 slot) internal pure returns (bytes32 result) {
65
+ assembly ("memory-safe") {
66
+ mstore(0x00, slot)
67
+ result := keccak256(0x00, 0x20)
68
+ }
69
+ }
70
+
71
+ /**
72
+ * @dev Derive the location of a mapping element from the key.
73
+ */
74
+ function deriveMapping(bytes32 slot, address key) internal pure returns (bytes32 result) {
75
+ assembly ("memory-safe") {
76
+ mstore(0x00, and(key, shr(96, not(0))))
77
+ mstore(0x20, slot)
78
+ result := keccak256(0x00, 0x40)
79
+ }
80
+ }
81
+
82
+ /**
83
+ * @dev Derive the location of a mapping element from the key.
84
+ */
85
+ function deriveMapping(bytes32 slot, bool key) internal pure returns (bytes32 result) {
86
+ assembly ("memory-safe") {
87
+ mstore(0x00, iszero(iszero(key)))
88
+ mstore(0x20, slot)
89
+ result := keccak256(0x00, 0x40)
90
+ }
91
+ }
92
+
93
+ /**
94
+ * @dev Derive the location of a mapping element from the key.
95
+ */
96
+ function deriveMapping(bytes32 slot, bytes32 key) internal pure returns (bytes32 result) {
97
+ assembly ("memory-safe") {
98
+ mstore(0x00, key)
99
+ mstore(0x20, slot)
100
+ result := keccak256(0x00, 0x40)
101
+ }
102
+ }
103
+
104
+ /**
105
+ * @dev Derive the location of a mapping element from the key.
106
+ */
107
+ function deriveMapping(bytes32 slot, uint256 key) internal pure returns (bytes32 result) {
108
+ assembly ("memory-safe") {
109
+ mstore(0x00, key)
110
+ mstore(0x20, slot)
111
+ result := keccak256(0x00, 0x40)
112
+ }
113
+ }
114
+
115
+ /**
116
+ * @dev Derive the location of a mapping element from the key.
117
+ */
118
+ function deriveMapping(bytes32 slot, int256 key) internal pure returns (bytes32 result) {
119
+ assembly ("memory-safe") {
120
+ mstore(0x00, key)
121
+ mstore(0x20, slot)
122
+ result := keccak256(0x00, 0x40)
123
+ }
124
+ }
125
+
126
+ /**
127
+ * @dev Derive the location of a mapping element from the key.
128
+ */
129
+ function deriveMapping(bytes32 slot, string memory key) internal pure returns (bytes32 result) {
130
+ assembly ("memory-safe") {
131
+ let length := mload(key)
132
+ let begin := add(key, 0x20)
133
+ let end := add(begin, length)
134
+ let cache := mload(end)
135
+ mstore(end, slot)
136
+ result := keccak256(begin, add(length, 0x20))
137
+ mstore(end, cache)
138
+ }
139
+ }
140
+
141
+ /**
142
+ * @dev Derive the location of a mapping element from the key.
143
+ */
144
+ function deriveMapping(bytes32 slot, bytes memory key) internal pure returns (bytes32 result) {
145
+ assembly ("memory-safe") {
146
+ let length := mload(key)
147
+ let begin := add(key, 0x20)
148
+ let end := add(begin, length)
149
+ let cache := mload(end)
150
+ mstore(end, slot)
151
+ result := keccak256(begin, add(length, 0x20))
152
+ mstore(end, cache)
153
+ }
154
+ }
155
+ }
@@ -0,0 +1,143 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // QRL Contracts (last updated v0.1.0) (utils/StorageSlot.hyp)
3
+ // This file was procedurally generated from scripts/generate/templates/StorageSlot.js.
4
+
5
+ pragma hyperion >=0.0;
6
+
7
+ /**
8
+ * @dev Library for reading and writing primitive types to specific storage slots.
9
+ *
10
+ * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
11
+ * This library helps with reading and writing to such slots without the need for inline assembly.
12
+ *
13
+ * The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
14
+ *
15
+ * Example usage to set ZRC-1967 implementation slot:
16
+ * ```hyperion
17
+ * contract ZRC1967 {
18
+ * // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
19
+ * bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
20
+ *
21
+ * function _getImplementation() internal view returns (address) {
22
+ * return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
23
+ * }
24
+ *
25
+ * function _setImplementation(address newImplementation) internal {
26
+ * require(newImplementation.code.length > 0);
27
+ * StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
28
+ * }
29
+ * }
30
+ * ```
31
+ *
32
+ * TIP: Consider using this library along with {SlotDerivation}.
33
+ */
34
+ library StorageSlot {
35
+ struct AddressSlot {
36
+ address value;
37
+ }
38
+
39
+ struct BooleanSlot {
40
+ bool value;
41
+ }
42
+
43
+ struct Bytes32Slot {
44
+ bytes32 value;
45
+ }
46
+
47
+ struct Uint256Slot {
48
+ uint256 value;
49
+ }
50
+
51
+ struct Int256Slot {
52
+ int256 value;
53
+ }
54
+
55
+ struct StringSlot {
56
+ string value;
57
+ }
58
+
59
+ struct BytesSlot {
60
+ bytes value;
61
+ }
62
+
63
+ /**
64
+ * @dev Returns an `AddressSlot` with member `value` located at `slot`.
65
+ */
66
+ function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
67
+ assembly ("memory-safe") {
68
+ r.slot := slot
69
+ }
70
+ }
71
+
72
+ /**
73
+ * @dev Returns a `BooleanSlot` with member `value` located at `slot`.
74
+ */
75
+ function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
76
+ assembly ("memory-safe") {
77
+ r.slot := slot
78
+ }
79
+ }
80
+
81
+ /**
82
+ * @dev Returns a `Bytes32Slot` with member `value` located at `slot`.
83
+ */
84
+ function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
85
+ assembly ("memory-safe") {
86
+ r.slot := slot
87
+ }
88
+ }
89
+
90
+ /**
91
+ * @dev Returns a `Uint256Slot` with member `value` located at `slot`.
92
+ */
93
+ function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
94
+ assembly ("memory-safe") {
95
+ r.slot := slot
96
+ }
97
+ }
98
+
99
+ /**
100
+ * @dev Returns a `Int256Slot` with member `value` located at `slot`.
101
+ */
102
+ function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) {
103
+ assembly ("memory-safe") {
104
+ r.slot := slot
105
+ }
106
+ }
107
+
108
+ /**
109
+ * @dev Returns a `StringSlot` with member `value` located at `slot`.
110
+ */
111
+ function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
112
+ assembly ("memory-safe") {
113
+ r.slot := slot
114
+ }
115
+ }
116
+
117
+ /**
118
+ * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
119
+ */
120
+ function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
121
+ assembly ("memory-safe") {
122
+ r.slot := store.slot
123
+ }
124
+ }
125
+
126
+ /**
127
+ * @dev Returns a `BytesSlot` with member `value` located at `slot`.
128
+ */
129
+ function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
130
+ assembly ("memory-safe") {
131
+ r.slot := slot
132
+ }
133
+ }
134
+
135
+ /**
136
+ * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
137
+ */
138
+ function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
139
+ assembly ("memory-safe") {
140
+ r.slot := store.slot
141
+ }
142
+ }
143
+ }