@morpho-org/midnight-sdk 0.1.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 (157) hide show
  1. package/README.md +252 -0
  2. package/lib/cjs/abis.d.ts +3170 -0
  3. package/lib/cjs/abis.js +4073 -0
  4. package/lib/cjs/api/MidnightApi.d.ts +532 -0
  5. package/lib/cjs/api/MidnightApi.js +713 -0
  6. package/lib/cjs/api/helpers.d.ts +20 -0
  7. package/lib/cjs/api/helpers.js +189 -0
  8. package/lib/cjs/api/index.d.ts +2 -0
  9. package/lib/cjs/api/index.js +21 -0
  10. package/lib/cjs/api/types.d.ts +453 -0
  11. package/lib/cjs/api/types.js +2 -0
  12. package/lib/cjs/constants.d.ts +168 -0
  13. package/lib/cjs/constants.js +187 -0
  14. package/lib/cjs/errors.d.ts +253 -0
  15. package/lib/cjs/errors.js +309 -0
  16. package/lib/cjs/fetch/Market.d.ts +65 -0
  17. package/lib/cjs/fetch/Market.js +117 -0
  18. package/lib/cjs/fetch/Offer.d.ts +74 -0
  19. package/lib/cjs/fetch/Offer.js +119 -0
  20. package/lib/cjs/fetch/Position.d.ts +85 -0
  21. package/lib/cjs/fetch/Position.js +148 -0
  22. package/lib/cjs/fetch/Ratifier.d.ts +38 -0
  23. package/lib/cjs/fetch/Ratifier.js +57 -0
  24. package/lib/cjs/fetch/index.d.ts +5 -0
  25. package/lib/cjs/fetch/index.js +21 -0
  26. package/lib/cjs/fetch/types.d.ts +57 -0
  27. package/lib/cjs/fetch/types.js +2 -0
  28. package/lib/cjs/fetch/utils.d.ts +8 -0
  29. package/lib/cjs/fetch/utils.js +18 -0
  30. package/lib/cjs/index.d.ts +18 -0
  31. package/lib/cjs/index.js +57 -0
  32. package/lib/cjs/market/Market.d.ts +613 -0
  33. package/lib/cjs/market/Market.js +503 -0
  34. package/lib/cjs/market/MarketUtils.d.ts +189 -0
  35. package/lib/cjs/market/MarketUtils.js +303 -0
  36. package/lib/cjs/market/Position.d.ts +424 -0
  37. package/lib/cjs/market/Position.js +425 -0
  38. package/lib/cjs/market/PositionUtils.d.ts +115 -0
  39. package/lib/cjs/market/PositionUtils.js +194 -0
  40. package/lib/cjs/market/index.d.ts +4 -0
  41. package/lib/cjs/market/index.js +20 -0
  42. package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
  43. package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
  44. package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
  45. package/lib/cjs/math/TakeAmountsLib.js +199 -0
  46. package/lib/cjs/math/TickLib.d.ts +200 -0
  47. package/lib/cjs/math/TickLib.js +272 -0
  48. package/lib/cjs/math/index.d.ts +3 -0
  49. package/lib/cjs/math/index.js +19 -0
  50. package/lib/cjs/offers/Offer.d.ts +534 -0
  51. package/lib/cjs/offers/Offer.js +343 -0
  52. package/lib/cjs/offers/OfferUtils.d.ts +565 -0
  53. package/lib/cjs/offers/OfferUtils.js +659 -0
  54. package/lib/cjs/offers/index.d.ts +2 -0
  55. package/lib/cjs/offers/index.js +18 -0
  56. package/lib/cjs/package.json +1 -0
  57. package/lib/cjs/queries/GetPosition.d.ts +55 -0
  58. package/lib/cjs/queries/GetPosition.js +74 -0
  59. package/lib/cjs/queries/index.d.ts +1 -0
  60. package/lib/cjs/queries/index.js +37 -0
  61. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  62. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
  63. package/lib/cjs/signatures/Group.d.ts +178 -0
  64. package/lib/cjs/signatures/Group.js +193 -0
  65. package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
  66. package/lib/cjs/signatures/GroupUtils.js +127 -0
  67. package/lib/cjs/signatures/Payload.d.ts +283 -0
  68. package/lib/cjs/signatures/Payload.js +570 -0
  69. package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
  70. package/lib/cjs/signatures/RatifierUtils.js +76 -0
  71. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
  72. package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
  73. package/lib/cjs/signatures/Tree.d.ts +241 -0
  74. package/lib/cjs/signatures/Tree.js +263 -0
  75. package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
  76. package/lib/cjs/signatures/TreeUtils.js +461 -0
  77. package/lib/cjs/signatures/index.d.ts +7 -0
  78. package/lib/cjs/signatures/index.js +23 -0
  79. package/lib/esm/abis.d.ts +3170 -0
  80. package/lib/esm/abis.js +4070 -0
  81. package/lib/esm/api/MidnightApi.d.ts +532 -0
  82. package/lib/esm/api/MidnightApi.js +709 -0
  83. package/lib/esm/api/helpers.d.ts +20 -0
  84. package/lib/esm/api/helpers.js +180 -0
  85. package/lib/esm/api/index.d.ts +2 -0
  86. package/lib/esm/api/index.js +2 -0
  87. package/lib/esm/api/types.d.ts +453 -0
  88. package/lib/esm/api/types.js +1 -0
  89. package/lib/esm/constants.d.ts +168 -0
  90. package/lib/esm/constants.js +184 -0
  91. package/lib/esm/errors.d.ts +253 -0
  92. package/lib/esm/errors.js +291 -0
  93. package/lib/esm/fetch/Market.d.ts +65 -0
  94. package/lib/esm/fetch/Market.js +113 -0
  95. package/lib/esm/fetch/Offer.d.ts +74 -0
  96. package/lib/esm/fetch/Offer.js +116 -0
  97. package/lib/esm/fetch/Position.d.ts +85 -0
  98. package/lib/esm/fetch/Position.js +144 -0
  99. package/lib/esm/fetch/Ratifier.d.ts +38 -0
  100. package/lib/esm/fetch/Ratifier.js +54 -0
  101. package/lib/esm/fetch/index.d.ts +5 -0
  102. package/lib/esm/fetch/index.js +5 -0
  103. package/lib/esm/fetch/types.d.ts +57 -0
  104. package/lib/esm/fetch/types.js +1 -0
  105. package/lib/esm/fetch/utils.d.ts +8 -0
  106. package/lib/esm/fetch/utils.js +12 -0
  107. package/lib/esm/index.d.ts +18 -0
  108. package/lib/esm/index.js +18 -0
  109. package/lib/esm/market/Market.d.ts +613 -0
  110. package/lib/esm/market/Market.js +498 -0
  111. package/lib/esm/market/MarketUtils.d.ts +189 -0
  112. package/lib/esm/market/MarketUtils.js +300 -0
  113. package/lib/esm/market/Position.d.ts +424 -0
  114. package/lib/esm/market/Position.js +420 -0
  115. package/lib/esm/market/PositionUtils.d.ts +115 -0
  116. package/lib/esm/market/PositionUtils.js +191 -0
  117. package/lib/esm/market/index.d.ts +4 -0
  118. package/lib/esm/market/index.js +4 -0
  119. package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
  120. package/lib/esm/math/ConsumableUnitsLib.js +74 -0
  121. package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
  122. package/lib/esm/math/TakeAmountsLib.js +196 -0
  123. package/lib/esm/math/TickLib.d.ts +200 -0
  124. package/lib/esm/math/TickLib.js +269 -0
  125. package/lib/esm/math/index.d.ts +3 -0
  126. package/lib/esm/math/index.js +3 -0
  127. package/lib/esm/offers/Offer.d.ts +534 -0
  128. package/lib/esm/offers/Offer.js +339 -0
  129. package/lib/esm/offers/OfferUtils.d.ts +565 -0
  130. package/lib/esm/offers/OfferUtils.js +656 -0
  131. package/lib/esm/offers/index.d.ts +2 -0
  132. package/lib/esm/offers/index.js +2 -0
  133. package/lib/esm/package.json +1 -0
  134. package/lib/esm/queries/GetPosition.d.ts +55 -0
  135. package/lib/esm/queries/GetPosition.js +71 -0
  136. package/lib/esm/queries/index.d.ts +1 -0
  137. package/lib/esm/queries/index.js +1 -0
  138. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  139. package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
  140. package/lib/esm/signatures/Group.d.ts +178 -0
  141. package/lib/esm/signatures/Group.js +189 -0
  142. package/lib/esm/signatures/GroupUtils.d.ts +191 -0
  143. package/lib/esm/signatures/GroupUtils.js +124 -0
  144. package/lib/esm/signatures/Payload.d.ts +283 -0
  145. package/lib/esm/signatures/Payload.js +564 -0
  146. package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
  147. package/lib/esm/signatures/RatifierUtils.js +73 -0
  148. package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
  149. package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
  150. package/lib/esm/signatures/Tree.d.ts +241 -0
  151. package/lib/esm/signatures/Tree.js +259 -0
  152. package/lib/esm/signatures/TreeUtils.d.ts +573 -0
  153. package/lib/esm/signatures/TreeUtils.js +458 -0
  154. package/lib/esm/signatures/index.d.ts +7 -0
  155. package/lib/esm/signatures/index.js +7 -0
  156. package/package.json +69 -0
  157. package/src/index.ts +19 -0
