-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPoolManager.sol
471 lines (396 loc) · 18.8 KB
/
PoolManager.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.26;
import {ERC7540VaultFactory} from "src/factories/ERC7540VaultFactory.sol";
import {ITrancheFactory} from "src/interfaces/factories/ITrancheFactory.sol";
import {ITranche} from "src/interfaces/token/ITranche.sol";
import {IHook} from "src/interfaces/token/IHook.sol";
import {IERC20Metadata, IERC20Wrapper} from "src/interfaces/IERC20.sol";
import {Auth} from "src/Auth.sol";
import {SafeTransferLib} from "src/libraries/SafeTransferLib.sol";
import {MathLib} from "src/libraries/MathLib.sol";
import {MessagesLib} from "src/libraries/MessagesLib.sol";
import {CastLib} from "src/libraries/CastLib.sol";
import {
Pool,
TrancheDetails,
TranchePrice,
UndeployedTranche,
VaultAsset,
Domain,
IPoolManager
} from "src/interfaces/IPoolManager.sol";
import {BytesLib} from "src/libraries/BytesLib.sol";
import {IEscrow} from "src/interfaces/IEscrow.sol";
import {IGateway} from "src/interfaces/gateway/IGateway.sol";
import {IGasService} from "src/interfaces/gateway/IGasService.sol";
import {IAuth} from "src/interfaces/IAuth.sol";
import {IRecoverable} from "src/interfaces/IRoot.sol";
/// @title Pool Manager
/// @notice This contract manages which pools & tranches exist,
/// as well as managing allowed pool currencies, and incoming and outgoing transfers.
contract PoolManager is Auth, IPoolManager {
using BytesLib for bytes;
using MathLib for uint256;
using CastLib for *;
uint8 internal constant MIN_DECIMALS = 2;
uint8 internal constant MAX_DECIMALS = 18;
IEscrow public immutable escrow;
IGateway public gateway;
address public investmentManager;
ERC7540VaultFactory public vaultFactory;
ITrancheFactory public trancheFactory;
IGasService public gasService;
mapping(uint64 poolId => Pool) internal _pools;
mapping(address => VaultAsset) internal _vaultToAsset;
mapping(uint64 poolId => mapping(bytes16 => UndeployedTranche)) internal _undeployedTranches;
/// @inheritdoc IPoolManager
mapping(uint128 assetId => address) public idToAsset;
/// @inheritdoc IPoolManager
mapping(address => uint128 assetId) public assetToId;
constructor(address escrow_, address vaultFactory_, address trancheFactory_) Auth(msg.sender) {
escrow = IEscrow(escrow_);
vaultFactory = ERC7540VaultFactory(vaultFactory_);
trancheFactory = ITrancheFactory(trancheFactory_);
}
// --- Administration ---
/// @inheritdoc IPoolManager
function file(bytes32 what, address data) external auth {
if (what == "gateway") gateway = IGateway(data);
else if (what == "investmentManager") investmentManager = data;
else if (what == "trancheFactory") trancheFactory = ITrancheFactory(data);
else if (what == "vaultFactory") vaultFactory = ERC7540VaultFactory(data);
else if (what == "gasService") gasService = IGasService(data);
else revert("PoolManager/file-unrecognized-param");
emit File(what, data);
}
/// @inheritdoc IRecoverable
function recoverTokens(address token, address to, uint256 amount) external auth {
SafeTransferLib.safeTransfer(token, to, amount);
}
// --- Outgoing message handling ---
/// @inheritdoc IPoolManager
function transferAssets(address asset, bytes32 recipient, uint128 amount) external {
uint128 assetId = assetToId[asset];
require(assetId != 0, "PoolManager/unknown-asset");
SafeTransferLib.safeTransferFrom(asset, msg.sender, address(escrow), amount);
gateway.send(
abi.encodePacked(uint8(MessagesLib.Call.TransferAssets), assetId, recipient, amount), address(this)
);
emit TransferAssets(asset, msg.sender, recipient, amount);
}
/// @inheritdoc IPoolManager
function transferTrancheTokens(
uint64 poolId,
bytes16 trancheId,
Domain destinationDomain,
uint64 destinationId,
bytes32 recipient,
uint128 amount
) external {
ITranche tranche = ITranche(getTranche(poolId, trancheId));
require(address(tranche) != address(0), "PoolManager/unknown-token");
tranche.burn(msg.sender, amount);
bytes9 domain = _formatDomain(destinationDomain, destinationId);
gateway.send(
abi.encodePacked(
uint8(MessagesLib.Call.TransferTrancheTokens), poolId, trancheId, domain, recipient, amount
),
address(this)
);
emit TransferTrancheTokens(poolId, trancheId, msg.sender, destinationDomain, destinationId, recipient, amount);
}
// --- Incoming message handling ---
/// @inheritdoc IPoolManager
function handle(bytes calldata message) external auth {
MessagesLib.Call call = MessagesLib.messageType(message);
if (call == MessagesLib.Call.AddAsset) {
addAsset(message.toUint128(1), message.toAddress(17));
} else if (call == MessagesLib.Call.AddPool) {
addPool(message.toUint64(1));
} else if (call == MessagesLib.Call.AddTranche) {
addTranche(
message.toUint64(1),
message.toBytes16(9),
message.slice(25, 128).bytes128ToString(),
message.toBytes32(153).toString(),
message.toUint8(185),
message.toAddress(186)
);
} else if (call == MessagesLib.Call.AllowAsset) {
allowAsset(message.toUint64(1), message.toUint128(9));
} else if (call == MessagesLib.Call.DisallowAsset) {
disallowAsset(message.toUint64(1), message.toUint128(9));
} else if (call == MessagesLib.Call.UpdateTranchePrice) {
updateTranchePrice(
message.toUint64(1),
message.toBytes16(9),
message.toUint128(25),
message.toUint128(41),
message.toUint64(57)
);
} else if (call == MessagesLib.Call.UpdateTrancheMetadata) {
updateTrancheMetadata(
message.toUint64(1),
message.toBytes16(9),
message.slice(25, 128).bytes128ToString(),
message.toBytes32(153).toString()
);
} else if (call == MessagesLib.Call.UpdateTrancheHook) {
updateTrancheHook(message.toUint64(1), message.toBytes16(9), message.toAddress(25));
} else if (call == MessagesLib.Call.TransferAssets) {
handleTransfer(message.toUint128(1), message.toAddress(17), message.toUint128(49));
} else if (call == MessagesLib.Call.TransferTrancheTokens) {
handleTransferTrancheTokens(
message.toUint64(1), message.toBytes16(9), message.toAddress(34), message.toUint128(66)
);
} else if (call == MessagesLib.Call.UpdateRestriction) {
updateRestriction(message.toUint64(1), message.toBytes16(9), message.slice(25, message.length - 25));
} else {
revert("PoolManager/invalid-message");
}
}
/// @inheritdoc IPoolManager
function addPool(uint64 poolId) public auth {
Pool storage pool = _pools[poolId];
require(pool.createdAt == 0, "PoolManager/pool-already-added");
pool.createdAt = block.timestamp;
emit AddPool(poolId);
}
/// @inheritdoc IPoolManager
function allowAsset(uint64 poolId, uint128 assetId) public auth {
require(isPoolActive(poolId), "PoolManager/invalid-pool");
address asset = idToAsset[assetId];
require(asset != address(0), "PoolManager/unknown-asset");
_pools[poolId].allowedAssets[asset] = true;
emit AllowAsset(poolId, asset);
}
/// @inheritdoc IPoolManager
function disallowAsset(uint64 poolId, uint128 assetId) public auth {
require(isPoolActive(poolId), "PoolManager/invalid-pool");
address asset = idToAsset[assetId];
require(asset != address(0), "PoolManager/unknown-asset");
delete _pools[poolId].allowedAssets[asset];
emit DisallowAsset(poolId, asset);
}
/// @inheritdoc IPoolManager
function addTranche(
uint64 poolId,
bytes16 trancheId,
string memory name,
string memory symbol,
uint8 decimals,
address hook
) public auth {
require(decimals >= MIN_DECIMALS, "PoolManager/too-few-tranche-token-decimals");
require(decimals <= MAX_DECIMALS, "PoolManager/too-many-tranche-token-decimals");
require(isPoolActive(poolId), "PoolManager/invalid-pool");
UndeployedTranche storage undeployedTranche = _undeployedTranches[poolId][trancheId];
require(undeployedTranche.decimals == 0, "PoolManager/tranche-already-exists");
require(getTranche(poolId, trancheId) == address(0), "PoolManager/tranche-already-deployed");
// Hook can be address zero if the tranche token is fully permissionless and has no custom logic
require(
hook == address(0) || IHook(hook).supportsInterface(type(IHook).interfaceId) == true,
"PoolManager/invalid-hook"
);
undeployedTranche.decimals = decimals;
undeployedTranche.tokenName = name;
undeployedTranche.tokenSymbol = symbol;
undeployedTranche.hook = hook;
emit AddTranche(poolId, trancheId);
}
/// @inheritdoc IPoolManager
function updateTrancheMetadata(uint64 poolId, bytes16 trancheId, string memory name, string memory symbol)
public
auth
{
ITranche tranche = ITranche(getTranche(poolId, trancheId));
require(address(tranche) != address(0), "PoolManager/unknown-token");
require(
keccak256(bytes(tranche.name())) != keccak256(bytes(name))
|| keccak256(bytes(tranche.symbol())) != keccak256(bytes(symbol)),
"PoolManager/old-metadata"
);
tranche.file("name", name);
tranche.file("symbol", symbol);
}
/// @inheritdoc IPoolManager
function updateTranchePrice(uint64 poolId, bytes16 trancheId, uint128 assetId, uint128 price, uint64 computedAt)
public
auth
{
TrancheDetails storage tranche = _pools[poolId].tranches[trancheId];
require(
tranche.token != address(0) || canTrancheBeDeployed(poolId, trancheId), "PoolManager/tranche-does-not-exist"
);
address asset = idToAsset[assetId];
require(computedAt >= tranche.prices[asset].computedAt, "PoolManager/cannot-set-older-price");
tranche.prices[asset] = TranchePrice(price, computedAt);
emit PriceUpdate(poolId, trancheId, asset, price, computedAt);
}
/// @inheritdoc IPoolManager
function updateRestriction(uint64 poolId, bytes16 trancheId, bytes memory update) public auth {
ITranche tranche = ITranche(getTranche(poolId, trancheId));
require(address(tranche) != address(0), "PoolManager/unknown-token");
address hook = tranche.hook();
require(hook != address(0), "PoolManager/invalid-hook");
IHook(hook).updateRestriction(address(tranche), update);
}
/// @inheritdoc IPoolManager
function updateTrancheHook(uint64 poolId, bytes16 trancheId, address hook) public auth {
ITranche tranche = ITranche(getTranche(poolId, trancheId));
require(address(tranche) != address(0), "PoolManager/unknown-token");
require(hook != tranche.hook(), "PoolManager/old-hook");
tranche.file("hook", hook);
}
/// @inheritdoc IPoolManager
function addAsset(uint128 assetId, address asset) public auth {
// Currency index on the Centrifuge side should start at 1
require(assetId != 0, "PoolManager/asset-id-has-to-be-greater-than-0");
require(idToAsset[assetId] == address(0), "PoolManager/asset-id-in-use");
require(assetToId[asset] == 0, "PoolManager/asset-address-in-use");
uint8 assetDecimals = IERC20Metadata(asset).decimals();
require(assetDecimals >= MIN_DECIMALS, "PoolManager/too-few-asset-decimals");
require(assetDecimals <= MAX_DECIMALS, "PoolManager/too-many-asset-decimals");
idToAsset[assetId] = asset;
assetToId[asset] = assetId;
// Give investment manager infinite approval for asset
// in the escrow to transfer to the user on redeem or withdraw
escrow.approveMax(asset, investmentManager);
// Give pool manager infinite approval for asset
// in the escrow to transfer to the user on transfer
escrow.approveMax(asset, address(this));
emit AddAsset(assetId, asset);
}
/// @inheritdoc IPoolManager
function handleTransfer(uint128 assetId, address recipient, uint128 amount) public auth {
address asset = idToAsset[assetId];
require(asset != address(0), "PoolManager/unknown-asset");
SafeTransferLib.safeTransferFrom(asset, address(escrow), recipient, amount);
}
/// @inheritdoc IPoolManager
function handleTransferTrancheTokens(uint64 poolId, bytes16 trancheId, address destinationAddress, uint128 amount)
public
auth
{
ITranche tranche = ITranche(getTranche(poolId, trancheId));
require(address(tranche) != address(0), "PoolManager/unknown-token");
tranche.mint(destinationAddress, amount);
}
// --- Public functions ---
// slither-disable-start reentrancy-eth
/// @inheritdoc IPoolManager
function deployTranche(uint64 poolId, bytes16 trancheId) external returns (address) {
require(canTrancheBeDeployed(poolId, trancheId), "PoolManager/tranche-not-added");
address[] memory trancheWards = new address[](2);
trancheWards[0] = investmentManager;
trancheWards[1] = address(this);
UndeployedTranche storage undeployedTranche = _undeployedTranches[poolId][trancheId];
address token = trancheFactory.newTranche(
poolId,
trancheId,
undeployedTranche.tokenName,
undeployedTranche.tokenSymbol,
undeployedTranche.decimals,
trancheWards
);
if (undeployedTranche.hook != address(0)) {
ITranche(token).file("hook", undeployedTranche.hook);
}
_pools[poolId].tranches[trancheId].token = token;
delete _undeployedTranches[poolId][trancheId];
// Give investment manager infinite approval for tranche tokens
// in the escrow to transfer to the user on deposit or mint
escrow.approveMax(token, investmentManager);
emit DeployTranche(poolId, trancheId, token);
return token;
}
// slither-disable-end reentrancy-eth
/// @inheritdoc IPoolManager
function deployVault(uint64 poolId, bytes16 trancheId, address asset) external returns (address) {
TrancheDetails storage tranche = _pools[poolId].tranches[trancheId];
require(tranche.token != address(0), "PoolManager/tranche-does-not-exist");
require(isAllowedAsset(poolId, asset), "PoolManager/asset-not-supported");
address vault = ITranche(tranche.token).vault(asset);
require(vault == address(0), "PoolManager/vault-already-deployed");
// Rely investment manager on vault so it can mint tokens
address[] memory vaultWards = new address[](1);
vaultWards[0] = investmentManager;
// Deploy vault
vault = vaultFactory.newVault(
poolId, trancheId, asset, tranche.token, address(escrow), investmentManager, vaultWards
);
// Check whether the ERC20 token is a wrapper
try IERC20Wrapper(asset).underlying() returns (address) {
_vaultToAsset[vault] = VaultAsset(asset, true);
} catch {
_vaultToAsset[vault] = VaultAsset(asset, false);
}
// Link vault to tranche token
IAuth(tranche.token).rely(vault);
ITranche(tranche.token).updateVault(asset, vault);
emit DeployVault(poolId, trancheId, asset, vault);
return vault;
}
/// @inheritdoc IPoolManager
function removeVault(uint64 poolId, bytes16 trancheId, address asset) external auth {
TrancheDetails storage tranche = _pools[poolId].tranches[trancheId];
require(tranche.token != address(0), "PoolManager/tranche-does-not-exist");
address vault = ITranche(tranche.token).vault(asset);
require(vault != address(0), "PoolManager/vault-not-deployed");
vaultFactory.denyVault(vault, investmentManager);
delete _vaultToAsset[vault];
IAuth(tranche.token).deny(vault);
ITranche(tranche.token).updateVault(asset, address(0));
emit RemoveVault(poolId, trancheId, asset, vault);
}
// --- Helpers ---
/// @inheritdoc IPoolManager
function isPoolActive(uint64 poolId) public view returns (bool) {
return _pools[poolId].createdAt > 0;
}
/// @inheritdoc IPoolManager
function getTranche(uint64 poolId, bytes16 trancheId) public view returns (address) {
TrancheDetails storage tranche = _pools[poolId].tranches[trancheId];
return tranche.token;
}
/// @inheritdoc IPoolManager
function canTrancheBeDeployed(uint64 poolId, bytes16 trancheId) public view returns (bool) {
return _undeployedTranches[poolId][trancheId].decimals > 0;
}
/// @inheritdoc IPoolManager
function getTranchePrice(uint64 poolId, bytes16 trancheId, address asset)
public
view
returns (uint128 price, uint64 computedAt)
{
TranchePrice memory value = _pools[poolId].tranches[trancheId].prices[asset];
require(value.computedAt > 0, "PoolManager/unknown-price");
price = value.price;
computedAt = value.computedAt;
}
/// @inheritdoc IPoolManager
function getVault(uint64 poolId, bytes16 trancheId, uint128 assetId) public view returns (address) {
address vault = ITranche(_pools[poolId].tranches[trancheId].token).vault(idToAsset[assetId]);
require(vault != address(0), "PoolManager/unknown-vault");
return vault;
}
/// @inheritdoc IPoolManager
function getVault(uint64 poolId, bytes16 trancheId, address asset) public view returns (address) {
address vault = ITranche(_pools[poolId].tranches[trancheId].token).vault(asset);
require(vault != address(0), "PoolManager/unknown-vault");
return vault;
}
/// @inheritdoc IPoolManager
function getVaultAsset(address vault) public view override returns (address, bool) {
VaultAsset memory _asset = _vaultToAsset[vault];
require(_asset.asset != address(0), "PoolManager/unknown-vault");
return (_asset.asset, _asset.isWrapper);
}
/// @inheritdoc IPoolManager
function isAllowedAsset(uint64 poolId, address asset) public view returns (bool) {
return _pools[poolId].allowedAssets[asset];
}
function _formatDomain(Domain domain, uint64 chainId) internal pure returns (bytes9) {
return bytes9(BytesLib.slice(abi.encodePacked(uint8(domain), chainId), 0, 9));
}
}