forked from PinguExchange/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStore.sol
111 lines (95 loc) · 3.76 KB
/
DataStore.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.17;
import './Governable.sol';
/// @title DataStore
/// @notice General purpose storage contract
/// @dev Access is restricted to governance
contract DataStore is Governable {
// Key-value stores
mapping(bytes32 => uint256) public uintValues;
mapping(bytes32 => int256) public intValues;
mapping(bytes32 => address) public addressValues;
mapping(bytes32 => bytes32) public dataValues;
mapping(bytes32 => bool) public boolValues;
mapping(bytes32 => string) public stringValues;
constructor() Governable() {}
/// @param key The key for the record
/// @param value value to store
/// @param overwrite Overwrites existing value if set to true
function setUint(string calldata key, uint256 value, bool overwrite) external onlyGov returns (bool) {
bytes32 hash = getHash(key);
if (overwrite || uintValues[hash] == 0) {
uintValues[hash] = value;
return true;
}
return false;
}
/// @param key The key for the record
function getUint(string calldata key) external view returns (uint256) {
return uintValues[getHash(key)];
}
/// @param key The key for the record
/// @param value value to store
/// @param overwrite Overwrites existing value if set to true
function setInt(string calldata key, int256 value, bool overwrite) external onlyGov returns (bool) {
bytes32 hash = getHash(key);
if (overwrite || intValues[hash] == 0) {
intValues[hash] = value;
return true;
}
return false;
}
/// @param key The key for the record
function getInt(string calldata key) external view returns (int256) {
return intValues[getHash(key)];
}
/// @param key The key for the record
/// @param value address to store
/// @param overwrite Overwrites existing value if set to true
function setAddress(string calldata key, address value, bool overwrite) external onlyGov returns (bool) {
bytes32 hash = getHash(key);
if (overwrite || addressValues[hash] == address(0)) {
addressValues[hash] = value;
return true;
}
return false;
}
/// @param key The key for the record
function getAddress(string calldata key) external view returns (address) {
return addressValues[getHash(key)];
}
/// @param key The key for the record
/// @param value byte value to store
function setData(string calldata key, bytes32 value) external onlyGov returns (bool) {
dataValues[getHash(key)] = value;
return true;
}
/// @param key The key for the record
function getData(string calldata key) external view returns (bytes32) {
return dataValues[getHash(key)];
}
/// @param key The key for the record
/// @param value value to store (true / false)
function setBool(string calldata key, bool value) external onlyGov returns (bool) {
boolValues[getHash(key)] = value;
return true;
}
/// @param key The key for the record
function getBool(string calldata key) external view returns (bool) {
return boolValues[getHash(key)];
}
/// @param key The key for the record
/// @param value string to store
function setString(string calldata key, string calldata value) external onlyGov returns (bool) {
stringValues[getHash(key)] = value;
return true;
}
/// @param key The key for the record
function getString(string calldata key) external view returns (string memory) {
return stringValues[getHash(key)];
}
/// @param key string to hash
function getHash(string memory key) public pure returns (bytes32) {
return keccak256(abi.encodePacked(key));
}
}