-
Notifications
You must be signed in to change notification settings - Fork 6
/
InvestmentManager.sol
640 lines (549 loc) · 26.3 KB
/
InvestmentManager.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.26;
import {Auth} from "src/Auth.sol";
import {CastLib} from "src/libraries/CastLib.sol";
import {MathLib} from "src/libraries/MathLib.sol";
import {SafeTransferLib} from "src/libraries/SafeTransferLib.sol";
import {MessagesLib} from "src/libraries/MessagesLib.sol";
import {BytesLib} from "src/libraries/BytesLib.sol";
import {IERC20, IERC20Metadata} from "src/interfaces/IERC20.sol";
import {IPoolManager} from "src/interfaces/IPoolManager.sol";
import {IInvestmentManager, InvestmentState} from "src/interfaces/IInvestmentManager.sol";
import {ITranche} from "src/interfaces/token/ITranche.sol";
import {IERC7540Vault} from "src/interfaces/IERC7540.sol";
import {IGateway} from "src/interfaces/gateway/IGateway.sol";
import {IRecoverable} from "src/interfaces/IRoot.sol";
/// @title Investment Manager
/// @notice This is the main contract vaults interact with for
/// both incoming and outgoing investment transactions.
contract InvestmentManager is Auth, IInvestmentManager {
using BytesLib for bytes;
using MathLib for uint256;
using CastLib for *;
/// @dev Prices are fixed-point integers with 18 decimals
uint8 internal constant PRICE_DECIMALS = 18;
address public immutable root;
address public immutable escrow;
IGateway public gateway;
IPoolManager public poolManager;
/// @inheritdoc IInvestmentManager
mapping(address vault => mapping(address investor => InvestmentState)) public investments;
constructor(address root_, address escrow_) Auth(msg.sender) {
root = root_;
escrow = escrow_;
}
// --- Administration ---
/// @inheritdoc IInvestmentManager
function file(bytes32 what, address data) external auth {
if (what == "gateway") gateway = IGateway(data);
else if (what == "poolManager") poolManager = IPoolManager(data);
else revert("InvestmentManager/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 IInvestmentManager
function requestDeposit(address vault, uint256 assets, address controller, address, /* owner */ address source)
public
auth
returns (bool)
{
IERC7540Vault vault_ = IERC7540Vault(vault);
uint128 _assets = assets.toUint128();
require(_assets != 0, "InvestmentManager/zero-amount-not-allowed");
uint64 poolId = vault_.poolId();
address asset = vault_.asset();
require(poolManager.isAllowedAsset(poolId, asset), "InvestmentManager/asset-not-allowed");
require(
_canTransfer(vault, address(0), controller, convertToShares(vault, assets)),
"InvestmentManager/transfer-not-allowed"
);
InvestmentState storage state = investments[vault][controller];
require(state.pendingCancelDepositRequest != true, "InvestmentManager/cancellation-is-pending");
state.pendingDepositRequest = state.pendingDepositRequest + _assets;
gateway.send(
abi.encodePacked(
uint8(MessagesLib.Call.DepositRequest),
poolId,
vault_.trancheId(),
controller.toBytes32(),
poolManager.assetToId(asset),
_assets
),
source
);
return true;
}
/// @inheritdoc IInvestmentManager
function requestRedeem(address vault, uint256 shares, address controller, /* owner */ address, address source)
public
auth
returns (bool)
{
uint128 _shares = shares.toUint128();
require(_shares != 0, "InvestmentManager/zero-amount-not-allowed");
IERC7540Vault vault_ = IERC7540Vault(vault);
// You cannot redeem using a disallowed asset, instead another vault will have to be used
require(poolManager.isAllowedAsset(vault_.poolId(), vault_.asset()), "InvestmentManager/asset-not-allowed");
return _processRedeemRequest(vault, _shares, controller, source, false);
}
/// @dev triggered indicates if the the _processRedeemRequest call was triggered from centrifugeChain
function _processRedeemRequest(address vault, uint128 shares, address controller, address source, bool triggered)
internal
returns (bool)
{
IERC7540Vault vault_ = IERC7540Vault(vault);
InvestmentState storage state = investments[vault][controller];
require(state.pendingCancelRedeemRequest != true || triggered, "InvestmentManager/cancellation-is-pending");
state.pendingRedeemRequest = state.pendingRedeemRequest + shares;
gateway.send(
abi.encodePacked(
uint8(MessagesLib.Call.RedeemRequest),
vault_.poolId(),
vault_.trancheId(),
controller.toBytes32(),
poolManager.assetToId(vault_.asset()),
shares
),
source
);
return true;
}
/// @inheritdoc IInvestmentManager
function cancelDepositRequest(address vault, address controller, address source) public auth {
IERC7540Vault _vault = IERC7540Vault(vault);
InvestmentState storage state = investments[vault][controller];
require(state.pendingDepositRequest > 0, "InvestmentManager/no-pending-deposit-request");
require(state.pendingCancelDepositRequest != true, "InvestmentManager/cancellation-is-pending");
state.pendingCancelDepositRequest = true;
gateway.send(
abi.encodePacked(
uint8(MessagesLib.Call.CancelDepositRequest),
_vault.poolId(),
_vault.trancheId(),
controller.toBytes32(),
poolManager.assetToId(_vault.asset())
),
source
);
}
/// @inheritdoc IInvestmentManager
function cancelRedeemRequest(address vault, address controller, address source) public auth {
IERC7540Vault _vault = IERC7540Vault(vault);
uint256 approximateTranchesPayout = pendingRedeemRequest(vault, controller);
require(approximateTranchesPayout > 0, "InvestmentManager/no-pending-redeem-request");
require(
_canTransfer(vault, address(0), controller, approximateTranchesPayout),
"InvestmentManager/transfer-not-allowed"
);
InvestmentState storage state = investments[vault][controller];
require(state.pendingCancelRedeemRequest != true, "InvestmentManager/cancellation-is-pending");
state.pendingCancelRedeemRequest = true;
gateway.send(
abi.encodePacked(
uint8(MessagesLib.Call.CancelRedeemRequest),
_vault.poolId(),
_vault.trancheId(),
controller.toBytes32(),
poolManager.assetToId(_vault.asset())
),
source
);
}
// --- Incoming message handling ---
/// @inheritdoc IInvestmentManager
function handle(bytes calldata message) public auth {
MessagesLib.Call call = MessagesLib.messageType(message);
if (call == MessagesLib.Call.FulfilledDepositRequest) {
fulfillDepositRequest(
message.toUint64(1),
message.toBytes16(9),
message.toAddress(25),
message.toUint128(57),
message.toUint128(73),
message.toUint128(89)
);
} else if (call == MessagesLib.Call.FulfilledRedeemRequest) {
fulfillRedeemRequest(
message.toUint64(1),
message.toBytes16(9),
message.toAddress(25),
message.toUint128(57),
message.toUint128(73),
message.toUint128(89)
);
} else if (call == MessagesLib.Call.FulfilledCancelDepositRequest) {
fulfillCancelDepositRequest(
message.toUint64(1),
message.toBytes16(9),
message.toAddress(25),
message.toUint128(57),
message.toUint128(73),
message.toUint128(89)
);
} else if (call == MessagesLib.Call.FulfilledCancelRedeemRequest) {
fulfillCancelRedeemRequest(
message.toUint64(1),
message.toBytes16(9),
message.toAddress(25),
message.toUint128(57),
message.toUint128(73)
);
} else if (call == MessagesLib.Call.TriggerRedeemRequest) {
triggerRedeemRequest(
message.toUint64(1),
message.toBytes16(9),
message.toAddress(25),
message.toUint128(57),
message.toUint128(73)
);
} else {
revert("InvestmentManager/invalid-message");
}
}
/// @inheritdoc IInvestmentManager
function fulfillDepositRequest(
uint64 poolId,
bytes16 trancheId,
address user,
uint128 assetId,
uint128 assets,
uint128 shares
) public auth {
address vault = poolManager.getVault(poolId, trancheId, assetId);
InvestmentState storage state = investments[vault][user];
require(state.pendingDepositRequest != 0, "InvestmentManager/no-pending-deposit-request");
state.depositPrice = _calculatePrice(vault, _maxDeposit(vault, user) + assets, state.maxMint + shares);
state.maxMint = state.maxMint + shares;
state.pendingDepositRequest = state.pendingDepositRequest > assets ? state.pendingDepositRequest - assets : 0;
if (state.pendingDepositRequest == 0) delete state.pendingCancelDepositRequest;
// Mint to escrow. Recipient can claim by calling deposit / mint
ITranche tranche = ITranche(IERC7540Vault(vault).share());
tranche.mint(address(escrow), shares);
IERC7540Vault(vault).onDepositClaimable(user, assets, shares);
}
/// @inheritdoc IInvestmentManager
function fulfillRedeemRequest(
uint64 poolId,
bytes16 trancheId,
address user,
uint128 assetId,
uint128 assets,
uint128 shares
) public auth {
address vault = poolManager.getVault(poolId, trancheId, assetId);
InvestmentState storage state = investments[vault][user];
require(state.pendingRedeemRequest != 0, "InvestmentManager/no-pending-redeem-request");
// Calculate new weighted average redeem price and update order book values
state.redeemPrice =
_calculatePrice(vault, state.maxWithdraw + assets, ((maxRedeem(vault, user)) + shares).toUint128());
state.maxWithdraw = state.maxWithdraw + assets;
state.pendingRedeemRequest = state.pendingRedeemRequest > shares ? state.pendingRedeemRequest - shares : 0;
if (state.pendingRedeemRequest == 0) delete state.pendingCancelRedeemRequest;
// Burn redeemed tranche tokens from escrow
ITranche tranche = ITranche(IERC7540Vault(vault).share());
tranche.burn(address(escrow), shares);
IERC7540Vault(vault).onRedeemClaimable(user, assets, shares);
}
/// @inheritdoc IInvestmentManager
function fulfillCancelDepositRequest(
uint64 poolId,
bytes16 trancheId,
address user,
uint128 assetId,
uint128 assets,
uint128 fulfillment
) public auth {
address vault = poolManager.getVault(poolId, trancheId, assetId);
InvestmentState storage state = investments[vault][user];
require(state.pendingCancelDepositRequest == true, "InvestmentManager/no-pending-cancel-deposit-request");
state.claimableCancelDepositRequest = state.claimableCancelDepositRequest + assets;
state.pendingDepositRequest =
state.pendingDepositRequest > fulfillment ? state.pendingDepositRequest - fulfillment : 0;
if (state.pendingDepositRequest == 0) delete state.pendingCancelDepositRequest;
IERC7540Vault(vault).onCancelDepositClaimable(user, assets);
}
/// @inheritdoc IInvestmentManager
function fulfillCancelRedeemRequest(uint64 poolId, bytes16 trancheId, address user, uint128 assetId, uint128 shares)
public
auth
{
address vault = poolManager.getVault(poolId, trancheId, assetId);
InvestmentState storage state = investments[vault][user];
require(state.pendingCancelRedeemRequest == true, "InvestmentManager/no-pending-cancel-redeem-request");
state.claimableCancelRedeemRequest = state.claimableCancelRedeemRequest + shares;
state.pendingRedeemRequest = state.pendingRedeemRequest > shares ? state.pendingRedeemRequest - shares : 0;
if (state.pendingRedeemRequest == 0) delete state.pendingCancelRedeemRequest;
IERC7540Vault(vault).onCancelRedeemClaimable(user, shares);
}
/// @inheritdoc IInvestmentManager
function triggerRedeemRequest(uint64 poolId, bytes16 trancheId, address user, uint128 assetId, uint128 shares)
public
auth
{
require(shares != 0, "InvestmentManager/tranche-token-amount-is-zero");
address vault = poolManager.getVault(poolId, trancheId, assetId);
// If there's any unclaimed deposits, claim those first
InvestmentState storage state = investments[vault][user];
uint128 tokensToTransfer = shares;
if (state.maxMint >= shares) {
// The full redeem request is covered by the claimable amount
tokensToTransfer = 0;
state.maxMint = state.maxMint - shares;
} else if (state.maxMint != 0) {
// The redeem request is only partially covered by the claimable amount
tokensToTransfer = shares - state.maxMint;
state.maxMint = 0;
}
require(_processRedeemRequest(vault, shares, user, msg.sender, true), "InvestmentManager/failed-redeem-request");
// Transfer the tranche token amount that was not covered by tokens still in escrow for claims,
// from user to escrow (lock tranche tokens in escrow)
if (tokensToTransfer != 0) {
require(
ITranche(address(IERC7540Vault(vault).share())).authTransferFrom(
user, user, address(escrow), tokensToTransfer
),
"InvestmentManager/transfer-failed"
);
}
emit TriggerRedeemRequest(poolId, trancheId, user, poolManager.idToAsset(assetId), shares);
IERC7540Vault(vault).onRedeemRequest(user, user, shares);
}
// --- View functions ---
/// @inheritdoc IInvestmentManager
function convertToShares(address vault, uint256 _assets) public view returns (uint256 shares) {
IERC7540Vault vault_ = IERC7540Vault(vault);
(uint128 latestPrice,) = poolManager.getTranchePrice(vault_.poolId(), vault_.trancheId(), vault_.asset());
shares = uint256(_calculateShares(_assets.toUint128(), vault, latestPrice, MathLib.Rounding.Down));
}
/// @inheritdoc IInvestmentManager
function convertToAssets(address vault, uint256 _shares) public view returns (uint256 assets) {
IERC7540Vault vault_ = IERC7540Vault(vault);
(uint128 latestPrice,) = poolManager.getTranchePrice(vault_.poolId(), vault_.trancheId(), vault_.asset());
assets = uint256(_calculateAssets(_shares.toUint128(), vault, latestPrice, MathLib.Rounding.Down));
}
/// @inheritdoc IInvestmentManager
function maxDeposit(address vault, address user) public view returns (uint256 assets) {
if (!_canTransfer(vault, address(escrow), user, 0)) return 0;
assets = uint256(_maxDeposit(vault, user));
}
function _maxDeposit(address vault, address user) internal view returns (uint128 assets) {
InvestmentState memory state = investments[vault][user];
assets = _calculateAssets(state.maxMint, vault, state.depositPrice, MathLib.Rounding.Down);
}
/// @inheritdoc IInvestmentManager
function maxMint(address vault, address user) public view returns (uint256 shares) {
if (!_canTransfer(vault, address(escrow), user, 0)) return 0;
shares = uint256(investments[vault][user].maxMint);
}
/// @inheritdoc IInvestmentManager
function maxWithdraw(address vault, address user) public view returns (uint256 assets) {
assets = uint256(investments[vault][user].maxWithdraw);
}
/// @inheritdoc IInvestmentManager
function maxRedeem(address vault, address user) public view returns (uint256 shares) {
InvestmentState memory state = investments[vault][user];
shares = uint256(_calculateShares(state.maxWithdraw, vault, state.redeemPrice, MathLib.Rounding.Down));
}
/// @inheritdoc IInvestmentManager
function pendingDepositRequest(address vault, address user) public view returns (uint256 assets) {
assets = uint256(investments[vault][user].pendingDepositRequest);
}
/// @inheritdoc IInvestmentManager
function pendingRedeemRequest(address vault, address user) public view returns (uint256 shares) {
shares = uint256(investments[vault][user].pendingRedeemRequest);
}
/// @inheritdoc IInvestmentManager
function pendingCancelDepositRequest(address vault, address user) public view returns (bool isPending) {
isPending = investments[vault][user].pendingCancelDepositRequest;
}
/// @inheritdoc IInvestmentManager
function pendingCancelRedeemRequest(address vault, address user) public view returns (bool isPending) {
isPending = investments[vault][user].pendingCancelRedeemRequest;
}
/// @inheritdoc IInvestmentManager
function claimableCancelDepositRequest(address vault, address user) public view returns (uint256 assets) {
assets = investments[vault][user].claimableCancelDepositRequest;
}
/// @inheritdoc IInvestmentManager
function claimableCancelRedeemRequest(address vault, address user) public view returns (uint256 shares) {
shares = investments[vault][user].claimableCancelRedeemRequest;
}
/// @inheritdoc IInvestmentManager
function priceLastUpdated(address vault) public view returns (uint64 lastUpdated) {
IERC7540Vault vault_ = IERC7540Vault(vault);
(, lastUpdated) = poolManager.getTranchePrice(vault_.poolId(), vault_.trancheId(), vault_.asset());
}
// --- Vault claim functions ---
/// @inheritdoc IInvestmentManager
function deposit(address vault, uint256 assets, address receiver, address controller)
public
auth
returns (uint256 shares)
{
require(assets <= _maxDeposit(vault, controller), "InvestmentManager/exceeds-max-deposit");
InvestmentState storage state = investments[vault][controller];
uint128 sharesUp = _calculateShares(assets.toUint128(), vault, state.depositPrice, MathLib.Rounding.Up);
uint128 sharesDown = _calculateShares(assets.toUint128(), vault, state.depositPrice, MathLib.Rounding.Down);
_processDeposit(state, sharesUp, sharesDown, vault, receiver);
shares = uint256(sharesDown);
}
/// @inheritdoc IInvestmentManager
function mint(address vault, uint256 shares, address receiver, address controller)
public
auth
returns (uint256 assets)
{
InvestmentState storage state = investments[vault][controller];
uint128 shares_ = shares.toUint128();
_processDeposit(state, shares_, shares_, vault, receiver);
assets = uint256(_calculateAssets(shares_, vault, state.depositPrice, MathLib.Rounding.Down));
}
function _processDeposit(
InvestmentState storage state,
uint128 sharesUp,
uint128 sharesDown,
address vault,
address receiver
) internal {
require(sharesUp <= state.maxMint, "InvestmentManager/exceeds-deposit-limits");
state.maxMint = state.maxMint > sharesUp ? state.maxMint - sharesUp : 0;
if (sharesDown > 0) {
require(
IERC20(IERC7540Vault(vault).share()).transferFrom(address(escrow), receiver, sharesDown),
"InvestmentManager/tranche-tokens-transfer-failed"
);
}
}
/// @inheritdoc IInvestmentManager
function redeem(address vault, uint256 shares, address receiver, address controller)
public
auth
returns (uint256 assets)
{
require(shares <= maxRedeem(vault, controller), "InvestmentManager/exceeds-max-redeem");
InvestmentState storage state = investments[vault][controller];
uint128 assetsUp = _calculateAssets(shares.toUint128(), vault, state.redeemPrice, MathLib.Rounding.Up);
uint128 assetsDown = _calculateAssets(shares.toUint128(), vault, state.redeemPrice, MathLib.Rounding.Down);
_processRedeem(state, assetsUp, assetsDown, vault, receiver);
assets = uint256(assetsDown);
}
/// @inheritdoc IInvestmentManager
function withdraw(address vault, uint256 assets, address receiver, address controller)
public
auth
returns (uint256 shares)
{
InvestmentState storage state = investments[vault][controller];
uint128 assets_ = assets.toUint128();
_processRedeem(state, assets_, assets_, vault, receiver);
shares = uint256(_calculateShares(assets_, vault, state.redeemPrice, MathLib.Rounding.Down));
}
function _processRedeem(
InvestmentState storage state,
uint128 assetsUp,
uint128 assetsDown,
address vault,
address receiver
) internal {
IERC7540Vault vault_ = IERC7540Vault(vault);
require(assetsUp <= state.maxWithdraw, "InvestmentManager/exceeds-redeem-limits");
state.maxWithdraw = state.maxWithdraw > assetsUp ? state.maxWithdraw - assetsUp : 0;
if (assetsDown > 0) SafeTransferLib.safeTransferFrom(vault_.asset(), address(escrow), receiver, assetsDown);
}
/// @inheritdoc IInvestmentManager
function claimCancelDepositRequest(address vault, address receiver, address controller)
public
auth
returns (uint256 assets)
{
InvestmentState storage state = investments[vault][controller];
assets = state.claimableCancelDepositRequest;
state.claimableCancelDepositRequest = 0;
if (assets > 0) {
SafeTransferLib.safeTransferFrom(IERC7540Vault(vault).asset(), address(escrow), receiver, assets);
}
}
/// @inheritdoc IInvestmentManager
function claimCancelRedeemRequest(address vault, address receiver, address controller)
public
auth
returns (uint256 shares)
{
InvestmentState storage state = investments[vault][controller];
shares = state.claimableCancelRedeemRequest;
state.claimableCancelRedeemRequest = 0;
if (shares > 0) {
require(
IERC20(IERC7540Vault(vault).share()).transferFrom(address(escrow), receiver, shares),
"InvestmentManager/tranche-tokens-transfer-failed"
);
}
}
// --- Helpers ---
/// @dev Calculates share amount based on asset amount and share price. Returned value is in share decimals.
function _calculateShares(uint128 assets, address vault, uint256 price, MathLib.Rounding rounding)
internal
view
returns (uint128 shares)
{
if (price == 0 || assets == 0) {
shares = 0;
} else {
(uint8 assetDecimals, uint8 shareDecimals) = _getPoolDecimals(vault);
uint256 sharesInPriceDecimals =
_toPriceDecimals(assets, assetDecimals).mulDiv(10 ** PRICE_DECIMALS, price, rounding);
shares = _fromPriceDecimals(sharesInPriceDecimals, shareDecimals);
}
}
/// @dev Calculates asset amount based on share amount and share price. Returned value is in asset decimals.
function _calculateAssets(uint128 shares, address vault, uint256 price, MathLib.Rounding rounding)
internal
view
returns (uint128 assets)
{
if (price == 0 || shares == 0) {
assets = 0;
} else {
(uint8 assetDecimals, uint8 shareDecimals) = _getPoolDecimals(vault);
uint256 assetsInPriceDecimals =
_toPriceDecimals(shares, shareDecimals).mulDiv(price, 10 ** PRICE_DECIMALS, rounding);
assets = _fromPriceDecimals(assetsInPriceDecimals, assetDecimals);
}
}
/// @dev Calculates share price and returns the value in price decimals
function _calculatePrice(address vault, uint128 assets, uint128 shares) internal view returns (uint256) {
if (assets == 0 || shares == 0) {
return 0;
}
(uint8 assetDecimals, uint8 shareDecimals) = _getPoolDecimals(vault);
return _toPriceDecimals(assets, assetDecimals).mulDiv(
10 ** PRICE_DECIMALS, _toPriceDecimals(shares, shareDecimals), MathLib.Rounding.Down
);
}
/// @dev When converting assets to shares using the price,
/// all values are normalized to PRICE_DECIMALS
function _toPriceDecimals(uint128 _value, uint8 decimals) internal pure returns (uint256) {
if (PRICE_DECIMALS == decimals) return uint256(_value);
return uint256(_value) * 10 ** (PRICE_DECIMALS - decimals);
}
/// @dev Converts decimals of the value from the price decimals back to the intended decimals
function _fromPriceDecimals(uint256 _value, uint8 decimals) internal pure returns (uint128) {
if (PRICE_DECIMALS == decimals) return _value.toUint128();
return (_value / 10 ** (PRICE_DECIMALS - decimals)).toUint128();
}
/// @dev Returns the asset decimals and the share decimals for a given vault
function _getPoolDecimals(address vault) internal view returns (uint8 assetDecimals, uint8 shareDecimals) {
assetDecimals = IERC20Metadata(IERC7540Vault(vault).asset()).decimals();
shareDecimals = IERC20Metadata(IERC7540Vault(vault).share()).decimals();
}
/// @dev Checks transfer restrictions for the vault shares. Sender (from) and receiver (to) have both to pass the
/// restrictions for a successful share transfer.
function _canTransfer(address vault, address from, address to, uint256 value) internal view returns (bool) {
ITranche share = ITranche(IERC7540Vault(vault).share());
return share.checkTransferRestriction(from, to, value);
}
}