@morpho-org/midnight-sdk 0.1.0 → 1.0.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 (118) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -46
  3. package/lib/cjs/abis.d.ts +1185 -996
  4. package/lib/cjs/abis.js +1445 -1211
  5. package/lib/cjs/api/MidnightApi.d.ts +22 -16
  6. package/lib/cjs/api/MidnightApi.js +22 -16
  7. package/lib/cjs/api/helpers.js +6 -3
  8. package/lib/cjs/api/types.d.ts +35 -9
  9. package/lib/cjs/constants.d.ts +12 -11
  10. package/lib/cjs/constants.js +14 -12
  11. package/lib/cjs/errors.d.ts +74 -1
  12. package/lib/cjs/errors.js +94 -3
  13. package/lib/cjs/fetch/Market.js +0 -1
  14. package/lib/cjs/fetch/index.d.ts +0 -1
  15. package/lib/cjs/fetch/index.js +0 -1
  16. package/lib/cjs/index.d.ts +1 -1
  17. package/lib/cjs/index.js +2 -23
  18. package/lib/cjs/market/Market.d.ts +99 -33
  19. package/lib/cjs/market/Market.js +137 -28
  20. package/lib/cjs/market/MarketUtils.d.ts +61 -41
  21. package/lib/cjs/market/MarketUtils.js +108 -48
  22. package/lib/cjs/market/Position.d.ts +16 -11
  23. package/lib/cjs/market/Position.js +18 -12
  24. package/lib/cjs/market/PositionUtils.d.ts +6 -4
  25. package/lib/cjs/market/PositionUtils.js +9 -6
  26. package/lib/cjs/math/TickLib.d.ts +22 -0
  27. package/lib/cjs/math/TickLib.js +30 -0
  28. package/lib/cjs/math/index.d.ts +0 -1
  29. package/lib/cjs/math/index.js +0 -1
  30. package/lib/cjs/offers/Offer.d.ts +217 -11
  31. package/lib/cjs/offers/Offer.js +215 -7
  32. package/lib/cjs/offers/OfferUtils.d.ts +245 -139
  33. package/lib/cjs/offers/OfferUtils.js +304 -36
  34. package/lib/cjs/queries/GetPosition.d.ts +1 -1
  35. package/lib/cjs/queries/GetPosition.js +1 -1
  36. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +66 -38
  37. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +147 -79
  38. package/lib/cjs/signatures/Group.d.ts +12 -4
  39. package/lib/cjs/signatures/Group.js +12 -4
  40. package/lib/cjs/signatures/GroupUtils.d.ts +12 -4
  41. package/lib/cjs/signatures/GroupUtils.js +6 -2
  42. package/lib/cjs/signatures/Payload.d.ts +277 -277
  43. package/lib/cjs/signatures/Payload.js +517 -534
  44. package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
  45. package/lib/cjs/signatures/RatifierUtils.js +94 -0
  46. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +21 -13
  47. package/lib/cjs/signatures/SetterRatifierUtils.js +25 -20
  48. package/lib/cjs/signatures/Tree.d.ts +28 -16
  49. package/lib/cjs/signatures/Tree.js +25 -13
  50. package/lib/cjs/signatures/TreeUtils.d.ts +181 -62
  51. package/lib/cjs/signatures/TreeUtils.js +73 -76
  52. package/lib/cjs/signatures/index.d.ts +1 -0
  53. package/lib/cjs/signatures/index.js +3 -0
  54. package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
  55. package/lib/cjs/signatures/offerStructInternal.js +72 -0
  56. package/lib/esm/abis.d.ts +1185 -996
  57. package/lib/esm/abis.js +1445 -1211
  58. package/lib/esm/api/MidnightApi.d.ts +22 -16
  59. package/lib/esm/api/MidnightApi.js +23 -17
  60. package/lib/esm/api/helpers.js +6 -3
  61. package/lib/esm/api/types.d.ts +35 -9
  62. package/lib/esm/constants.d.ts +12 -11
  63. package/lib/esm/constants.js +13 -11
  64. package/lib/esm/errors.d.ts +74 -1
  65. package/lib/esm/errors.js +89 -2
  66. package/lib/esm/fetch/Market.js +0 -1
  67. package/lib/esm/fetch/index.d.ts +0 -1
  68. package/lib/esm/fetch/index.js +0 -1
  69. package/lib/esm/index.d.ts +1 -1
  70. package/lib/esm/index.js +1 -1
  71. package/lib/esm/market/Market.d.ts +99 -33
  72. package/lib/esm/market/Market.js +137 -28
  73. package/lib/esm/market/MarketUtils.d.ts +61 -41
  74. package/lib/esm/market/MarketUtils.js +109 -49
  75. package/lib/esm/market/Position.d.ts +16 -11
  76. package/lib/esm/market/Position.js +19 -13
  77. package/lib/esm/market/PositionUtils.d.ts +6 -4
  78. package/lib/esm/market/PositionUtils.js +9 -6
  79. package/lib/esm/math/TickLib.d.ts +22 -0
  80. package/lib/esm/math/TickLib.js +31 -1
  81. package/lib/esm/math/index.d.ts +0 -1
  82. package/lib/esm/math/index.js +0 -1
  83. package/lib/esm/offers/Offer.d.ts +217 -11
  84. package/lib/esm/offers/Offer.js +216 -8
  85. package/lib/esm/offers/OfferUtils.d.ts +245 -139
  86. package/lib/esm/offers/OfferUtils.js +306 -38
  87. package/lib/esm/queries/GetPosition.d.ts +1 -1
  88. package/lib/esm/queries/GetPosition.js +1 -1
  89. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +66 -38
  90. package/lib/esm/signatures/EcrecoverRatifierUtils.js +149 -81
  91. package/lib/esm/signatures/Group.d.ts +12 -4
  92. package/lib/esm/signatures/Group.js +12 -4
  93. package/lib/esm/signatures/GroupUtils.d.ts +12 -4
  94. package/lib/esm/signatures/GroupUtils.js +6 -2
  95. package/lib/esm/signatures/Payload.d.ts +277 -277
  96. package/lib/esm/signatures/Payload.js +517 -531
  97. package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
  98. package/lib/esm/signatures/RatifierUtils.js +94 -0
  99. package/lib/esm/signatures/SetterRatifierUtils.d.ts +21 -13
  100. package/lib/esm/signatures/SetterRatifierUtils.js +25 -20
  101. package/lib/esm/signatures/Tree.d.ts +28 -16
  102. package/lib/esm/signatures/Tree.js +25 -13
  103. package/lib/esm/signatures/TreeUtils.d.ts +181 -62
  104. package/lib/esm/signatures/TreeUtils.js +75 -78
  105. package/lib/esm/signatures/index.d.ts +1 -0
  106. package/lib/esm/signatures/index.js +1 -0
  107. package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
  108. package/lib/esm/signatures/offerStructInternal.js +67 -0
  109. package/package.json +28 -32
  110. package/src/index.ts +1 -1
  111. package/lib/cjs/fetch/Offer.d.ts +0 -74
  112. package/lib/cjs/fetch/Offer.js +0 -119
  113. package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
  114. package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
  115. package/lib/esm/fetch/Offer.d.ts +0 -74
  116. package/lib/esm/fetch/Offer.js +0 -116
  117. package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
  118. package/lib/esm/math/ConsumableUnitsLib.js +0 -74
