-
Notifications
You must be signed in to change notification settings - Fork 3
/
HyperdriveRegistry.sol
392 lines (347 loc) · 14.9 KB
/
HyperdriveRegistry.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
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;
import { IHyperdrive } from "../interfaces/IHyperdrive.sol";
import { IHyperdriveFactory } from "../interfaces/IHyperdriveFactory.sol";
import { IHyperdriveGovernedRegistry } from "../interfaces/IHyperdriveGovernedRegistry.sol";
import { IHyperdriveRegistry } from "../interfaces/IHyperdriveRegistry.sol";
import { VERSION } from "../libraries/Constants.sol";
import { SafeCast } from "../libraries/SafeCast.sol";
/// @author DELV
/// @title HyperdriveRegistry
/// @notice Allows an admin address to manage a list of registered Hyperdrive
/// instances and factories. This provides a convenient place for
/// consumers of Hyperdrive contracts to query a list of well-known
/// pools.
/// @custom:disclaimer The language used in this code is for coding convenience
/// only, and is not intended to, and does not, have any
/// particular legal or regulatory significance.
contract HyperdriveRegistry is
IHyperdriveRegistry,
IHyperdriveGovernedRegistry
{
using SafeCast for *;
/// @notice The registry's name.
string public name;
/// @notice The registry's version.
string public constant version = VERSION;
/// @notice The registry's admin address.
address public admin;
/// @dev A list of all of the Hyperdrive factories that have been added to
/// the Hyperdrive registry.
address[] internal _factories;
/// @dev A mapping from hyperdrive factories to info associated with those
/// factories.
mapping(address factory => FactoryInfoInternal info) internal _factoryInfo;
/// @dev A list of all of the Hyperdrive instances that have been added to
/// the Hyperdrive registry.
address[] internal _instances;
/// @dev A mapping from hyperdrive instances to info associated with those
/// instances.
mapping(address hyperdrive => InstanceInfoInternal info)
internal _instanceInfo;
/// @notice Instantiates the hyperdrive registry.
/// @param _name The registry's name.
constructor(string memory _name) {
admin = msg.sender;
name = _name;
}
/// @dev Ensures that the modified function is only called by the admin.
modifier onlyAdmin() {
if (msg.sender != admin) {
revert IHyperdriveGovernedRegistry.Unauthorized();
}
_;
}
/// @inheritdoc IHyperdriveGovernedRegistry
function updateAdmin(address _admin) external override onlyAdmin {
admin = _admin;
emit AdminUpdated(_admin);
}
/// @inheritdoc IHyperdriveGovernedRegistry
function setFactoryInfo(
address[] calldata __factories,
uint128[] calldata _data
) external override onlyAdmin {
// Ensure that the arrays have the same length.
if (__factories.length != _data.length) {
revert IHyperdriveGovernedRegistry.InputLengthMismatch();
}
// Add the Hyperdrive factory data to the registry.
for (uint256 i = 0; i < __factories.length; i++) {
// If the updated data is zero, we are deleting the entry. We remove
// the entry from the list of Hyperdrive factories and delete the
// stored info. If the existing data is zero, we can simply skip
// this entry.
uint256 data = _factoryInfo[__factories[i]].data;
if (_data[i] == 0 && data != 0) {
_removeFactory(__factories[i]);
}
// If the updated data is non-zero and the existing data is non-zero,
// we are updating an existing entry in the registry.
else if (_data[i] != 0 && data != 0) {
_updateFactory(__factories[i], _data[i]);
}
// If the updated data is non-zero and the existing data is zero,
// we are adding a new entry to the registry.
else if (_data[i] != 0 && data == 0) {
_addFactory(__factories[i], _data[i]);
}
// Otherwise, the update is attempting to remove a non-existant
// entry. To avoid emitting a confusing event, we continue.
else {
continue;
}
// Emit an event recording the update.
emit FactoryInfoUpdated(__factories[i], _data[i]);
}
}
/// @inheritdoc IHyperdriveGovernedRegistry
function setInstanceInfo(
address[] calldata __instances,
uint128[] calldata _data,
address[] calldata __factories
) external override onlyAdmin {
// Ensure that the arrays have the same length.
if (
__instances.length != _data.length ||
__instances.length != __factories.length
) {
revert IHyperdriveGovernedRegistry.InputLengthMismatch();
}
// Add the Hyperdrive data to the registry.
for (uint256 i = 0; i < __instances.length; i++) {
// If the updated data is zero, we are deleting the entry. The
// factory should also be zero, and we remove the entry from the
// list of Hyperdrive instances and delete the stored info. If the
// existing data is zero, we can simply skip this entry.
uint256 data = _instanceInfo[__instances[i]].data;
if (_data[i] == 0 && data != 0) {
// Ensure that the factory address is zero.
if (__factories[i] != address(0)) {
revert IHyperdriveGovernedRegistry.InvalidFactory();
}
// Remove the entry from the registry.
_removeInstance(__instances[i]);
}
// If the updated data is non-zero and the existing data is non-zero,
// we are updating an existing entry in the registry.
else if (_data[i] != 0 && data != 0) {
_updateInstance(__instances[i], _data[i], __factories[i]);
}
// If the updated data is non-zero and the existing data is zero,
// we are adding a new entry to the registry.
else if (_data[i] != 0 && data == 0) {
_addInstance(__instances[i], _data[i], __factories[i]);
}
// Otherwise, the update is attempting to remove a non-existant
// entry. To avoid emitting a confusing event, we continue.
else {
continue;
}
// Emit an event recording the update.
emit InstanceInfoUpdated(__instances[i], _data[i], __factories[i]);
}
}
/// @inheritdoc IHyperdriveRegistry
function getNumberOfFactories() external view returns (uint256) {
return _factories.length;
}
/// @inheritdoc IHyperdriveRegistry
function getFactoriesInRange(
uint256 _startIndex,
uint256 _endIndex
) external view returns (address[] memory factories) {
// If the indexes are malformed, revert.
if (_startIndex >= _endIndex) {
revert IHyperdriveGovernedRegistry.InvalidIndexes();
}
if (_endIndex > _factories.length) {
revert IHyperdriveGovernedRegistry.EndIndexTooLarge();
}
// Get the registered factories in the range.
factories = new address[](_endIndex - _startIndex);
for (uint256 i = _startIndex; i < _endIndex; i++) {
factories[i - _startIndex] = _factories[i];
}
return factories;
}
/// @inheritdoc IHyperdriveRegistry
function getFactoryAtIndex(uint256 _index) external view returns (address) {
return _factories[_index];
}
/// @inheritdoc IHyperdriveRegistry
function getFactoryInfo(
address[] calldata __factories
) external view override returns (FactoryInfo[] memory info) {
info = new FactoryInfo[](__factories.length);
for (uint256 i = 0; i < __factories.length; i++) {
IHyperdriveFactory factory = IHyperdriveFactory(__factories[i]);
info[i] = FactoryInfo({
data: _factoryInfo[__factories[i]].data,
name: factory.name(),
version: factory.version()
});
}
return info;
}
/// @inheritdoc IHyperdriveRegistry
function getNumberOfInstances() external view returns (uint256) {
return _instances.length;
}
/// @inheritdoc IHyperdriveRegistry
function getInstancesInRange(
uint256 _startIndex,
uint256 _endIndex
) external view returns (address[] memory instances) {
// If the indexes are malformed, revert.
if (_startIndex >= _endIndex) {
revert IHyperdriveGovernedRegistry.InvalidIndexes();
}
if (_endIndex > _instances.length) {
revert IHyperdriveGovernedRegistry.EndIndexTooLarge();
}
// Get the registered instances in the range.
instances = new address[](_endIndex - _startIndex);
for (uint256 i = _startIndex; i < _endIndex; i++) {
instances[i - _startIndex] = _instances[i];
}
return instances;
}
/// @inheritdoc IHyperdriveRegistry
function getInstanceAtIndex(
uint256 _index
) external view returns (address) {
return _instances[_index];
}
/// @inheritdoc IHyperdriveRegistry
function getInstanceInfo(
address[] calldata __instances
) external view override returns (InstanceInfo[] memory info) {
info = new InstanceInfo[](__instances.length);
for (uint256 i = 0; i < __instances.length; i++) {
IHyperdrive instance = IHyperdrive(__instances[i]);
info[i] = InstanceInfo({
data: _instanceInfo[__instances[i]].data,
factory: _instanceInfo[__instances[i]].factory,
name: instance.name(),
version: instance.version()
});
}
return info;
}
/// @dev Adds a new Hyperdrive factory to the registry.
/// @param _factory The factory to add.
/// @param _data The data associated with the new factory.
function _addFactory(address _factory, uint128 _data) internal {
// Add the new factory to the list of Hyperdrive factories.
uint256 index = _factories.length;
_factories.push(_factory);
// Add the entry to the mapping.
_factoryInfo[_factory] = FactoryInfoInternal({
data: _data,
index: uint128(index)
});
}
/// @dev Adds a new Hyperdrive factory to the registry or updates an
/// existing Hyperdrive factory in the registry.
/// @param _factory The Hyperdrive factory to update.
/// @param _data The data associated with the new factory.
function _updateFactory(address _factory, uint128 _data) internal {
_factoryInfo[_factory].data = _data;
}
/// @dev Removes a Hyperdrive factory from the registry.
/// @param _factory The Hyperdrive factory to remove.
function _removeFactory(address _factory) internal {
// Delete the entry from the factories list. If the factory isn't the
// last item in the list, the item is replaced with the last item in the
// list.
uint128 index = _factoryInfo[_factory].index;
uint256 length = _factories.length;
if (index != length - 1) {
// Update the index of the entry that will replace the removed entry
// in the list.
address replacementFactory = _factories[length - 1];
_factoryInfo[replacementFactory].index = index;
// Replace the entry that is being removed.
_factories[index] = replacementFactory;
}
_factories.pop();
// Delete the entry from the mapping.
delete _factoryInfo[_factory];
}
/// @dev Adds a new Hyperdrive instance to the registry.
/// @param _instance The instance to add.
/// @param _data The data associated with the new instance.
/// @param _factory The factory that deployed the new instance.
function _addInstance(
address _instance,
uint128 _data,
address _factory
) internal {
// Verify that the Hyperdrive instance was actually deployed by the
// factory. If the updated factory address is zero, we skip this check.
if (
_factory != address(0) &&
!IHyperdriveFactory(_factory).isInstance(_instance)
) {
revert IHyperdriveGovernedRegistry.InvalidFactory();
}
// Add the new instance to the list of Hyperdrive instances.
uint256 index = _instances.length;
_instances.push(_instance);
// Add the entry to the mapping.
_instanceInfo[_instance] = InstanceInfoInternal({
data: _data,
factory: _factory,
index: uint128(index)
});
}
/// @dev Adds a new Hyperdrive instance to the registry or updates an
/// existing Hyperdrive instance in the registry.
/// @param _instance The Hyperdrive instance to update.
/// @param _data The data associated with the new instance.
/// @param _factory The factory that deployed the new instance.
function _updateInstance(
address _instance,
uint128 _data,
address _factory
) internal {
// If the existing factory address is non-zero, we verify that
// the updated factory address is the same. Otherwise, if the
// updated factory address is non-zero, verify that the
// Hyperdrive instance was actually deployed by the factory. If
// the updated factory address is zero, we skip this check.
address factory = _instanceInfo[_instance].factory;
if (
(factory != address(0) && factory != _factory) ||
(factory == address(0) &&
_factory != address(0) &&
!IHyperdriveFactory(_factory).isInstance(_instance))
) {
revert IHyperdriveGovernedRegistry.InvalidFactory();
}
// Update the entry in the mapping.
_instanceInfo[_instance].data = _data;
_instanceInfo[_instance].factory = _factory;
}
/// @dev Removes a Hyperdrive instance from the registry.
/// @param _instance The Hyperdrive instance to remove.
function _removeInstance(address _instance) internal {
// Delete the entry from the instances list. If the instance isn't the
// last item in the list, the item is replaced with the last item in the
// list.
uint128 index = _instanceInfo[_instance].index;
uint256 length = _instances.length;
if (index != length - 1) {
// Update the index of the entry that will replace the removed entry
// in the list.
address replacementInstance = _instances[length - 1];
_instanceInfo[replacementInstance].index = index;
// Replace the entry that is being removed.
_instances[index] = replacementInstance;
}
_instances.pop();
// Delete the entry from the mapping.
delete _instanceInfo[_instance];
}
}