@morpho-org/midnight-sdk 1.1.1 → 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 (41) hide show
  1. package/lib/cjs/fetch/Position.d.ts +1 -1
  2. package/lib/cjs/fetch/Position.js +5 -1
  3. package/lib/cjs/index.d.ts +1 -0
  4. package/lib/cjs/index.js +1 -0
  5. package/lib/cjs/market/index.d.ts +0 -2
  6. package/lib/cjs/market/index.js +0 -2
  7. package/lib/cjs/offers/OfferChainUtils.d.ts +179 -0
  8. package/lib/cjs/offers/OfferChainUtils.js +446 -0
  9. package/lib/cjs/offers/index.d.ts +1 -0
  10. package/lib/cjs/offers/index.js +1 -0
  11. package/lib/cjs/{market → position}/Position.d.ts +83 -6
  12. package/lib/cjs/{market → position}/Position.js +83 -5
  13. package/lib/{esm/market → cjs/position}/PositionUtils.d.ts +26 -1
  14. package/lib/cjs/{market → position}/PositionUtils.js +27 -0
  15. package/lib/cjs/position/index.d.ts +2 -0
  16. package/lib/cjs/position/index.js +18 -0
  17. package/lib/cjs/signatures/RatifierUtils.js +2 -1
  18. package/lib/cjs/signatures/Tree.js +3 -1
  19. package/lib/cjs/signatures/TreeUtils.d.ts +2 -2
  20. package/lib/cjs/signatures/TreeUtils.js +17 -8
  21. package/lib/esm/fetch/Position.d.ts +1 -1
  22. package/lib/esm/fetch/Position.js +5 -1
  23. package/lib/esm/index.d.ts +1 -0
  24. package/lib/esm/index.js +1 -0
  25. package/lib/esm/market/index.d.ts +0 -2
  26. package/lib/esm/market/index.js +0 -2
  27. package/lib/esm/offers/OfferChainUtils.d.ts +179 -0
  28. package/lib/esm/offers/OfferChainUtils.js +443 -0
  29. package/lib/esm/offers/index.d.ts +1 -0
  30. package/lib/esm/offers/index.js +1 -0
  31. package/lib/esm/{market → position}/Position.d.ts +83 -6
  32. package/lib/esm/{market → position}/Position.js +83 -5
  33. package/lib/{cjs/market → esm/position}/PositionUtils.d.ts +26 -1
  34. package/lib/esm/{market → position}/PositionUtils.js +27 -0
  35. package/lib/esm/position/index.d.ts +2 -0
  36. package/lib/esm/position/index.js +2 -0
  37. package/lib/esm/signatures/RatifierUtils.js +2 -1
  38. package/lib/esm/signatures/Tree.js +3 -1
  39. package/lib/esm/signatures/TreeUtils.d.ts +2 -2
  40. package/lib/esm/signatures/TreeUtils.js +17 -8
  41. package/package.json +2 -2
@@ -1,5 +1,5 @@
1
1
  import type { Address, Client, Hash } from "viem";
2
- import { AccrualPosition, Position } from "../market/index.js";
2
+ import { AccrualPosition, Position } from "../position/index.js";
3
3
  import type { MidnightFetchParams } from "./types.js";
