@morpho-org/midnight-sdk 1.0.0 → 1.2.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 (86) hide show
  1. package/lib/cjs/api/MidnightApi.js +67 -3
  2. package/lib/cjs/api/helpers.d.ts +11 -1
  3. package/lib/cjs/api/helpers.js +48 -1
  4. package/lib/cjs/errors.d.ts +15 -0
  5. package/lib/cjs/errors.js +21 -1
  6. package/lib/cjs/fetch/Market.js +37 -2
  7. package/lib/cjs/fetch/Position.d.ts +1 -1
  8. package/lib/cjs/fetch/Position.js +5 -1
  9. package/lib/cjs/fetch/utils.d.ts +1 -1
  10. package/lib/cjs/fetch/utils.js +9 -2
  11. package/lib/cjs/index.d.ts +1 -0
  12. package/lib/cjs/index.js +1 -0
  13. package/lib/cjs/market/index.d.ts +0 -2
  14. package/lib/cjs/market/index.js +0 -2
  15. package/lib/cjs/offers/Offer.d.ts +1 -1
  16. package/lib/cjs/offers/OfferChainUtils.d.ts +179 -0
  17. package/lib/cjs/offers/OfferChainUtils.js +446 -0
  18. package/lib/cjs/offers/OfferUtils.d.ts +5 -5
  19. package/lib/cjs/offers/OfferUtils.js +26 -13
  20. package/lib/cjs/offers/index.d.ts +1 -0
  21. package/lib/cjs/offers/index.js +1 -0
  22. package/lib/cjs/{market → position}/Position.d.ts +83 -6
  23. package/lib/cjs/{market → position}/Position.js +83 -5
  24. package/lib/{esm/market → cjs/position}/PositionUtils.d.ts +26 -1
  25. package/lib/cjs/{market → position}/PositionUtils.js +27 -0
  26. package/lib/cjs/position/index.d.ts +2 -0
  27. package/lib/cjs/position/index.js +18 -0
  28. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +118 -0
  29. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +149 -1
  30. package/lib/cjs/signatures/Group.js +5 -3
  31. package/lib/cjs/signatures/GroupUtils.d.ts +48 -0
  32. package/lib/cjs/signatures/GroupUtils.js +51 -0
  33. package/lib/cjs/signatures/RatifierUtils.js +15 -3
  34. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +31 -0
  35. package/lib/cjs/signatures/SetterRatifierUtils.js +38 -0
  36. package/lib/cjs/signatures/Tree.js +13 -3
  37. package/lib/cjs/signatures/TreeUtils.d.ts +6 -3
  38. package/lib/cjs/signatures/TreeUtils.js +35 -13
  39. package/lib/cjs/signatures/eip712.d.ts +22 -0
  40. package/lib/cjs/signatures/eip712.js +26 -0
  41. package/lib/cjs/signatures/index.d.ts +1 -0
  42. package/lib/cjs/signatures/index.js +1 -0
  43. package/lib/esm/api/MidnightApi.js +68 -4
  44. package/lib/esm/api/helpers.d.ts +11 -1
  45. package/lib/esm/api/helpers.js +48 -2
  46. package/lib/esm/errors.d.ts +15 -0
  47. package/lib/esm/errors.js +19 -0
  48. package/lib/esm/fetch/Market.js +38 -3
  49. package/lib/esm/fetch/Position.d.ts +1 -1
  50. package/lib/esm/fetch/Position.js +5 -1
  51. package/lib/esm/fetch/utils.d.ts +1 -1
  52. package/lib/esm/fetch/utils.js +9 -2
  53. package/lib/esm/index.d.ts +1 -0
  54. package/lib/esm/index.js +1 -0
  55. package/lib/esm/market/index.d.ts +0 -2
  56. package/lib/esm/market/index.js +0 -2
  57. package/lib/esm/offers/Offer.d.ts +1 -1
  58. package/lib/esm/offers/OfferChainUtils.d.ts +179 -0
  59. package/lib/esm/offers/OfferChainUtils.js +443 -0
  60. package/lib/esm/offers/OfferUtils.d.ts +5 -5
  61. package/lib/esm/offers/OfferUtils.js +28 -15
  62. package/lib/esm/offers/index.d.ts +1 -0
  63. package/lib/esm/offers/index.js +1 -0
  64. package/lib/esm/{market → position}/Position.d.ts +83 -6
  65. package/lib/esm/{market → position}/Position.js +83 -5
  66. package/lib/{cjs/market → esm/position}/PositionUtils.d.ts +26 -1
  67. package/lib/esm/{market → position}/PositionUtils.js +27 -0
  68. package/lib/esm/position/index.d.ts +2 -0
  69. package/lib/esm/position/index.js +2 -0
  70. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +118 -0
  71. package/lib/esm/signatures/EcrecoverRatifierUtils.js +151 -3
  72. package/lib/esm/signatures/Group.js +5 -3
  73. package/lib/esm/signatures/GroupUtils.d.ts +48 -0
  74. package/lib/esm/signatures/GroupUtils.js +51 -0
  75. package/lib/esm/signatures/RatifierUtils.js +16 -4
  76. package/lib/esm/signatures/SetterRatifierUtils.d.ts +31 -0
  77. package/lib/esm/signatures/SetterRatifierUtils.js +38 -0
  78. package/lib/esm/signatures/Tree.js +14 -4
  79. package/lib/esm/signatures/TreeUtils.d.ts +6 -3
  80. package/lib/esm/signatures/TreeUtils.js +35 -13
  81. package/lib/esm/signatures/eip712.d.ts +22 -0
  82. package/lib/esm/signatures/eip712.js +22 -0
  83. package/lib/esm/signatures/index.d.ts +1 -0
  84. package/lib/esm/signatures/index.js +1 -0
  85. package/package.json +8 -7
  86. package/src/index.ts +0 -19
