-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataStoreModule.sol
More file actions
164 lines (143 loc) · 5.16 KB
/
DataStoreModule.sol
File metadata and controls
164 lines (143 loc) · 5.16 KB
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: BUSL-1.1
pragma solidity =0.8.20;
// external - contracts
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
// internal - interfaces
import {IDataStoreModule} from "../../interfaces/modules/IDataStoreModule.sol";
// internal - structs
import {DataStoreModuleStorage} from "./structs/storage.sol";
// internal - libraries
import {DataStoreModuleLib as DSML} from "./libs/DataStoreModuleLib.sol";
/**
* @title DSM: DataStore Module
*
* @notice A storage management tool designed to create a safe and scalable storage layout
* for upgradable contracts with various types of data classes (users, packages, definitions).
*
* @dev review: this module delegates its functionality to DSML (DataStoreModuleLib).
* DSM or DSML has NO access control.
*
* @dev There are no additional functionalities implemented apart from the library.
*
* @dev NO function needs to be overriden when inherited.
*
* @dev __DataStoreModule_init (or _unchained) call is not necessary when inherited.
*
* @dev No storage-altering external/public functions are exposed here, only view/pure external functions.
*
* @author Ice Bear & Crash Bandicoot
*/
abstract contract DataStoreModule is IDataStoreModule, Initializable {
using DSML for DataStoreModuleStorage;
/**
* @custom:section ** VARIABLES **
*
* @dev Do not add any other variables here. Modules do not have a gap.
* Library's main struct has a gap, providing up to 16 storage slots for this module.
*/
// keccak256(abi.encode(uint256(keccak256("geode.storage.DataStoreModule")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant DataStoreModuleStorageLocation =
0xa3ee0f890fa2a50cc11476f86783721ec49c3aba88b83a957fe08235f6485c00;
function _getDataStoreModuleStorage() internal pure returns (DataStoreModuleStorage storage $) {
assembly {
$.slot := DataStoreModuleStorageLocation
}
}
/**
* @custom:section ** INITIALIZING **
*/
function __DataStoreModule_init() internal onlyInitializing {}
function __DataStoreModule_init_unchained() internal onlyInitializing {}
/**
* @custom:section ** HELPER FUNCTIONS **
*
* @custom:visibility -> pure-external
*/
/**
* @notice useful function for string inputs - returns same with the DSML.generateId
* @dev id is generated by keccak(name, type)
*/
function generateId(
string calldata _name,
uint256 _type
) external pure virtual override returns (uint256 id) {
id = uint256(keccak256(abi.encode(_name, _type)));
}
/**
* @notice useful view function for string inputs - returns same with the DSML.generateId
*/
function getKey(
uint256 _id,
bytes32 _param
) external pure virtual override returns (bytes32 key) {
return DSML.getKey(_id, _param);
}
/**
* @custom:section ** DATA GETTER FUNCTIONS **
*
* @custom:visibility -> view-external
*/
/**
* @dev useful for outside reach, shouldn't be used within contracts as a referance
* @return allIdsByType is an array of IDs of the given TYPE from Datastore,
* returns a specific index
*/
function allIdsByType(
uint256 _type,
uint256 _index
) external view virtual override returns (uint256) {
DataStoreModuleStorage storage $ = _getDataStoreModuleStorage();
return $.allIdsByType[_type][_index];
}
function allIdsByTypeLength(uint256 _type) external view virtual override returns (uint256) {
DataStoreModuleStorage storage $ = _getDataStoreModuleStorage();
return $.allIdsByType[_type].length;
}
function readUint(uint256 id, bytes32 key) external view virtual override returns (uint256 data) {
DataStoreModuleStorage storage $ = _getDataStoreModuleStorage();
data = $.readUint(id, key);
}
function readAddress(
uint256 id,
bytes32 key
) external view virtual override returns (address data) {
DataStoreModuleStorage storage $ = _getDataStoreModuleStorage();
data = $.readAddress(id, key);
}
function readBytes(
uint256 id,
bytes32 key
) external view virtual override returns (bytes memory data) {
DataStoreModuleStorage storage $ = _getDataStoreModuleStorage();
data = $.readBytes(id, key);
}
/**
* @custom:section ** ARRAY GETTER FUNCTIONS **
*
* @custom:visibility -> view-external
*/
function readUintArray(
uint256 id,
bytes32 key,
uint256 index
) external view virtual override returns (uint256 data) {
DataStoreModuleStorage storage $ = _getDataStoreModuleStorage();
data = $.readUintArray(id, key, index);
}
function readBytesArray(
uint256 id,
bytes32 key,
uint256 index
) external view virtual override returns (bytes memory data) {
DataStoreModuleStorage storage $ = _getDataStoreModuleStorage();
data = $.readBytesArray(id, key, index);
}
function readAddressArray(
uint256 id,
bytes32 key,
uint256 index
) external view virtual override returns (address data) {
DataStoreModuleStorage storage $ = _getDataStoreModuleStorage();
data = $.readAddressArray(id, key, index);
}
}