@morpho-org/blue-sdk 5.16.0 → 5.18.0

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 (184) hide show
  1. package/lib/{addresses.d.ts → cjs/addresses.d.ts} +12 -0
  2. package/lib/{addresses.js → cjs/addresses.js} +6 -0
  3. package/lib/cjs/package.json +1 -0
  4. package/lib/{position → cjs/position}/PreLiquidationPosition.d.ts +3 -3
  5. package/lib/{position → cjs/position}/PreLiquidationPosition.js +16 -16
  6. package/lib/{preLiquidation.d.ts → cjs/preLiquidation.d.ts} +1 -1
  7. package/lib/{preLiquidation.js → cjs/preLiquidation.js} +2 -2
  8. package/lib/{vault → cjs/vault}/index.d.ts +1 -1
  9. package/lib/{vault → cjs/vault}/index.js +1 -1
  10. package/lib/{vault → cjs/vault}/v2/VaultV2.d.ts +5 -5
  11. package/lib/{vault → cjs/vault}/v2/VaultV2.js +25 -25
  12. package/lib/{vault → cjs/vault}/v2/VaultV2Adapter.d.ts +2 -2
  13. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1Adapter.d.ts +7 -7
  14. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1Adapter.js +9 -9
  15. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +6 -6
  16. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1AdapterV2.js +8 -8
  17. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoVaultV1Adapter.d.ts +6 -6
  18. package/lib/{vault → cjs/vault}/v2/VaultV2MorphoVaultV1Adapter.js +2 -2
  19. package/lib/esm/addresses.d.ts +2243 -0
  20. package/lib/esm/addresses.js +1315 -0
  21. package/lib/esm/chain.d.ts +405 -0
  22. package/lib/esm/chain.js +276 -0
  23. package/lib/esm/constants.d.ts +20 -0
  24. package/lib/esm/constants.js +21 -0
  25. package/lib/esm/errors.d.ts +101 -0
  26. package/lib/esm/errors.js +192 -0
  27. package/lib/esm/holding/AssetBalances.d.ts +48 -0
  28. package/lib/esm/holding/AssetBalances.js +38 -0
  29. package/lib/esm/holding/Holding.d.ts +63 -0
  30. package/lib/esm/holding/Holding.js +63 -0
  31. package/lib/esm/holding/index.d.ts +2 -0
  32. package/lib/esm/holding/index.js +2 -0
  33. package/lib/esm/index.d.ts +14 -0
  34. package/lib/esm/index.js +14 -0
  35. package/lib/esm/market/Market.d.ts +424 -0
  36. package/lib/esm/market/Market.js +576 -0
  37. package/lib/esm/market/MarketParams.d.ts +74 -0
  38. package/lib/esm/market/MarketParams.js +91 -0
  39. package/lib/esm/market/MarketUtils.d.ts +273 -0
  40. package/lib/esm/market/MarketUtils.js +323 -0
  41. package/lib/esm/market/index.d.ts +3 -0
  42. package/lib/esm/market/index.js +3 -0
  43. package/lib/esm/math/AdaptiveCurveIrmLib.d.ts +40 -0
  44. package/lib/esm/math/AdaptiveCurveIrmLib.js +133 -0
  45. package/lib/esm/math/MathLib.d.ts +102 -0
  46. package/lib/esm/math/MathLib.js +168 -0
  47. package/lib/esm/math/SharesMath.d.ts +12 -0
  48. package/lib/esm/math/SharesMath.js +18 -0
  49. package/lib/esm/math/index.d.ts +3 -0
  50. package/lib/esm/math/index.js +3 -0
  51. package/lib/esm/package.json +1 -0
  52. package/lib/esm/position/Position.d.ts +147 -0
  53. package/lib/esm/position/Position.js +237 -0
  54. package/lib/esm/position/PreLiquidationPosition.d.ts +60 -0
  55. package/lib/esm/position/PreLiquidationPosition.js +97 -0
  56. package/lib/esm/position/index.d.ts +2 -0
  57. package/lib/esm/position/index.js +2 -0
  58. package/lib/esm/preLiquidation.d.ts +15 -0
  59. package/lib/esm/preLiquidation.js +91 -0
  60. package/lib/esm/token/ConstantWrappedToken.d.ts +17 -0
  61. package/lib/esm/token/ConstantWrappedToken.js +30 -0
  62. package/lib/esm/token/Eip5267Domain.d.ts +55 -0
  63. package/lib/esm/token/Eip5267Domain.js +66 -0
  64. package/lib/esm/token/ExchangeRateWrappedToken.d.ts +11 -0
  65. package/lib/esm/token/ExchangeRateWrappedToken.js +17 -0
  66. package/lib/esm/token/Token.d.ts +52 -0
  67. package/lib/esm/token/Token.js +62 -0
  68. package/lib/esm/token/VaultToken.d.ts +23 -0
  69. package/lib/esm/token/VaultToken.js +27 -0
  70. package/lib/esm/token/WrappedToken.d.ts +17 -0
  71. package/lib/esm/token/WrappedToken.js +29 -0
  72. package/lib/esm/token/index.d.ts +6 -0
  73. package/lib/esm/token/index.js +6 -0
  74. package/lib/esm/types.d.ts +26 -0
  75. package/lib/esm/types.js +13 -0
  76. package/lib/esm/user/User.d.ts +20 -0
  77. package/lib/esm/user/User.js +19 -0
  78. package/lib/esm/user/index.d.ts +1 -0
  79. package/lib/esm/user/index.js +1 -0
  80. package/lib/esm/utils.d.ts +13 -0
  81. package/lib/esm/utils.js +10 -0
  82. package/lib/esm/vault/Vault.d.ts +214 -0
  83. package/lib/esm/vault/Vault.js +289 -0
  84. package/lib/esm/vault/VaultConfig.d.ts +11 -0
  85. package/lib/esm/vault/VaultConfig.js +10 -0
  86. package/lib/esm/vault/VaultMarketAllocation.d.ts +20 -0
  87. package/lib/esm/vault/VaultMarketAllocation.js +27 -0
  88. package/lib/esm/vault/VaultMarketConfig.d.ts +43 -0
  89. package/lib/esm/vault/VaultMarketConfig.js +39 -0
  90. package/lib/esm/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
  91. package/lib/esm/vault/VaultMarketPublicAllocatorConfig.js +24 -0
  92. package/lib/esm/vault/VaultUser.d.ts +26 -0
  93. package/lib/esm/vault/VaultUser.js +24 -0
  94. package/lib/esm/vault/VaultUtils.d.ts +16 -0
  95. package/lib/esm/vault/VaultUtils.js +17 -0
  96. package/lib/esm/vault/index.d.ts +8 -0
  97. package/lib/esm/vault/index.js +8 -0
  98. package/lib/esm/vault/v2/VaultV2.d.ts +87 -0
  99. package/lib/esm/vault/v2/VaultV2.js +154 -0
  100. package/lib/esm/vault/v2/VaultV2Adapter.d.ts +29 -0
  101. package/lib/esm/vault/v2/VaultV2Adapter.js +12 -0
  102. package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.d.ts +30 -0
  103. package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.js +54 -0
  104. package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +33 -0
  105. package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.js +63 -0
  106. package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.d.ts +24 -0
  107. package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.js +36 -0
  108. package/lib/esm/vault/v2/index.d.ts +5 -0
  109. package/lib/esm/vault/v2/index.js +5 -0
  110. package/package.json +16 -7
  111. package/src/index.ts +14 -0
  112. /package/lib/{chain.d.ts → cjs/chain.d.ts} +0 -0
  113. /package/lib/{chain.js → cjs/chain.js} +0 -0
  114. /package/lib/{constants.d.ts → cjs/constants.d.ts} +0 -0
  115. /package/lib/{constants.js → cjs/constants.js} +0 -0
  116. /package/lib/{errors.d.ts → cjs/errors.d.ts} +0 -0
  117. /package/lib/{errors.js → cjs/errors.js} +0 -0
  118. /package/lib/{holding → cjs/holding}/AssetBalances.d.ts +0 -0
  119. /package/lib/{holding → cjs/holding}/AssetBalances.js +0 -0
  120. /package/lib/{holding → cjs/holding}/Holding.d.ts +0 -0
  121. /package/lib/{holding → cjs/holding}/Holding.js +0 -0
  122. /package/lib/{holding → cjs/holding}/index.d.ts +0 -0
  123. /package/lib/{holding → cjs/holding}/index.js +0 -0
  124. /package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
  125. /package/lib/{index.js → cjs/index.js} +0 -0
  126. /package/lib/{market → cjs/market}/Market.d.ts +0 -0
  127. /package/lib/{market → cjs/market}/Market.js +0 -0
  128. /package/lib/{market → cjs/market}/MarketParams.d.ts +0 -0
  129. /package/lib/{market → cjs/market}/MarketParams.js +0 -0
  130. /package/lib/{market → cjs/market}/MarketUtils.d.ts +0 -0
  131. /package/lib/{market → cjs/market}/MarketUtils.js +0 -0
  132. /package/lib/{market → cjs/market}/index.d.ts +0 -0
  133. /package/lib/{market → cjs/market}/index.js +0 -0
  134. /package/lib/{math → cjs/math}/AdaptiveCurveIrmLib.d.ts +0 -0
  135. /package/lib/{math → cjs/math}/AdaptiveCurveIrmLib.js +0 -0
  136. /package/lib/{math → cjs/math}/MathLib.d.ts +0 -0
  137. /package/lib/{math → cjs/math}/MathLib.js +0 -0
  138. /package/lib/{math → cjs/math}/SharesMath.d.ts +0 -0
  139. /package/lib/{math → cjs/math}/SharesMath.js +0 -0
  140. /package/lib/{math → cjs/math}/index.d.ts +0 -0
  141. /package/lib/{math → cjs/math}/index.js +0 -0
  142. /package/lib/{position → cjs/position}/Position.d.ts +0 -0
  143. /package/lib/{position → cjs/position}/Position.js +0 -0
  144. /package/lib/{position → cjs/position}/index.d.ts +0 -0
  145. /package/lib/{position → cjs/position}/index.js +0 -0
  146. /package/lib/{token → cjs/token}/ConstantWrappedToken.d.ts +0 -0
  147. /package/lib/{token → cjs/token}/ConstantWrappedToken.js +0 -0
  148. /package/lib/{token → cjs/token}/Eip5267Domain.d.ts +0 -0
  149. /package/lib/{token → cjs/token}/Eip5267Domain.js +0 -0
  150. /package/lib/{token → cjs/token}/ExchangeRateWrappedToken.d.ts +0 -0
  151. /package/lib/{token → cjs/token}/ExchangeRateWrappedToken.js +0 -0
  152. /package/lib/{token → cjs/token}/Token.d.ts +0 -0
  153. /package/lib/{token → cjs/token}/Token.js +0 -0
  154. /package/lib/{token → cjs/token}/VaultToken.d.ts +0 -0
  155. /package/lib/{token → cjs/token}/VaultToken.js +0 -0
  156. /package/lib/{token → cjs/token}/WrappedToken.d.ts +0 -0
  157. /package/lib/{token → cjs/token}/WrappedToken.js +0 -0
  158. /package/lib/{token → cjs/token}/index.d.ts +0 -0
  159. /package/lib/{token → cjs/token}/index.js +0 -0
  160. /package/lib/{types.d.ts → cjs/types.d.ts} +0 -0
  161. /package/lib/{types.js → cjs/types.js} +0 -0
  162. /package/lib/{user → cjs/user}/User.d.ts +0 -0
  163. /package/lib/{user → cjs/user}/User.js +0 -0
  164. /package/lib/{user → cjs/user}/index.d.ts +0 -0
  165. /package/lib/{user → cjs/user}/index.js +0 -0
  166. /package/lib/{utils.d.ts → cjs/utils.d.ts} +0 -0
  167. /package/lib/{utils.js → cjs/utils.js} +0 -0
  168. /package/lib/{vault → cjs/vault}/Vault.d.ts +0 -0
  169. /package/lib/{vault → cjs/vault}/Vault.js +0 -0
  170. /package/lib/{vault → cjs/vault}/VaultConfig.d.ts +0 -0
  171. /package/lib/{vault → cjs/vault}/VaultConfig.js +0 -0
  172. /package/lib/{vault → cjs/vault}/VaultMarketAllocation.d.ts +0 -0
  173. /package/lib/{vault → cjs/vault}/VaultMarketAllocation.js +0 -0
  174. /package/lib/{vault → cjs/vault}/VaultMarketConfig.d.ts +0 -0
  175. /package/lib/{vault → cjs/vault}/VaultMarketConfig.js +0 -0
  176. /package/lib/{vault → cjs/vault}/VaultMarketPublicAllocatorConfig.d.ts +0 -0
  177. /package/lib/{vault → cjs/vault}/VaultMarketPublicAllocatorConfig.js +0 -0
  178. /package/lib/{vault → cjs/vault}/VaultUser.d.ts +0 -0
  179. /package/lib/{vault → cjs/vault}/VaultUser.js +0 -0
  180. /package/lib/{vault → cjs/vault}/VaultUtils.d.ts +0 -0
  181. /package/lib/{vault → cjs/vault}/VaultUtils.js +0 -0
  182. /package/lib/{vault → cjs/vault}/v2/VaultV2Adapter.js +0 -0
  183. /package/lib/{vault → cjs/vault}/v2/index.d.ts +0 -0
  184. /package/lib/{vault → cjs/vault}/v2/index.js +0 -0
