-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathAssetAttributesRegistry.sol
249 lines (229 loc) · 9.96 KB
/
AssetAttributesRegistry.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
//SPDX-License-Identifier: MIT
pragma solidity 0.8.2;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts-0.8/utils/Context.sol";
import "../catalyst/GemsCatalystsRegistry.sol";
import "../common/BaseWithStorage/WithAdmin.sol";
import "../common/BaseWithStorage/WithMinter.sol";
import "../common/BaseWithStorage/WithUpgrader.sol";
/// @notice Allows setting the gems and catalysts of an asset
contract AssetAttributesRegistry is WithMinter, WithUpgrader, IAssetAttributesRegistry, Context {
uint256 internal constant MAX_NUM_GEMS = 15;
uint256 private constant IS_NFT = 0x0000000000000000000000000000000000000000800000000000000000000000;
uint256 private constant NOT_IS_NFT = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFFFFFFFFFFFFFF;
uint256 private constant NOT_NFT_INDEX = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF800000007FFFFFFFFFFFFFFF;
GemsCatalystsRegistry internal immutable _gemsCatalystsRegistry;
mapping(uint256 => Record) internal _records;
// used to allow migration to specify blockNumber when setting catalyst/gems
address public migrationContract;
// used to to set catalyst without burning actual ERC20 (cross layer deposit)
address public overLayerDepositor;
struct Record {
uint16 catalystId; // start at 1
uint16[MAX_NUM_GEMS] gemIds;
}
event CatalystApplied(uint256 indexed assetId, uint16 indexed catalystId, uint16[] gemIds, uint64 blockNumber);
event GemsAdded(uint256 indexed assetId, uint16[] gemIds, uint64 blockNumber);
/// @notice AssetAttributesRegistry depends on
/// @param gemsCatalystsRegistry: GemsCatalystsRegistry for fetching attributes
/// @param admin: for setting the migration contract address
/// @param minter: allowed to set gems and catalysts for a given asset
constructor(
GemsCatalystsRegistry gemsCatalystsRegistry,
address admin,
address minter,
address upgrader
) {
_gemsCatalystsRegistry = gemsCatalystsRegistry;
_admin = admin;
_minter = minter;
_upgrader = upgrader;
}
function getCatalystRegistry() external view override returns (address) {
return address(_gemsCatalystsRegistry);
}
/// @notice get the record data (catalyst id, gems ids list) for an asset id
/// @param assetId id of the asset
function getRecord(uint256 assetId)
external
view
override
returns (
bool exists,
uint16 catalystId,
uint16[] memory gemIds
)
{
catalystId = _records[assetId].catalystId;
if (catalystId == 0 && assetId & IS_NFT != 0) {
// fallback on collection catalyst
assetId = _getCollectionId(assetId);
catalystId = _records[assetId].catalystId;
}
uint16[MAX_NUM_GEMS] memory fixedGemIds = _records[assetId].gemIds;
exists = catalystId != 0;
gemIds = new uint16[](MAX_NUM_GEMS);
uint8 i = 0;
while (fixedGemIds[i] != 0) {
gemIds[i] = (fixedGemIds[i]);
i++;
}
}
/// @notice getAttributes
/// @param assetId id of the asset
/// @return values The array of values(256) requested.
function getAttributes(uint256 assetId, GemEvent[] calldata events)
external
view
override
returns (uint32[] memory values)
{
return _gemsCatalystsRegistry.getAttributes(_records[assetId].catalystId, assetId, events);
}
/// @notice sets the catalyst and gems when an asset goes over layers
/// @param assetId id of the asset
/// @param catalystId id of the catalyst to set
/// @param gemIds list of gems ids to set
function setCatalystWhenDepositOnOtherLayer(
uint256 assetId,
uint16 catalystId,
uint16[] calldata gemIds
) external override {
require(
_msgSender() == overLayerDepositor || _msgSender() == _admin,
"AssetAttributesRegistry: not overLayerDepositor"
);
// We have to ignore all 0 gemid in case of L2 to L1 deposit
// In this case we get gems data in a form of an array of MAX_NUM_GEMS padded with 0
if (gemIds.length == MAX_NUM_GEMS) {
uint256 firstZeroIndex;
for (firstZeroIndex = 0; firstZeroIndex < gemIds.length; firstZeroIndex++) {
if (gemIds[firstZeroIndex] == 0) {
break;
}
}
uint16[] memory gemIdsWithoutZero = new uint16[](firstZeroIndex);
// find first 0
for (uint256 i = 0; i < firstZeroIndex; i++) {
gemIdsWithoutZero[i] = gemIds[i];
}
_setCatalyst(assetId, catalystId, gemIdsWithoutZero, _getBlockNumber(), false);
} else {
_setCatalyst(assetId, catalystId, gemIds, _getBlockNumber(), false);
}
}
/// @notice sets the catalyst and gems for an asset, minter only
/// @param assetId id of the asset
/// @param catalystId id of the catalyst to set
/// @param gemIds list of gems ids to set
function setCatalyst(
uint256 assetId,
uint16 catalystId,
uint16[] calldata gemIds
) external virtual override {
require(_msgSender() == _minter || _msgSender() == _upgrader, "NOT_AUTHORIZED_MINTER");
_setCatalyst(assetId, catalystId, gemIds, _getBlockNumber(), true);
}
/// @notice sets the catalyst and gems for an asset for a given block number, migration contract only
/// @param assetId id of the asset
/// @param catalystId id of the catalyst to set
/// @param gemIds list of gems ids to set
/// @param blockNumber block number
function setCatalystWithBlockNumber(
uint256 assetId,
uint16 catalystId,
uint16[] calldata gemIds,
uint64 blockNumber
) external override {
require(_msgSender() == migrationContract, "NOT_AUTHORIZED_MIGRATION");
_setCatalyst(assetId, catalystId, gemIds, blockNumber, true);
}
/// @notice adds gems to an existing list of gems of an asset, upgrader only
/// @param assetId id of the asset
/// @param gemIds list of gems ids to set
function addGems(uint256 assetId, uint16[] calldata gemIds) external virtual override {
require(_msgSender() == _upgrader, "NOT_AUTHORIZED_UPGRADER");
require(assetId & IS_NFT != 0, "INVALID_NOT_NFT");
require(gemIds.length != 0, "INVALID_GEMS_0");
uint16 catalystId = _records[assetId].catalystId;
uint16[MAX_NUM_GEMS] memory gemIdsToStore;
if (catalystId == 0) {
// fallback on collection catalyst
uint256 collectionId = _getCollectionId(assetId);
catalystId = _records[collectionId].catalystId;
if (catalystId != 0) {
_records[assetId].catalystId = catalystId;
gemIdsToStore = _records[collectionId].gemIds;
}
} else {
gemIdsToStore = _records[assetId].gemIds;
}
require(catalystId != 0, "NO_CATALYST_SET");
uint8 j = 0;
uint8 i = 0;
for (i = 0; i < MAX_NUM_GEMS; i++) {
if (j == gemIds.length) {
break;
}
if (gemIdsToStore[i] == 0) {
require(gemIds[j] != 0, "INVALID_GEM_ID");
gemIdsToStore[i] = gemIds[j];
j++;
}
}
uint8 maxGems = _gemsCatalystsRegistry.getMaxGems(catalystId);
require(i <= maxGems, "GEMS_TOO_MANY");
_records[assetId].gemIds = gemIdsToStore;
uint64 blockNumber = _getBlockNumber();
emit GemsAdded(assetId, gemIds, blockNumber);
}
/// @notice set the migration contract address, admin or migration contract only
/// @param _migrationContract address of the migration contract
function setMigrationContract(address _migrationContract) external override {
address currentMigrationContract = migrationContract;
if (currentMigrationContract == address(0)) {
require(_msgSender() == _admin, "NOT_AUTHORIZED");
migrationContract = _migrationContract;
} else {
require(_msgSender() == currentMigrationContract, "NOT_AUTHORIZED_MIGRATION");
migrationContract = _migrationContract;
}
}
/// @dev Set a catalyst for the given asset.
/// @param assetId The asset to set a catalyst on.
/// @param catalystId The catalyst to set.
/// @param gemIds The gems to embed in the catalyst.
/// @param blockNumber The blocknumber to emit in the event.
/// @param hasToEmitEvent boolean to indicate if we want to emit an event
function _setCatalyst(
uint256 assetId,
uint16 catalystId,
uint16[] memory gemIds,
uint64 blockNumber,
bool hasToEmitEvent
) internal virtual {
require(gemIds.length <= MAX_NUM_GEMS, "GEMS_MAX_REACHED");
uint8 maxGems = _gemsCatalystsRegistry.getMaxGems(catalystId);
require(gemIds.length <= maxGems, "GEMS_TOO_MANY");
uint16[MAX_NUM_GEMS] memory gemIdsToStore;
for (uint8 i = 0; i < gemIds.length; i++) {
require(gemIds[i] != 0, "INVALID_GEM_ID");
gemIdsToStore[i] = gemIds[i];
}
_records[assetId] = Record(catalystId, gemIdsToStore);
if (hasToEmitEvent) {
emit CatalystApplied(assetId, catalystId, gemIds, blockNumber);
}
}
/// @dev Get the collection Id for an asset.
/// @param assetId The asset to get the collection id for.
/// @return The id of the collection the asset belongs to.
function _getCollectionId(uint256 assetId) internal pure returns (uint256) {
return assetId & NOT_NFT_INDEX & NOT_IS_NFT; // compute the same as Asset to get collectionId
}
/// @dev Get a blocknumber for use when querying attributes.
/// @return blockNumber The current blocknumber + 1.
function _getBlockNumber() internal view returns (uint64 blockNumber) {
blockNumber = uint64(block.number + 1);
}
}