@morpho-org/blue-sdk 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/lib/addresses.d.ts +168 -0
  2. package/lib/addresses.js +169 -0
  3. package/lib/chain/chain.constants.d.ts +3 -0
  4. package/lib/chain/chain.constants.js +232 -0
  5. package/lib/chain/chain.types.d.ts +20 -0
  6. package/lib/chain/chain.types.js +30 -0
  7. package/lib/chain/chain.utils.d.ts +14 -0
  8. package/lib/chain/chain.utils.js +30 -0
  9. package/lib/chain/index.js +18 -0
  10. package/lib/constants.d.ts +8 -0
  11. package/lib/constants.js +13 -0
  12. package/lib/errors.d.ts +37 -0
  13. package/lib/errors.js +71 -0
  14. package/lib/ethers/index.js +18 -0
  15. package/lib/ethers/safeGetAddress.d.ts +1 -0
  16. package/lib/ethers/safeGetAddress.js +6 -0
  17. package/lib/ethers/safeParseUnits.d.ts +2 -0
  18. package/lib/ethers/safeParseUnits.js +25 -0
  19. package/lib/evm.d.ts +36 -0
  20. package/lib/evm.js +113 -0
  21. package/lib/helpers/format/format.d.ts +98 -0
  22. package/lib/helpers/format/format.js +301 -0
  23. package/lib/helpers/format/index.js +17 -0
  24. package/lib/helpers/getChecksumedAddress.d.ts +7 -0
  25. package/lib/helpers/getChecksumedAddress.js +17 -0
  26. package/lib/helpers/index.js +20 -0
  27. package/{src/helpers/isZeroAddressOrUnset.ts → lib/helpers/isZeroAddressOrUnset.d.ts} +1 -7
  28. package/lib/helpers/isZeroAddressOrUnset.js +14 -0
  29. package/lib/helpers/locale.d.ts +36 -0
  30. package/lib/helpers/locale.js +86 -0
  31. package/lib/holding/Holding.d.ts +60 -0
  32. package/lib/holding/Holding.js +31 -0
  33. package/lib/holding/index.js +17 -0
  34. package/lib/index.d.ts +33 -0
  35. package/lib/index.js +62 -0
  36. package/lib/market/Market.d.ts +159 -0
  37. package/lib/market/Market.js +240 -0
  38. package/lib/market/MarketConfig.d.ts +44 -0
  39. package/lib/market/MarketConfig.js +56 -0
  40. package/lib/market/MarketUtils.d.ts +165 -0
  41. package/lib/market/MarketUtils.js +182 -0
  42. package/lib/market/index.js +19 -0
  43. package/lib/maths/AdaptiveCurveIrmLib.d.ts +37 -0
  44. package/lib/maths/AdaptiveCurveIrmLib.js +116 -0
  45. package/lib/maths/MathLib.d.ts +94 -0
  46. package/lib/maths/MathLib.js +153 -0
  47. package/lib/maths/MathUtils.d.ts +15 -0
  48. package/lib/maths/MathUtils.js +33 -0
  49. package/lib/maths/SharesMath.d.ts +12 -0
  50. package/lib/maths/SharesMath.js +22 -0
  51. package/lib/maths/index.js +20 -0
  52. package/lib/notifications.d.ts +98 -0
  53. package/lib/notifications.js +52 -0
  54. package/lib/position/Position.d.ts +118 -0
  55. package/lib/position/Position.js +145 -0
  56. package/lib/position/index.js +17 -0
  57. package/lib/signatures/index.d.ts +12 -0
  58. package/lib/signatures/index.js +39 -0
  59. package/lib/signatures/manager.d.ts +10 -0
  60. package/lib/signatures/manager.js +37 -0
  61. package/lib/signatures/permit.d.ts +21 -0
  62. package/lib/signatures/permit.js +101 -0
  63. package/lib/signatures/permit2.d.ts +20 -0
  64. package/lib/signatures/permit2.js +91 -0
  65. package/lib/signatures/types.d.ts +13 -0
  66. package/lib/signatures/types.js +2 -0
  67. package/lib/signatures/utils.d.ts +6 -0
  68. package/lib/signatures/utils.js +44 -0
  69. package/lib/tests/mocks/markets.d.ts +17 -0
  70. package/lib/tests/mocks/markets.js +108 -0
  71. package/lib/token/ERC20Metadata.d.ts +249 -0
  72. package/lib/token/ERC20Metadata.js +81 -0
  73. package/lib/token/Token.d.ts +45 -0
  74. package/lib/token/Token.js +39 -0
  75. package/lib/token/TokenNamespace.d.ts +18 -0
  76. package/lib/token/TokenNamespace.js +55 -0
  77. package/lib/token/WrappedToken.d.ts +42 -0
  78. package/lib/token/WrappedToken.js +87 -0
  79. package/lib/token/index.js +18 -0
  80. package/lib/types.d.ts +29 -0
  81. package/lib/types.js +23 -0
  82. package/lib/user/User.d.ts +20 -0
  83. package/lib/user/User.js +11 -0
  84. package/lib/user/index.js +18 -0
  85. package/lib/user/user.types.d.ts +18 -0
  86. package/lib/user/user.types.js +2 -0
  87. package/lib/vault/Vault.d.ts +167 -0
  88. package/lib/vault/Vault.js +156 -0
  89. package/lib/vault/VaultAllocation.d.ts +38 -0
  90. package/lib/vault/VaultAllocation.js +18 -0
  91. package/lib/vault/VaultConfig.d.ts +23 -0
  92. package/lib/vault/VaultConfig.js +26 -0
  93. package/lib/vault/VaultUtils.d.ts +17 -0
  94. package/lib/vault/VaultUtils.js +17 -0
  95. package/lib/vault/index.js +20 -0
  96. package/package.json +5 -5
  97. package/src/addresses.ts +0 -261
  98. package/src/chain/chain.constants.ts +0 -235
  99. package/src/chain/chain.test.ts +0 -51
  100. package/src/chain/chain.types.ts +0 -42
  101. package/src/chain/chain.utils.ts +0 -44
  102. package/src/constants.ts +0 -18
  103. package/src/errors.ts +0 -75
  104. package/src/ethers/ethers.test.ts +0 -17
  105. package/src/ethers/safeGetAddress.ts +0 -4
  106. package/src/ethers/safeParseUnits.ts +0 -29
  107. package/src/evm.ts +0 -172
  108. package/src/helpers/format/format.test.ts +0 -340
  109. package/src/helpers/format/format.ts +0 -416
  110. package/src/helpers/getChecksumedAddress.ts +0 -15
  111. package/src/helpers/locale.ts +0 -108
  112. package/src/holding/Holding.ts +0 -109
  113. package/src/market/Market.ts +0 -479
  114. package/src/market/MarketConfig.ts +0 -108
  115. package/src/market/MarketUtils.test.ts +0 -25
  116. package/src/market/MarketUtils.ts +0 -467
  117. package/src/maths/AdaptiveCurveIrmLib.ts +0 -143
  118. package/src/maths/MathLib.ts +0 -208
  119. package/src/maths/MathUtils.ts +0 -31
  120. package/src/maths/SharesMath.ts +0 -40
  121. package/src/notifications.ts +0 -167
  122. package/src/position/Position.ts +0 -251
  123. package/src/signatures/index.ts +0 -18
  124. package/src/signatures/manager.ts +0 -50
  125. package/src/signatures/permit.ts +0 -126
  126. package/src/signatures/permit2.ts +0 -120
  127. package/src/signatures/types.ts +0 -18
  128. package/src/signatures/utils.ts +0 -83
  129. package/src/tests/mocks/markets.ts +0 -110
  130. package/src/token/ERC20Metadata.ts +0 -124
  131. package/src/token/Token.ts +0 -83
  132. package/src/token/TokenNamespace.ts +0 -76
  133. package/src/token/WrappedToken.ts +0 -142
  134. package/src/types.ts +0 -37
  135. package/src/user/User.ts +0 -32
  136. package/src/user/user.types.ts +0 -23
  137. package/src/vault/Vault.ts +0 -370
  138. package/src/vault/VaultAllocation.ts +0 -58
  139. package/src/vault/VaultConfig.ts +0 -55
  140. package/src/vault/VaultUtils.ts +0 -47
  141. /package/{src/chain/index.ts → lib/chain/index.d.ts} +0 -0
  142. /package/{src/ethers/index.ts → lib/ethers/index.d.ts} +0 -0
  143. /package/{src/helpers/format/index.ts → lib/helpers/format/index.d.ts} +0 -0
  144. /package/{src/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
  145. /package/{src/holding/index.ts → lib/holding/index.d.ts} +0 -0
  146. /package/{src/market/index.ts → lib/market/index.d.ts} +0 -0
  147. /package/{src/maths/index.ts → lib/maths/index.d.ts} +0 -0
  148. /package/{src/position/index.ts → lib/position/index.d.ts} +0 -0
  149. /package/{src/token/index.ts → lib/token/index.d.ts} +0 -0
  150. /package/{src/user/index.ts → lib/user/index.d.ts} +0 -0
  151. /package/{src/vault/index.ts → lib/vault/index.d.ts} +0 -0
package/lib/types.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isMarketId = exports.isFetched = exports.TransactionType = void 0;
4
+ const ethers_1 = require("ethers");
5
+ /**
6
+ * The possible transaction type on the Blue contract
7
+ */
8
+ var TransactionType;
9
+ (function (TransactionType) {
10
+ TransactionType["Supply"] = "Supply";
11
+ TransactionType["SupplyCollateral"] = "Supply Collateral";
12
+ TransactionType["Withdraw"] = "Withdraw";
13
+ TransactionType["WithdrawCollateral"] = "Withdraw Collateral";
14
+ TransactionType["Borrow"] = "Borrow";
15
+ TransactionType["Repay"] = "Repay";
16
+ })(TransactionType || (exports.TransactionType = TransactionType = {}));
17
+ // TODO: replace with isDefined
18
+ function isFetched(v) {
19
+ return v !== undefined && v !== null;
20
+ }
21
+ exports.isFetched = isFetched;
22
+ const isMarketId = (value) => (0, ethers_1.isHexString)(value, 32);
23
+ exports.isMarketId = isMarketId;
@@ -0,0 +1,20 @@
1
+ import { Address } from "../types";
2
+ export declare class User {
3
+ /**
4
+ * The user's address.
5
+ */
6
+ readonly address: Address;
7
+ /**
8
+ * Whether the bundler is authorized to manage the user's position on Morpho Blue.
9
+ */
10
+ isBundlerAuthorized: boolean;
11
+ /**
12
+ * The user's nonce on Morpho Blue.
13
+ */
14
+ morphoNonce: bigint;
15
+ constructor({ address, isBundlerAuthorized, morphoNonce, }: {
16
+ address: Address;
17
+ isBundlerAuthorized: boolean;
18
+ morphoNonce: bigint;
19
+ });
20
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.User = void 0;
4
+ class User {
5
+ constructor({ address, isBundlerAuthorized, morphoNonce, }) {
6
+ this.address = address;
7
+ this.isBundlerAuthorized = isBundlerAuthorized;
8
+ this.morphoNonce = morphoNonce;
9
+ }
10
+ }
11
+ exports.User = User;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./user.types"), exports);
18
+ __exportStar(require("./User"), exports);
@@ -0,0 +1,18 @@
1
+ import { Token } from "../token";
2
+ export interface MaxBalanceDecomposition {
3
+ maxBalance: bigint;
4
+ decomposition: {
5
+ base: BalanceDecompositionFragment;
6
+ } & {
7
+ [T in Exclude<PeripheralTokenType, "base">]?: BalanceDecompositionFragment;
8
+ };
9
+ }
10
+ export type PeripheralTokenType = "base" | "wrapped" | "erc4626" | "wrapped-erc4626";
11
+ export interface PeripheralToken {
12
+ token: Token;
13
+ type: PeripheralTokenType;
14
+ }
15
+ export interface BalanceDecompositionFragment extends PeripheralToken {
16
+ underlyingValue: bigint;
17
+ value: bigint;
18
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,167 @@
1
+ import { BigNumberish } from "ethers";
2
+ import { CapacityLimit } from "../market";
3
+ import { RoundingDirection } from "../maths";
4
+ import { Token } from "../token";
5
+ import { Address, MarketId } from "../types";
6
+ import { InputVaultAllocation, VaultAllocation } from "./VaultAllocation";
7
+ import { VaultConfig } from "./VaultConfig";
8
+ export interface Pending<T> {
9
+ value: T;
10
+ validAt: bigint;
11
+ }
12
+ export interface VaultPublicAllocatorConfig {
13
+ /**
14
+ * The PublicAllocator's admin address.
15
+ */
16
+ admin: Address;
17
+ /**
18
+ * The PublicAllocator's reallocation fee (in native token).
19
+ */
20
+ fee: bigint;
21
+ /**
22
+ * The PublicAllocator's reallocation fee accrued so far (in native token).
23
+ */
24
+ accruedFee: bigint;
25
+ }
26
+ export interface InputVault {
27
+ config: VaultConfig;
28
+ curator: Address;
29
+ owner: Address;
30
+ guardian: Address;
31
+ fee: BigNumberish;
32
+ feeRecipient: Address;
33
+ skimRecipient: Address;
34
+ pendingTimelock: Pending<BigNumberish>;
35
+ pendingGuardian: Pending<Address>;
36
+ pendingOwner: Address;
37
+ timelock: BigNumberish;
38
+ supplyQueue: MarketId[];
39
+ withdrawQueue: MarketId[];
40
+ totalSupply: BigNumberish;
41
+ totalAssets: BigNumberish;
42
+ lastTotalAssets: BigNumberish;
43
+ publicAllocatorConfig?: VaultPublicAllocatorConfig;
44
+ }
45
+ export declare class Vault implements InputVault {
46
+ /**
47
+ * The MetaMorpho vault's config.
48
+ */
49
+ readonly config: VaultConfig;
50
+ /**
51
+ * The MetaMorpho vault's owner address.
52
+ */
53
+ owner: Address;
54
+ /**
55
+ * The MetaMorpho vault's curator address.
56
+ */
57
+ curator: Address;
58
+ /**
59
+ * The MetaMorpho vault's guardian address.
60
+ */
61
+ guardian: Address;
62
+ /**
63
+ * The MetaMorpho vault's skim recipient address (mostly used to skim reward tokens claimed to the vault).
64
+ */
65
+ skimRecipient: Address;
66
+ /**
67
+ * The MetaMorpho vault's fee recipient address.
68
+ */
69
+ feeRecipient: Address;
70
+ /**
71
+ * The MetaMorpho vault's timelock (in seconds).
72
+ */
73
+ timelock: bigint;
74
+ /**
75
+ * The MetaMorpho vault's fee.
76
+ */
77
+ fee: bigint;
78
+ /**
79
+ * The MetaMorpho vault's pending owner address and activation timestamp.
80
+ */
81
+ pendingOwner: Address;
82
+ /**
83
+ * The MetaMorpho vault's pending guardian address and activation timestamp.
84
+ */
85
+ pendingGuardian: Pending<Address>;
86
+ /**
87
+ * The MetaMorpho vault's pending timelock (in seconds) and activation timestamp.
88
+ */
89
+ pendingTimelock: Pending<bigint>;
90
+ /**
91
+ * The MetaMorpho vault's ordered supply queue.
92
+ */
93
+ supplyQueue: MarketId[];
94
+ /**
95
+ * The MetaMorpho vault's ordered withdraw queue.
96
+ */
97
+ withdrawQueue: MarketId[];
98
+ /**
99
+ * The ERC4626 vault's total supply of shares.
100
+ */
101
+ totalSupply: bigint;
102
+ /**
103
+ * The ERC4626 vault's total assets.
104
+ */
105
+ totalAssets: bigint;
106
+ /**
107
+ * The MetaMorpho vault's last total assets used to calculate performance fees.
108
+ */
109
+ lastTotalAssets: bigint;
110
+ /**
111
+ * The MetaMorpho vault's public allocator configuration.
112
+ */
113
+ publicAllocatorConfig?: VaultPublicAllocatorConfig;
114
+ constructor({ config, curator, owner, guardian, publicAllocatorConfig, fee, feeRecipient, skimRecipient, pendingTimelock, pendingGuardian, pendingOwner, timelock, supplyQueue, withdrawQueue, totalSupply, totalAssets, lastTotalAssets, }: InputVault);
115
+ get address(): string;
116
+ get asset(): string;
117
+ /**
118
+ * The amount of interest in assets accrued since the last interaction with the vault.
119
+ */
120
+ get totalInterest(): bigint;
121
+ toAssets(shares: BigNumberish, rounding?: RoundingDirection): bigint;
122
+ toShares(assets: BigNumberish, rounding?: RoundingDirection): bigint;
123
+ }
124
+ export interface CollateralAllocation {
125
+ address: Address;
126
+ lltvs: Set<bigint>;
127
+ oracles: Set<Address>;
128
+ markets: Set<MarketId>;
129
+ proportion: bigint;
130
+ }
131
+ export interface InputAccrualVault extends Omit<InputVault, "withdrawQueue" | "totalAssets"> {
132
+ }
133
+ export declare class AccrualVault extends Vault implements InputAccrualVault {
134
+ /**
135
+ * The allocation of the vault on each market enabled.
136
+ */
137
+ readonly allocations: Map<MarketId, VaultAllocation>;
138
+ /**
139
+ * The ERC4626 vault's share token.
140
+ */
141
+ token: Token;
142
+ /**
143
+ * The proportion of assets of the vault supplied to markets collateralized by each collateral asset.
144
+ */
145
+ readonly collateralAllocations: Map<Address, CollateralAllocation>;
146
+ constructor(vault: InputAccrualVault,
147
+ /**
148
+ * The allocation of the vault on each market of the withdraw queue,
149
+ * in the same order as the withdraw queue.
150
+ */
151
+ allocations: Omit<InputVaultAllocation, "proportion">[]);
152
+ /**
153
+ * The vault's liquidity directly available from allocated markets.
154
+ */
155
+ get liquidity(): bigint;
156
+ /**
157
+ * The MetaMorpho vault's average APY on its assets, including the performance fee.
158
+ */
159
+ get avgApy(): bigint;
160
+ /**
161
+ * The MetaMorpho vault's average APY on its assets, excluding the performance fee.
162
+ */
163
+ get netApy(): bigint;
164
+ getDepositCapacityLimit(assets: bigint): CapacityLimit;
165
+ getWithdrawCapacityLimit(shares: bigint): CapacityLimit;
166
+ accrueInterest(timestamp: bigint): AccrualVault;
167
+ }
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AccrualVault = exports.Vault = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const market_1 = require("../market");
6
+ const maths_1 = require("../maths");
7
+ const token_1 = require("../token");
8
+ const VaultAllocation_1 = require("./VaultAllocation");
9
+ const VaultUtils_1 = require("./VaultUtils");
10
+ class Vault {
11
+ constructor({ config, curator, owner, guardian, publicAllocatorConfig, fee, feeRecipient, skimRecipient, pendingTimelock, pendingGuardian, pendingOwner, timelock, supplyQueue, withdrawQueue, totalSupply, totalAssets, lastTotalAssets, }) {
12
+ this.config = config;
13
+ this.curator = curator;
14
+ this.owner = owner;
15
+ this.guardian = guardian;
16
+ this.fee = (0, ethers_1.toBigInt)(fee);
17
+ this.feeRecipient = feeRecipient;
18
+ this.skimRecipient = skimRecipient;
19
+ this.pendingTimelock = {
20
+ value: (0, ethers_1.toBigInt)(pendingTimelock.value),
21
+ validAt: pendingTimelock.validAt,
22
+ };
23
+ this.pendingGuardian = pendingGuardian;
24
+ this.pendingOwner = pendingOwner;
25
+ this.timelock = (0, ethers_1.toBigInt)(timelock);
26
+ this.supplyQueue = supplyQueue;
27
+ this.withdrawQueue = withdrawQueue;
28
+ this.totalSupply = (0, ethers_1.toBigInt)(totalSupply);
29
+ this.totalAssets = (0, ethers_1.toBigInt)(totalAssets);
30
+ this.lastTotalAssets = (0, ethers_1.toBigInt)(lastTotalAssets);
31
+ this.publicAllocatorConfig = publicAllocatorConfig;
32
+ }
33
+ get address() {
34
+ return this.config.address;
35
+ }
36
+ get asset() {
37
+ return this.config.asset;
38
+ }
39
+ /**
40
+ * The amount of interest in assets accrued since the last interaction with the vault.
41
+ */
42
+ get totalInterest() {
43
+ return maths_1.MathUtils.zeroFloorSub(this.totalAssets, this.lastTotalAssets);
44
+ }
45
+ toAssets(shares, rounding) {
46
+ return VaultUtils_1.VaultUtils.toAssets(shares, this, this.config, rounding);
47
+ }
48
+ toShares(assets, rounding) {
49
+ return VaultUtils_1.VaultUtils.toShares(assets, this, this.config, rounding);
50
+ }
51
+ }
52
+ exports.Vault = Vault;
53
+ class AccrualVault extends Vault {
54
+ constructor(vault,
55
+ /**
56
+ * The allocation of the vault on each market of the withdraw queue,
57
+ * in the same order as the withdraw queue.
58
+ */
59
+ allocations) {
60
+ super({
61
+ ...vault,
62
+ withdrawQueue: allocations.map(({ position }) => position.market.id),
63
+ totalAssets: allocations.reduce((total, { position }) => total + position.supplyAssets, 0n),
64
+ });
65
+ this.allocations = new Map(allocations.map(({ config, position }) => [
66
+ position.market.id,
67
+ new VaultAllocation_1.VaultAllocation({
68
+ config,
69
+ position,
70
+ proportion: this.totalAssets === 0n
71
+ ? 0n
72
+ : maths_1.MathLib.wDivDown(position.supplyAssets, this.totalAssets),
73
+ }),
74
+ ]));
75
+ this.token = new token_1.Token(this.config);
76
+ this.collateralAllocations = new Map();
77
+ for (const { position, proportion } of this.allocations.values()) {
78
+ const address = position.market.config.collateralToken;
79
+ let exposure = this.collateralAllocations.get(address);
80
+ if (!exposure)
81
+ this.collateralAllocations.set(address, (exposure = {
82
+ address,
83
+ lltvs: new Set(),
84
+ oracles: new Set(),
85
+ markets: new Set(),
86
+ proportion: 0n,
87
+ }));
88
+ exposure.lltvs.add(position.market.config.lltv);
89
+ exposure.oracles.add(position.market.config.oracle);
90
+ exposure.markets.add(position.market.id);
91
+ exposure.proportion += proportion;
92
+ }
93
+ }
94
+ /**
95
+ * The vault's liquidity directly available from allocated markets.
96
+ */
97
+ get liquidity() {
98
+ return Array.from(this.allocations.values()).reduce((total, { position }) => total + position.withdrawCapacityLimit.value, 0n);
99
+ }
100
+ /**
101
+ * The MetaMorpho vault's average APY on its assets, including the performance fee.
102
+ */
103
+ get avgApy() {
104
+ if (this.totalAssets === 0n)
105
+ return 0n;
106
+ return (Array.from(this.allocations.values()).reduce((total, { position }) => total + position.market.supplyApy * position.supplyAssets, 0n) / this.totalAssets);
107
+ }
108
+ /**
109
+ * The MetaMorpho vault's average APY on its assets, excluding the performance fee.
110
+ */
111
+ get netApy() {
112
+ return maths_1.MathLib.wMulDown(this.avgApy, maths_1.MathLib.WAD - this.fee);
113
+ }
114
+ getDepositCapacityLimit(assets) {
115
+ const suppliable = Array.from(this.allocations.values()).reduce((total, { config: { cap }, position: { marketId, supplyAssets } }) => maths_1.MathLib.min(total +
116
+ (this.supplyQueue.includes(marketId)
117
+ ? maths_1.MathUtils.zeroFloorSub(cap, supplyAssets)
118
+ : 0n), ethers_1.MaxUint256), 0n);
119
+ if (assets > suppliable)
120
+ return {
121
+ value: suppliable,
122
+ limiter: market_1.CapacityLimitReason.cap,
123
+ };
124
+ return {
125
+ value: assets,
126
+ limiter: market_1.CapacityLimitReason.balance,
127
+ };
128
+ }
129
+ getWithdrawCapacityLimit(shares) {
130
+ const assets = this.toAssets(shares);
131
+ const { liquidity } = this;
132
+ if (assets > liquidity)
133
+ return {
134
+ value: liquidity,
135
+ limiter: market_1.CapacityLimitReason.liquidity,
136
+ };
137
+ return {
138
+ value: assets,
139
+ limiter: market_1.CapacityLimitReason.balance,
140
+ };
141
+ }
142
+ accrueInterest(timestamp) {
143
+ const vault = new AccrualVault(this, Array.from(this.allocations.values(), ({ config, position }) => ({
144
+ config,
145
+ position: position.accrueInterest(timestamp),
146
+ })));
147
+ const feeAssets = maths_1.MathLib.wMulDown(vault.totalInterest, vault.fee);
148
+ vault.totalAssets -= feeAssets;
149
+ const feeShares = vault.toShares(feeAssets, "Down");
150
+ vault.totalAssets += feeAssets;
151
+ vault.totalSupply += feeShares;
152
+ vault.lastTotalAssets = vault.totalAssets;
153
+ return vault;
154
+ }
155
+ }
156
+ exports.AccrualVault = AccrualVault;
@@ -0,0 +1,38 @@
1
+ import { AccrualPosition } from "../position";
2
+ import { Pending } from "./Vault";
3
+ /**
4
+ * The vault's configuration of a market on the PublicAllocator.
5
+ */
6
+ export interface VaultMarketPublicAllocatorConfig {
7
+ maxIn: bigint;
8
+ maxOut: bigint;
9
+ }
10
+ export interface VaultMarketConfig {
11
+ cap: bigint;
12
+ pendingCap: Pending<bigint>;
13
+ removableAt: bigint;
14
+ enabled: boolean;
15
+ readonly publicAllocatorConfig?: VaultMarketPublicAllocatorConfig;
16
+ }
17
+ export interface InputVaultAllocation {
18
+ config: VaultMarketConfig;
19
+ position: AccrualPosition;
20
+ proportion: bigint;
21
+ }
22
+ export declare class VaultAllocation implements InputVaultAllocation {
23
+ /**
24
+ * The vault's configuration on the corresponding market.
25
+ */
26
+ config: VaultMarketConfig;
27
+ /**
28
+ * The vault's position on the corresponding market.
29
+ */
30
+ readonly position: AccrualPosition;
31
+ /**
32
+ * The vault's proportion of assets supplied on the corresponding market,
33
+ * with regard to the total assets of the vault.
34
+ */
35
+ readonly proportion: bigint;
36
+ constructor({ config, position, proportion }: InputVaultAllocation);
37
+ get utilization(): bigint;
38
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VaultAllocation = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const maths_1 = require("../maths");
6
+ class VaultAllocation {
7
+ constructor({ config, position, proportion }) {
8
+ this.config = config;
9
+ this.position = position;
10
+ this.proportion = proportion;
11
+ }
12
+ get utilization() {
13
+ if (this.config.cap === 0n)
14
+ return ethers_1.MaxUint256;
15
+ return maths_1.MathLib.wDivDown(this.position.supplyAssets, this.config.cap);
16
+ }
17
+ }
18
+ exports.VaultAllocation = VaultAllocation;
@@ -0,0 +1,23 @@
1
+ import { ChainId } from "../chain";
2
+ import { Address } from "../types";
3
+ interface InputVaultConfig {
4
+ address: Address;
5
+ decimals: number;
6
+ decimalsOffset: bigint;
7
+ symbol: string;
8
+ name: string;
9
+ asset: Address;
10
+ }
11
+ export declare class VaultConfig implements InputVaultConfig {
12
+ readonly chainId?: number | undefined;
13
+ protected static readonly _CACHE: Record<number, Record<Address, VaultConfig>>;
14
+ static get(address: Address, chainId: ChainId): VaultConfig;
15
+ readonly address: Address;
16
+ readonly decimals: number;
17
+ readonly decimalsOffset: bigint;
18
+ readonly symbol: string;
19
+ readonly name: string;
20
+ readonly asset: Address;
21
+ constructor({ address, decimals, decimalsOffset, symbol, name, asset, }: InputVaultConfig, chainId?: number | undefined);
22
+ }
23
+ export {};
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VaultConfig = void 0;
4
+ const errors_1 = require("../errors");
5
+ class VaultConfig {
6
+ static get(address, chainId) {
7
+ const config = VaultConfig._CACHE[chainId]?.[address];
8
+ if (!config)
9
+ throw new errors_1.UnknownVaultConfigError(address);
10
+ return config;
11
+ }
12
+ constructor({ address, decimals, decimalsOffset, symbol, name, asset, }, chainId) {
13
+ var _a;
14
+ this.chainId = chainId;
15
+ this.address = address;
16
+ this.decimals = decimals;
17
+ this.decimalsOffset = decimalsOffset;
18
+ this.symbol = symbol;
19
+ this.name = name;
20
+ this.asset = asset;
21
+ if (chainId != null)
22
+ ((_a = VaultConfig._CACHE)[chainId] ?? (_a[chainId] = {}))[address] = this;
23
+ }
24
+ }
25
+ exports.VaultConfig = VaultConfig;
26
+ VaultConfig._CACHE = {};
@@ -0,0 +1,17 @@
1
+ import { BigNumberish } from "ethers";
2
+ import { RoundingDirection } from "../maths";
3
+ export declare namespace VaultUtils {
4
+ const VIRTUAL_ASSETS = 1n;
5
+ function toAssets(shares: BigNumberish, { totalAssets, totalSupply, }: {
6
+ totalAssets: BigNumberish;
7
+ totalSupply: BigNumberish;
8
+ }, { decimalsOffset }: {
9
+ decimalsOffset: BigNumberish;
10
+ }, rounding?: RoundingDirection): bigint;
11
+ function toShares(assets: BigNumberish, { totalAssets, totalSupply, }: {
12
+ totalAssets: BigNumberish;
13
+ totalSupply: BigNumberish;
14
+ }, { decimalsOffset }: {
15
+ decimalsOffset: BigNumberish;
16
+ }, rounding?: RoundingDirection): bigint;
17
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VaultUtils = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const maths_1 = require("../maths");
6
+ var VaultUtils;
7
+ (function (VaultUtils) {
8
+ VaultUtils.VIRTUAL_ASSETS = 1n;
9
+ function toAssets(shares, { totalAssets, totalSupply, }, { decimalsOffset }, rounding = "Down") {
10
+ return maths_1.MathLib.mulDiv(shares, (0, ethers_1.toBigInt)(totalAssets) + VaultUtils.VIRTUAL_ASSETS, (0, ethers_1.toBigInt)(totalSupply) + 10n ** (0, ethers_1.toBigInt)(decimalsOffset), rounding);
11
+ }
12
+ VaultUtils.toAssets = toAssets;
13
+ function toShares(assets, { totalAssets, totalSupply, }, { decimalsOffset }, rounding = "Up") {
14
+ return maths_1.MathLib.mulDiv(assets, (0, ethers_1.toBigInt)(totalSupply) + 10n ** (0, ethers_1.toBigInt)(decimalsOffset), (0, ethers_1.toBigInt)(totalAssets) + VaultUtils.VIRTUAL_ASSETS, rounding);
15
+ }
16
+ VaultUtils.toShares = toShares;
17
+ })(VaultUtils || (exports.VaultUtils = VaultUtils = {}));
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./VaultUtils"), exports);
18
+ __exportStar(require("./VaultConfig"), exports);
19
+ __exportStar(require("./VaultAllocation"), exports);
20
+ __exportStar(require("./Vault"), exports);
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@morpho-org/blue-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "author": "Morpho Association <contact@morpho.org>",
5
5
  "license": "MIT",
6
6
  "main": "src/index.ts",
7
7
  "files": [
8
- "src"
8
+ "lib"
9
9
  ],
10
10
  "scripts": {
11
11
  "prepublish": "yarn build",
@@ -15,14 +15,14 @@
15
15
  "test:blue-sdk": "yarn test"
16
16
  },
17
17
  "dependencies": {
18
- "@morpho-org/morpho-ts": "1.0.0",
18
+ "@morpho-org/morpho-ts": "1.0.1",
19
19
  "ethers": "^6.12.1",
20
20
  "ethers-multicall-provider": "^6.3.0",
21
21
  "ethers-types": "^3.15.1",
22
22
  "rxjs": "^7.8.1"
23
23
  },
24
24
  "devDependencies": {
25
- "@morpho-org/morpho-test": "1.0.0",
25
+ "@morpho-org/morpho-test": "1.0.1",
26
26
  "@types/jest": "^29.5.12",
27
27
  "jest": "^29.6.2",
28
28
  "ts-jest": "^29.1.1",
@@ -50,5 +50,5 @@
50
50
  ],
51
51
  "preset": "ts-jest"
52
52
  },
53
- "gitHead": "ba4bc39fbf7d9e2fe2282b451940fb90305544fa"
53
+ "gitHead": "474aa49a57a91da7d47a0bf666b5fccede4d395e"
54
54
  }