forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContractStorage.sol
61 lines (45 loc) · 1.97 KB
/
ContractStorage.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
pragma solidity ^0.5.16;
// Internal References
import "./interfaces/IAddressResolver.sol";
// https://docs.synthetix.io/contracts/source/contracts/contractstorage
contract ContractStorage {
IAddressResolver public resolverProxy;
mapping(bytes32 => bytes32) public hashes;
constructor(address _resolver) internal {
// ReadProxyAddressResolver
resolverProxy = IAddressResolver(_resolver);
}
/* ========== INTERNAL FUNCTIONS ========== */
function _memoizeHash(bytes32 contractName) internal returns (bytes32) {
bytes32 hashKey = hashes[contractName];
if (hashKey == bytes32(0)) {
// set to unique hash at the time of creation
hashKey = keccak256(abi.encodePacked(msg.sender, contractName, block.number));
hashes[contractName] = hashKey;
}
return hashKey;
}
/* ========== VIEWS ========== */
/* ========== RESTRICTED FUNCTIONS ========== */
function migrateContractKey(
bytes32 fromContractName,
bytes32 toContractName,
bool removeAccessFromPreviousContract
) external onlyContract(fromContractName) {
require(hashes[fromContractName] != bytes32(0), "Cannot migrate empty contract");
hashes[toContractName] = hashes[fromContractName];
if (removeAccessFromPreviousContract) {
delete hashes[fromContractName];
}
emit KeyMigrated(fromContractName, toContractName, removeAccessFromPreviousContract);
}
/* ========== MODIFIERS ========== */
modifier onlyContract(bytes32 contractName) {
address callingContract =
resolverProxy.requireAndGetAddress(contractName, "Cannot find contract in Address Resolver");
require(callingContract == msg.sender, "Can only be invoked by the configured contract");
_;
}
/* ========== EVENTS ========== */
event KeyMigrated(bytes32 fromContractName, bytes32 toContractName, bool removeAccessFromPreviousContract);
}