@@ -0,0 +1,289 @@
1
+ import { Time } from "@morpho-org/morpho-ts";
2
+ import { MarketUtils } from "../market/index.js";
3
+ import { MathLib } from "../math/index.js";
4
+ import { VaultToken } from "../token/index.js";
5
+ import { CapacityLimitReason } from "../utils.js";
6
+ import { VaultMarketAllocation, } from "./VaultMarketAllocation.js";
7
+ export class Vault extends VaultToken {
8
+ /**
9
+ * The MetaMorpho vault's owner address.
10
+ */
11
+ owner;
12
+ /**
13
+ * The MetaMorpho vault's curator address.
14
+ */
15
+ curator;
16
+ /**
17
+ * The MetaMorpho vault's guardian address.
18
+ */
19
+ guardian;
20
+ /**
21
+ * The MetaMorpho vault's skim recipient address (mostly used to skim reward tokens claimed to the vault).
22
+ */
23
+ skimRecipient;
24
+ /**
25
+ * The MetaMorpho vault's fee recipient address.
26
+ */
27
+ feeRecipient;
28
+ /**
29
+ * The MetaMorpho vault's timelock (in seconds).
30
+ */
31
+ timelock;
32
+ /**
33
+ * The MetaMorpho vault's fee.
34
+ */
35
+ fee;
36
+ /**
37
+ * The MetaMorpho vault's pending owner address and activation timestamp.
38
+ */
39
+ pendingOwner;
40
+ /**
41
+ * The MetaMorpho vault's pending guardian address and activation timestamp.
42
+ */
43
+ pendingGuardian;
44
+ /**
45
+ * The MetaMorpho vault's pending timelock (in seconds) and activation timestamp.
46
+ */
47
+ pendingTimelock;
48
+ /**
49
+ * The MetaMorpho vault's ordered supply queue.
50
+ */
51
+ supplyQueue;
52
+ /**
53
+ * The MetaMorpho vault's ordered withdraw queue.
54
+ */
55
+ withdrawQueue;
56
+ /**
57
+ * The MetaMorpho vault's last total assets used to calculate performance fees.
58
+ */
59
+ lastTotalAssets;
60
+ /**
61
+ * The MetaMorpho vault's lost assets due to realized bad debt.
62
+ * Only defined for MetaMorpho V1.1 vaults.
63
+ */
64
+ lostAssets;
65
+ /**
66
+ * The MetaMorpho vault's public allocator configuration.
67
+ */
68
+ publicAllocatorConfig;
69
+ constructor({ curator, owner, guardian, publicAllocatorConfig, fee, feeRecipient, skimRecipient, pendingTimelock, pendingGuardian, pendingOwner, timelock, supplyQueue, withdrawQueue, totalSupply, totalAssets, lastTotalAssets, lostAssets, ...config }) {
70
+ super(config, { totalAssets, totalSupply });
71
+ this.curator = curator;
72
+ this.owner = owner;
73
+ this.guardian = guardian;
74
+ this.fee = fee;
75
+ this.feeRecipient = feeRecipient;
76
+ this.skimRecipient = skimRecipient;
77
+ this.pendingTimelock = {
78
+ value: pendingTimelock.value,
79
+ validAt: pendingTimelock.validAt,
80
+ };
81
+ this.pendingGuardian = pendingGuardian;
82
+ this.pendingOwner = pendingOwner;
83
+ this.timelock = timelock;
84
+ this.supplyQueue = supplyQueue;
85
+ this.withdrawQueue = withdrawQueue;
86
+ this.lastTotalAssets = lastTotalAssets;
87
+ this.lostAssets = lostAssets;
88
+ this.publicAllocatorConfig = publicAllocatorConfig;
89
+ }
90
+ /**
91
+ * The amount of interest in assets accrued since the last interaction with the vault.
92
+ */
93
+ get totalInterest() {
94
+ return MathLib.zeroFloorSub(this.totalAssets, this.lastTotalAssets);
95
+ }
96
+ toAssets(shares, rounding = "Down") {
97
+ return this._unwrap(shares, rounding);
98
+ }
99
+ toShares(assets, rounding = "Up") {
100
+ return this._wrap(assets, rounding);
101
+ }
102
+ }
103
+ export class AccrualVault extends Vault {
104
+ /**
105
+ * The allocation of the vault on each market enabled.
106
+ */
107
+ allocations;
108
+ /**
109
+ * The proportion of assets of the vault supplied to markets collateralized by each collateral asset.
110
+ */
111
+ collateralAllocations;
112
+ constructor(vault,
113
+ /**
114
+ * The allocation of the vault on each market of the withdraw queue,
115
+ * in the same order as the withdraw queue.
116
+ */
117
+ allocations) {
118
+ super({
119
+ ...vault,
120
+ withdrawQueue: allocations.map(({ position }) => position.market.id),
121
+ totalAssets: allocations.reduce((total, { position }) => total + position.supplyAssets, 0n),
122
+ });
123
+ this.allocations = new Map(allocations.map((allocation) => [
124
+ allocation.position.market.id,
125
+ new VaultMarketAllocation(allocation),
126
+ ]));
127
+ this.collateralAllocations = new Map();
128
+ for (const { marketId, position } of this.allocations.values()) {
129
+ const address = position.market.params.collateralToken;
130
+ let exposure = this.collateralAllocations.get(address);
131
+ if (!exposure)
132
+ this.collateralAllocations.set(address, (exposure = {
133
+ address,
134
+ lltvs: new Set(),
135
+ oracles: new Set(),
136
+ markets: new Set(),
137
+ proportion: 0n,
138
+ }));
139
+ exposure.lltvs.add(position.market.params.lltv);
140
+ exposure.oracles.add(position.market.params.oracle);
141
+ exposure.markets.add(marketId);
142
+ exposure.proportion += this.getAllocationProportion(marketId);
143
+ }
144
+ }
145
+ /**
146
+ * The vault's liquidity directly available from allocated markets.
147
+ */
148
+ get liquidity() {
149
+ return this.allocations
150
+ .values()
151
+ .reduce((total, { position }) => total + position.withdrawCapacityLimit.value, 0n);
152
+ }
153
+ /**
154
+ * The MetaMorpho vault's current instantaneous Annual Percentage Yield (APY)
155
+ * weighted-averaged over its market deposits, before deducting the performance fee.
156
+ * If interested in the APY at a specific timestamp, use `getApy(timestamp)` instead.
157
+ */
158
+ get apy() {
159
+ return this.getApy();
160
+ }
161
+ /**
162
+ * The MetaMorpho vault's current instantaneous Annual Percentage Yield (APY)
163
+ * weighted-averaged over its market deposits, after deducting the performance fee.
164
+ * If interested in the APY at a specific timestamp, use `getNetApy(timestamp)` instead.
165
+ */
166
+ get netApy() {
167
+ return this.getNetApy();
168
+ }
169
+ /**
170
+ * The MetaMorpho vault's per-second rate at which interest _would_ accrue,
171
+ * weighted-averaged over its market deposits, before deducting the performance fee,
172
+ * if interest was to be accrued on each market at the given timestamp.
173
+ */
174
+ _getAvgRate(timestamp = Time.timestamp()) {
175
+ if (this.totalAssets === 0n)
176
+ return 0n;
177
+ return (this.allocations
178
+ .values()
179
+ .reduce((total, { position }) => total +
180
+ position.market.getAvgSupplyRate(timestamp) * position.supplyAssets, 0n) / this.totalAssets);
181
+ }
182
+ /**
183
+ * The MetaMorpho vault's experienced Annual Percentage Yield (APY)
184
+ * weighted-averaged over its market deposits, before deducting the performance fee,
185
+ * if interest was to be accrued on each market at the given timestamp.
186
+ */
187
+ getApy(timestamp = Time.timestamp()) {
188
+ if (this.totalAssets === 0n)
189
+ return 0;
190
+ return MarketUtils.rateToApy(this._getAvgRate(timestamp));
191
+ }
192
+ /**
193
+ * The MetaMorpho vault's experienced Annual Percentage Yield (APY)
194
+ * weighted-averaged over its market deposits, after deducting the performance fee,
195
+ * if interest was to be accrued on each market at the given timestamp.
196
+ */
197
+ getNetApy(timestamp = Time.timestamp()) {
198
+ return MarketUtils.rateToApy(MathLib.wMulDown(this._getAvgRate(timestamp), MathLib.WAD - this.fee));
199
+ }
200
+ getAllocationProportion(marketId) {
201
+ if (this.totalAssets === 0n)
202
+ return 0n;
203
+ const allocation = this.allocations.get(marketId);
204
+ if (!allocation)
205
+ return 0n;
206
+ return MathLib.wDivDown(allocation.position.supplyAssets, this.totalAssets);
207
+ }
208
+ /**
209
+ * Returns the deposit capacity limit of a given amount of assets on the vault.
210
+ * @param assets The maximum amount of assets to deposit.
211
+ * @deprecated Use `maxDeposit` instead.
212
+ */
213
+ getDepositCapacityLimit(assets) {
214
+ return this.maxDeposit(assets);
215
+ }
216
+ /**
217
+ * Returns the withdraw capacity limit corresponding to a given amount of shares of the vault.
218
+ * @param shares The maximum amount of shares to redeem.
219
+ * @deprecated Use `maxWithdraw` instead.
220
+ */
221
+ getWithdrawCapacityLimit(shares) {
222
+ return this.maxWithdraw(shares);
223
+ }
224
+ /**
225
+ * Returns the maximum amount of assets that can be deposited to the vault.
226
+ * @param assets The maximum amount of assets to deposit.
227
+ */
228
+ maxDeposit(assets) {
229
+ assets = BigInt(assets);
230
+ const suppliable = this.allocations
231
+ .values()
232
+ .reduce((total, { config: { cap }, position: { marketId, supplyAssets } }) => MathLib.min(total +
233
+ (this.supplyQueue.includes(marketId)
234
+ ? MathLib.zeroFloorSub(cap, supplyAssets)
235
+ : 0n), MathLib.MAX_UINT_256), 0n);
236
+ if (assets > suppliable)
237
+ return {
238
+ value: suppliable,
239
+ limiter: CapacityLimitReason.cap,
240
+ };
241
+ return {
242
+ value: assets,
243
+ limiter: CapacityLimitReason.balance,
244
+ };
245
+ }
246
+ /**
247
+ * Returns the maximum amount of assets that can be withdrawn from the vault.
248
+ * @param shares The maximum amount of shares to redeem.
249
+ */
250
+ maxWithdraw(shares) {
251
+ const assets = this.toAssets(shares);
252
+ const { liquidity } = this;
253
+ if (assets > liquidity)
254
+ return {
255
+ value: liquidity,
256
+ limiter: CapacityLimitReason.liquidity,
257
+ };
258
+ return {
259
+ value: assets,
260
+ limiter: CapacityLimitReason.balance,
261
+ };
262
+ }
263
+ /**
264
+ * Returns a new vault derived from this vault, whose interest has been accrued up to the given timestamp.
265
+ * @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to each of the vault's market's `lastUpdate`.
266
+ */
267
+ accrueInterest(timestamp) {
268
+ const vault = new AccrualVault(this,
269
+ // Keep withdraw queue order.
270
+ this.withdrawQueue.map((marketId) => {
271
+ const { config, position } = this.allocations.get(marketId);
272
+ return {
273
+ config,
274
+ position: position.accrueInterest(timestamp),
275
+ };
276
+ }));
277
+ if (vault.lostAssets != null) {
278
+ vault.lostAssets += MathLib.max(vault.lastTotalAssets - vault.lostAssets - vault.totalAssets, 0n);
279
+ vault.totalAssets += vault.lostAssets;
280
+ }
281
+ const feeAssets = MathLib.wMulDown(vault.totalInterest, vault.fee);
282
+ vault.totalAssets -= feeAssets;
283
+ const feeShares = vault.toShares(feeAssets, "Down");
284
+ vault.totalAssets += feeAssets;
285
+ vault.totalSupply += feeShares;
286
+ vault.lastTotalAssets = vault.totalAssets;
287
+ return vault;
288
+ }
289
+ }
@@ -0,0 +1,11 @@
1
+ import { type IToken, Token } from "../token/Token.js";
2
+ import type { Address, BigIntish } from "../types.js";
3
+ export interface IVaultConfig extends Omit<IToken, "decimals"> {
4
+ decimalsOffset: BigIntish;
5
+ asset: Address;
6
+ }
7
+ export declare class VaultConfig extends Token implements IVaultConfig {
8
+ readonly decimalsOffset: bigint;
9
+ readonly asset: `0x${string}`;
10
+ constructor({ decimalsOffset, asset, ...config }: IVaultConfig);
11
+ }
@@ -0,0 +1,10 @@
1
+ import { Token } from "../token/Token.js";
2
+ export class VaultConfig extends Token {
3
+ decimalsOffset;
4
+ asset;
5
+ constructor({ decimalsOffset, asset, ...config }) {
6
+ super({ ...config, decimals: 18 });
7
+ this.decimalsOffset = BigInt(decimalsOffset);
8
+ this.asset = asset;
9
+ }
10
+ }
@@ -0,0 +1,20 @@
1
+ import type { AccrualPosition } from "../position/index.js";
2
+ import { type IVaultMarketConfig, VaultMarketConfig } from "./VaultMarketConfig.js";
3
+ export interface IVaultMarketAllocation {
4
+ config: IVaultMarketConfig;
5
+ position: AccrualPosition;
6
+ }
7
+ export declare class VaultMarketAllocation implements IVaultMarketAllocation {
8
+ /**
9
+ * The vault's configuration on the corresponding market.
10
+ */
11
+ readonly config: VaultMarketConfig;
12
+ /**
13
+ * The vault's position on the corresponding market.
14
+ */
15
+ readonly position: AccrualPosition;
16
+ constructor({ config, position }: IVaultMarketAllocation);
17
+ get vault(): `0x${string}`;
18
+ get marketId(): import("../types.js").MarketId;
19
+ get utilization(): bigint;
20
+ }
@@ -0,0 +1,27 @@
1
+ import { MathLib } from "../math/index.js";
2
+ import { VaultMarketConfig, } from "./VaultMarketConfig.js";
3
+ export class VaultMarketAllocation {
4
+ /**
5
+ * The vault's configuration on the corresponding market.
6
+ */
7
+ config;
8
+ /**
9
+ * The vault's position on the corresponding market.
10
+ */
11
+ position;
12
+ constructor({ config, position }) {
13
+ this.config = new VaultMarketConfig(config);
14
+ this.position = position;
15
+ }
16
+ get vault() {
17
+ return this.config.vault;
18
+ }
19
+ get marketId() {
20
+ return this.config.marketId;
21
+ }
22
+ get utilization() {
23
+ if (this.config.cap === 0n)
24
+ return MathLib.MAX_UINT_256;
25
+ return MathLib.wDivDown(this.position.supplyAssets, this.config.cap);
26
+ }
27
+ }
@@ -0,0 +1,43 @@
1
+ import type { Address, MarketId } from "../types.js";
2
+ import type { Pending } from "./Vault.js";
3
+ import type { VaultMarketPublicAllocatorConfig } from "./VaultMarketPublicAllocatorConfig.js";
4
+ export interface IVaultMarketConfig {
5
+ vault: Address;
6
+ marketId: MarketId;
7
+ cap: bigint;
8
+ pendingCap: Pending<bigint>;
9
+ removableAt: bigint;
10
+ enabled: boolean;
11
+ publicAllocatorConfig?: VaultMarketPublicAllocatorConfig;
12
+ }
13
+ export declare class VaultMarketConfig implements IVaultMarketConfig {
14
+ /**
15
+ * The vault's address.
16
+ */
17
+ readonly vault: Address;
18
+ /**
19
+ * The market's id.
20
+ */
21
+ readonly marketId: MarketId;
22
+ /**
23
+ * The maximum amount of tokens that can be allocated to this market.
24
+ */
25
+ cap: bigint;
26
+ /**
27
+ * The pending maximum amount of tokens that can be allocated to this market.
28
+ */
29
+ pendingCap: Pending<bigint>;
30
+ /**
31
+ * The timestamp at which the market can be removed from the withdraw queue.
32
+ */
33
+ removableAt: bigint;
34
+ /**
35
+ * Whether this market is enabled, i.e. whether additional tokens can be allocated to it.
36
+ */
37
+ enabled: boolean;
38
+ /**
39
+ * The vault's PublicAllocator configuration on the corresponding market.
40
+ */
41
+ readonly publicAllocatorConfig?: VaultMarketPublicAllocatorConfig;
42
+ constructor({ vault, marketId, cap, pendingCap, removableAt, enabled, publicAllocatorConfig, }: IVaultMarketConfig);
43
+ }
@@ -0,0 +1,39 @@
1
+ export class VaultMarketConfig {
2
+ /**
3
+ * The vault's address.
4
+ */
5
+ vault;
6
+ /**
7
+ * The market's id.
8
+ */
9
+ marketId;
10
+ /**
11
+ * The maximum amount of tokens that can be allocated to this market.
12
+ */
13
+ cap;
14
+ /**
15
+ * The pending maximum amount of tokens that can be allocated to this market.
16
+ */
17
+ pendingCap;
18
+ /**
19
+ * The timestamp at which the market can be removed from the withdraw queue.
20
+ */
21
+ removableAt;
22
+ /**
23
+ * Whether this market is enabled, i.e. whether additional tokens can be allocated to it.
24
+ */
25
+ enabled;
26
+ /**
27
+ * The vault's PublicAllocator configuration on the corresponding market.
28
+ */
29
+ publicAllocatorConfig;
30
+ constructor({ vault, marketId, cap, pendingCap, removableAt, enabled, publicAllocatorConfig, }) {
31
+ this.vault = vault;
32
+ this.marketId = marketId;
33
+ this.cap = cap;
34
+ this.pendingCap = pendingCap;
35
+ this.removableAt = removableAt;
36
+ this.enabled = enabled;
37
+ this.publicAllocatorConfig = publicAllocatorConfig;
38
+ }
39
+ }
@@ -0,0 +1,29 @@
1
+ import type { Address, MarketId } from "../types.js";
2
+ /**
3
+ * The vault's configuration of a market on the PublicAllocator.
4
+ */
5
+ export interface IVaultMarketPublicAllocatorConfig {
6
+ vault: Address;
7
+ marketId: MarketId;
8
+ maxIn: bigint;
9
+ maxOut: bigint;
10
+ }
11
+ export declare class VaultMarketPublicAllocatorConfig implements IVaultMarketPublicAllocatorConfig {
12
+ /**
13
+ * The vault's address.
14
+ */
15
+ readonly vault: Address;
16
+ /**
17
+ * The market's id.
18
+ */
19
+ readonly marketId: MarketId;
20
+ /**
21
+ * The maximum amount of tokens that can be allocated to this market by the vault via the PublicAllocator.
22
+ */
23
+ maxIn: bigint;
24
+ /**
25
+ * The maximum amount of tokens that can be allocated out of this market by the vault via the PublicAllocator.
26
+ */
27
+ maxOut: bigint;
28
+ constructor({ vault, marketId, maxIn, maxOut, }: IVaultMarketPublicAllocatorConfig);
29
+ }
@@ -0,0 +1,24 @@
1
+ export class VaultMarketPublicAllocatorConfig {
2
+ /**
3
+ * The vault's address.
4
+ */
5
+ vault;
6
+ /**
7
+ * The market's id.
8
+ */
9
+ marketId;
10
+ /**
11
+ * The maximum amount of tokens that can be allocated to this market by the vault via the PublicAllocator.
12
+ */
13
+ maxIn;
14
+ /**
15
+ * The maximum amount of tokens that can be allocated out of this market by the vault via the PublicAllocator.
16
+ */
17
+ maxOut;
18
+ constructor({ vault, marketId, maxIn, maxOut, }) {
19
+ this.vault = vault;
20
+ this.marketId = marketId;
21
+ this.maxIn = maxIn;
22
+ this.maxOut = maxOut;
23
+ }
24
+ }
@@ -0,0 +1,26 @@
1
+ import type { Address } from "../types.js";
2
+ export interface IVaultUser {
3
+ vault: Address;
4
+ user: Address;
5
+ isAllocator: boolean;
6
+ allowance: bigint;
7
+ }
8
+ export declare class VaultUser implements IVaultUser {
9
+ /**
10
+ * The vault's address.
11
+ */
12
+ readonly vault: Address;
13
+ /**
14
+ * The user's address.
15
+ */
16
+ readonly user: Address;
17
+ /**
18
+ * Whether the user is an allocator of the vault.
19
+ */
20
+ isAllocator: boolean;
21
+ /**
22
+ * The allowance of the vault over the user's underlying assets.
23
+ */
24
+ allowance: bigint;
25
+ constructor({ vault, user, isAllocator, allowance }: IVaultUser);
26
+ }
@@ -0,0 +1,24 @@
1
+ export class VaultUser {
2
+ /**
3
+ * The vault's address.
4
+ */
5
+ vault;
6
+ /**
7
+ * The user's address.
8
+ */
9
+ user;
10
+ /**
11
+ * Whether the user is an allocator of the vault.
12
+ */
13
+ isAllocator;
14
+ /**
15
+ * The allowance of the vault over the user's underlying assets.
16
+ */
17
+ allowance;
18
+ constructor({ vault, user, isAllocator, allowance }) {
19
+ this.vault = vault;
20
+ this.user = user;
21
+ this.isAllocator = isAllocator;
22
+ this.allowance = allowance;
23
+ }
24
+ }
@@ -0,0 +1,16 @@
1
+ import { type RoundingDirection } from "../math/index.js";
2
+ import type { BigIntish } from "../types.js";
3
+ export declare namespace VaultUtils {
4
+ const VIRTUAL_ASSETS = 1n;
5
+ function decimalsOffset(decimals: BigIntish): bigint;
6
+ function toAssets(shares: BigIntish, { totalAssets, totalSupply, decimalsOffset, }: {
7
+ totalAssets: BigIntish;
8
+ totalSupply: BigIntish;
9
+ decimalsOffset: BigIntish;
10
+ }, rounding?: RoundingDirection): bigint;
11
+ function toShares(assets: BigIntish, { totalAssets, totalSupply, decimalsOffset, }: {
12
+ totalAssets: BigIntish;
13
+ totalSupply: BigIntish;
14
+ decimalsOffset: BigIntish;
15
+ }, rounding?: RoundingDirection): bigint;
16
+ }
@@ -0,0 +1,17 @@
1
+ import { MathLib } from "../math/index.js";
2
+ export var VaultUtils;
3
+ (function (VaultUtils) {
4
+ VaultUtils.VIRTUAL_ASSETS = 1n;
5
+ function decimalsOffset(decimals) {
6
+ return MathLib.zeroFloorSub(18n, decimals);
7
+ }
8
+ VaultUtils.decimalsOffset = decimalsOffset;
9
+ function toAssets(shares, { totalAssets, totalSupply, decimalsOffset, }, rounding = "Down") {
10
+ return MathLib.mulDiv(shares, BigInt(totalAssets) + VaultUtils.VIRTUAL_ASSETS, BigInt(totalSupply) + 10n ** BigInt(decimalsOffset), rounding);
11
+ }
12
+ VaultUtils.toAssets = toAssets;
13
+ function toShares(assets, { totalAssets, totalSupply, decimalsOffset, }, rounding = "Up") {
14
+ return MathLib.mulDiv(assets, BigInt(totalSupply) + 10n ** BigInt(decimalsOffset), BigInt(totalAssets) + VaultUtils.VIRTUAL_ASSETS, rounding);
15
+ }
16
+ VaultUtils.toShares = toShares;
17
+ })(VaultUtils || (VaultUtils = {}));
@@ -0,0 +1,8 @@
1
+ export * from "./VaultUtils.js";
2
+ export * from "./VaultConfig.js";
3
+ export * from "./VaultMarketAllocation.js";
4
+ export * from "./VaultMarketConfig.js";
5
+ export * from "./VaultMarketPublicAllocatorConfig.js";
6
+ export * from "./VaultUser.js";
7
+ export * from "./Vault.js";
8
+ export * from "./v2/index.js";
@@ -0,0 +1,8 @@
1
+ export * from "./VaultUtils.js";
2
+ export * from "./VaultConfig.js";
3
+ export * from "./VaultMarketAllocation.js";
4
+ export * from "./VaultMarketConfig.js";
5
+ export * from "./VaultMarketPublicAllocatorConfig.js";
6
+ export * from "./VaultUser.js";
7
+ export * from "./Vault.js";
8
+ export * from "./v2/index.js";