@@ -0,0 +1,420 @@
1
+ import { MathLib } from "@morpho-org/morpho-ts";
2
+ import { Market } from "./Market.js";
3
+ import { PositionUtils } from "./PositionUtils.js";
4
+ /**
5
+ * Midnight user position as stored by the core contract.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { Position } from "@morpho-org/midnight-sdk";
10
+ *
11
+ * const position = new Position({
12
+ * credit: 0n,
13
+ * pendingFee: 0n,
14
+ * lastLossFactor: 0n,
15
+ * lastAccrual: 0n,
16
+ * debt: 0n,
17
+ * collateralBitmap: 0n,
18
+ * collateral: [],
19
+ * });
20
+ * console.log(position.debt);
21
+ * ```
22
+ */
23
+ export class Position {
24
+ /** User credit. */
25
+ credit;
26
+ /** Pending continuous fee. */
27
+ pendingFee;
28
+ /** Last loss factor seen by the position. */
29
+ lastLossFactor;
30
+ /** Last accrual timestamp. */
31
+ lastAccrual;
32
+ /** User debt. */
33
+ debt;
34
+ /** Collateral bitmap. */
35
+ collateralBitmap;
36
+ /** Collateral balances by index. */
37
+ collateral;
38
+ constructor(position) {
39
+ this.credit = BigInt(position.credit);
40
+ this.pendingFee = BigInt(position.pendingFee);
41
+ this.lastLossFactor = BigInt(position.lastLossFactor);
42
+ this.lastAccrual = BigInt(position.lastAccrual);
43
+ this.debt = BigInt(position.debt);
44
+ this.collateralBitmap = BigInt(position.collateralBitmap);
45
+ this.collateral = position.collateral.map((assets) => BigInt(assets));
46
+ }
47
+ /**
48
+ * Returns credit net of pending fee, floored to zero for invalid raw inputs.
49
+ *
50
+ * @returns Credit minus pending fee.
51
+ * @example
52
+ * ```ts
53
+ * import { Position } from "@morpho-org/midnight-sdk";
54
+ *
55
+ * const position = new Position({
56
+ * credit: 1_000n,
57
+ * pendingFee: 100n,
58
+ * lastLossFactor: 0n,
59
+ * lastAccrual: 0n,
60
+ * debt: 0n,
61
+ * collateralBitmap: 1n,
62
+ * collateral: [50n],
63
+ * });
64
+ * console.log(position.faceValue);
65
+ * ```
66
+ */
67
+ get faceValue() {
68
+ return MathLib.zeroFloorSub(this.credit, this.pendingFee);
69
+ }
70
+ }
71
+ /**
72
+ * Midnight position paired with its hydrated market.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * import { registerCustomAddresses } from "@morpho-org/morpho-ts";
77
+ * import { AccrualPosition } from "@morpho-org/midnight-sdk";
78
+ *
79
+ * registerCustomAddresses({
80
+ * addresses: {
81
+ * 31337: {
82
+ * morpho: "0x0000000000000000000000000000000000000001",
83
+ * bundler3: {
84
+ * bundler3: "0x0000000000000000000000000000000000000002",
85
+ * generalAdapter1: "0x0000000000000000000000000000000000000003",
86
+ * },
87
+ * adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
88
+ * midnight: "0x0000000000000000000000000000000000001000",
89
+ * },
90
+ * },
91
+ * });
92
+ *
93
+ * const position = new AccrualPosition(
94
+ * {
95
+ * credit: 1_000n,
96
+ * pendingFee: 100n,
97
+ * lastLossFactor: 0n,
98
+ * lastAccrual: 1_000n,
99
+ * debt: 0n,
100
+ * collateralBitmap: 1n,
101
+ * collateral: [50n],
102
+ * },
103
+ * {
104
+ * chainId: 31337,
105
+ * params: {
106
+ * loanToken: "0x0000000000000000000000000000000000006000",
107
+ * collateralParams: [
108
+ * {
109
+ * token: "0x0000000000000000000000000000000000007000",
110
+ * lltv: 770000000000000000n,
111
+ * maxLif: 1061007957559681697n,
112
+ * oracle: "0x0000000000000000000000000000000000008000",
113
+ * },
114
+ * ],
115
+ * maturity: 54_000n,
116
+ * rcfThreshold: 0n,
117
+ * enterGate: "0x0000000000000000000000000000000000000000",
118
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
119
+ * },
120
+ * totalUnits: 1_000n,
121
+ * lossFactor: 0n,
122
+ * withdrawable: 500n,
123
+ * continuousFeeCredit: 0n,
124
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
125
+ * continuousFee: 10,
126
+ * tickSpacing: 4,
127
+ * },
128
+ * );
129
+ * console.log(position.market.id);
130
+ * ```
131
+ */
132
+ export class AccrualPosition extends Position {
133
+ /** Hydrated market for this position. */
134
+ market;
135
+ constructor(position, market) {
136
+ super(position);
137
+ this.market = market instanceof Market ? market : new Market(market);
138
+ }
139
+ /**
140
+ * Returns the position collateral balance at an index configured by the market.
141
+ *
142
+ * @param index - Collateral index.
143
+ * @returns Collateral balance, or undefined when the index is not configured.
144
+ * @example
145
+ * ```ts
146
+ * import { registerCustomAddresses } from "@morpho-org/morpho-ts";
147
+ * import { AccrualPosition } from "@morpho-org/midnight-sdk";
148
+ *
149
+ * registerCustomAddresses({
150
+ * addresses: {
151
+ * 31337: {
152
+ * morpho: "0x0000000000000000000000000000000000000001",
153
+ * bundler3: {
154
+ * bundler3: "0x0000000000000000000000000000000000000002",
155
+ * generalAdapter1: "0x0000000000000000000000000000000000000003",
156
+ * },
157
+ * adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
158
+ * midnight: "0x0000000000000000000000000000000000001000",
159
+ * },
160
+ * },
161
+ * });
162
+ *
163
+ * const position = new AccrualPosition(
164
+ * {
165
+ * credit: 1_000n,
166
+ * pendingFee: 100n,
167
+ * lastLossFactor: 0n,
168
+ * lastAccrual: 1_000n,
169
+ * debt: 0n,
170
+ * collateralBitmap: 1n,
171
+ * collateral: [50n],
172
+ * },
173
+ * {
174
+ * chainId: 31337,
175
+ * params: {
176
+ * loanToken: "0x0000000000000000000000000000000000006000",
177
+ * collateralParams: [
178
+ * {
179
+ * token: "0x0000000000000000000000000000000000007000",
180
+ * lltv: 770000000000000000n,
181
+ * maxLif: 1061007957559681697n,
182
+ * oracle: "0x0000000000000000000000000000000000008000",
183
+ * },
184
+ * ],
185
+ * maturity: 54_000n,
186
+ * rcfThreshold: 0n,
187
+ * enterGate: "0x0000000000000000000000000000000000000000",
188
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
189
+ * },
190
+ * totalUnits: 1_000n,
191
+ * lossFactor: 0n,
192
+ * withdrawable: 500n,
193
+ * continuousFeeCredit: 0n,
194
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
195
+ * continuousFee: 10,
196
+ * tickSpacing: 4,
197
+ * },
198
+ * );
199
+ * const balance = position.getCollateralBalanceByIndex(0);
200
+ * console.log(balance);
201
+ * ```
202
+ */
203
+ getCollateralBalanceByIndex(index) {
204
+ const normalizedIndex = BigInt(index);
205
+ if (this.market.getCollateralParamsByIndex(normalizedIndex) == null)
206
+ return undefined;
207
+ return this.collateral[Number(normalizedIndex)] ?? 0n;
208
+ }
209
+ /**
210
+ * Returns the position collateral balance for a configured token.
211
+ *
212
+ * @param token - Collateral token address.
213
+ * @returns Collateral balance, or undefined when the token is not configured.
214
+ * @example
215
+ * ```ts
216
+ * import { registerCustomAddresses } from "@morpho-org/morpho-ts";
217
+ * import { AccrualPosition } from "@morpho-org/midnight-sdk";
218
+ *
219
+ * registerCustomAddresses({
220
+ * addresses: {
221
+ * 31337: {
222
+ * morpho: "0x0000000000000000000000000000000000000001",
223
+ * bundler3: {
224
+ * bundler3: "0x0000000000000000000000000000000000000002",
225
+ * generalAdapter1: "0x0000000000000000000000000000000000000003",
226
+ * },
227
+ * adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
228
+ * midnight: "0x0000000000000000000000000000000000001000",
229
+ * },
230
+ * },
231
+ * });
232
+ *
233
+ * const collateralToken = "0x0000000000000000000000000000000000007000";
234
+ * const position = new AccrualPosition(
235
+ * {
236
+ * credit: 1_000n,
237
+ * pendingFee: 100n,
238
+ * lastLossFactor: 0n,
239
+ * lastAccrual: 1_000n,
240
+ * debt: 0n,
241
+ * collateralBitmap: 1n,
242
+ * collateral: [50n],
243
+ * },
244
+ * {
245
+ * chainId: 31337,
246
+ * params: {
247
+ * loanToken: "0x0000000000000000000000000000000000006000",
248
+ * collateralParams: [
249
+ * {
250
+ * token: collateralToken,
251
+ * lltv: 770000000000000000n,
252
+ * maxLif: 1061007957559681697n,
253
+ * oracle: "0x0000000000000000000000000000000000008000",
254
+ * },
255
+ * ],
256
+ * maturity: 54_000n,
257
+ * rcfThreshold: 0n,
258
+ * enterGate: "0x0000000000000000000000000000000000000000",
259
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
260
+ * },
261
+ * totalUnits: 1_000n,
262
+ * lossFactor: 0n,
263
+ * withdrawable: 500n,
264
+ * continuousFeeCredit: 0n,
265
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
266
+ * continuousFee: 10,
267
+ * tickSpacing: 4,
268
+ * },
269
+ * );
270
+ * const balance = position.getCollateralBalanceByToken(collateralToken);
271
+ * console.log(balance);
272
+ * ```
273
+ */
274
+ getCollateralBalanceByToken(token) {
275
+ const index = this.market.getCollateralIndexByToken(token);
276
+ return index == null ? undefined : this.getCollateralBalanceByIndex(index);
277
+ }
278
+ /**
279
+ * Returns the market settlement fee at a timestamp.
280
+ *
281
+ * @param timestamp - Timestamp used to compute time to maturity.
282
+ * @returns WAD-scaled settlement fee.
283
+ * @example
284
+ * ```ts
285
+ * import { registerCustomAddresses } from "@morpho-org/morpho-ts";
286
+ * import { AccrualPosition } from "@morpho-org/midnight-sdk";
287
+ *
288
+ * registerCustomAddresses({
289
+ * addresses: {
290
+ * 31337: {
291
+ * morpho: "0x0000000000000000000000000000000000000001",
292
+ * bundler3: {
293
+ * bundler3: "0x0000000000000000000000000000000000000002",
294
+ * generalAdapter1: "0x0000000000000000000000000000000000000003",
295
+ * },
296
+ * adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
297
+ * midnight: "0x0000000000000000000000000000000000001000",
298
+ * },
299
+ * },
300
+ * });
301
+ *
302
+ * const position = new AccrualPosition(
303
+ * {
304
+ * credit: 1_000n,
305
+ * pendingFee: 100n,
306
+ * lastLossFactor: 0n,
307
+ * lastAccrual: 1_000n,
308
+ * debt: 0n,
309
+ * collateralBitmap: 1n,
310
+ * collateral: [50n],
311
+ * },
312
+ * {
313
+ * chainId: 31337,
314
+ * params: {
315
+ * loanToken: "0x0000000000000000000000000000000000006000",
316
+ * collateralParams: [
317
+ * {
318
+ * token: "0x0000000000000000000000000000000000007000",
319
+ * lltv: 770000000000000000n,
320
+ * maxLif: 1061007957559681697n,
321
+ * oracle: "0x0000000000000000000000000000000000008000",
322
+ * },
323
+ * ],
324
+ * maturity: 54_000n,
325
+ * rcfThreshold: 0n,
326
+ * enterGate: "0x0000000000000000000000000000000000000000",
327
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
328
+ * },
329
+ * totalUnits: 1_000n,
330
+ * lossFactor: 0n,
331
+ * withdrawable: 500n,
332
+ * continuousFeeCredit: 0n,
333
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
334
+ * continuousFee: 10,
335
+ * tickSpacing: 4,
336
+ * },
337
+ * );
338
+ * const fee = position.getSettlementFee(0n);
339
+ * console.log(fee);
340
+ * ```
341
+ */
342
+ getSettlementFee(timestamp) {
343
+ return this.market.getSettlementFee(this.market.timeToMaturity(timestamp));
344
+ }
345
+ /**
346
+ * Returns a new position locally accrued like Midnight `updatePositionView`.
347
+ *
348
+ * @param timestamp - Timestamp at which to accrue. Must be greater than or equal to `lastAccrual`.
349
+ * @returns New accrual position with updated credit, pending fee, last loss factor, last accrual, and market continuous-fee credit.
350
+ * @throws {InvalidPositionAccrualTimestampError} when timestamp is before `lastAccrual`.
351
+ * @throws {InvalidPositionLossFactorError} when the market loss factor is older than the position loss factor.
352
+ * @throws {InvalidPositionAccrualStateError} when raw inputs violate Midnight accounting invariants.
353
+ * @example
354
+ * ```ts
355
+ * import { registerCustomAddresses } from "@morpho-org/morpho-ts";
356
+ * import { AccrualPosition } from "@morpho-org/midnight-sdk";
357
+ *
358
+ * registerCustomAddresses({
359
+ * addresses: {
360
+ * 31337: {
361
+ * morpho: "0x0000000000000000000000000000000000000001",
362
+ * bundler3: {
363
+ * bundler3: "0x0000000000000000000000000000000000000002",
364
+ * generalAdapter1: "0x0000000000000000000000000000000000000003",
365
+ * },
366
+ * adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
367
+ * midnight: "0x0000000000000000000000000000000000001000",
368
+ * },
369
+ * },
370
+ * });
371
+ *
372
+ * const position = new AccrualPosition(
373
+ * {
374
+ * credit: 1_000n,
375
+ * pendingFee: 100n,
376
+ * lastLossFactor: 0n,
377
+ * lastAccrual: 1_000n,
378
+ * debt: 0n,
379
+ * collateralBitmap: 1n,
380
+ * collateral: [50n],
381
+ * },
382
+ * {
383
+ * chainId: 31337,
384
+ * params: {
385
+ * loanToken: "0x0000000000000000000000000000000000006000",
386
+ * collateralParams: [
387
+ * {
388
+ * token: "0x0000000000000000000000000000000000007000",
389
+ * lltv: 770000000000000000n,
390
+ * maxLif: 1061007957559681697n,
391
+ * oracle: "0x0000000000000000000000000000000000008000",
392
+ * },
393
+ * ],
394
+ * maturity: 54_000n,
395
+ * rcfThreshold: 0n,
396
+ * enterGate: "0x0000000000000000000000000000000000000000",
397
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
398
+ * },
399
+ * totalUnits: 1_000n,
400
+ * lossFactor: 0n,
401
+ * withdrawable: 500n,
402
+ * continuousFeeCredit: 0n,
403
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
404
+ * continuousFee: 10,
405
+ * tickSpacing: 4,
406
+ * },
407
+ * );
408
+ * const accrued = position.accrueInterest(1_500n);
409
+ * console.log(accrued.credit);
410
+ * ```
411
+ */
412
+ accrueInterest(timestamp) {
413
+ const { position, market } = PositionUtils.accrueInterest({
414
+ position: this,
415
+ market: this.market,
416
+ timestamp,
417
+ });
418
+ return new AccrualPosition(position, market);
419
+ }
420
+ }
@@ -0,0 +1,115 @@
1
+ import { type BigIntish } from "@morpho-org/morpho-ts";
2
+ import type { IMarket } from "./Market.js";
3
+ import type { IPosition } from "./Position.js";
4
+ /**
5
+ * Domain helpers for Midnight positions.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { PositionUtils } from "@morpho-org/midnight-sdk";
10
+ *
11
+ * const accrued = PositionUtils.accrueInterest({
12
+ * position: {
13
+ * credit: 1_000n,
14
+ * pendingFee: 100n,
15
+ * lastLossFactor: 0n,
16
+ * lastAccrual: 1_000n,
17
+ * debt: 0n,
18
+ * collateralBitmap: 1n,
19
+ * collateral: [50n],
20
+ * },
21
+ * market: {
22
+ * chainId: 8453,
23
+ * params: {
24
+ * loanToken: "0x0000000000000000000000000000000000006000",
25
+ * collateralParams: [
26
+ * {
27
+ * token: "0x0000000000000000000000000000000000007000",
28
+ * lltv: 770000000000000000n,
29
+ * maxLif: 1061007957559681697n,
30
+ * oracle: "0x0000000000000000000000000000000000008000",
31
+ * },
32
+ * ],
33
+ * maturity: 54_000n,
34
+ * rcfThreshold: 0n,
35
+ * enterGate: "0x0000000000000000000000000000000000000000",
36
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
37
+ * },
38
+ * totalUnits: 1_000n,
39
+ * lossFactor: 0n,
40
+ * withdrawable: 500n,
41
+ * continuousFeeCredit: 0n,
42
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
43
+ * continuousFee: 10,
44
+ * tickSpacing: 4,
45
+ * },
46
+ * timestamp: 1_500n,
47
+ * });
48
+ * console.log(accrued.position.credit);
49
+ * ```
50
+ */
51
+ export declare namespace PositionUtils {
52
+ /**
53
+ * Returns plain Midnight position and market objects accrued like Midnight `updatePositionView`.
54
+ *
55
+ * @param params.position - Position state to accrue.
56
+ * @param params.market - Hydrated market state used for loss factor and continuous-fee accrual.
57
+ * @param params.timestamp - Timestamp at which to accrue.
58
+ * @returns New plain position and market objects with updated credit, pending fee, last loss factor, last accrual, and market continuous-fee credit.
59
+ * @throws {InvalidPositionAccrualTimestampError} when timestamp is before `lastAccrual`.
60
+ * @throws {InvalidPositionLossFactorError} when the market loss factor is older than the position loss factor.
61
+ * @throws {InvalidPositionAccrualStateError} when raw inputs violate Midnight accounting invariants.
62
+ * @example
63
+ * ```ts
64
+ * import { PositionUtils } from "@morpho-org/midnight-sdk";
65
+ *
66
+ * const { position, market } = PositionUtils.accrueInterest({
67
+ * position: {
68
+ * credit: 1_000n,
69
+ * pendingFee: 100n,
70
+ * lastLossFactor: 0n,
71
+ * lastAccrual: 1_000n,
72
+ * debt: 0n,
73
+ * collateralBitmap: 1n,
74
+ * collateral: [50n],
75
+ * },
76
+ * market: {
77
+ * chainId: 8453,
78
+ * params: {
79
+ * loanToken: "0x0000000000000000000000000000000000006000",
80
+ * collateralParams: [
81
+ * {
82
+ * token: "0x0000000000000000000000000000000000007000",
83
+ * lltv: 770000000000000000n,
84
+ * maxLif: 1061007957559681697n,
85
+ * oracle: "0x0000000000000000000000000000000000008000",
86
+ * },
87
+ * ],
88
+ * maturity: 54_000n,
89
+ * rcfThreshold: 0n,
90
+ * enterGate: "0x0000000000000000000000000000000000000000",
91
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
92
+ * },
93
+ * totalUnits: 1_000n,
94
+ * lossFactor: 0n,
95
+ * withdrawable: 500n,
96
+ * continuousFeeCredit: 0n,
97
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
98
+ * continuousFee: 10,
99
+ * tickSpacing: 4,
100
+ * },
101
+ * timestamp: 1_500n,
102
+ * });
103
+ * console.log(position.lastAccrual, market.continuousFeeCredit);
104
+ * ```
105
+ */
106
+ function accrueInterest(params: {
107
+ readonly position: IPosition;
108
+ readonly market: IMarket;
109
+ readonly timestamp: BigIntish;
110
+ }): {
111
+ readonly position: IPosition;
112
+ readonly market: IMarket;
113
+ readonly accruedFee: bigint;
114
+ };
115
+ }