forked from neptune-mutual-blue/protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoreKeyUtil.sol
613 lines (540 loc) · 13.4 KB
/
StoreKeyUtil.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
// solhint-disable func-order
pragma solidity 0.8.0;
import "../interfaces/IStore.sol";
library StoreKeyUtil {
function setUintByKey(
IStore s,
bytes32 key,
uint256 value
) external {
require(key > 0, "Invalid key");
return s.setUint(key, value);
}
function setUintByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
uint256 value
) external {
return s.setUint(_getKey(key1, key2), value);
}
function setUintByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address account,
uint256 value
) external {
return s.setUint(_getKey(key1, key2, account), value);
}
function addUintByKey(
IStore s,
bytes32 key,
uint256 value
) external {
require(key > 0, "Invalid key");
return s.addUint(key, value);
}
function addUintByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
uint256 value
) external {
return s.addUint(_getKey(key1, key2), value);
}
function addUintByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address account,
uint256 value
) external {
return s.addUint(_getKey(key1, key2, account), value);
}
function subtractUintByKey(
IStore s,
bytes32 key,
uint256 value
) external {
require(key > 0, "Invalid key");
return s.subtractUint(key, value);
}
function subtractUintByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
uint256 value
) external {
return s.subtractUint(_getKey(key1, key2), value);
}
function subtractUintByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address account,
uint256 value
) external {
return s.subtractUint(_getKey(key1, key2, account), value);
}
function setStringByKey(
IStore s,
bytes32 key,
string memory value
) external {
require(key > 0, "Invalid key");
s.setString(key, value);
}
function setStringByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
string memory value
) external {
return s.setString(_getKey(key1, key2), value);
}
function setBytes32ByKey(
IStore s,
bytes32 key,
bytes32 value
) external {
require(key > 0, "Invalid key");
s.setBytes32(key, value);
}
function setBytes32ByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 value
) external {
return s.setBytes32(_getKey(key1, key2), value);
}
function setBoolByKey(
IStore s,
bytes32 key,
bool value
) external {
require(key > 0, "Invalid key");
return s.setBool(key, value);
}
function setBoolByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bool value
) external {
return s.setBool(_getKey(key1, key2), value);
}
function setBoolByKeys(
IStore s,
bytes32 key,
address account,
bool value
) external {
return s.setBool(_getKey(key, account), value);
}
function setAddressByKey(
IStore s,
bytes32 key,
address value
) external {
require(key > 0, "Invalid key");
return s.setAddress(key, value);
}
function setAddressByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address value
) external {
return s.setAddress(_getKey(key1, key2), value);
}
function setAddressByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3,
address value
) external {
return s.setAddress(_getKey(key1, key2, key3), value);
}
function setAddressArrayByKey(
IStore s,
bytes32 key,
address value
) external {
require(key > 0, "Invalid key");
return s.setAddressArrayItem(key, value);
}
function setAddressArrayByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address value
) external {
return s.setAddressArrayItem(_getKey(key1, key2), value);
}
function setAddressArrayByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3,
address value
) external {
return s.setAddressArrayItem(_getKey(key1, key2, key3), value);
}
function setAddressBooleanByKey(
IStore s,
bytes32 key,
address account,
bool value
) external {
require(key > 0, "Invalid key");
return s.setAddressBoolean(key, account, value);
}
function setAddressBooleanByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address account,
bool value
) external {
return s.setAddressBoolean(_getKey(key1, key2), account, value);
}
function setAddressBooleanByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3,
address account,
bool value
) external {
return s.setAddressBoolean(_getKey(key1, key2, key3), account, value);
}
function deleteUintByKey(IStore s, bytes32 key) external {
require(key > 0, "Invalid key");
return s.deleteUint(key);
}
function deleteUintByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external {
return s.deleteUint(_getKey(key1, key2));
}
function deleteBytes32ByKey(IStore s, bytes32 key) external {
require(key > 0, "Invalid key");
s.deleteBytes32(key);
}
function deleteBytes32ByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external {
return s.deleteBytes32(_getKey(key1, key2));
}
function deleteBoolByKey(IStore s, bytes32 key) external {
require(key > 0, "Invalid key");
return s.deleteBool(key);
}
function deleteBoolByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external {
return s.deleteBool(_getKey(key1, key2));
}
function deleteBoolByKeys(
IStore s,
bytes32 key,
address account
) external {
return s.deleteBool(_getKey(key, account));
}
function deleteAddressByKey(IStore s, bytes32 key) external {
require(key > 0, "Invalid key");
return s.deleteAddress(key);
}
function deleteAddressByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external {
return s.deleteAddress(_getKey(key1, key2));
}
function deleteAddressByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3
) external {
return s.deleteAddress(_getKey(key1, key2, key3));
}
function deleteAddressArrayByKey(
IStore s,
bytes32 key,
address value
) external {
require(key > 0, "Invalid key");
return s.deleteAddressArrayItem(key, value);
}
function deleteAddressArrayByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address value
) external {
return s.deleteAddressArrayItem(_getKey(key1, key2), value);
}
function deleteAddressArrayByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3,
address value
) external {
return s.deleteAddressArrayItem(_getKey(key1, key2, key3), value);
}
function deleteAddressArrayByIndexByKey(
IStore s,
bytes32 key,
uint256 index
) external {
require(key > 0, "Invalid key");
return s.deleteAddressArrayItemByIndex(key, index);
}
function deleteAddressArrayByIndexByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
uint256 index
) external {
return s.deleteAddressArrayItemByIndex(_getKey(key1, key2), index);
}
function deleteAddressArrayByIndexByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3,
uint256 index
) external {
return s.deleteAddressArrayItemByIndex(_getKey(key1, key2, key3), index);
}
function getUintByKey(IStore s, bytes32 key) external view returns (uint256) {
require(key > 0, "Invalid key");
return s.getUint(key);
}
function getUintByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external view returns (uint256) {
return s.getUint(_getKey(key1, key2));
}
function getUintByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address account
) external view returns (uint256) {
return s.getUint(_getKey(key1, key2, account));
}
function getStringByKey(IStore s, bytes32 key) external view returns (string memory) {
require(key > 0, "Invalid key");
return s.getString(key);
}
function getStringByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external view returns (string memory) {
return s.getString(_getKey(key1, key2));
}
function getBytes32ByKey(IStore s, bytes32 key) external view returns (bytes32) {
require(key > 0, "Invalid key");
return s.getBytes32(key);
}
function getBytes32ByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external view returns (bytes32) {
return s.getBytes32(_getKey(key1, key2));
}
function getBoolByKey(IStore s, bytes32 key) external view returns (bool) {
require(key > 0, "Invalid key");
return s.getBool(key);
}
function getBoolByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external view returns (bool) {
return s.getBool(_getKey(key1, key2));
}
function getBoolByKeys(
IStore s,
bytes32 key,
address account
) external view returns (bool) {
return s.getBool(_getKey(key, account));
}
function getAddressByKey(IStore s, bytes32 key) external view returns (address) {
require(key > 0, "Invalid key");
return s.getAddress(key);
}
function getAddressByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external view returns (address) {
return s.getAddress(_getKey(key1, key2));
}
function getAddressByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3
) external view returns (address) {
return s.getAddress(_getKey(key1, key2, key3));
}
function getAddressBooleanByKey(
IStore s,
bytes32 key,
address account
) external view returns (bool) {
require(key > 0, "Invalid key");
return s.getAddressBoolean(key, account);
}
function getAddressBooleanByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address account
) external view returns (bool) {
return s.getAddressBoolean(_getKey(key1, key2), account);
}
function getAddressBooleanByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3,
address account
) external view returns (bool) {
return s.getAddressBoolean(_getKey(key1, key2, key3), account);
}
function countAddressArrayByKey(IStore s, bytes32 key) external view returns (uint256) {
require(key > 0, "Invalid key");
return s.countAddressArrayItems(key);
}
function countAddressArrayByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external view returns (uint256) {
return s.countAddressArrayItems(_getKey(key1, key2));
}
function countAddressArrayByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3
) external view returns (uint256) {
return s.countAddressArrayItems(_getKey(key1, key2, key3));
}
function getAddressArrayByKey(IStore s, bytes32 key) external view returns (address[] memory) {
require(key > 0, "Invalid key");
return s.getAddressArray(key);
}
function getAddressArrayByKeys(
IStore s,
bytes32 key1,
bytes32 key2
) external view returns (address[] memory) {
return s.getAddressArray(_getKey(key1, key2));
}
function getAddressArrayByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3
) external view returns (address[] memory) {
return s.getAddressArray(_getKey(key1, key2, key3));
}
function getAddressArrayItemPositionByKey(
IStore s,
bytes32 key,
address addressToFind
) external view returns (uint256) {
require(key > 0, "Invalid key");
return s.getAddressArrayItemPosition(key, addressToFind);
}
function getAddressArrayItemPositionByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
address addressToFind
) external view returns (uint256) {
return s.getAddressArrayItemPosition(_getKey(key1, key2), addressToFind);
}
function getAddressArrayItemPositionByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3,
address addressToFind
) external view returns (uint256) {
return s.getAddressArrayItemPosition(_getKey(key1, key2, key3), addressToFind);
}
function getAddressArrayItemByIndexByKey(
IStore s,
bytes32 key,
uint256 index
) external view returns (address) {
require(key > 0, "Invalid key");
return s.getAddressArrayItemByIndex(key, index);
}
function getAddressArrayItemByIndexByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
uint256 index
) external view returns (address) {
return s.getAddressArrayItemByIndex(_getKey(key1, key2), index);
}
function getAddressArrayItemByIndexByKeys(
IStore s,
bytes32 key1,
bytes32 key2,
bytes32 key3,
uint256 index
) external view returns (address) {
return s.getAddressArrayItemByIndex(_getKey(key1, key2, key3), index);
}
function _getKey(bytes32 key1, bytes32 key2) private pure returns (bytes32) {
require(key1 > 0 && key2 > 0, "Invalid key(s)");
return keccak256(abi.encodePacked(key1, key2));
}
function _getKey(
bytes32 key1,
bytes32 key2,
bytes32 key3
) private pure returns (bytes32) {
require(key1 > 0 && key2 > 0 && key3 > 0, "Invalid key(s)");
return keccak256(abi.encodePacked(key1, key2, key3));
}
function _getKey(bytes32 key, address account) private pure returns (bytes32) {
require(key > 0 && account != address(0), "Invalid key(s)");
return keccak256(abi.encodePacked(key, account));
}
function _getKey(
bytes32 key1,
bytes32 key2,
address account
) private pure returns (bytes32) {
require(key1 > 0 && key2 > 0 && account != address(0), "Invalid key(s)");
return keccak256(abi.encodePacked(key1, key2, account));
}
}