@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,237 @@
1
+ import { BlueErrors } from "../errors.js";
2
+ import { Market, } from "../market/index.js";
3
+ import { MathLib } from "../math/MathLib.js";
4
+ import { CapacityLimitReason } from "../utils.js";
5
+ export class Position {
6
+ /**
7
+ * The user holding this position.
8
+ */
9
+ user;
10
+ /**
11
+ * The id of the market on which this position is held.
12
+ */
13
+ marketId;
14
+ /**
15
+ * The amount of supply shares held with this position.
16
+ */
17
+ supplyShares;
18
+ /**
19
+ * The amount of borrow shares held with this position.
20
+ */
21
+ borrowShares;
22
+ /**
23
+ * The amount of collateral assets held with this position.
24
+ */
25
+ collateral;
26
+ constructor({ user, marketId, supplyShares, borrowShares, collateral, }) {
27
+ this.user = user;
28
+ this.marketId = marketId;
29
+ this.supplyShares = supplyShares;
30
+ this.borrowShares = borrowShares;
31
+ this.collateral = collateral;
32
+ }
33
+ }
34
+ export class AccrualPosition extends Position {
35
+ _market;
36
+ constructor(position, market) {
37
+ const _market = new Market(market);
38
+ super({ ...position, marketId: _market.id });
39
+ this._market = _market;
40
+ }
41
+ /**
42
+ * The market on which this position is held.
43
+ */
44
+ get market() {
45
+ return this._market;
46
+ }
47
+ get supplyAssets() {
48
+ return this._market.toSupplyAssets(this.supplyShares);
49
+ }
50
+ get borrowAssets() {
51
+ return this._market.toBorrowAssets(this.borrowShares);
52
+ }
53
+ /**
54
+ * The value of this position's collateral quoted in loan assets.
55
+ * `undefined` if the market's oracle is undefined or reverts.
56
+ */
57
+ get collateralValue() {
58
+ return this._market.getCollateralValue(this.collateral);
59
+ }
60
+ /**
61
+ * The maximum amount of loan assets that can be borrowed against this position's collateral.
62
+ * `undefined` if the market's oracle is undefined or reverts.
63
+ */
64
+ get maxBorrowAssets() {
65
+ return this._market.getMaxBorrowAssets(this.collateral);
66
+ }
67
+ /**
68
+ * The maximum additional amount of assets that can be borrowed against this position's collateral.
69
+ * `undefined` if the market's oracle is undefined or reverts.
70
+ */
71
+ get maxBorrowableAssets() {
72
+ const { maxBorrowAssets } = this;
73
+ if (maxBorrowAssets == null)
74
+ return;
75
+ return MathLib.zeroFloorSub(maxBorrowAssets, this.borrowAssets);
76
+ }
77
+ /**
78
+ * The maximum amount of collateral that can be seized in exchange for the outstanding debt.
79
+ * `undefined` if the market's oracle is undefined or reverts.
80
+ */
81
+ get seizableCollateral() {
82
+ return this._market.getSeizableCollateral(this);
83
+ }
84
+ /**
85
+ * The maximum amount of collateral that can be withdrawn.
86
+ * `undefined` if the market's oracle is undefined or reverts.
87
+ */
88
+ get withdrawableCollateral() {
89
+ return this._market.getWithdrawableCollateral(this);
90
+ }
91
+ /**
92
+ * Whether this position is healthy.
93
+ * `undefined` if the market's oracle is undefined or reverts.
94
+ */
95
+ get isHealthy() {
96
+ return this._market.isHealthy(this);
97
+ }
98
+ /**
99
+ * Whether this position can be liquidated.
100
+ * `undefined` if the market's oracle is undefined or reverts.
101
+ */
102
+ get isLiquidatable() {
103
+ const isHealthy = this._market.isHealthy(this);
104
+ if (isHealthy == null)
105
+ return;
106
+ return !isHealthy;
107
+ }
108
+ /**
109
+ * The price of the collateral quoted in loan assets that would allow this position to be liquidated.
110
+ * `null` if the position has no borrow.
111
+ */
112
+ get liquidationPrice() {
113
+ return this._market.getLiquidationPrice(this);
114
+ }
115
+ /**
116
+ * The price variation required for the position to reach its liquidation threshold (scaled by WAD).
117
+ * Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
118
+ * `undefined` if the market's oracle is undefined or reverts.
119
+ * `null` if the position is not a borrow.
120
+ */
121
+ get priceVariationToLiquidationPrice() {
122
+ return this._market.getPriceVariationToLiquidationPrice(this);
123
+ }
124
+ /**
125
+ * This position's Loan-To-Value (debt over collateral power, scaled by WAD).
126
+ * If the collateral price is 0, LTV is `MaxUint256`.
127
+ * `undefined` if the market's oracle is undefined or reverts.
128
+ */
129
+ get ltv() {
130
+ return this._market.getLtv(this);
131
+ }
132
+ /**
133
+ * This position's health factor (collateral power over debt, scaled by WAD).
134
+ * If the debt is 0, health factor is `MaxUint256`.
135
+ * `undefined` if the market's oracle is undefined or reverts.
136
+ */
137
+ get healthFactor() {
138
+ return this._market.getHealthFactor(this);
139
+ }
140
+ /**
141
+ * The percentage of this position's borrow power currently used (scaled by WAD).
142
+ * If the collateral price is 0, usage is `MaxUint256`.
143
+ */
144
+ get borrowCapacityUsage() {
145
+ return this._market.getBorrowCapacityUsage(this);
146
+ }
147
+ /**
148
+ * Returns the maximum amount of loan assets that can be withdrawn given a certain supply position
149
+ * and a balance of loan assets, and the reason for the limit.
150
+ */
151
+ get withdrawCapacityLimit() {
152
+ return this._market.getWithdrawCapacityLimit(this);
153
+ }
154
+ /**
155
+ * Returns a new position derived from this position, whose interest has been accrued up to the given timestamp.
156
+ * @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to the market's `lastUpdate`.
157
+ */
158
+ accrueInterest(timestamp) {
159
+ return new AccrualPosition(this, this._market.accrueInterest(timestamp));
160
+ }
161
+ supply(assets, shares, timestamp) {
162
+ let { _market: market } = this;
163
+ ({ market, assets, shares } = market.supply(assets, shares, timestamp));
164
+ const position = new AccrualPosition(this, market);
165
+ position.supplyShares += shares;
166
+ return { position, assets, shares };
167
+ }
168
+ withdraw(assets, shares, timestamp) {
169
+ let { _market: market } = this;
170
+ ({ market, assets, shares } = market.withdraw(assets, shares, timestamp));
171
+ const position = new AccrualPosition(this, market);
172
+ position.supplyShares -= shares;
173
+ if (position.supplyShares < 0n)
174
+ throw new BlueErrors.InsufficientPosition(position.user, position.marketId);
175
+ return { position, assets, shares };
176
+ }
177
+ supplyCollateral(assets) {
178
+ this.collateral += assets;
179
+ return new AccrualPosition(this, new Market(this._market));
180
+ }
181
+ withdrawCollateral(assets, timestamp) {
182
+ if (this._market.price == null)
183
+ throw new BlueErrors.UnknownOraclePrice(this.marketId);
184
+ const position = this.accrueInterest(timestamp);
185
+ position.collateral -= assets;
186
+ if (position.collateral < 0n)
187
+ throw new BlueErrors.InsufficientPosition(position.user, position.marketId);
188
+ if (!position.isHealthy)
189
+ throw new BlueErrors.InsufficientCollateral(position.user, position.marketId);
190
+ return position;
191
+ }
192
+ borrow(assets, shares, timestamp) {
193
+ let { _market: market } = this;
194
+ if (market.price == null)
195
+ throw new BlueErrors.UnknownOraclePrice(market.id);
196
+ ({ market, assets, shares } = market.borrow(assets, shares, timestamp));
197
+ const position = new AccrualPosition(this, market);
198
+ position.borrowShares += shares;
199
+ if (!position.isHealthy)
200
+ throw new BlueErrors.InsufficientCollateral(this.user, this.marketId);
201
+ return { position, assets, shares };
202
+ }
203
+ repay(assets, shares, timestamp) {
204
+ let { _market: market } = this;
205
+ ({ market, assets, shares } = market.repay(assets, shares, timestamp));
206
+ const position = new AccrualPosition(this, market);
207
+ position.borrowShares -= shares;
208
+ if (position.borrowShares < 0n)
209
+ throw new BlueErrors.InsufficientPosition(position.user, position.marketId);
210
+ return { position, assets, shares };
211
+ }
212
+ getBorrowCapacityLimit(options) {
213
+ return this._market.getBorrowCapacityLimit(this, options);
214
+ }
215
+ getWithdrawCollateralCapacityLimit(options) {
216
+ return this._market.getWithdrawCollateralCapacityLimit(this, options);
217
+ }
218
+ getRepayCapacityLimit(loanTokenBalance) {
219
+ return this._market.getRepayCapacityLimit(this.borrowShares, loanTokenBalance);
220
+ }
221
+ getMaxCapacities(loanTokenBalance, collateralTokenBalance, options) {
222
+ return {
223
+ supply: {
224
+ value: loanTokenBalance,
225
+ limiter: CapacityLimitReason.balance,
226
+ },
227
+ withdraw: this.withdrawCapacityLimit,
228
+ borrow: this.getBorrowCapacityLimit(options?.borrow),
229
+ repay: this.getRepayCapacityLimit(loanTokenBalance),
230
+ supplyCollateral: {
231
+ value: collateralTokenBalance,
232
+ limiter: CapacityLimitReason.balance,
233
+ },
234
+ withdrawCollateral: this.getWithdrawCollateralCapacityLimit(options?.withdrawCollateral),
235
+ };
236
+ }
237
+ }
@@ -0,0 +1,60 @@
1
+ import type { Address } from "viem";
2
+ import { type IMarket, Market } from "../market/index.js";
3
+ import type { BigIntish } from "../types.js";
4
+ import { AccrualPosition, type IAccrualPosition } from "./Position.js";
5
+ export interface IPreLiquidationParams {
6
+ preLltv: BigIntish;
7
+ preLCF1: BigIntish;
8
+ preLCF2: BigIntish;
9
+ preLIF1: BigIntish;
10
+ preLIF2: BigIntish;
11
+ preLiquidationOracle: Address;
12
+ }
13
+ export declare class PreLiquidationParams implements IPreLiquidationParams {
14
+ readonly preLltv: bigint;
15
+ readonly preLCF1: bigint;
16
+ readonly preLCF2: bigint;
17
+ readonly preLIF1: bigint;
18
+ readonly preLIF2: bigint;
19
+ readonly preLiquidationOracle: `0x${string}`;
20
+ constructor({ preLltv, preLCF1, preLCF2, preLIF1, preLIF2, preLiquidationOracle, }: IPreLiquidationParams);
21
+ getCloseFactor(quotient: BigIntish): bigint;
22
+ getIncentiveFactor(quotient: BigIntish): bigint;
23
+ }
24
+ export interface IPreLiquidationPosition extends IAccrualPosition {
25
+ /**
26
+ * The pre-liquidation parameters of the associated PreLiquidation contract.
27
+ */
28
+ preLiquidationParams: IPreLiquidationParams;
29
+ /**
30
+ * The address of the PreLiquidation contract this position is associated to.
31
+ */
32
+ preLiquidation: Address;
33
+ /**
34
+ * The price of the collateral quoted in loan assets used by the PreLiquidation contract.
35
+ * `undefined` if the oracle reverts.
36
+ */
37
+ preLiquidationOraclePrice?: BigIntish;
38
+ }
39
+ export declare class PreLiquidationPosition extends AccrualPosition implements IPreLiquidationPosition {
40
+ readonly preLiquidationParams: PreLiquidationParams;
41
+ readonly preLiquidation: `0x${string}`;
42
+ readonly preLiquidationOraclePrice?: bigint;
43
+ protected readonly _baseMarket: Market;
44
+ constructor({ preLiquidationParams, preLiquidation, preLiquidationOraclePrice, ...position }: IPreLiquidationPosition, market: IMarket);
45
+ get market(): Market;
46
+ protected get _lltv(): bigint;
47
+ /**
48
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
49
+ * `undefined` if it may be liquidatable on Morpho.
50
+ */
51
+ get isHealthy(): boolean | undefined;
52
+ /**
53
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
54
+ */
55
+ get isLiquidatable(): boolean | undefined;
56
+ /**
57
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
58
+ */
59
+ get seizableCollateral(): bigint | undefined;
60
+ }
@@ -0,0 +1,97 @@
1
+ import { ORACLE_PRICE_SCALE } from "../constants.js";
2
+ import { Market } from "../market/index.js";
3
+ import { MathLib, SharesMath } from "../math/index.js";
4
+ import { AccrualPosition } from "./Position.js";
5
+ export class PreLiquidationParams {
6
+ preLltv;
7
+ preLCF1;
8
+ preLCF2;
9
+ preLIF1;
10
+ preLIF2;
11
+ preLiquidationOracle;
12
+ constructor({ preLltv, preLCF1, preLCF2, preLIF1, preLIF2, preLiquidationOracle, }) {
13
+ this.preLltv = BigInt(preLltv);
14
+ this.preLCF1 = BigInt(preLCF1);
15
+ this.preLCF2 = BigInt(preLCF2);
16
+ this.preLIF1 = BigInt(preLIF1);
17
+ this.preLIF2 = BigInt(preLIF2);
18
+ this.preLiquidationOracle = preLiquidationOracle;
19
+ }
20
+ getCloseFactor(quotient) {
21
+ return (this.preLCF1 + MathLib.wMulDown(quotient, this.preLCF2 - this.preLCF1));
22
+ }
23
+ getIncentiveFactor(quotient) {
24
+ return (this.preLIF1 + MathLib.wMulDown(quotient, this.preLIF2 - this.preLIF1));
25
+ }
26
+ }
27
+ export class PreLiquidationPosition extends AccrualPosition {
28
+ preLiquidationParams;
29
+ preLiquidation;
30
+ preLiquidationOraclePrice;
31
+ _baseMarket;
32
+ constructor({ preLiquidationParams, preLiquidation, preLiquidationOraclePrice, ...position }, market) {
33
+ super(position, {
34
+ ...market,
35
+ params: {
36
+ ...market.params,
37
+ lltv: BigInt(preLiquidationParams.preLltv),
38
+ },
39
+ price: preLiquidationOraclePrice != null
40
+ ? BigInt(preLiquidationOraclePrice)
41
+ : undefined,
42
+ });
43
+ this.preLiquidationParams = new PreLiquidationParams(preLiquidationParams);
44
+ this.preLiquidation = preLiquidation;
45
+ if (preLiquidationOraclePrice != null)
46
+ this.preLiquidationOraclePrice = BigInt(preLiquidationOraclePrice);
47
+ this._baseMarket = new Market(market);
48
+ }
49
+ get market() {
50
+ return this._baseMarket;
51
+ }
52
+ get _lltv() {
53
+ return this._baseMarket.params.lltv;
54
+ }
55
+ /**
56
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
57
+ * `undefined` if it may be liquidatable on Morpho.
58
+ */
59
+ get isHealthy() {
60
+ const { collateralValue } = this;
61
+ if (collateralValue == null)
62
+ return;
63
+ const { borrowAssets } = this;
64
+ if (borrowAssets > MathLib.wMulDown(collateralValue, this._lltv))
65
+ return;
66
+ return (borrowAssets <=
67
+ MathLib.wMulDown(collateralValue, this.preLiquidationParams.preLltv));
68
+ }
69
+ /**
70
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
71
+ */
72
+ get isLiquidatable() {
73
+ const { collateralValue } = this;
74
+ if (collateralValue == null)
75
+ return;
76
+ const { borrowAssets } = this;
77
+ return (borrowAssets >
78
+ MathLib.wMulDown(collateralValue, this.preLiquidationParams.preLltv) &&
79
+ borrowAssets <= MathLib.wMulDown(collateralValue, this._lltv));
80
+ }
81
+ /**
82
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
83
+ */
84
+ get seizableCollateral() {
85
+ if (this._market.price == null)
86
+ return;
87
+ if (!this.isLiquidatable)
88
+ return 0n;
89
+ const { ltv } = this;
90
+ if (ltv == null)
91
+ return;
92
+ const quotient = MathLib.wDivDown(ltv - this.preLiquidationParams.preLltv, this._lltv - this.preLiquidationParams.preLltv);
93
+ const repayableShares = MathLib.wMulDown(this.borrowShares, this.preLiquidationParams.getCloseFactor(quotient));
94
+ const repayableAssets = MathLib.wMulDown(SharesMath.toAssets(repayableShares, this._market.totalBorrowAssets, this._market.totalBorrowShares, "Down"), this.preLiquidationParams.getIncentiveFactor(quotient));
95
+ return MathLib.mulDivDown(repayableAssets, ORACLE_PRICE_SCALE, this._market.price);
96
+ }
97
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./Position.js";
2
+ export * from "./PreLiquidationPosition.js";
@@ -0,0 +1,2 @@
1
+ export * from "./Position.js";
2
+ export * from "./PreLiquidationPosition.js";
@@ -0,0 +1,15 @@
1
+ import type { BigIntish } from "./types.js";
2
+ export declare const defaultPreLiquidationParamsRegistry: Map<bigint, {
3
+ preLltv: bigint;
4
+ preLCF1: bigint;
5
+ preLCF2: bigint;
6
+ preLIF1: bigint;
7
+ preLIF2: bigint;
8
+ }>;
9
+ export declare const getDefaultPreLiquidationParams: (lltv: BigIntish) => {
10
+ preLltv: bigint;
11
+ preLCF1: bigint;
12
+ preLCF2: bigint;
13
+ preLIF1: bigint;
14
+ preLIF2: bigint;
15
+ };
@@ -0,0 +1,91 @@
1
+ import { parseEther } from "viem";
2
+ import { UnsupportedPreLiquidationParamsError } from "./errors.js";
3
+ export const defaultPreLiquidationParamsRegistry = new Map([
4
+ [
5
+ parseEther("0.385"),
6
+ {
7
+ preLltv: 301514568055515563n,
8
+ preLCF1: 22637943984157107n,
9
+ preLCF2: 349673199983645648n,
10
+ preLIF1: parseEther("1.15"),
11
+ preLIF2: parseEther("1.15"),
12
+ },
13
+ ],
14
+ [
15
+ parseEther("0.625"),
16
+ {
17
+ preLltv: 562591950487445723n,
18
+ preLCF1: 7543182567291709n,
19
+ preLCF2: 279542312587328718n,
20
+ preLIF1: 1126760563380281690n,
21
+ preLIF2: 1126760563380281690n,
22
+ },
23
+ ],
24
+ [
25
+ parseEther("0.77"),
26
+ {
27
+ preLltv: 727366070175296029n,
28
+ preLCF1: 3706417131700377n,
29
+ preLCF2: 256643181309902852n,
30
+ preLIF1: 1074113856068743286n,
31
+ preLIF2: 1074113856068743286n,
32
+ },
33
+ ],
34
+ [
35
+ parseEther("0.86"),
36
+ {
37
+ preLltv: 832603694978499652n,
38
+ preLCF1: 2001493508968667n,
39
+ preLCF2: 245311807032632372n,
40
+ preLIF1: 1043841336116910229n,
41
+ preLIF2: 1043841336116910229n,
42
+ },
43
+ ],
44
+ [
45
+ parseEther("0.915"),
46
+ {
47
+ preLltv: 897868776651447149n,
48
+ preLCF1: 1135586186384195n,
49
+ preLCF2: 239205538157954887n,
50
+ preLIF1: 1026167265264238070n,
51
+ preLIF2: 1026167265264238070n,
52
+ },
53
+ ],
54
+ [
55
+ parseEther("0.945"),
56
+ {
57
+ preLltv: 933746617913300027n,
58
+ preLCF1: 709220796660800n,
59
+ preLCF2: 236098907251355946n,
60
+ preLIF1: 1016776817488561260n,
61
+ preLIF2: 1016776817488561260n,
62
+ },
63
+ ],
64
+ [
65
+ parseEther("0.965"),
66
+ {
67
+ preLltv: 957768981497388846n,
68
+ preLCF1: 441038514876104n,
69
+ preLCF2: 234108264807531861n,
70
+ preLIF1: 1010611419909044972n,
71
+ preLIF2: 1010611419909044972n,
72
+ },
73
+ ],
74
+ [
75
+ parseEther("0.98"),
76
+ {
77
+ preLltv: 975838577830248552n,
78
+ preLCF1: 247773050273784n,
79
+ preLCF2: 232655340599010079n,
80
+ preLIF1: 1006036217303822937n,
81
+ preLIF2: 1006036217303822937n,
82
+ },
83
+ ],
84
+ ]);
85
+ export const getDefaultPreLiquidationParams = (lltv) => {
86
+ lltv = BigInt(lltv);
87
+ const defaultParams = defaultPreLiquidationParamsRegistry.get(lltv);
88
+ if (defaultParams == null)
89
+ throw new UnsupportedPreLiquidationParamsError(lltv);
90
+ return defaultParams;
91
+ };
@@ -0,0 +1,17 @@
1
+ import { type RoundingDirection } from "../math/index.js";
2
+ import type { Address, BigIntish } from "../types.js";
3
+ import type { IToken } from "./Token.js";
4
+ import { WrappedToken } from "./WrappedToken.js";
5
+ export declare class ConstantWrappedToken extends WrappedToken {
6
+ readonly underlyingDecimals: bigint;
7
+ constructor(token: IToken, underlying: Address, underlyingDecimals?: BigIntish);
8
+ toWrappedExactAmountIn(unwrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
9
+ /** The amount of unwrappedTokens that should be wrapped to receive `wrappedAmount` */
10
+ toWrappedExactAmountOut(wrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
11
+ /** The expected amount when unwrapping `wrappedAmount` */
12
+ toUnwrappedExactAmountIn(wrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
13
+ /** The amount of wrappedTokens that should be unwrapped to receive `unwrappedAmount` */
14
+ toUnwrappedExactAmountOut(unwrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
15
+ protected _wrap(amount: bigint): bigint;
16
+ protected _unwrap(amount: bigint): bigint;
17
+ }
@@ -0,0 +1,30 @@
1
+ import { MathLib } from "../math/index.js";
2
+ import { WrappedToken } from "./WrappedToken.js";
3
+ export class ConstantWrappedToken extends WrappedToken {
4
+ underlyingDecimals;
5
+ constructor(token, underlying, underlyingDecimals = 0) {
6
+ super(token, underlying);
7
+ this.underlyingDecimals = BigInt(underlyingDecimals);
8
+ }
9
+ toWrappedExactAmountIn(unwrappedAmount, _slippage, rounding = "Down") {
10
+ return super.toWrappedExactAmountIn(unwrappedAmount, 0n, rounding);
11
+ }
12
+ /** The amount of unwrappedTokens that should be wrapped to receive `wrappedAmount` */
13
+ toWrappedExactAmountOut(wrappedAmount, _slippage, rounding = "Up") {
14
+ return super.toWrappedExactAmountOut(wrappedAmount, 0n, rounding);
15
+ }
16
+ /** The expected amount when unwrapping `wrappedAmount` */
17
+ toUnwrappedExactAmountIn(wrappedAmount, _slippage, rounding = "Down") {
18
+ return super.toUnwrappedExactAmountIn(wrappedAmount, 0n, rounding);
19
+ }
20
+ /** The amount of wrappedTokens that should be unwrapped to receive `unwrappedAmount` */
21
+ toUnwrappedExactAmountOut(unwrappedAmount, _slippage, rounding = "Up") {
22
+ return super.toUnwrappedExactAmountOut(unwrappedAmount, 0n, rounding);
23
+ }
24
+ _wrap(amount) {
25
+ return MathLib.mulDivDown(amount, 10n ** BigInt(this.decimals), 10n ** this.underlyingDecimals);
26
+ }
27
+ _unwrap(amount) {
28
+ return MathLib.mulDivDown(amount, 10n ** this.underlyingDecimals, 10n ** BigInt(this.decimals));
29
+ }
30
+ }
@@ -0,0 +1,55 @@
1
+ import type { Address } from "../types.js";
2
+ export declare const EIP_712_FIELDS: readonly ["name", "version", "chainId", "verifyingContract", "salt"];
3
+ export type Eip712Field = (typeof EIP_712_FIELDS)[number];
4
+ export interface IEip5267Domain {
5
+ fields: `0x${string}`;
6
+ name: string;
7
+ version: string;
8
+ chainId: bigint;
9
+ verifyingContract: Address;
10
+ salt: `0x${string}`;
11
+ extensions: readonly bigint[];
12
+ }
13
+ export declare class Eip5267Domain implements IEip5267Domain {
14
+ /**
15
+ * A bit map where bit i is set to 1 if and only if domain field i is present (0 ≤ i ≤ 4).
16
+ * Bits are read from least significant to most significant, and fields are indexed in the order that is specified by EIP-712, identical to the order in which they are listed in the function type.
17
+ */
18
+ readonly fields: `0x${string}`;
19
+ /**
20
+ * The user readable name of signing domain, i.e. the name of the DApp or the protocol.
21
+ */
22
+ readonly name: string;
23
+ /**
24
+ * The current major version of the signing domain.
25
+ * Signatures from different versions are not compatible.
26
+ */
27
+ readonly version: string;
28
+ /**
29
+ * The EIP-155 chain id.
30
+ */
31
+ readonly chainId: bigint;
32
+ /**
33
+ * The address of the contract that will verify the EIP-712 signature.
34
+ */
35
+ readonly verifyingContract: `0x${string}`;
36
+ /**
37
+ * A disambiguating salt for the protocol.
38
+ * This can be used as a domain separator of last resort.
39
+ */
40
+ readonly salt: `0x${string}`;
41
+ /**
42
+ * A list of EIP numbers, each of which MUST refer to an EIP that extends EIP-712 with new domain fields, along with a method to obtain the value for those fields, and potentially conditions for inclusion.
43
+ * The value of fields does not affect their inclusion.
44
+ */
45
+ readonly extensions: readonly bigint[];
46
+ readonly eip712Domain: {
47
+ name?: string | undefined;
48
+ chainId?: number | undefined;
49
+ version?: string | undefined;
50
+ verifyingContract?: `0x${string}` | undefined;
51
+ salt?: `0x${string}` | undefined;
52
+ };
53
+ constructor({ fields, name, version, chainId, verifyingContract, salt, extensions, }: IEip5267Domain);
54
+ private asEip712Domain;
55
+ }
@@ -0,0 +1,66 @@
1
+ export const EIP_712_FIELDS = [
2
+ "name",
3
+ "version",
4
+ "chainId",
5
+ "verifyingContract",
6
+ "salt",
7
+ ];
8
+ export class Eip5267Domain {
9
+ /**
10
+ * A bit map where bit i is set to 1 if and only if domain field i is present (0 ≤ i ≤ 4).
11
+ * Bits are read from least significant to most significant, and fields are indexed in the order that is specified by EIP-712, identical to the order in which they are listed in the function type.
12
+ */
13
+ fields;
14
+ /**
15
+ * The user readable name of signing domain, i.e. the name of the DApp or the protocol.
16
+ */
17
+ name;
18
+ /**
19
+ * The current major version of the signing domain.
20
+ * Signatures from different versions are not compatible.
21
+ */
22
+ version;
23
+ /**
24
+ * The EIP-155 chain id.
25
+ */
26
+ chainId;
27
+ /**
28
+ * The address of the contract that will verify the EIP-712 signature.
29
+ */
30
+ verifyingContract;
31
+ /**
32
+ * A disambiguating salt for the protocol.
33
+ * This can be used as a domain separator of last resort.
34
+ */
35
+ salt;
36
+ /**
37
+ * A list of EIP numbers, each of which MUST refer to an EIP that extends EIP-712 with new domain fields, along with a method to obtain the value for those fields, and potentially conditions for inclusion.
38
+ * The value of fields does not affect their inclusion.
39
+ */
40
+ extensions;
41
+ eip712Domain;
42
+ constructor({ fields, name, version, chainId, verifyingContract, salt, extensions, }) {
43
+ this.fields = fields;
44
+ this.name = name;
45
+ this.version = version;
46
+ this.chainId = chainId;
47
+ this.verifyingContract = verifyingContract;
48
+ this.salt = salt;
49
+ this.extensions = extensions;
50
+ this.eip712Domain = this.asEip712Domain();
51
+ }
52
+ asEip712Domain() {
53
+ const fields = BigInt(this.fields);
54
+ return EIP_712_FIELDS.reduce((acc, field, i) => {
55
+ if (fields & (2n ** BigInt(i))) {
56
+ // @ts-expect-error Typescript doesn't infer value type based on field.
57
+ acc[field] =
58
+ field === "chainId"
59
+ ? // Signature does not correspond if chainId is a bigint.
60
+ Number(this.chainId)
61
+ : this[field];
62
+ }
63
+ return acc;
64
+ }, {});
65
+ }
66
+ }