4
4
  /**
5
5
  * Fetches a Midnight position by id and user.
@@ -6,7 +6,7 @@ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const actions_1 = require("viem/actions");
7
7
  const abis_js_1 = require("../abis.js");
8
8
  const constants_js_1 = require("../constants.js");
9
- const index_js_1 = require("../market/index.js");
9
+ const index_js_1 = require("../position/index.js");
10
10
  const GetPosition_js_1 = require("../queries/GetPosition.js");
11
11
  const Market_js_1 = require("./Market.js");
12
12
  const utils_js_1 = require("./utils.js");
@@ -64,6 +64,8 @@ async function fetchPosition(client, params) {
64
64
  });
65
65
  const collateral = position.collateral;
66
66
  return new index_js_1.Position({
67
+ user: params.user,
68
+ marketId: params.marketId,
67
69
  credit: position.credit,
68
70
  pendingFee: position.pendingFee,
69
71
  lastLossFactor: position.lastLossFactor,
@@ -96,6 +98,8 @@ async function fetchPosition(client, params) {
96
98
  });
97
99
  }));
98
100
  return new index_js_1.Position({
101
+ user: params.user,
102
+ marketId: params.marketId,
99
103
  credit: position[0],
100
104
  pendingFee: position[1],
101
105
  lastLossFactor: position[2],
@@ -5,6 +5,7 @@ export * from "./fetch/index.js";
5
5
  export * from "./market/index.js";
6
6
  export * from "./math/index.js";
7
7
  export * from "./offers/index.js";
8
+ export * from "./position/index.js";
8
9
  export * from "./signatures/index.js";
9
10
  /**
10
11
  * Namespace for Midnight mempool payload utilities.
package/lib/cjs/index.js CHANGED
@@ -22,6 +22,7 @@ __exportStar(require("./fetch/index.js"), exports);
22
22
  __exportStar(require("./market/index.js"), exports);
23
23
  __exportStar(require("./math/index.js"), exports);
24
24
  __exportStar(require("./offers/index.js"), exports);
25
+ __exportStar(require("./position/index.js"), exports);
25
26
  __exportStar(require("./signatures/index.js"), exports);
26
27
  /**
27
28
  * Namespace for Midnight mempool payload utilities.
@@ -1,4 +1,2 @@
1
1
  export * from "./Market.js";
2
2
  export * from "./MarketUtils.js";
3
- export * from "./Position.js";
4
- export * from "./PositionUtils.js";
@@ -16,5 +16,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./Market.js"), exports);
18
18
  __exportStar(require("./MarketUtils.js"), exports);
19
- __exportStar(require("./Position.js"), exports);
20
- __exportStar(require("./PositionUtils.js"), exports);
@@ -0,0 +1,179 @@
1
+ import { type BigIntish } from "@morpho-org/morpho-ts";
2
+ /** One time-bounded offer leg to convert into a Midnight `Offer.create` input. */
3
+ export interface FixedRateOfferChainLeg {
4
+ /** Spacing-aligned Midnight tick for this offer. */
5
+ readonly tick: bigint;
6
+ /** First timestamp at which the offer leg is active. */
7
+ readonly startTimestamp: bigint;
8
+ /** Last timestamp at which the offer leg is active. */
9
+ readonly expiryTimestamp: bigint;
10
+ }
11
+ /** Parameters shared by lend-only and borrow-only fixed-rate offer-chain builders. */
12
+ export interface BuildFixedRateOfferChainParams {
13
+ /** Target yearly fixed rate as a decimal number, for example `0.05` for 5%. */
14
+ readonly targetRate: number;
15
+ /** Tick spacing enforced by the market. */
16
+ readonly tickSpacing: BigIntish;
17
+ /** Market maturity timestamp in seconds. */
18
+ readonly maturityTimestamp: BigIntish;
19
+ /** First timestamp the chain should cover. */
20
+ readonly chainStartTimestamp: BigIntish;
21
+ /** Latest timestamp the chain may cover. */
22
+ readonly chainEndTimestamp: BigIntish;
23
+ }
24
+ /**
25
+ * Utilities for building time-bounded Midnight offer chains.
26
+ *
27
+ * A Midnight offer has one fixed price, so its displayed yearly rate changes
28
+ * as maturity approaches. The markets app uses these helpers when a maker wants
29
+ * to post, for example, a 5% order for several months: the app builds several
30
+ * adjacent offers sharing the same reserve, each with a different tick and time
31
+ * window, so the order reviews and renders as a stable 5% order across the
32
+ * selected window instead of drifting upward as time passes.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { OfferChainUtils } from "@morpho-org/midnight-sdk";
37
+ *
38
+ * const legs = OfferChainUtils.buildLendFixedRateOfferChain({
39
+ * targetRate: 0.05,
40
+ * tickSpacing: 4n,
41
+ * maturityTimestamp: 1_798_761_600n,
42
+ * chainStartTimestamp: 1_767_225_600n,
43
+ * chainEndTimestamp: 1_791_153_600n,
44
+ * });
45
+ * console.log(legs[0]?.tick);
46
+ * ```
47
+ */
48
+ export declare namespace OfferChainUtils {
49
+ /**
50
+ * Maximum fraction of the initial time-to-maturity covered by a fixed-rate offer chain.
51
+ *
52
+ * Use this cap before displaying an expiry picker for a fixed-rate maker
53
+ * order. The last part of the maturity window is intentionally excluded
54
+ * because tiny time changes cause large displayed-rate drift.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * import { OfferChainUtils } from "@morpho-org/midnight-sdk";
59
+ *
60
+ * console.log(OfferChainUtils.MAX_EXPIRY_TTM_FRACTION);
61
+ * ```
62
+ */
63
+ const MAX_EXPIRY_TTM_FRACTION = 0.75;
64
+ /**
65
+ * Builds borrow-side sell-offer legs that approximate one fixed maker rate over time.
66
+ *
67
+ * Use before a make-borrow or supply-collateral-and-make-borrow flow when a
68
+ * borrower wants one displayed fixed borrow rate across a longer window. Map
69
+ * the returned legs to `Offer.create({ buy: false, ... })`, then group and
70
+ * submit them through the maker flow.
71
+ *
72
+ * Borrow chains read their target rate at each leg's expiry edge. If a leg
73
+ * would need to extend past `chainEndTimestamp`, it is dropped rather than
74
+ * clamped so the expiry edge remains the recoverable target-rate edge.
75
+ *
76
+ * @param params - Fixed-rate offer-chain parameters without a side field.
77
+ * @param params.targetRate - Target yearly fixed rate expressed as a decimal number.
78
+ * @param params.tickSpacing - Tick spacing enforced by the market.
79
+ * @param params.maturityTimestamp - Market maturity timestamp in seconds.
80
+ * @param params.chainStartTimestamp - First timestamp the chain should cover.
81
+ * @param params.chainEndTimestamp - Latest timestamp the chain may cover.
82
+ * @returns Borrow-side offer legs that can be mapped to `Offer.create({ buy: false, ... })`.
83
+ * @throws {InvalidOfferParameterError} when an input is invalid or the end timestamp exceeds the supported horizon.
84
+ * @example
85
+ * ```ts
86
+ * import { Offer, OfferChainUtils } from "@morpho-org/midnight-sdk";
87
+ *
88
+ * const legs = OfferChainUtils.buildBorrowFixedRateOfferChain({
89
+ * targetRate: 0.08,
90
+ * tickSpacing: market.tickSpacing,
91
+ * maturityTimestamp: market.params.maturity,
92
+ * chainStartTimestamp: now,
93
+ * chainEndTimestamp: expiry,
94
+ * });
95
+ * const offers = legs.map((leg) =>
96
+ * Offer.create({
97
+ * ...baseOffer,
98
+ * buy: false,
99
+ * tick: leg.tick,
100
+ * start: leg.startTimestamp,
101
+ * expiry: leg.expiryTimestamp,
102
+ * }),
103
+ * );
104
+ * ```
105
+ */
106
+ function buildBorrowFixedRateOfferChain(params: BuildFixedRateOfferChainParams): readonly FixedRateOfferChainLeg[];
107
+ /**
108
+ * Builds lend-side buy-offer legs that approximate one fixed maker rate over time.
109
+ *
110
+ * Use before a make-lend flow when a lender wants one displayed fixed lend
111
+ * rate across a longer window. Map the returned legs to
112
+ * `Offer.create({ buy: true, ... })`, then group and submit them through the
113
+ * maker flow.
114
+ *
115
+ * Lend chains read their target rate at each leg's start edge. The first leg
116
+ * may start before `chainStartTimestamp`; clamping that edge would move the
117
+ * recoverable display rate below the maker's target.
118
+ *
119
+ * @param params - Fixed-rate offer-chain parameters without a side field.
120
+ * @param params.targetRate - Target yearly fixed rate expressed as a decimal number.
121
+ * @param params.tickSpacing - Tick spacing enforced by the market.
122
+ * @param params.maturityTimestamp - Market maturity timestamp in seconds.
123
+ * @param params.chainStartTimestamp - First timestamp the chain should cover.
124
+ * @param params.chainEndTimestamp - Latest timestamp the chain may cover.
125
+ * @returns Lend-side offer legs that can be mapped to `Offer.create({ buy: true, ... })`.
126
+ * @throws {InvalidOfferParameterError} when an input is invalid or the end timestamp exceeds the supported horizon.
127
+ * @example
128
+ * ```ts
129
+ * import { Offer, OfferChainUtils } from "@morpho-org/midnight-sdk";
130
+ *
131
+ * const legs = OfferChainUtils.buildLendFixedRateOfferChain({
132
+ * targetRate: 0.05,
133
+ * tickSpacing: market.tickSpacing,
134
+ * maturityTimestamp: market.params.maturity,
135
+ * chainStartTimestamp: now,
136
+ * chainEndTimestamp: expiry,
137
+ * });
138
+ * const offers = legs.map((leg) =>
139
+ * Offer.create({
140
+ * ...baseOffer,
141
+ * buy: true,
142
+ * tick: leg.tick,
143
+ * start: leg.startTimestamp,
144
+ * expiry: leg.expiryTimestamp,
145
+ * }),
146
+ * );
147
+ * ```
148
+ */
149
+ function buildLendFixedRateOfferChain(params: BuildFixedRateOfferChainParams): readonly FixedRateOfferChainLeg[];
150
+ /**
151
+ * Returns the latest supported end timestamp for a fixed-rate offer chain.
152
+ *
153
+ * Chains intentionally stop before the final part of the maturity window
154
+ * because rate sensitivity accelerates near maturity and would require too
155
+ * many short-lived offers for a stable maker quote.
156
+ * Call this when constraining a make-offer expiry selector, then pass the
157
+ * selected timestamp to the lend-only or borrow-only chain builder.
158
+ *
159
+ * @param params - Maturity and chain-start timestamps.
160
+ * @param params.maturityTimestamp - Market maturity timestamp in seconds.
161
+ * @param params.chainStartTimestamp - First timestamp the chain should cover.
162
+ * @returns Latest accepted chain end timestamp.
163
+ * @throws {InvalidOfferParameterError} when a timestamp is invalid.
164
+ * @example
165
+ * ```ts
166
+ * import { OfferChainUtils } from "@morpho-org/midnight-sdk";
167
+ *
168
+ * const maxExpiry = OfferChainUtils.getMaxFixedRateOfferChainEndTimestamp({
169
+ * maturityTimestamp: market.params.maturity,
170
+ * chainStartTimestamp: now,
171
+ * });
172
+ * console.log(maxExpiry);
173
+ * ```
174
+ */
175
+ function getMaxFixedRateOfferChainEndTimestamp(params: {
176
+ readonly maturityTimestamp: BigIntish;
177
+ readonly chainStartTimestamp: BigIntish;
178
+ }): bigint;
179
+ }