• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RigoBlock / v3-contracts / 13261352678

11 Feb 2025 10:59AM UTC coverage: 84.94% (+2.6%) from 82.368%
13261352678

Pull #622

github

web-flow
Merge f22d260e3 into 08bd3b51b
Pull Request #622: feat: automated nav

761 of 962 branches covered (79.11%)

Branch coverage included in aggregate %.

523 of 711 new or added lines in 28 files covered. (73.56%)

18 existing lines in 5 files now uncovered.

1698 of 1933 relevant lines covered (87.84%)

44.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

9.09
/contracts/protocol/libraries/SlotDerivation.sol
1
// SPDX-License-Identifier: MIT
2
// OpenZeppelin Contracts (last updated v5.1.0) (utils/SlotDerivation.sol)
3
// This file was procedurally generated from scripts/generate/templates/SlotDerivation.js.
4

5
pragma solidity ^0.8.20;
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 solidity language / compiler.
11
 *
12
 * See https://docs.soliditylang.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
 * ```solidity
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.erc7201Slot().deriveMapping(key).getAddressSlot().value = newValue;
26
 *     }
27
 *
28
 *     function getValueInNamespace(uint256 key) internal view returns (address) {
29
 *         return _NAMESPACE.erc7201Slot().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 ERC-7201 slot from a string (namespace).
44
     */
45
    function erc7201Slot(string memory namespace) internal pure returns (bytes32 slot) {
NEW
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) {
NEW
56
        unchecked {
×
NEW
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) {
NEW
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") {
277✔
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) {
NEW
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) {
NEW
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) {
NEW
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) {
NEW
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) {
NEW
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) {
NEW
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
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc