This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVaultAccount.sol
48 lines (43 loc) · 1.99 KB
/
VaultAccount.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
// SPDX-License-Identifier: ISC
pragma solidity ^0.8.19;
import {IERC4626Extended} from "../interfaces/IERC4626Extended.sol";
struct VaultAccount {
uint128 amount; // Total amount, analogous to market cap
uint128 shares; // Total shares, analogous to shares outstanding
}
/// @title VaultAccount Library
/// @author Drake Evans (Frax Finance) github.com/drakeevans, modified from work by @Boring_Crypto github.com/boring_crypto
/// @notice Provides a library for use with the VaultAccount struct, provides convenient math implementations
/// @dev Uses uint128 to save on storage
library VaultAccountingLibrary {
function totalAmount(VaultAccount memory total, address vault) internal view returns (uint256 amount) {
if (vault == address(0)) {
return total.amount;
}
return total.amount + IERC4626Extended(vault).totalAvailableAssetsForVault(address(this));
}
/// @notice Calculates the shares value in relationship to `amount` and `total`
/// @dev Given an amount, return the appropriate number of shares
function toShares(VaultAccount memory total, uint256 amount, bool roundUp) internal pure returns (uint256 shares) {
if (total.amount == 0) {
shares = amount;
} else {
shares = (amount * total.shares) / total.amount;
if (roundUp && (shares * total.amount) / total.shares < amount) {
shares = shares + 1;
}
}
}
/// @notice Calculates the amount value in relationship to `shares` and `total`
/// @dev Given a number of shares, returns the appropriate amount
function toAmount(VaultAccount memory total, uint256 shares, bool roundUp) internal pure returns (uint256 amount) {
if (total.shares == 0) {
amount = shares;
} else {
amount = (shares * total.amount) / total.shares;
if (roundUp && (amount * total.shares) / total.amount < shares) {
amount = amount + 1;
}
}
}
}