-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTWMultichainRegistry.sol
More file actions
114 lines (86 loc) · 4.07 KB
/
Copy pathTWMultichainRegistry.sol
File metadata and controls
114 lines (86 loc) · 4.07 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
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.11;
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "@openzeppelin/contracts/utils/Multicall.sol";
import "@openzeppelin/contracts/metatx/ERC2771Context.sol";
import "./interfaces/ITWMultichainRegistry.sol";
contract TWMultichainRegistry is ITWMultichainRegistry, Multicall, ERC2771Context, AccessControlEnumerable {
bytes32 public constant OPERATOR_ROLE = keccak256("OPERATOR_ROLE");
using EnumerableSet for EnumerableSet.AddressSet;
using EnumerableSet for EnumerableSet.UintSet;
/// @dev wallet address => [contract addresses]
mapping(address => mapping(uint256 => EnumerableSet.AddressSet)) private deployments;
/// @dev contract address deployed => imported metadata uri
mapping(uint256 => mapping(address => string)) private addressToMetadataUri;
EnumerableSet.UintSet private chainIds;
constructor(address _trustedForwarder) ERC2771Context(_trustedForwarder) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
// slither-disable-next-line similar-names
function add(
address _deployer,
address _deployment,
uint256 _chainId,
string memory metadataUri
) external {
require(hasRole(OPERATOR_ROLE, _msgSender()) || _deployer == _msgSender(), "not operator or deployer.");
bool added = deployments[_deployer][_chainId].add(_deployment);
require(added, "failed to add");
chainIds.add(_chainId);
if (bytes(metadataUri).length > 0) {
addressToMetadataUri[_chainId][_deployment] = metadataUri;
}
emit Added(_deployer, _deployment, _chainId, metadataUri);
}
// slither-disable-next-line similar-names
function remove(
address _deployer,
address _deployment,
uint256 _chainId
) external {
require(hasRole(OPERATOR_ROLE, _msgSender()) || _deployer == _msgSender(), "not operator or deployer.");
bool removed = deployments[_deployer][_chainId].remove(_deployment);
require(removed, "failed to remove");
emit Deleted(_deployer, _deployment, _chainId);
}
function getAll(address _deployer) external view returns (Deployment[] memory allDeployments) {
uint256 totalDeployments;
uint256 chainIdsLen = chainIds.length();
for (uint256 i = 0; i < chainIdsLen; i += 1) {
uint256 chainId = chainIds.at(i);
totalDeployments += deployments[_deployer][chainId].length();
}
allDeployments = new Deployment[](totalDeployments);
uint256 idx;
for (uint256 j = 0; j < chainIdsLen; j += 1) {
uint256 chainId = chainIds.at(j);
uint256 len = deployments[_deployer][chainId].length();
address[] memory deploymentAddrs = deployments[_deployer][chainId].values();
for (uint256 k = 0; k < len; k += 1) {
allDeployments[idx] = Deployment({
deploymentAddress: deploymentAddrs[k],
chainId: chainId,
metadataURI: addressToMetadataUri[chainId][deploymentAddrs[k]]
});
idx += 1;
}
}
}
function count(address _deployer) external view returns (uint256 deploymentCount) {
uint256 chainIdsLen = chainIds.length();
for (uint256 i = 0; i < chainIdsLen; i += 1) {
uint256 chainId = chainIds.at(i);
deploymentCount += deployments[_deployer][chainId].length();
}
}
function getMetadataUri(uint256 _chainId, address _deployment) external view returns (string memory metadataUri) {
metadataUri = addressToMetadataUri[_chainId][_deployment];
}
function _msgSender() internal view virtual override(Context, ERC2771Context) returns (address sender) {
return ERC2771Context._msgSender();
}
function _msgData() internal view virtual override(Context, ERC2771Context) returns (bytes calldata) {
return ERC2771Context._msgData();
}
}