@@ -4,17 +4,20 @@ exports.MarketUtils = void 0;
4
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
5
  const viem_1 = require("viem");
6
6
  const constants_js_1 = require("../constants.js");
7
+ const errors_js_1 = require("../errors.js");
7
8
  const Market_js_1 = require("./Market.js");
8
9
  const SSTORE2_PREFIX = "0x600b380380600b5f395ff3";
9
10
  const collateralParamsHashParams = [
10
11
  { name: "typehash", type: "bytes32" },
11
12
  { name: "token", type: "address" },
12
13
  { name: "lltv", type: "uint256" },
13
- { name: "maxLif", type: "uint256" },
14
+ { name: "liquidationCursor", type: "uint256" },
14
15
  { name: "oracle", type: "address" },
15
16
  ];
16
17
  const marketHashParams = [
17
18
  { name: "typehash", type: "bytes32" },
19
+ { name: "chainId", type: "uint256" },
20
+ { name: "midnight", type: "address" },
18
21
  { name: "loanToken", type: "address" },
19
22
  { name: "collateralParamsHash", type: "bytes32" },
20
23
  { name: "maturity", type: "uint256" },
@@ -25,6 +28,8 @@ const marketHashParams = [
25
28
  const marketParamsAbiParameter = {
26
29
  type: "tuple",
27
30
  components: [
31
+ { name: "chainId", type: "uint256" },
32
+ { name: "midnight", type: "address" },
28
33
  { name: "loanToken", type: "address" },
29
34
  {
30
35
  name: "collateralParams",
@@ -32,7 +37,7 @@ const marketParamsAbiParameter = {
32
37
  components: [
33
38
  { name: "token", type: "address" },
34
39
  { name: "lltv", type: "uint256" },
35
- { name: "maxLif", type: "uint256" },
40
+ { name: "liquidationCursor", type: "uint256" },
36
41
  { name: "oracle", type: "address" },
37
42
  ],
38
43
  },
@@ -59,7 +64,7 @@ var MarketUtils;
59
64
  *
60
65
  * @param params.token - Collateral token address.
61
66
  * @param params.lltv - WAD-scaled liquidation loan-to-value.
62
- * @param params.maxLif - WAD-scaled maximum liquidation incentive factor.
67
+ * @param params.liquidationCursor - WAD-scaled liquidation cursor.
63
68
  * @param params.oracle - Oracle address used to price this collateral.
64
69
  * @returns Collateral params with bigint fields.
65
70
  * @example
@@ -69,17 +74,31 @@ var MarketUtils;
69
74
  * const collateral = MarketUtils.toCollateralParams({
70
75
  * token: "0x0000000000000000000000000000000000000001",
71
76
  * lltv: 770000000000000000n,
72
- * maxLif: 1061007957559681697n,
77
+ * liquidationCursor: 250000000000000000n,
73
78
  * oracle: "0x0000000000000000000000000000000000000002",
74
79
  * });
75
- * console.log(collateral.maxLif);
80
+ * console.log(collateral.liquidationCursor);
76
81
  * ```
77
82
  */
78
83
  function toCollateralParams(params) {
84
+ let lltv;
85
+ let liquidationCursor;
86
+ try {
87
+ lltv = BigInt(params.lltv);
88
+ liquidationCursor = BigInt(params.liquidationCursor);
89
+ }
90
+ catch (cause) {
91
+ throw new errors_js_1.InvalidMarketParameterError({
92
+ parameter: "collateralParams",
93
+ value: params,
94
+ instruction: "Provide bigint-compatible lltv and liquidationCursor values.",
95
+ cause,
96
+ });
97
+ }
79
98
  return {
80
99
  token: params.token,
81
- lltv: BigInt(params.lltv),
82
- maxLif: BigInt(params.maxLif),
100
+ lltv,
101
+ liquidationCursor,
83
102
  oracle: params.oracle,
84
103
  };
85
104
  }
@@ -94,12 +113,14 @@ var MarketUtils;
94
113
  * import { MarketUtils } from "@morpho-org/midnight-sdk";
95
114
  *
96
115
  * const struct = MarketUtils.toStruct({
116
+ * chainId: 8453,
117
+ * midnight: "0x0000000000000000000000000000000000001000",
97
118
  * loanToken: "0x0000000000000000000000000000000000006000",
98
119
  * collateralParams: [
99
120
  * {
100
121
  * token: "0x0000000000000000000000000000000000007000",
101
122
  * lltv: 770000000000000000n,
102
- * maxLif: 1061007957559681697n,
123
+ * liquidationCursor: 250000000000000000n,
103
124
  * oracle: "0x0000000000000000000000000000000000008000",
104
125
  * },
105
126
  * ],
@@ -114,11 +135,13 @@ var MarketUtils;
114
135
  function toStruct(market) {
115
136
  const params = Market_js_1.MarketParams.from(market);
116
137
  return {
138
+ chainId: params.chainId,
139
+ midnight: params.midnight,
117
140
  loanToken: params.loanToken,
118
141
  collateralParams: params.collateralParams.map((collateral) => ({
119
142
  token: collateral.token,
120
143
  lltv: collateral.lltv,
121
- maxLif: collateral.maxLif,
144
+ liquidationCursor: collateral.liquidationCursor,
122
145
  oracle: collateral.oracle,
123
146
  })),
124
147
  maturity: params.maturity,
@@ -128,19 +151,66 @@ var MarketUtils;
128
151
  };
129
152
  }
130
153
  MarketUtils.toStruct = toStruct;
154
+ /**
155
+ * Returns configured collateral by index.
156
+ *
157
+ * @param market - Market params or hydrated market.
158
+ * @param index - Collateral index to look up.
159
+ * @returns Collateral entry for the configured index.
160
+ * @throws {UnknownCollateralIndexError} when the index is not configured.
161
+ * @example
162
+ * ```ts
163
+ * import { MarketUtils } from "@morpho-org/midnight-sdk";
164
+ *
165
+ * const collateral = MarketUtils.getCollateralByIndex({
166
+ * chainId: 8453,
167
+ * midnight: "0x0000000000000000000000000000000000001000",
168
+ * loanToken: "0x0000000000000000000000000000000000000001",
169
+ * collateralParams: [
170
+ * {
171
+ * token: "0x0000000000000000000000000000000000000002",
172
+ * lltv: 770000000000000000n,
173
+ * liquidationCursor: 250000000000000000n,
174
+ * oracle: "0x0000000000000000000000000000000000000003",
175
+ * },
176
+ * ],
177
+ * maturity: 1n,
178
+ * rcfThreshold: 0n,
179
+ * enterGate: "0x0000000000000000000000000000000000000000",
180
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
181
+ * }, 0n);
182
+ * console.log(collateral.token);
183
+ * ```
184
+ */
185
+ function getCollateralByIndex(market, index) {
186
+ const normalizedIndex = BigInt(index);
187
+ const params = Market_js_1.MarketParams.from(market);
188
+ const collateral = normalizedIndex >= 0n &&
189
+ normalizedIndex <= BigInt(Number.MAX_SAFE_INTEGER)
190
+ ? params.collateralParams[Number(normalizedIndex)]
191
+ : undefined;
192
+ if (collateral == null) {
193
+ throw new errors_js_1.UnknownCollateralIndexError({
194
+ market: toId(params),
195
+ collateralIndex: normalizedIndex,
196
+ });
197
+ }
198
+ return collateral;
199
+ }
200
+ MarketUtils.getCollateralByIndex = getCollateralByIndex;
131
201
  /**
132
202
  * Returns the liquidation incentive factor for an LLTV and liquidation cursor.
133
203
  *
134
204
  * @param input - WAD-scaled LLTV or collateral params carrying an LLTV.
135
- * @param cursor - WAD-scaled liquidation cursor, usually `LIQUIDATION_CURSOR_LOW` or `LIQUIDATION_CURSOR_HIGH`.
205
+ * @param cursor - WAD-scaled liquidation cursor.
136
206
  * @returns WAD-scaled liquidation incentive factor.
137
207
  * @example
138
208
  * ```ts
139
- * import { LIQUIDATION_CURSOR_LOW, MarketUtils } from "@morpho-org/midnight-sdk";
209
+ * import { MarketUtils } from "@morpho-org/midnight-sdk";
140
210
  *
141
211
  * const liquidationIncentiveFactor = MarketUtils.getLiquidationIncentiveFactor(
142
212
  * 770000000000000000n,
143
- * LIQUIDATION_CURSOR_LOW,
213
+ * 250000000000000000n,
144
214
  * );
145
215
  * console.log(liquidationIncentiveFactor);
146
216
  * ```
@@ -209,12 +279,14 @@ var MarketUtils;
209
279
  * import { MarketUtils } from "@morpho-org/midnight-sdk";
210
280
  *
211
281
  * const hash = MarketUtils.hash({
282
+ * chainId: 8453,
283
+ * midnight: "0x0000000000000000000000000000000000001000",
212
284
  * loanToken: "0x0000000000000000000000000000000000000001",
213
285
  * collateralParams: [
214
286
  * {
215
287
  * token: "0x0000000000000000000000000000000000000002",
216
288
  * lltv: 770000000000000000n,
217
- * maxLif: 1061007957559681697n,
289
+ * liquidationCursor: 250000000000000000n,
218
290
  * oracle: "0x0000000000000000000000000000000000000003",
219
291
  * },
220
292
  * ],
@@ -235,12 +307,14 @@ var MarketUtils;
235
307
  constants_js_1.COLLATERAL_PARAMS_TYPEHASH,
236
308
  params.token,
237
309
  BigInt(params.lltv),
238
- BigInt(params.maxLif),
310
+ BigInt(params.liquidationCursor),
239
311
  params.oracle,
240
312
  ])));
241
313
  const collateralParamsHash = (0, viem_1.keccak256)((0, viem_1.concat)(collateralParamHashes));
242
314
  return (0, viem_1.keccak256)((0, viem_1.encodeAbiParameters)(marketHashParams, [
243
315
  constants_js_1.MARKET_TYPEHASH,
316
+ BigInt(marketParams.chainId),
317
+ marketParams.midnight,
244
318
  marketParams.loanToken,
245
319
  collateralParamsHash,
246
320
  BigInt(marketParams.maturity),
@@ -253,51 +327,37 @@ var MarketUtils;
253
327
  /**
254
328
  * Computes the Midnight id for a market using `IdLib.toId`.
255
329
  *
256
- * @param params.market - Market to hash.
257
- * @param params.chainId - EIP-155 chain id used to resolve the core Midnight address from the registry.
330
+ * @param market - Market to hash.
258
331
  * @returns Market id.
259
- * @throws {UnsupportedChainIdError} when no address registry exists for `chainId`.
260
- * @throws {UnknownAddressError} when the registry has no Midnight address for `chainId`.
261
332
  * @example
262
333
  * ```ts
263
- * import { ChainId, registerCustomAddresses } from "@morpho-org/morpho-ts";
264
334
  * import { MarketUtils } from "@morpho-org/midnight-sdk";
265
335
  *
266
- * registerCustomAddresses({
267
- * addresses: {
268
- * [ChainId.BaseMainnet]: {
269
- * midnight: "0x0000000000000000000000000000000000000001",
270
- * },
271
- * },
272
- * });
273
- *
274
336
  * const id = MarketUtils.toId({
275
- * market: {
276
- * loanToken: "0x0000000000000000000000000000000000000001",
277
- * collateralParams: [
278
- * {
279
- * token: "0x0000000000000000000000000000000000000002",
280
- * lltv: 770000000000000000n,
281
- * maxLif: 1061007957559681697n,
282
- * oracle: "0x0000000000000000000000000000000000000003",
283
- * },
284
- * ],
285
- * maturity: 1n,
286
- * rcfThreshold: 0n,
287
- * enterGate: "0x0000000000000000000000000000000000000000",
288
- * liquidatorGate: "0x0000000000000000000000000000000000000000",
289
- * },
290
- * chainId: ChainId.BaseMainnet,
337
+ * chainId: 8453,
338
+ * midnight: "0x0000000000000000000000000000000000001000",
339
+ * loanToken: "0x0000000000000000000000000000000000000001",
340
+ * collateralParams: [
341
+ * {
342
+ * token: "0x0000000000000000000000000000000000000002",
343
+ * lltv: 770000000000000000n,
344
+ * liquidationCursor: 250000000000000000n,
345
+ * oracle: "0x0000000000000000000000000000000000000003",
346
+ * },
347
+ * ],
348
+ * maturity: 1n,
349
+ * rcfThreshold: 0n,
350
+ * enterGate: "0x0000000000000000000000000000000000000000",
351
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
291
352
  * });
292
353
  * console.log(id);
293
354
  * ```
294
355
  */
295
- function toId(params) {
296
- const chainId = BigInt(params.chainId);
297
- const midnight = (0, morpho_ts_1.getChainAddress)(Number(chainId), "midnight");
298
- const encodedMarket = (0, viem_1.encodeAbiParameters)([marketParamsAbiParameter], [toStruct(params.market)]);
356
+ function toId(market) {
357
+ const marketParams = Market_js_1.MarketParams.from(market);
358
+ const encodedMarket = (0, viem_1.encodeAbiParameters)([marketParamsAbiParameter], [toStruct(marketParams)]);
299
359
  const creationHash = (0, viem_1.keccak256)((0, viem_1.concat)([SSTORE2_PREFIX, encodedMarket]));
300
- return (0, viem_1.keccak256)((0, viem_1.encodePacked)(["uint8", "address", "uint256", "bytes32"], [255, midnight, chainId, creationHash]));
360
+ return (0, viem_1.keccak256)((0, viem_1.encodePacked)(["uint8", "address", "uint256", "bytes32"], [255, marketParams.midnight, 0n, creationHash]));
301
361
  }
302
362
  MarketUtils.toId = toId;
303
363
  })(MarketUtils || (exports.MarketUtils = MarketUtils = {}));
@@ -125,14 +125,15 @@ export declare class Position {
125
125
  * collateral: [50n],
126
126
  * },
127
127
  * {
128
- * chainId: 31337,
129
128
  * params: {
129
+ * chainId: 31337,
130
+ * midnight: "0x0000000000000000000000000000000000001000",
130
131
  * loanToken: "0x0000000000000000000000000000000000006000",
131
132
  * collateralParams: [
132
133
  * {
133
134
  * token: "0x0000000000000000000000000000000000007000",
134
135
  * lltv: 770000000000000000n,
135
- * maxLif: 1061007957559681697n,
136
+ * liquidationCursor: 250000000000000000n,
136
137
  * oracle: "0x0000000000000000000000000000000000008000",
137
138
  * },
138
139
  * ],
@@ -156,7 +157,7 @@ export declare class Position {
156
157
  export declare class AccrualPosition extends Position {
157
158
  /** Hydrated market for this position. */
158
159
  readonly market: Market;
159
- constructor(position: IPosition, market: IMarket);
160
+ constructor(position: IPosition, market: IMarket | Market);
160
161
  /**
161
162
  * Returns the position collateral balance at an index configured by the market.
162
163
  *
@@ -192,14 +193,15 @@ export declare class AccrualPosition extends Position {
192
193
  * collateral: [50n],
193
194
  * },
194
195
  * {
195
- * chainId: 31337,
196
196
  * params: {
197
+ * chainId: 31337,
198
+ * midnight: "0x0000000000000000000000000000000000001000",
197
199
  * loanToken: "0x0000000000000000000000000000000000006000",
198
200
  * collateralParams: [
199
201
  * {
200
202
  * token: "0x0000000000000000000000000000000000007000",
201
203
  * lltv: 770000000000000000n,
202
- * maxLif: 1061007957559681697n,
204
+ * liquidationCursor: 250000000000000000n,
203
205
  * oracle: "0x0000000000000000000000000000000000008000",
204
206
  * },
205
207
  * ],
@@ -258,14 +260,15 @@ export declare class AccrualPosition extends Position {
258
260
  * collateral: [50n],
259
261
  * },
260
262
  * {
261
- * chainId: 31337,
262
263
  * params: {
264
+ * chainId: 31337,
265
+ * midnight: "0x0000000000000000000000000000000000001000",
263
266
  * loanToken: "0x0000000000000000000000000000000000006000",
264
267
  * collateralParams: [
265
268
  * {
266
269
  * token: collateralToken,
267
270
  * lltv: 770000000000000000n,
268
- * maxLif: 1061007957559681697n,
271
+ * liquidationCursor: 250000000000000000n,
269
272
  * oracle: "0x0000000000000000000000000000000000008000",
270
273
  * },
271
274
  * ],
@@ -323,14 +326,15 @@ export declare class AccrualPosition extends Position {
323
326
  * collateral: [50n],
324
327
  * },
325
328
  * {
326
- * chainId: 31337,
327
329
  * params: {
330
+ * chainId: 31337,
331
+ * midnight: "0x0000000000000000000000000000000000001000",
328
332
  * loanToken: "0x0000000000000000000000000000000000006000",
329
333
  * collateralParams: [
330
334
  * {
331
335
  * token: "0x0000000000000000000000000000000000007000",
332
336
  * lltv: 770000000000000000n,
333
- * maxLif: 1061007957559681697n,
337
+ * liquidationCursor: 250000000000000000n,
334
338
  * oracle: "0x0000000000000000000000000000000000008000",
335
339
  * },
336
340
  * ],
@@ -391,14 +395,15 @@ export declare class AccrualPosition extends Position {
391
395
  * collateral: [50n],
392
396
  * },
393
397
  * {
394
- * chainId: 31337,
395
398
  * params: {
399
+ * chainId: 31337,
400
+ * midnight: "0x0000000000000000000000000000000000001000",
396
401
  * loanToken: "0x0000000000000000000000000000000000006000",
397
402
  * collateralParams: [
398
403
  * {
399
404
  * token: "0x0000000000000000000000000000000000007000",
400
405
  * lltv: 770000000000000000n,
401
- * maxLif: 1061007957559681697n,
406
+ * liquidationCursor: 250000000000000000n,
402
407
  * oracle: "0x0000000000000000000000000000000000008000",
403
408
  * },
404
409
  * ],
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AccrualPosition = exports.Position = void 0;
4
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const errors_js_1 = require("../errors.js");
5
6
  const Market_js_1 = require("./Market.js");
6
7
  const PositionUtils_js_1 = require("./PositionUtils.js");
7
8
  /**
@@ -105,14 +106,15 @@ exports.Position = Position;
105
106
  * collateral: [50n],
106
107
  * },
107
108
  * {
108
- * chainId: 31337,
109
109
  * params: {
110
+ * chainId: 31337,
111
+ * midnight: "0x0000000000000000000000000000000000001000",
110
112
  * loanToken: "0x0000000000000000000000000000000000006000",
111
113
  * collateralParams: [
112
114
  * {
113
115
  * token: "0x0000000000000000000000000000000000007000",
114
116
  * lltv: 770000000000000000n,
115
- * maxLif: 1061007957559681697n,
117
+ * liquidationCursor: 250000000000000000n,
116
118
  * oracle: "0x0000000000000000000000000000000000008000",
117
119
  * },
118
120
  * ],
@@ -138,7 +140,7 @@ class AccrualPosition extends Position {
138
140
  market;
139
141
  constructor(position, market) {
140
142
  super(position);
141
- this.market = market instanceof Market_js_1.Market ? market : new Market_js_1.Market(market);
143
+ this.market = Market_js_1.Market.from(market);
142
144
  }
143
145
  /**
144
146
  * Returns the position collateral balance at an index configured by the market.
@@ -175,14 +177,15 @@ class AccrualPosition extends Position {
175
177
  * collateral: [50n],
176
178
  * },
177
179
  * {
178
- * chainId: 31337,
179
180
  * params: {
181
+ * chainId: 31337,
182
+ * midnight: "0x0000000000000000000000000000000000001000",
180
183
  * loanToken: "0x0000000000000000000000000000000000006000",
181
184
  * collateralParams: [
182
185
  * {
183
186
  * token: "0x0000000000000000000000000000000000007000",
184
187
  * lltv: 770000000000000000n,
185
- * maxLif: 1061007957559681697n,
188
+ * liquidationCursor: 250000000000000000n,
186
189
  * oracle: "0x0000000000000000000000000000000000008000",
187
190
  * },
188
191
  * ],
@@ -206,7 +209,7 @@ class AccrualPosition extends Position {
206
209
  */
207
210
  getCollateralBalanceByIndex(index) {
208
211
  const normalizedIndex = BigInt(index);
209
- if (this.market.getCollateralParamsByIndex(normalizedIndex) == null)
212
+ if ((0, morpho_ts_1._try)(() => this.market.getCollateralByIndex(normalizedIndex), errors_js_1.UnknownCollateralIndexError) == null)
210
213
  return undefined;
211
214
  return this.collateral[Number(normalizedIndex)] ?? 0n;
212
215
  }
@@ -246,14 +249,15 @@ class AccrualPosition extends Position {
246
249
  * collateral: [50n],
247
250
  * },
248
251
  * {
249
- * chainId: 31337,
250
252
  * params: {
253
+ * chainId: 31337,
254
+ * midnight: "0x0000000000000000000000000000000000001000",
251
255
  * loanToken: "0x0000000000000000000000000000000000006000",
252
256
  * collateralParams: [
253
257
  * {
254
258
  * token: collateralToken,
255
259
  * lltv: 770000000000000000n,
256
- * maxLif: 1061007957559681697n,
260
+ * liquidationCursor: 250000000000000000n,
257
261
  * oracle: "0x0000000000000000000000000000000000008000",
258
262
  * },
259
263
  * ],
@@ -314,14 +318,15 @@ class AccrualPosition extends Position {
314
318
  * collateral: [50n],
315
319
  * },
316
320
  * {
317
- * chainId: 31337,
318
321
  * params: {
322
+ * chainId: 31337,
323
+ * midnight: "0x0000000000000000000000000000000000001000",
319
324
  * loanToken: "0x0000000000000000000000000000000000006000",
320
325
  * collateralParams: [
321
326
  * {
322
327
  * token: "0x0000000000000000000000000000000000007000",
323
328
  * lltv: 770000000000000000n,
324
- * maxLif: 1061007957559681697n,
329
+ * liquidationCursor: 250000000000000000n,
325
330
  * oracle: "0x0000000000000000000000000000000000008000",
326
331
  * },
327
332
  * ],
@@ -384,14 +389,15 @@ class AccrualPosition extends Position {
384
389
  * collateral: [50n],
385
390
  * },
386
391
  * {
387
- * chainId: 31337,
388
392
  * params: {
393
+ * chainId: 31337,
394
+ * midnight: "0x0000000000000000000000000000000000001000",
389
395
  * loanToken: "0x0000000000000000000000000000000000006000",
390
396
  * collateralParams: [
391
397
  * {
392
398
  * token: "0x0000000000000000000000000000000000007000",
393
399
  * lltv: 770000000000000000n,
394
- * maxLif: 1061007957559681697n,
400
+ * liquidationCursor: 250000000000000000n,
395
401
  * oracle: "0x0000000000000000000000000000000000008000",
396
402
  * },
397
403
  * ],
@@ -19,14 +19,15 @@ import type { IPosition } from "./Position.js";
19
19
  * collateral: [50n],
20
20
  * },
21
21
  * market: {
22
- * chainId: 8453,
23
22
  * params: {
23
+ * chainId: 8453,
24
+ * midnight: "0x0000000000000000000000000000000000001000",
24
25
  * loanToken: "0x0000000000000000000000000000000000006000",
25
26
  * collateralParams: [
26
27
  * {
27
28
  * token: "0x0000000000000000000000000000000000007000",
28
29
  * lltv: 770000000000000000n,
29
- * maxLif: 1061007957559681697n,
30
+ * liquidationCursor: 250000000000000000n,
30
31
  * oracle: "0x0000000000000000000000000000000000008000",
31
32
  * },
32
33
  * ],
@@ -74,14 +75,15 @@ export declare namespace PositionUtils {
74
75
  * collateral: [50n],
75
76
  * },
76
77
  * market: {
77
- * chainId: 8453,
78
78
  * params: {
79
+ * chainId: 8453,
80
+ * midnight: "0x0000000000000000000000000000000000001000",
79
81
  * loanToken: "0x0000000000000000000000000000000000006000",
80
82
  * collateralParams: [
81
83
  * {
82
84
  * token: "0x0000000000000000000000000000000000007000",
83
85
  * lltv: 770000000000000000n,
84
- * maxLif: 1061007957559681697n,
86
+ * liquidationCursor: 250000000000000000n,
85
87
  * oracle: "0x0000000000000000000000000000000000008000",
86
88
  * },
87
89
  * ],
@@ -21,14 +21,15 @@ const errors_js_1 = require("../errors.js");
21
21
  * collateral: [50n],
22
22
  * },
23
23
  * market: {
24
- * chainId: 8453,
25
24
  * params: {
25
+ * chainId: 8453,
26
+ * midnight: "0x0000000000000000000000000000000000001000",
26
27
  * loanToken: "0x0000000000000000000000000000000000006000",
27
28
  * collateralParams: [
28
29
  * {
29
30
  * token: "0x0000000000000000000000000000000000007000",
30
31
  * lltv: 770000000000000000n,
31
- * maxLif: 1061007957559681697n,
32
+ * liquidationCursor: 250000000000000000n,
32
33
  * oracle: "0x0000000000000000000000000000000000008000",
33
34
  * },
34
35
  * ],
@@ -77,14 +78,15 @@ var PositionUtils;
77
78
  * collateral: [50n],
78
79
  * },
79
80
  * market: {
80
- * chainId: 8453,
81
81
  * params: {
82
+ * chainId: 8453,
83
+ * midnight: "0x0000000000000000000000000000000000001000",
82
84
  * loanToken: "0x0000000000000000000000000000000000006000",
83
85
  * collateralParams: [
84
86
  * {
85
87
  * token: "0x0000000000000000000000000000000000007000",
86
88
  * lltv: 770000000000000000n,
87
- * maxLif: 1061007957559681697n,
89
+ * liquidationCursor: 250000000000000000n,
88
90
  * oracle: "0x0000000000000000000000000000000000008000",
89
91
  * },
90
92
  * ],
@@ -146,10 +148,12 @@ var PositionUtils;
146
148
  const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
147
149
  token: collateralParam.token,
148
150
  lltv: BigInt(collateralParam.lltv),
149
- maxLif: BigInt(collateralParam.maxLif),
151
+ liquidationCursor: BigInt(collateralParam.liquidationCursor),
150
152
  oracle: collateralParam.oracle,
151
153
  }));
152
154
  const marketParams = {
155
+ chainId: BigInt(params.market.params.chainId),
156
+ midnight: params.market.params.midnight,
153
157
  loanToken: params.market.params.loanToken,
154
158
  collateralParams,
155
159
  maturity,
@@ -177,7 +181,6 @@ var PositionUtils;
177
181
  collateral,
178
182
  },
179
183
  market: {
180
- chainId: BigInt(params.market.chainId),
181
184
  params: marketParams,
182
185
  totalUnits: BigInt(params.market.totalUnits),
183
186
  lossFactor: marketLossFactor,
@@ -178,6 +178,28 @@ export declare namespace TickLib {
178
178
  * ```
179
179
  */
180
180
  function tickToRate(tick: BigIntish): bigint;
181
+ /**
182
+ * Converts a Midnight tick into a WAD simple annual percentage rate.
183
+ *
184
+ * This is an SDK-only rate display convenience. It annualizes the fixed
185
+ * period rate from {@link tickToRate} over `timeToMaturity` seconds.
186
+ *
187
+ * @param tick - Midnight tick.
188
+ * @param timeToMaturity - Seconds until the market matures.
189
+ * @returns WAD simple APR rounded up.
190
+ * @throws {NegativeValueError} when `tick` or `timeToMaturity` is negative.
191
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
192
+ * @throws {DivisionByZeroError} when the tick price or `timeToMaturity` is zero.
193
+ * @example
194
+ * ```ts
195
+ * import { TickLib } from "@morpho-org/midnight-sdk";
196
+ * import { Time } from "@morpho-org/morpho-ts";
197
+ *
198
+ * const apr = TickLib.tickToApr(6744n, Time.s.from.y(1n));
199
+ * console.log(apr);
200
+ * ```
201
+ */
202
+ function tickToApr(tick: BigIntish, timeToMaturity: BigIntish): bigint;
181
203
  /**
182
204
  * Asserts that a tick is aligned to the provided spacing.
183
205
  *
@@ -239,6 +239,36 @@ var TickLib;
239
239
  return morpho_ts_1.MathLib.mulDiv(morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD, price, "Up") - morpho_ts_1.MathLib.WAD;
240
240
  }
241
241
  TickLib.tickToRate = tickToRate;
242
+ /**
243
+ * Converts a Midnight tick into a WAD simple annual percentage rate.
244
+ *
245
+ * This is an SDK-only rate display convenience. It annualizes the fixed
246
+ * period rate from {@link tickToRate} over `timeToMaturity` seconds.
247
+ *
248
+ * @param tick - Midnight tick.
249
+ * @param timeToMaturity - Seconds until the market matures.
250
+ * @returns WAD simple APR rounded up.
251
+ * @throws {NegativeValueError} when `tick` or `timeToMaturity` is negative.
252
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
253
+ * @throws {DivisionByZeroError} when the tick price or `timeToMaturity` is zero.
254
+ * @example
255
+ * ```ts
256
+ * import { TickLib } from "@morpho-org/midnight-sdk";
257
+ * import { Time } from "@morpho-org/morpho-ts";
258
+ *
259
+ * const apr = TickLib.tickToApr(6744n, Time.s.from.y(1n));
260
+ * console.log(apr);
261
+ * ```
262
+ */
263
+ function tickToApr(tick, timeToMaturity) {
264
+ const normalizedTimeToMaturity = BigInt(timeToMaturity);
265
+ (0, morpho_ts_1.assertNonNegative)("timeToMaturity", normalizedTimeToMaturity);
266
+ if (normalizedTimeToMaturity === 0n) {
267
+ throw new morpho_ts_1.DivisionByZeroError("timeToMaturity");
268
+ }
269
+ return morpho_ts_1.MathLib.mulDiv(tickToRate(tick), morpho_ts_1.Time.s.from.y(1n), normalizedTimeToMaturity, "Up");
270
+ }
271
+ TickLib.tickToApr = tickToApr;
242
272
  /**
243
273
  * Asserts that a tick is aligned to the provided spacing.
244
274
  *
@@ -1,3 +1,2 @@
1
- export * from "./ConsumableUnitsLib.js";
2
1
  export * from "./TakeAmountsLib.js";
3
2
  export * from "./TickLib.js";
@@ -14,6 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./ConsumableUnitsLib.js"), exports);
18
17
  __exportStar(require("./TakeAmountsLib.js"), exports);
19
18
  __exportStar(require("./TickLib.js"), exports);