@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.
- package/LICENSE +21 -0
- package/README.md +56 -46
- package/lib/cjs/abis.d.ts +1185 -996
- package/lib/cjs/abis.js +1445 -1211
- package/lib/cjs/api/MidnightApi.d.ts +22 -16
- package/lib/cjs/api/MidnightApi.js +22 -16
- package/lib/cjs/api/helpers.js +6 -3
- package/lib/cjs/api/types.d.ts +35 -9
- package/lib/cjs/constants.d.ts +12 -11
- package/lib/cjs/constants.js +14 -12
- package/lib/cjs/errors.d.ts +74 -1
- package/lib/cjs/errors.js +94 -3
- package/lib/cjs/fetch/Market.js +0 -1
- package/lib/cjs/fetch/index.d.ts +0 -1
- package/lib/cjs/fetch/index.js +0 -1
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +2 -23
- package/lib/cjs/market/Market.d.ts +99 -33
- package/lib/cjs/market/Market.js +137 -28
- package/lib/cjs/market/MarketUtils.d.ts +61 -41
- package/lib/cjs/market/MarketUtils.js +108 -48
- package/lib/cjs/market/Position.d.ts +16 -11
- package/lib/cjs/market/Position.js +18 -12
- package/lib/cjs/market/PositionUtils.d.ts +6 -4
- package/lib/cjs/market/PositionUtils.js +9 -6
- package/lib/cjs/math/TickLib.d.ts +22 -0
- package/lib/cjs/math/TickLib.js +30 -0
- package/lib/cjs/math/index.d.ts +0 -1
- package/lib/cjs/math/index.js +0 -1
- package/lib/cjs/offers/Offer.d.ts +217 -11
- package/lib/cjs/offers/Offer.js +215 -7
- package/lib/cjs/offers/OfferUtils.d.ts +245 -139
- package/lib/cjs/offers/OfferUtils.js +304 -36
- package/lib/cjs/queries/GetPosition.d.ts +1 -1
- package/lib/cjs/queries/GetPosition.js +1 -1
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +66 -38
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +147 -79
- package/lib/cjs/signatures/Group.d.ts +12 -4
- package/lib/cjs/signatures/Group.js +12 -4
- package/lib/cjs/signatures/GroupUtils.d.ts +12 -4
- package/lib/cjs/signatures/GroupUtils.js +6 -2
- package/lib/cjs/signatures/Payload.d.ts +277 -277
- package/lib/cjs/signatures/Payload.js +517 -534
- package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
- package/lib/cjs/signatures/RatifierUtils.js +94 -0
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +21 -13
- package/lib/cjs/signatures/SetterRatifierUtils.js +25 -20
- package/lib/cjs/signatures/Tree.d.ts +28 -16
- package/lib/cjs/signatures/Tree.js +25 -13
- package/lib/cjs/signatures/TreeUtils.d.ts +181 -62
- package/lib/cjs/signatures/TreeUtils.js +73 -76
- package/lib/cjs/signatures/index.d.ts +1 -0
- package/lib/cjs/signatures/index.js +3 -0
- package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
- package/lib/cjs/signatures/offerStructInternal.js +72 -0
- package/lib/esm/abis.d.ts +1185 -996
- package/lib/esm/abis.js +1445 -1211
- package/lib/esm/api/MidnightApi.d.ts +22 -16
- package/lib/esm/api/MidnightApi.js +23 -17
- package/lib/esm/api/helpers.js +6 -3
- package/lib/esm/api/types.d.ts +35 -9
- package/lib/esm/constants.d.ts +12 -11
- package/lib/esm/constants.js +13 -11
- package/lib/esm/errors.d.ts +74 -1
- package/lib/esm/errors.js +89 -2
- package/lib/esm/fetch/Market.js +0 -1
- package/lib/esm/fetch/index.d.ts +0 -1
- package/lib/esm/fetch/index.js +0 -1
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/market/Market.d.ts +99 -33
- package/lib/esm/market/Market.js +137 -28
- package/lib/esm/market/MarketUtils.d.ts +61 -41
- package/lib/esm/market/MarketUtils.js +109 -49
- package/lib/esm/market/Position.d.ts +16 -11
- package/lib/esm/market/Position.js +19 -13
- package/lib/esm/market/PositionUtils.d.ts +6 -4
- package/lib/esm/market/PositionUtils.js +9 -6
- package/lib/esm/math/TickLib.d.ts +22 -0
- package/lib/esm/math/TickLib.js +31 -1
- package/lib/esm/math/index.d.ts +0 -1
- package/lib/esm/math/index.js +0 -1
- package/lib/esm/offers/Offer.d.ts +217 -11
- package/lib/esm/offers/Offer.js +216 -8
- package/lib/esm/offers/OfferUtils.d.ts +245 -139
- package/lib/esm/offers/OfferUtils.js +306 -38
- package/lib/esm/queries/GetPosition.d.ts +1 -1
- package/lib/esm/queries/GetPosition.js +1 -1
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +66 -38
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +149 -81
- package/lib/esm/signatures/Group.d.ts +12 -4
- package/lib/esm/signatures/Group.js +12 -4
- package/lib/esm/signatures/GroupUtils.d.ts +12 -4
- package/lib/esm/signatures/GroupUtils.js +6 -2
- package/lib/esm/signatures/Payload.d.ts +277 -277
- package/lib/esm/signatures/Payload.js +517 -531
- package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
- package/lib/esm/signatures/RatifierUtils.js +94 -0
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +21 -13
- package/lib/esm/signatures/SetterRatifierUtils.js +25 -20
- package/lib/esm/signatures/Tree.d.ts +28 -16
- package/lib/esm/signatures/Tree.js +25 -13
- package/lib/esm/signatures/TreeUtils.d.ts +181 -62
- package/lib/esm/signatures/TreeUtils.js +75 -78
- package/lib/esm/signatures/index.d.ts +1 -0
- package/lib/esm/signatures/index.js +1 -0
- package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
- package/lib/esm/signatures/offerStructInternal.js +67 -0
- package/package.json +28 -32
- package/src/index.ts +1 -1
- package/lib/cjs/fetch/Offer.d.ts +0 -74
- package/lib/cjs/fetch/Offer.js +0 -119
- package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
- package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
- package/lib/esm/fetch/Offer.d.ts +0 -74
- package/lib/esm/fetch/Offer.js +0 -116
- package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
- package/lib/esm/math/ConsumableUnitsLib.js +0 -74
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { BigIntish } from "@morpho-org/morpho-ts";
|
|
2
2
|
import type { Address, Hash, Hex } from "viem";
|
|
3
|
-
import { type IMarket, type IMarketParams, MarketParams } from "../market/index.js";
|
|
3
|
+
import { type IMarket, type IMarketParams, Market, MarketParams } from "../market/index.js";
|
|
4
4
|
/**
|
|
5
5
|
* Plain make-side offer input accepted by {@link Offer}.
|
|
6
6
|
*
|
|
@@ -19,12 +19,14 @@ import { type IMarket, type IMarketParams, MarketParams } from "../market/index.
|
|
|
19
19
|
*
|
|
20
20
|
* const offer: IOffer = {
|
|
21
21
|
* market: {
|
|
22
|
+
* chainId: 8453,
|
|
23
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
22
24
|
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
23
25
|
* collateralParams: [
|
|
24
26
|
* {
|
|
25
27
|
* token: "0x0000000000000000000000000000000000007000",
|
|
26
28
|
* lltv: 770000000000000000n,
|
|
27
|
-
*
|
|
29
|
+
* liquidationCursor: 250000000000000000n,
|
|
28
30
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
29
31
|
* },
|
|
30
32
|
* ],
|
|
@@ -99,12 +101,14 @@ export interface IOffer {
|
|
|
99
101
|
*
|
|
100
102
|
* const offer = Offer.create({
|
|
101
103
|
* market: {
|
|
104
|
+
* chainId: 8453,
|
|
105
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
102
106
|
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
103
107
|
* collateralParams: [
|
|
104
108
|
* {
|
|
105
109
|
* token: "0x0000000000000000000000000000000000007000",
|
|
106
110
|
* lltv: 770000000000000000n,
|
|
107
|
-
*
|
|
111
|
+
* liquidationCursor: 250000000000000000n,
|
|
108
112
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
109
113
|
* },
|
|
110
114
|
* ],
|
|
@@ -132,7 +136,7 @@ export interface IOffer {
|
|
|
132
136
|
*/
|
|
133
137
|
export declare class Offer {
|
|
134
138
|
/** Market this offer trades. */
|
|
135
|
-
readonly market: MarketParams;
|
|
139
|
+
readonly market: MarketParams | Market;
|
|
136
140
|
/** Whether the maker buys units. */
|
|
137
141
|
readonly buy: boolean;
|
|
138
142
|
/** Offer maker. */
|
|
@@ -178,12 +182,14 @@ export declare class Offer {
|
|
|
178
182
|
*
|
|
179
183
|
* const offer = Offer.from({
|
|
180
184
|
* market: {
|
|
185
|
+
* chainId: 8453,
|
|
186
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
181
187
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
182
188
|
* collateralParams: [
|
|
183
189
|
* {
|
|
184
190
|
* token: "0x0000000000000000000000000000000000007000",
|
|
185
191
|
* lltv: 770000000000000000n,
|
|
186
|
-
*
|
|
192
|
+
* liquidationCursor: 250000000000000000n,
|
|
187
193
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
188
194
|
* },
|
|
189
195
|
* ],
|
|
@@ -231,12 +237,14 @@ export declare class Offer {
|
|
|
231
237
|
*
|
|
232
238
|
* const offer = Offer.create({
|
|
233
239
|
* market: {
|
|
240
|
+
* chainId: 8453,
|
|
241
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
234
242
|
* loanToken,
|
|
235
243
|
* collateralParams: [
|
|
236
244
|
* {
|
|
237
245
|
* token: collateralToken,
|
|
238
246
|
* lltv: 770000000000000000n,
|
|
239
|
-
*
|
|
247
|
+
* liquidationCursor: 250000000000000000n,
|
|
240
248
|
* oracle,
|
|
241
249
|
* },
|
|
242
250
|
* ],
|
|
@@ -254,7 +262,7 @@ export declare class Offer {
|
|
|
254
262
|
* });
|
|
255
263
|
*
|
|
256
264
|
* const group = offer.group;
|
|
257
|
-
*
|
|
265
|
+
* console.log(group);
|
|
258
266
|
* ```
|
|
259
267
|
*/
|
|
260
268
|
get group(): Hash;
|
|
@@ -278,12 +286,14 @@ export declare class Offer {
|
|
|
278
286
|
*
|
|
279
287
|
* const offer = Offer.create({
|
|
280
288
|
* market: {
|
|
289
|
+
* chainId: 8453,
|
|
290
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
281
291
|
* loanToken,
|
|
282
292
|
* collateralParams: [
|
|
283
293
|
* {
|
|
284
294
|
* token: collateralToken,
|
|
285
295
|
* lltv: 770000000000000000n,
|
|
286
|
-
*
|
|
296
|
+
* liquidationCursor: 250000000000000000n,
|
|
287
297
|
* oracle,
|
|
288
298
|
* },
|
|
289
299
|
* ],
|
|
@@ -305,6 +315,196 @@ export declare class Offer {
|
|
|
305
315
|
* ```
|
|
306
316
|
*/
|
|
307
317
|
get hash(): Hash;
|
|
318
|
+
/**
|
|
319
|
+
* WAD zero-coupon price at this offer's tick.
|
|
320
|
+
*
|
|
321
|
+
* @returns WAD price rounded to the protocol price quantum.
|
|
322
|
+
* @throws {NegativeValueError} when `tick` is negative.
|
|
323
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* import { Offer } from "@morpho-org/midnight-sdk";
|
|
327
|
+
*
|
|
328
|
+
* const offer = Offer.create({
|
|
329
|
+
* market: {
|
|
330
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
331
|
+
* collateralParams: [
|
|
332
|
+
* {
|
|
333
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
334
|
+
* lltv: 770000000000000000n,
|
|
335
|
+
* liquidationCursor: 250000000000000000n,
|
|
336
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
337
|
+
* },
|
|
338
|
+
* ],
|
|
339
|
+
* maturity: 54_000n,
|
|
340
|
+
* rcfThreshold: 0n,
|
|
341
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
342
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
343
|
+
* },
|
|
344
|
+
* buy: true,
|
|
345
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
346
|
+
* tick: 5_000n,
|
|
347
|
+
* expiry: 3_600n,
|
|
348
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
349
|
+
* maxUnits: 100n,
|
|
350
|
+
* });
|
|
351
|
+
* console.log(offer.price);
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
get price(): bigint;
|
|
355
|
+
/**
|
|
356
|
+
* Converts this offer's tick into a WAD per-second simple rate at a timestamp.
|
|
357
|
+
*
|
|
358
|
+
* @param timestamp - Timestamp at which the rate is calculated.
|
|
359
|
+
* @returns WAD per-second simple rate rounded up.
|
|
360
|
+
* @throws {NegativeValueError} when market maturity, `timestamp`, or `tick` is negative.
|
|
361
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
362
|
+
* @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
|
|
363
|
+
* @example
|
|
364
|
+
* ```ts
|
|
365
|
+
* import { Offer } from "@morpho-org/midnight-sdk";
|
|
366
|
+
*
|
|
367
|
+
* const offer = Offer.create({
|
|
368
|
+
* market: {
|
|
369
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
370
|
+
* collateralParams: [
|
|
371
|
+
* {
|
|
372
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
373
|
+
* lltv: 770000000000000000n,
|
|
374
|
+
* liquidationCursor: 250000000000000000n,
|
|
375
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
376
|
+
* },
|
|
377
|
+
* ],
|
|
378
|
+
* maturity: 54_000n,
|
|
379
|
+
* rcfThreshold: 0n,
|
|
380
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
381
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
382
|
+
* },
|
|
383
|
+
* buy: true,
|
|
384
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
385
|
+
* tick: 5_000n,
|
|
386
|
+
* expiry: 3_600n,
|
|
387
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
388
|
+
* maxUnits: 100n,
|
|
389
|
+
* });
|
|
390
|
+
* const rate = offer.getRate(1_000n);
|
|
391
|
+
* console.log(rate);
|
|
392
|
+
* ```
|
|
393
|
+
*/
|
|
394
|
+
getRate(timestamp: BigIntish): bigint;
|
|
395
|
+
/**
|
|
396
|
+
* Converts this offer's tick into a WAD simple annual percentage rate at a timestamp.
|
|
397
|
+
*
|
|
398
|
+
* @param timestamp - Timestamp at which the APR is calculated.
|
|
399
|
+
* @returns WAD simple APR rounded up.
|
|
400
|
+
* @throws {NegativeValueError} when market maturity, `timestamp`, or `tick` is negative.
|
|
401
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
402
|
+
* @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
|
|
403
|
+
* @example
|
|
404
|
+
* ```ts
|
|
405
|
+
* import { Offer } from "@morpho-org/midnight-sdk";
|
|
406
|
+
*
|
|
407
|
+
* const offer = Offer.create({
|
|
408
|
+
* market: {
|
|
409
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
410
|
+
* collateralParams: [
|
|
411
|
+
* {
|
|
412
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
413
|
+
* lltv: 770000000000000000n,
|
|
414
|
+
* liquidationCursor: 250000000000000000n,
|
|
415
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
416
|
+
* },
|
|
417
|
+
* ],
|
|
418
|
+
* maturity: 54_000n,
|
|
419
|
+
* rcfThreshold: 0n,
|
|
420
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
421
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
422
|
+
* },
|
|
423
|
+
* buy: true,
|
|
424
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
425
|
+
* tick: 5_000n,
|
|
426
|
+
* expiry: 3_600n,
|
|
427
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
428
|
+
* maxUnits: 100n,
|
|
429
|
+
* });
|
|
430
|
+
* const apr = offer.getApr(1_000n);
|
|
431
|
+
* console.log(apr);
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
getApr(timestamp: BigIntish): bigint;
|
|
435
|
+
/**
|
|
436
|
+
* Returns remaining units accepted by this offer's cap at a timestamp.
|
|
437
|
+
*
|
|
438
|
+
* The offer must carry a hydrated {@link Market} so the current market
|
|
439
|
+
* continuous fee and settlement-fee buckets are available locally. Fetch the
|
|
440
|
+
* current `consumed(maker, group)` value separately and pass it as input.
|
|
441
|
+
*
|
|
442
|
+
* @param params.consumed - Amount already consumed from this offer's group.
|
|
443
|
+
* @param params.timestamp - Timestamp used to compute the market time to maturity.
|
|
444
|
+
* @returns Remaining consumable units accepted by Midnight `take`.
|
|
445
|
+
* @throws {InvalidOfferParameterError} when this offer does not carry hydrated market state or has invalid caps.
|
|
446
|
+
* @throws {NegativeValueError} when `consumed`, `timestamp`, offer limits, or delegated math inputs are negative.
|
|
447
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
448
|
+
* @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy-offer price.
|
|
449
|
+
* @example
|
|
450
|
+
* ```ts
|
|
451
|
+
* import { Offer } from "@morpho-org/midnight-sdk";
|
|
452
|
+
*
|
|
453
|
+
* const offer = Offer.from({
|
|
454
|
+
* market: {
|
|
455
|
+
* params: {
|
|
456
|
+
* chainId: 8453,
|
|
457
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
458
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
459
|
+
* collateralParams: [
|
|
460
|
+
* {
|
|
461
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
462
|
+
* lltv: 770000000000000000n,
|
|
463
|
+
* liquidationCursor: 250000000000000000n,
|
|
464
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
465
|
+
* },
|
|
466
|
+
* ],
|
|
467
|
+
* maturity: 54_000n,
|
|
468
|
+
* rcfThreshold: 0n,
|
|
469
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
470
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
471
|
+
* },
|
|
472
|
+
* totalUnits: 1_000n,
|
|
473
|
+
* lossFactor: 0n,
|
|
474
|
+
* withdrawable: 500n,
|
|
475
|
+
* continuousFeeCredit: 0n,
|
|
476
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
477
|
+
* continuousFee: 10,
|
|
478
|
+
* tickSpacing: 4,
|
|
479
|
+
* },
|
|
480
|
+
* buy: true,
|
|
481
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
482
|
+
* start: 0n,
|
|
483
|
+
* expiry: 3_600n,
|
|
484
|
+
* tick: 5_000n,
|
|
485
|
+
* callback: "0x0000000000000000000000000000000000000000",
|
|
486
|
+
* callbackData: "0x",
|
|
487
|
+
* receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000000",
|
|
488
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
489
|
+
* reduceOnly: false,
|
|
490
|
+
* maxUnits: 100n,
|
|
491
|
+
* maxAssets: 0n,
|
|
492
|
+
* continuousFeeCap: 317097919n,
|
|
493
|
+
* });
|
|
494
|
+
* const consumed = await readContract(client, {
|
|
495
|
+
* address: "0x0000000000000000000000000000000000001000",
|
|
496
|
+
* abi: midnightAbi,
|
|
497
|
+
* functionName: "consumed",
|
|
498
|
+
* args: [offer.maker, offer.group],
|
|
499
|
+
* });
|
|
500
|
+
* const units = offer.getConsumableUnits({ consumed, timestamp: 1_000n });
|
|
501
|
+
* console.log(units);
|
|
502
|
+
* ```
|
|
503
|
+
*/
|
|
504
|
+
getConsumableUnits(params: {
|
|
505
|
+
readonly consumed: BigIntish;
|
|
506
|
+
readonly timestamp: BigIntish;
|
|
507
|
+
}): bigint;
|
|
308
508
|
/**
|
|
309
509
|
* Creates a validated maker-side Midnight offer.
|
|
310
510
|
*
|
|
@@ -347,12 +547,14 @@ export declare class Offer {
|
|
|
347
547
|
*
|
|
348
548
|
* const offer = Offer.create({
|
|
349
549
|
* market: {
|
|
550
|
+
* chainId: 8453,
|
|
551
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
350
552
|
* loanToken,
|
|
351
553
|
* collateralParams: [
|
|
352
554
|
* {
|
|
353
555
|
* token: collateralToken,
|
|
354
556
|
* lltv: 770000000000000000n,
|
|
355
|
-
*
|
|
557
|
+
* liquidationCursor: 250000000000000000n,
|
|
356
558
|
* oracle,
|
|
357
559
|
* },
|
|
358
560
|
* ],
|
|
@@ -390,12 +592,14 @@ export declare class Offer {
|
|
|
390
592
|
*
|
|
391
593
|
* const offer: OfferStruct = {
|
|
392
594
|
* market: {
|
|
595
|
+
* chainId: 8453,
|
|
596
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
393
597
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
394
598
|
* collateralParams: [
|
|
395
599
|
* {
|
|
396
600
|
* token: "0x0000000000000000000000000000000000007000",
|
|
397
601
|
* lltv: 770000000000000000n,
|
|
398
|
-
*
|
|
602
|
+
* liquidationCursor: 250000000000000000n,
|
|
399
603
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
400
604
|
* },
|
|
401
605
|
* ],
|
|
@@ -470,12 +674,14 @@ export interface OfferStruct {
|
|
|
470
674
|
*
|
|
471
675
|
* const params: BuildOfferParams = {
|
|
472
676
|
* market: {
|
|
677
|
+
* chainId: 8453,
|
|
678
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
473
679
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
474
680
|
* collateralParams: [
|
|
475
681
|
* {
|
|
476
682
|
* token: "0x0000000000000000000000000000000000007000",
|
|
477
683
|
* lltv: 770000000000000000n,
|
|
478
|
-
*
|
|
684
|
+
* liquidationCursor: 250000000000000000n,
|
|
479
685
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
480
686
|
* },
|
|
481
687
|
* ],
|
package/lib/esm/offers/Offer.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { zeroAddress } from "viem";
|
|
2
|
-
import { MarketParams, } from "../market/index.js";
|
|
2
|
+
import { Market, MarketParams, } from "../market/index.js";
|
|
3
3
|
import { OfferUtils } from "./OfferUtils.js";
|
|
4
4
|
/**
|
|
5
5
|
* Maker-side Midnight offer.
|
|
@@ -19,12 +19,14 @@ import { OfferUtils } from "./OfferUtils.js";
|
|
|
19
19
|
*
|
|
20
20
|
* const offer = Offer.create({
|
|
21
21
|
* market: {
|
|
22
|
+
* chainId: 8453,
|
|
23
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
22
24
|
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
23
25
|
* collateralParams: [
|
|
24
26
|
* {
|
|
25
27
|
* token: "0x0000000000000000000000000000000000007000",
|
|
26
28
|
* lltv: 770000000000000000n,
|
|
27
|
-
*
|
|
29
|
+
* liquidationCursor: 250000000000000000n,
|
|
28
30
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
29
31
|
* },
|
|
30
32
|
* ],
|
|
@@ -82,7 +84,10 @@ export class Offer {
|
|
|
82
84
|
/** Maximum market continuous fee accepted by this offer. */
|
|
83
85
|
continuousFeeCap;
|
|
84
86
|
constructor(offer) {
|
|
85
|
-
this.market =
|
|
87
|
+
this.market =
|
|
88
|
+
"params" in offer.market
|
|
89
|
+
? Market.from(offer.market)
|
|
90
|
+
: MarketParams.from(offer.market);
|
|
86
91
|
this.buy = offer.buy;
|
|
87
92
|
this.maker = offer.maker;
|
|
88
93
|
this.start = BigInt(offer.start);
|
|
@@ -114,12 +119,14 @@ export class Offer {
|
|
|
114
119
|
*
|
|
115
120
|
* const offer = Offer.from({
|
|
116
121
|
* market: {
|
|
122
|
+
* chainId: 8453,
|
|
123
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
117
124
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
118
125
|
* collateralParams: [
|
|
119
126
|
* {
|
|
120
127
|
* token: "0x0000000000000000000000000000000000007000",
|
|
121
128
|
* lltv: 770000000000000000n,
|
|
122
|
-
*
|
|
129
|
+
* liquidationCursor: 250000000000000000n,
|
|
123
130
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
124
131
|
* },
|
|
125
132
|
* ],
|
|
@@ -169,12 +176,14 @@ export class Offer {
|
|
|
169
176
|
*
|
|
170
177
|
* const offer = Offer.create({
|
|
171
178
|
* market: {
|
|
179
|
+
* chainId: 8453,
|
|
180
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
172
181
|
* loanToken,
|
|
173
182
|
* collateralParams: [
|
|
174
183
|
* {
|
|
175
184
|
* token: collateralToken,
|
|
176
185
|
* lltv: 770000000000000000n,
|
|
177
|
-
*
|
|
186
|
+
* liquidationCursor: 250000000000000000n,
|
|
178
187
|
* oracle,
|
|
179
188
|
* },
|
|
180
189
|
* ],
|
|
@@ -192,7 +201,7 @@ export class Offer {
|
|
|
192
201
|
* });
|
|
193
202
|
*
|
|
194
203
|
* const group = offer.group;
|
|
195
|
-
*
|
|
204
|
+
* console.log(group);
|
|
196
205
|
* ```
|
|
197
206
|
*/
|
|
198
207
|
get group() {
|
|
@@ -219,12 +228,14 @@ export class Offer {
|
|
|
219
228
|
*
|
|
220
229
|
* const offer = Offer.create({
|
|
221
230
|
* market: {
|
|
231
|
+
* chainId: 8453,
|
|
232
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
222
233
|
* loanToken,
|
|
223
234
|
* collateralParams: [
|
|
224
235
|
* {
|
|
225
236
|
* token: collateralToken,
|
|
226
237
|
* lltv: 770000000000000000n,
|
|
227
|
-
*
|
|
238
|
+
* liquidationCursor: 250000000000000000n,
|
|
228
239
|
* oracle,
|
|
229
240
|
* },
|
|
230
241
|
* ],
|
|
@@ -249,6 +260,201 @@ export class Offer {
|
|
|
249
260
|
this.cachedHash ??= OfferUtils.hash(this);
|
|
250
261
|
return this.cachedHash;
|
|
251
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* WAD zero-coupon price at this offer's tick.
|
|
265
|
+
*
|
|
266
|
+
* @returns WAD price rounded to the protocol price quantum.
|
|
267
|
+
* @throws {NegativeValueError} when `tick` is negative.
|
|
268
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
269
|
+
* @example
|
|
270
|
+
* ```ts
|
|
271
|
+
* import { Offer } from "@morpho-org/midnight-sdk";
|
|
272
|
+
*
|
|
273
|
+
* const offer = Offer.create({
|
|
274
|
+
* market: {
|
|
275
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
276
|
+
* collateralParams: [
|
|
277
|
+
* {
|
|
278
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
279
|
+
* lltv: 770000000000000000n,
|
|
280
|
+
* liquidationCursor: 250000000000000000n,
|
|
281
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
282
|
+
* },
|
|
283
|
+
* ],
|
|
284
|
+
* maturity: 54_000n,
|
|
285
|
+
* rcfThreshold: 0n,
|
|
286
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
287
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
288
|
+
* },
|
|
289
|
+
* buy: true,
|
|
290
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
291
|
+
* tick: 5_000n,
|
|
292
|
+
* expiry: 3_600n,
|
|
293
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
294
|
+
* maxUnits: 100n,
|
|
295
|
+
* });
|
|
296
|
+
* console.log(offer.price);
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
get price() {
|
|
300
|
+
return OfferUtils.getPrice(this);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Converts this offer's tick into a WAD per-second simple rate at a timestamp.
|
|
304
|
+
*
|
|
305
|
+
* @param timestamp - Timestamp at which the rate is calculated.
|
|
306
|
+
* @returns WAD per-second simple rate rounded up.
|
|
307
|
+
* @throws {NegativeValueError} when market maturity, `timestamp`, or `tick` is negative.
|
|
308
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
309
|
+
* @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
|
|
310
|
+
* @example
|
|
311
|
+
* ```ts
|
|
312
|
+
* import { Offer } from "@morpho-org/midnight-sdk";
|
|
313
|
+
*
|
|
314
|
+
* const offer = Offer.create({
|
|
315
|
+
* market: {
|
|
316
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
317
|
+
* collateralParams: [
|
|
318
|
+
* {
|
|
319
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
320
|
+
* lltv: 770000000000000000n,
|
|
321
|
+
* liquidationCursor: 250000000000000000n,
|
|
322
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
323
|
+
* },
|
|
324
|
+
* ],
|
|
325
|
+
* maturity: 54_000n,
|
|
326
|
+
* rcfThreshold: 0n,
|
|
327
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
328
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
329
|
+
* },
|
|
330
|
+
* buy: true,
|
|
331
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
332
|
+
* tick: 5_000n,
|
|
333
|
+
* expiry: 3_600n,
|
|
334
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
335
|
+
* maxUnits: 100n,
|
|
336
|
+
* });
|
|
337
|
+
* const rate = offer.getRate(1_000n);
|
|
338
|
+
* console.log(rate);
|
|
339
|
+
* ```
|
|
340
|
+
*/
|
|
341
|
+
getRate(timestamp) {
|
|
342
|
+
return OfferUtils.getRate({ offer: this, timestamp });
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Converts this offer's tick into a WAD simple annual percentage rate at a timestamp.
|
|
346
|
+
*
|
|
347
|
+
* @param timestamp - Timestamp at which the APR is calculated.
|
|
348
|
+
* @returns WAD simple APR rounded up.
|
|
349
|
+
* @throws {NegativeValueError} when market maturity, `timestamp`, or `tick` is negative.
|
|
350
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
351
|
+
* @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
|
|
352
|
+
* @example
|
|
353
|
+
* ```ts
|
|
354
|
+
* import { Offer } from "@morpho-org/midnight-sdk";
|
|
355
|
+
*
|
|
356
|
+
* const offer = Offer.create({
|
|
357
|
+
* market: {
|
|
358
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
359
|
+
* collateralParams: [
|
|
360
|
+
* {
|
|
361
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
362
|
+
* lltv: 770000000000000000n,
|
|
363
|
+
* liquidationCursor: 250000000000000000n,
|
|
364
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
365
|
+
* },
|
|
366
|
+
* ],
|
|
367
|
+
* maturity: 54_000n,
|
|
368
|
+
* rcfThreshold: 0n,
|
|
369
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
370
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
371
|
+
* },
|
|
372
|
+
* buy: true,
|
|
373
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
374
|
+
* tick: 5_000n,
|
|
375
|
+
* expiry: 3_600n,
|
|
376
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
377
|
+
* maxUnits: 100n,
|
|
378
|
+
* });
|
|
379
|
+
* const apr = offer.getApr(1_000n);
|
|
380
|
+
* console.log(apr);
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
getApr(timestamp) {
|
|
384
|
+
return OfferUtils.getApr({ offer: this, timestamp });
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Returns remaining units accepted by this offer's cap at a timestamp.
|
|
388
|
+
*
|
|
389
|
+
* The offer must carry a hydrated {@link Market} so the current market
|
|
390
|
+
* continuous fee and settlement-fee buckets are available locally. Fetch the
|
|
391
|
+
* current `consumed(maker, group)` value separately and pass it as input.
|
|
392
|
+
*
|
|
393
|
+
* @param params.consumed - Amount already consumed from this offer's group.
|
|
394
|
+
* @param params.timestamp - Timestamp used to compute the market time to maturity.
|
|
395
|
+
* @returns Remaining consumable units accepted by Midnight `take`.
|
|
396
|
+
* @throws {InvalidOfferParameterError} when this offer does not carry hydrated market state or has invalid caps.
|
|
397
|
+
* @throws {NegativeValueError} when `consumed`, `timestamp`, offer limits, or delegated math inputs are negative.
|
|
398
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
399
|
+
* @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy-offer price.
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* import { Offer } from "@morpho-org/midnight-sdk";
|
|
403
|
+
*
|
|
404
|
+
* const offer = Offer.from({
|
|
405
|
+
* market: {
|
|
406
|
+
* params: {
|
|
407
|
+
* chainId: 8453,
|
|
408
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
409
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
410
|
+
* collateralParams: [
|
|
411
|
+
* {
|
|
412
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
413
|
+
* lltv: 770000000000000000n,
|
|
414
|
+
* liquidationCursor: 250000000000000000n,
|
|
415
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
416
|
+
* },
|
|
417
|
+
* ],
|
|
418
|
+
* maturity: 54_000n,
|
|
419
|
+
* rcfThreshold: 0n,
|
|
420
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
421
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
422
|
+
* },
|
|
423
|
+
* totalUnits: 1_000n,
|
|
424
|
+
* lossFactor: 0n,
|
|
425
|
+
* withdrawable: 500n,
|
|
426
|
+
* continuousFeeCredit: 0n,
|
|
427
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
428
|
+
* continuousFee: 10,
|
|
429
|
+
* tickSpacing: 4,
|
|
430
|
+
* },
|
|
431
|
+
* buy: true,
|
|
432
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
433
|
+
* start: 0n,
|
|
434
|
+
* expiry: 3_600n,
|
|
435
|
+
* tick: 5_000n,
|
|
436
|
+
* callback: "0x0000000000000000000000000000000000000000",
|
|
437
|
+
* callbackData: "0x",
|
|
438
|
+
* receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000000",
|
|
439
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
440
|
+
* reduceOnly: false,
|
|
441
|
+
* maxUnits: 100n,
|
|
442
|
+
* maxAssets: 0n,
|
|
443
|
+
* continuousFeeCap: 317097919n,
|
|
444
|
+
* });
|
|
445
|
+
* const consumed = await readContract(client, {
|
|
446
|
+
* address: "0x0000000000000000000000000000000000001000",
|
|
447
|
+
* abi: midnightAbi,
|
|
448
|
+
* functionName: "consumed",
|
|
449
|
+
* args: [offer.maker, offer.group],
|
|
450
|
+
* });
|
|
451
|
+
* const units = offer.getConsumableUnits({ consumed, timestamp: 1_000n });
|
|
452
|
+
* console.log(units);
|
|
453
|
+
* ```
|
|
454
|
+
*/
|
|
455
|
+
getConsumableUnits(params) {
|
|
456
|
+
return OfferUtils.getConsumableUnits({ offer: this, ...params });
|
|
457
|
+
}
|
|
252
458
|
/**
|
|
253
459
|
* Creates a validated maker-side Midnight offer.
|
|
254
460
|
*
|
|
@@ -291,12 +497,14 @@ export class Offer {
|
|
|
291
497
|
*
|
|
292
498
|
* const offer = Offer.create({
|
|
293
499
|
* market: {
|
|
500
|
+
* chainId: 8453,
|
|
501
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
294
502
|
* loanToken,
|
|
295
503
|
* collateralParams: [
|
|
296
504
|
* {
|
|
297
505
|
* token: collateralToken,
|
|
298
506
|
* lltv: 770000000000000000n,
|
|
299
|
-
*
|
|
507
|
+
* liquidationCursor: 250000000000000000n,
|
|
300
508
|
* oracle,
|
|
301
509
|
* },
|
|
302
510
|
* ],
|