@@ -1,6 +1,6 @@
1
1
  import { type BigIntish } from "@morpho-org/morpho-ts";
2
- import type { Address } from "viem";
3
- import { type IMarket, Market } from "./Market.js";
2
+ import type { Address, Hash } from "viem";
3
+ import { type IMarket, Market } from "../market/Market.js";
4
4
  /**
5
5
  * Plain input accepted by {@link Position}.
6
6
  *
@@ -9,6 +9,8 @@ import { type IMarket, Market } from "./Market.js";
9
9
  * import type { IPosition } from "@morpho-org/midnight-sdk";
10
10
  *
11
11
  * const position: IPosition = {
12
+ * user: "0x0000000000000000000000000000000000009000",
13
+ * marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
12
14
  * credit: 0n,
13
15
  * pendingFee: 0n,
14
16
  * lastLossFactor: 0n,
@@ -20,6 +22,10 @@ import { type IMarket, Market } from "./Market.js";
20
22
  * ```
21
23
  */
22
24
  export interface IPosition {
25
+ /** User holding this position. */
26
+ readonly user: Address;
27
+ /** Id of the market on which this position is held. */
28
+ readonly marketId: Hash;
23
29
  /** User credit. */
24
30
  readonly credit: BigIntish;
25
31
  /** Pending continuous fee. */
@@ -43,6 +49,8 @@ export interface IPosition {
43
49
  * import { Position } from "@morpho-org/midnight-sdk";
44
50
  *
45
51
  * const position = new Position({
52
+ * user: "0x0000000000000000000000000000000000009000",
53
+ * marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
46
54
  * credit: 0n,
47
55
  * pendingFee: 0n,
48
56
  * lastLossFactor: 0n,
@@ -54,7 +62,11 @@ export interface IPosition {
54
62
  * console.log(position.debt);
55
63
  * ```
56
64
  */
57
- export declare class Position {
65
+ export declare class Position implements IPosition {
66
+ /** User holding this position. */
67
+ readonly user: Address;
68
+ /** Id of the market on which this position is held. */
69
+ readonly marketId: Hash;
58
70
  /** User credit. */
59
71
  readonly credit: bigint;
60
72
  /** Pending continuous fee. */
@@ -79,6 +91,8 @@ export declare class Position {
79
91
  * import { Position } from "@morpho-org/midnight-sdk";
80
92
  *
81
93
  * const position = new Position({
94
+ * user: "0x0000000000000000000000000000000000009000",
95
+ * marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
82
96
  * credit: 1_000n,
83
97
  * pendingFee: 100n,
84
98
  * lastLossFactor: 0n,
@@ -92,6 +106,9 @@ export declare class Position {
92
106
  */
93
107
  get faceValue(): bigint;
94
108
  }
109
+ /** Plain input shape for a Midnight position paired with hydrated market state. */
110
+ export interface IAccrualPosition extends Omit<IPosition, "marketId"> {
111
+ }
95
112
  /**
96
113
  * Midnight position paired with its hydrated market.
97
114
  *
@@ -116,6 +133,7 @@ export declare class Position {
116
133
  *
117
134
  * const position = new AccrualPosition(
118
135
  * {
136
+ * user: "0x0000000000000000000000000000000000009000",
119
137
  * credit: 1_000n,
120
138
  * pendingFee: 100n,
121
139
  * lastLossFactor: 0n,
@@ -154,10 +172,65 @@ export declare class Position {
154
172
  * console.log(position.market.id);
155
173
  * ```
156
174
  */
157
- export declare class AccrualPosition extends Position {
175
+ export declare class AccrualPosition extends Position implements IAccrualPosition {
176
+ protected readonly _market: Market;
177
+ constructor(position: IAccrualPosition, market: IMarket | Market);
158
178
  /** Hydrated market for this position. */
159
- readonly market: Market;
160
- constructor(position: IPosition, market: IMarket | Market);
179
+ get market(): Market;
180
+ /**
181
+ * Returns the maximum credit units currently withdrawable from this position.
182
+ *
183
+ * The protocol capacity is limited by both the accrued position credit and
184
+ * the market's currently withdrawable liquidity. Default redemption flows may
185
+ * still choose the position face value.
186
+ *
187
+ * @returns Maximum credit units currently withdrawable.
188
+ * @example
189
+ * ```ts
190
+ * import { AccrualPosition } from "@morpho-org/midnight-sdk";
191
+ *
192
+ * const position = new AccrualPosition(
193
+ * {
194
+ * user: "0x0000000000000000000000000000000000009000",
195
+ * credit: 1_000n,
196
+ * pendingFee: 100n,
197
+ * lastLossFactor: 0n,
198
+ * lastAccrual: 1_000n,
199
+ * debt: 0n,
200
+ * collateralBitmap: 1n,
201
+ * collateral: [50n],
202
+ * },
203
+ * {
204
+ * params: {
205
+ * chainId: 8453,
206
+ * midnight: "0x0000000000000000000000000000000000001000",
207
+ * loanToken: "0x0000000000000000000000000000000000006000",
208
+ * collateralParams: [
209
+ * {
210
+ * token: "0x0000000000000000000000000000000000007000",
211
+ * lltv: 770000000000000000n,
212
+ * liquidationCursor: 250000000000000000n,
213
+ * oracle: "0x0000000000000000000000000000000000008000",
214
+ * },
215
+ * ],
216
+ * maturity: 54_000n,
217
+ * rcfThreshold: 0n,
218
+ * enterGate: "0x0000000000000000000000000000000000000000",
219
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
220
+ * },
221
+ * totalUnits: 1_000n,
222
+ * lossFactor: 0n,
223
+ * withdrawable: 950n,
224
+ * continuousFeeCredit: 0n,
225
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
226
+ * continuousFee: 10,
227
+ * tickSpacing: 4,
228
+ * },
229
+ * );
230
+ * console.log(position.withdrawable); // 950n
231
+ * ```
232
+ */
233
+ get withdrawable(): bigint;
161
234
  /**
162
235
  * Returns the position collateral balance at an index configured by the market.
163
236
  *
@@ -184,6 +257,7 @@ export declare class AccrualPosition extends Position {
184
257
  *
185
258
  * const position = new AccrualPosition(
186
259
  * {
260
+ * user: "0x0000000000000000000000000000000000009000",
187
261
  * credit: 1_000n,
188
262
  * pendingFee: 100n,
189
263
  * lastLossFactor: 0n,
@@ -251,6 +325,7 @@ export declare class AccrualPosition extends Position {
251
325
  * const collateralToken = "0x0000000000000000000000000000000000007000";
252
326
  * const position = new AccrualPosition(
253
327
  * {
328
+ * user: "0x0000000000000000000000000000000000009000",
254
329
  * credit: 1_000n,
255
330
  * pendingFee: 100n,
256
331
  * lastLossFactor: 0n,
@@ -317,6 +392,7 @@ export declare class AccrualPosition extends Position {
317
392
  *
318
393
  * const position = new AccrualPosition(
319
394
  * {
395
+ * user: "0x0000000000000000000000000000000000009000",
320
396
  * credit: 1_000n,
321
397
  * pendingFee: 100n,
322
398
  * lastLossFactor: 0n,
@@ -386,6 +462,7 @@ export declare class AccrualPosition extends Position {
386
462
  *
387
463
  * const position = new AccrualPosition(
388
464
  * {
465
+ * user: "0x0000000000000000000000000000000000009000",
389
466
  * credit: 1_000n,
390
467
  * pendingFee: 100n,
391
468
  * lastLossFactor: 0n,
@@ -3,7 +3,7 @@ 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
5
  const errors_js_1 = require("../errors.js");
6
- const Market_js_1 = require("./Market.js");
6
+ const Market_js_1 = require("../market/Market.js");
7
7
  const PositionUtils_js_1 = require("./PositionUtils.js");
8
8
  /**
9
9
  * Midnight user position as stored by the core contract.
@@ -13,6 +13,8 @@ const PositionUtils_js_1 = require("./PositionUtils.js");
13
13
  * import { Position } from "@morpho-org/midnight-sdk";
14
14
  *
15
15
  * const position = new Position({
16
+ * user: "0x0000000000000000000000000000000000009000",
17
+ * marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
16
18
  * credit: 0n,
17
19
  * pendingFee: 0n,
18
20
  * lastLossFactor: 0n,
@@ -25,6 +27,10 @@ const PositionUtils_js_1 = require("./PositionUtils.js");
25
27
  * ```
26
28
  */
27
29
  class Position {
30
+ /** User holding this position. */
31
+ user;
32
+ /** Id of the market on which this position is held. */
33
+ marketId;
28
34
  /** User credit. */
29
35
  credit;
30
36
  /** Pending continuous fee. */
@@ -40,6 +46,8 @@ class Position {
40
46
  /** Collateral balances by index. */
41
47
  collateral;
42
48
  constructor(position) {
49
+ this.user = position.user;
50
+ this.marketId = position.marketId;
43
51
  this.credit = BigInt(position.credit);
44
52
  this.pendingFee = BigInt(position.pendingFee);
45
53
  this.lastLossFactor = BigInt(position.lastLossFactor);
@@ -57,6 +65,8 @@ class Position {
57
65
  * import { Position } from "@morpho-org/midnight-sdk";
58
66
  *
59
67
  * const position = new Position({
68
+ * user: "0x0000000000000000000000000000000000009000",
69
+ * marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
60
70
  * credit: 1_000n,
61
71
  * pendingFee: 100n,
62
72
  * lastLossFactor: 0n,
@@ -97,6 +107,7 @@ exports.Position = Position;
97
107
  *
98
108
  * const position = new AccrualPosition(
99
109
  * {
110
+ * user: "0x0000000000000000000000000000000000009000",
100
111
  * credit: 1_000n,
101
112
  * pendingFee: 100n,
102
113
  * lastLossFactor: 0n,
@@ -136,11 +147,74 @@ exports.Position = Position;
136
147
  * ```
137
148
  */
138
149
  class AccrualPosition extends Position {
139
- /** Hydrated market for this position. */
140
- market;
150
+ _market;
141
151
  constructor(position, market) {
142
- super(position);
143
- this.market = Market_js_1.Market.from(market);
152
+ const _market = Market_js_1.Market.from(market);
153
+ super({ ...position, marketId: _market.id });
154
+ this._market = _market;
155
+ }
156
+ /** Hydrated market for this position. */
157
+ get market() {
158
+ return this._market;
159
+ }
160
+ /**
161
+ * Returns the maximum credit units currently withdrawable from this position.
162
+ *
163
+ * The protocol capacity is limited by both the accrued position credit and
164
+ * the market's currently withdrawable liquidity. Default redemption flows may
165
+ * still choose the position face value.
166
+ *
167
+ * @returns Maximum credit units currently withdrawable.
168
+ * @example
169
+ * ```ts
170
+ * import { AccrualPosition } from "@morpho-org/midnight-sdk";
171
+ *
172
+ * const position = new AccrualPosition(
173
+ * {
174
+ * user: "0x0000000000000000000000000000000000009000",
175
+ * credit: 1_000n,
176
+ * pendingFee: 100n,
177
+ * lastLossFactor: 0n,
178
+ * lastAccrual: 1_000n,
179
+ * debt: 0n,
180
+ * collateralBitmap: 1n,
181
+ * collateral: [50n],
182
+ * },
183
+ * {
184
+ * params: {
185
+ * chainId: 8453,
186
+ * midnight: "0x0000000000000000000000000000000000001000",
187
+ * loanToken: "0x0000000000000000000000000000000000006000",
188
+ * collateralParams: [
189
+ * {
190
+ * token: "0x0000000000000000000000000000000000007000",
191
+ * lltv: 770000000000000000n,
192
+ * liquidationCursor: 250000000000000000n,
193
+ * oracle: "0x0000000000000000000000000000000000008000",
194
+ * },
195
+ * ],
196
+ * maturity: 54_000n,
197
+ * rcfThreshold: 0n,
198
+ * enterGate: "0x0000000000000000000000000000000000000000",
199
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
200
+ * },
201
+ * totalUnits: 1_000n,
202
+ * lossFactor: 0n,
203
+ * withdrawable: 950n,
204
+ * continuousFeeCredit: 0n,
205
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
206
+ * continuousFee: 10,
207
+ * tickSpacing: 4,
208
+ * },
209
+ * );
210
+ * console.log(position.withdrawable); // 950n
211
+ * ```
212
+ */
213
+ get withdrawable() {
214
+ return PositionUtils_js_1.PositionUtils.getWithdrawable({
215
+ position: this,
216
+ market: this.market,
217
+ });
144
218
  }
145
219
  /**
146
220
  * Returns the position collateral balance at an index configured by the market.
@@ -168,6 +242,7 @@ class AccrualPosition extends Position {
168
242
  *
169
243
  * const position = new AccrualPosition(
170
244
  * {
245
+ * user: "0x0000000000000000000000000000000000009000",
171
246
  * credit: 1_000n,
172
247
  * pendingFee: 100n,
173
248
  * lastLossFactor: 0n,
@@ -240,6 +315,7 @@ class AccrualPosition extends Position {
240
315
  * const collateralToken = "0x0000000000000000000000000000000000007000";
241
316
  * const position = new AccrualPosition(
242
317
  * {
318
+ * user: "0x0000000000000000000000000000000000009000",
243
319
  * credit: 1_000n,
244
320
  * pendingFee: 100n,
245
321
  * lastLossFactor: 0n,
@@ -309,6 +385,7 @@ class AccrualPosition extends Position {
309
385
  *
310
386
  * const position = new AccrualPosition(
311
387
  * {
388
+ * user: "0x0000000000000000000000000000000000009000",
312
389
  * credit: 1_000n,
313
390
  * pendingFee: 100n,
314
391
  * lastLossFactor: 0n,
@@ -380,6 +457,7 @@ class AccrualPosition extends Position {
380
457
  *
381
458
  * const position = new AccrualPosition(
382
459
  * {
460
+ * user: "0x0000000000000000000000000000000000009000",
383
461
  * credit: 1_000n,
384
462
  * pendingFee: 100n,
385
463
  * lastLossFactor: 0n,
@@ -1,5 +1,5 @@
1
1
  import { type BigIntish } from "@morpho-org/morpho-ts";
2
- import type { IMarket } from "./Market.js";
2
+ import type { IMarket } from "../market/Market.js";
3
3
  import type { IPosition } from "./Position.js";
4
4
  /**
5
5
  * Domain helpers for Midnight positions.
@@ -10,6 +10,8 @@ import type { IPosition } from "./Position.js";
10
10
  *
11
11
  * const accrued = PositionUtils.accrueInterest({
12
12
  * position: {
13
+ * user: "0x0000000000000000000000000000000000009000",
14
+ * marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
13
15
  * credit: 1_000n,
14
16
  * pendingFee: 100n,
15
17
  * lastLossFactor: 0n,
@@ -50,6 +52,27 @@ import type { IPosition } from "./Position.js";
50
52
  * ```
51
53
  */
52
54
  export declare namespace PositionUtils {
55
+ /**
56
+ * Returns the maximum credit units currently withdrawable by a position.
57
+ *
58
+ * @param params.position - Position whose credit limits the withdrawal.
59
+ * @param params.market - Market whose available liquidity limits the withdrawal.
60
+ * @returns The lower of the position credit and market withdrawable liquidity.
61
+ * @example
62
+ * ```ts
63
+ * import { PositionUtils } from "@morpho-org/midnight-sdk";
64
+ *
65
+ * const withdrawable = PositionUtils.getWithdrawable({
66
+ * position: { credit: 1_000n },
67
+ * market: { withdrawable: 500n },
68
+ * });
69
+ * console.log(withdrawable); // 500n
70
+ * ```
71
+ */
72
+ function getWithdrawable(params: {
73
+ readonly position: Pick<IPosition, "credit">;
74
+ readonly market: Pick<IMarket, "withdrawable">;
75
+ }): bigint;
53
76
  /**
54
77
  * Returns plain Midnight position and market objects accrued like Midnight `updatePositionView`.
55
78
  *
@@ -66,6 +89,8 @@ export declare namespace PositionUtils {
66
89
  *
67
90
  * const { position, market } = PositionUtils.accrueInterest({
68
91
  * position: {
92
+ * user: "0x0000000000000000000000000000000000009000",
93
+ * marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
69
94
  * credit: 1_000n,
70
95
  * pendingFee: 100n,
71
96
  * lastLossFactor: 0n,
@@ -12,6 +12,8 @@ const errors_js_1 = require("../errors.js");
12
12
  *
13
13
  * const accrued = PositionUtils.accrueInterest({
14
14
  * position: {
15
+ * user: "0x0000000000000000000000000000000000009000",
16
+ * marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
15
17
  * credit: 1_000n,
16
18
  * pendingFee: 100n,
17
19
  * lastLossFactor: 0n,
@@ -53,6 +55,27 @@ const errors_js_1 = require("../errors.js");
53
55
  */
54
56
  var PositionUtils;
55
57
  (function (PositionUtils) {
58
+ /**
59
+ * Returns the maximum credit units currently withdrawable by a position.
60
+ *
61
+ * @param params.position - Position whose credit limits the withdrawal.
62
+ * @param params.market - Market whose available liquidity limits the withdrawal.
63
+ * @returns The lower of the position credit and market withdrawable liquidity.
64
+ * @example
65
+ * ```ts
66
+ * import { PositionUtils } from "@morpho-org/midnight-sdk";
67
+ *
68
+ * const withdrawable = PositionUtils.getWithdrawable({
69
+ * position: { credit: 1_000n },
70
+ * market: { withdrawable: 500n },
71
+ * });
72
+ * console.log(withdrawable); // 500n
73
+ * ```
74
+ */
75
+ function getWithdrawable(params) {
76
+ return morpho_ts_1.MathLib.min(BigInt(params.position.credit), BigInt(params.market.withdrawable));
77
+ }
78
+ PositionUtils.getWithdrawable = getWithdrawable;
56
79
  /**
57
80
  * Returns plain Midnight position and market objects accrued like Midnight `updatePositionView`.
58
81
  *
@@ -69,6 +92,8 @@ var PositionUtils;
69
92
  *
70
93
  * const { position, market } = PositionUtils.accrueInterest({
71
94
  * position: {
95
+ * user: "0x0000000000000000000000000000000000009000",
96
+ * marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
72
97
  * credit: 1_000n,
73
98
  * pendingFee: 100n,
74
99
  * lastLossFactor: 0n,
@@ -172,6 +197,8 @@ var PositionUtils;
172
197
  ];
173
198
  return {
174
199
  position: {
200
+ user: params.position.user,
201
+ marketId: params.position.marketId,
175
202
  credit: postSlashCredit - accruedFee,
176
203
  pendingFee: postSlashPendingFee - accruedFee,
177
204
  lastLossFactor: marketLossFactor,
@@ -0,0 +1,2 @@
1
+ export * from "./Position.js";
2
+ export * from "./PositionUtils.js";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Position.js"), exports);
18
+ __exportStar(require("./PositionUtils.js"), exports);
@@ -1,5 +1,6 @@
1
1
  import { type BigIntish } from "@morpho-org/morpho-ts";
2
2
  import { type Account, type Address, type Chain, type Client, type Hash, type Hex, type Signature, type Transport } from "viem";
3
+ import { type IOffer } from "../offers/index.js";
3
4
  import type { Payload } from "./Payload.js";
4
5
  import { type RatifierTreeInput, type TreeProof } from "./TreeUtils.js";
5
6
  declare const typedDataTypes: {
@@ -365,6 +366,40 @@ export interface EcrecoverRatifierDataParams {
365
366
  /** Ecrecover signature for the tree root. */
366
367
  readonly signature: EcrecoverSignatureInput;
367
368
  }
369
+ /** Parameters for reconstructing an EcrecoverRatifier digest from decoded ratifier data fields. */
370
+ export interface EcrecoverRatifierRootDigestParams {
371
+ /** Observed EIP-155 chain id from the log or execution context. Must match `offer.market.chainId`. */
372
+ readonly chainId: BigIntish;
373
+ /** Offer whose ratifier address and market chain id define the EIP-712 domain. */
374
+ readonly offer: IOffer;
375
+ /** Merkle root embedded in the ratifier data. */
376
+ readonly root: Hash;
377
+ /** Number of sibling hashes in the Merkle proof. */
378
+ readonly proofLength: number;
379
+ }
380
+ /** Parameters for reconstructing an EcrecoverRatifier digest from encoded ratifier data. */
381
+ export interface EcrecoverRatifierDataDigestParams {
382
+ /** Observed EIP-155 chain id from the log or execution context. Must match `offer.market.chainId`. */
383
+ readonly chainId: BigIntish;
384
+ /** Offer carried by the payload item. */
385
+ readonly offer: IOffer;
386
+ /** ABI-encoded Ecrecover ratifier data carried by the payload item. */
387
+ readonly ratifierData: Hex;
388
+ }
389
+ /** Parameters for locally verifying Ecrecover ratifier data attached to one payload item. */
390
+ export interface EcrecoverRatifierDataVerificationParams {
391
+ /** Observed EIP-155 chain id from the log or execution context. Must match `offer.market.chainId`. */
392
+ readonly chainId: BigIntish;
393
+ /** Offer carried by the payload item. */
394
+ readonly offer: IOffer;
395
+ /** ABI-encoded Ecrecover ratifier data carried by the payload item. */
396
+ readonly ratifierData: Hex;
397
+ }
398
+ /** Decoded Ecrecover ratifier data after local proof verification and signature recovery. */
399
+ export interface VerifiedEcrecoverRatifierData extends DecodedEcrecoverRatifierData {
400
+ /** Signer recovered from the Ecrecover typed-data digest. */
401
+ readonly signer: Address;
402
+ }
368
403
  /**
369
404
  * EcrecoverRatifier-specific pure utilities.
370
405
  *
@@ -501,6 +536,89 @@ export declare namespace EcrecoverRatifierUtils {
501
536
  * ```
502
537
  */
503
538
  function digest(params: EcrecoverRatifierTypedDataParams): `0x${string}`;
539
+ /**
540
+ * Builds the EcrecoverRatifier digest from a payload offer, root, and proof height.
541
+ *
542
+ * Use this on the take-side or in indexers after decoding ratifier data, when
543
+ * the original full tree is not available but the payload item includes the
544
+ * offer and proof metadata needed to reconstruct the signed digest. Pass the
545
+ * observed log or execution chain id so cross-chain-spoofed payloads are
546
+ * rejected before signature recovery.
547
+ *
548
+ * @param params.chainId - Observed EIP-155 chain id from the log or execution context.
549
+ * @param params.offer - Offer carried by the payload item.
550
+ * @param params.root - Merkle root embedded in the ratifier data.
551
+ * @param params.proofLength - Number of sibling hashes in the Merkle proof.
552
+ * @returns EIP-712 digest signed by the maker or authorized signer.
553
+ * @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
554
+ * @throws {InvalidTreeHeightError} when `proofLength` exceeds 20.
555
+ * @example
556
+ * ```ts
557
+ * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
558
+ * import { zeroHash } from "viem";
559
+ *
560
+ * const digest = EcrecoverRatifierUtils.digestForRoot({
561
+ * chainId: 8453n,
562
+ * offer,
563
+ * root: zeroHash,
564
+ * proofLength: 0,
565
+ * });
566
+ * console.log(digest);
567
+ * ```
568
+ */
569
+ function digestForRoot(params: EcrecoverRatifierRootDigestParams): Hash;
570
+ /**
571
+ * Builds the EcrecoverRatifier digest from one payload item's encoded ratifier data.
572
+ *
573
+ * @param params.chainId - Observed EIP-155 chain id from the log or execution context.
574
+ * @param params.offer - Offer carried by the payload item.
575
+ * @param params.ratifierData - ABI-encoded Ecrecover ratifier data.
576
+ * @returns EIP-712 digest signed by the maker or authorized signer.
577
+ * @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
578
+ * @throws {InvalidTreeHeightError} when the proof height exceeds 20.
579
+ * @example
580
+ * ```ts
581
+ * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
582
+ *
583
+ * const digest = EcrecoverRatifierUtils.digestRatifierData({
584
+ * chainId: 8453n,
585
+ * offer,
586
+ * ratifierData,
587
+ * });
588
+ * console.log(digest);
589
+ * ```
590
+ */
591
+ function digestRatifierData(params: EcrecoverRatifierDataDigestParams): Hash;
592
+ /**
593
+ * Verifies an Ecrecover ratifier-data proof and recovers its signer.
594
+ *
595
+ * This helper intentionally does not check `Midnight.isAuthorized` or
596
+ * `EcrecoverRatifier.isRootCanceled` state. Consumers must query both at the
597
+ * observed block before treating a recovered item as executable: the returned
598
+ * signer must be the maker or authorized by the maker, the offer ratifier must
599
+ * be authorized by the maker, and `isRootCanceled(maker, root)` must be false.
600
+ *
601
+ * @param params.chainId - Observed EIP-155 chain id from the log or execution context.
602
+ * @param params.offer - Offer carried by the payload item.
603
+ * @param params.ratifierData - ABI-encoded Ecrecover ratifier data.
604
+ * @returns Decoded ratifier data plus recovered signer.
605
+ * @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
606
+ * @throws {InvalidEcrecoverSignatureVError} when the decoded signature `v` is not 27 or 28.
607
+ * @throws {InvalidTreeError} when the proof does not include `offer` in `root`.
608
+ * @throws {InvalidTreeHeightError} when the proof height exceeds 20.
609
+ * @example
610
+ * ```ts
611
+ * import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
612
+ *
613
+ * const verified = await EcrecoverRatifierUtils.verifyRatifierData({
614
+ * chainId: 8453n,
615
+ * offer,
616
+ * ratifierData,
617
+ * });
618
+ * console.log(verified.signer);
619
+ * ```
620
+ */
621
+ function verifyRatifierData(params: EcrecoverRatifierDataVerificationParams): Promise<VerifiedEcrecoverRatifierData>;
504
622
  /**
505
623
  * Signs EcrecoverRatifier typed data through a viem client.
506
624
  *