@morpho-org/midnight-sdk 1.1.1 → 1.2.1

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 (53) hide show
  1. package/lib/cjs/api/helpers.js +2 -4
  2. package/lib/cjs/api/version.generated.d.ts +2 -0
  3. package/lib/cjs/api/version.generated.js +6 -0
  4. package/lib/cjs/fetch/Position.d.ts +1 -1
  5. package/lib/cjs/fetch/Position.js +5 -1
  6. package/lib/cjs/index.d.ts +1 -0
  7. package/lib/cjs/index.js +1 -0
  8. package/lib/cjs/market/index.d.ts +0 -2
  9. package/lib/cjs/market/index.js +0 -2
  10. package/lib/cjs/offers/Offer.d.ts +14 -10
  11. package/lib/cjs/offers/Offer.js +11 -7
  12. package/lib/cjs/offers/OfferChainUtils.d.ts +179 -0
  13. package/lib/cjs/offers/OfferChainUtils.js +446 -0
  14. package/lib/cjs/offers/index.d.ts +1 -0
  15. package/lib/cjs/offers/index.js +1 -0
  16. package/lib/cjs/{market → position}/Position.d.ts +83 -6
  17. package/lib/cjs/{market → position}/Position.js +83 -5
  18. package/lib/{esm/market → cjs/position}/PositionUtils.d.ts +26 -1
  19. package/lib/cjs/{market → position}/PositionUtils.js +27 -0
  20. package/lib/cjs/position/index.d.ts +2 -0
  21. package/lib/cjs/position/index.js +18 -0
  22. package/lib/cjs/signatures/RatifierUtils.js +4 -5
  23. package/lib/cjs/signatures/Tree.d.ts +3 -2
  24. package/lib/cjs/signatures/Tree.js +7 -4
  25. package/lib/cjs/signatures/TreeUtils.d.ts +4 -7
  26. package/lib/cjs/signatures/TreeUtils.js +22 -16
  27. package/lib/esm/api/helpers.js +1 -3
  28. package/lib/esm/api/version.generated.d.ts +2 -0
  29. package/lib/esm/api/version.generated.js +3 -0
  30. package/lib/esm/fetch/Position.d.ts +1 -1
  31. package/lib/esm/fetch/Position.js +5 -1
  32. package/lib/esm/index.d.ts +1 -0
  33. package/lib/esm/index.js +1 -0
  34. package/lib/esm/market/index.d.ts +0 -2
  35. package/lib/esm/market/index.js +0 -2
  36. package/lib/esm/offers/Offer.d.ts +14 -10
  37. package/lib/esm/offers/Offer.js +12 -8
  38. package/lib/esm/offers/OfferChainUtils.d.ts +179 -0
  39. package/lib/esm/offers/OfferChainUtils.js +443 -0
  40. package/lib/esm/offers/index.d.ts +1 -0
  41. package/lib/esm/offers/index.js +1 -0
  42. package/lib/esm/{market → position}/Position.d.ts +83 -6
  43. package/lib/esm/{market → position}/Position.js +83 -5
  44. package/lib/{cjs/market → esm/position}/PositionUtils.d.ts +26 -1
  45. package/lib/esm/{market → position}/PositionUtils.js +27 -0
  46. package/lib/esm/position/index.d.ts +2 -0
  47. package/lib/esm/position/index.js +2 -0
  48. package/lib/esm/signatures/RatifierUtils.js +5 -6
  49. package/lib/esm/signatures/Tree.d.ts +3 -2
  50. package/lib/esm/signatures/Tree.js +8 -5
  51. package/lib/esm/signatures/TreeUtils.d.ts +4 -7
  52. package/lib/esm/signatures/TreeUtils.js +22 -16
  53. package/package.json +4 -3
@@ -12,10 +12,8 @@ const morpho_ts_1 = require("@morpho-org/morpho-ts");
12
12
  const viem_1 = require("viem");
13
13
  const errors_js_1 = require("../errors.js");
14
14
  const index_js_1 = require("../market/index.js");
15
+ const version_generated_js_1 = require("./version.generated.js");
15
16
  const DEFAULT_MIDNIGHT_API_URL = new URL("/v0/midnight", morpho_ts_1.BLUE_API_BASE_URL);
16
- // Keep this literal in source so the browser ESM build does not import package.json
17
- // through Node-only createRequire/module shims.
18
- const MIDNIGHT_SDK_VERSION = "1.0.1";
19
17
  /** @internal Sends one Midnight API request and maps non-2xx responses to SDK errors. */
20
18
  async function requestMidnightApi(params) {
21
19
  const baseUrl = new URL(params.baseUrl ?? DEFAULT_MIDNIGHT_API_URL);
@@ -46,7 +44,7 @@ async function requestMidnightApi(params) {
46
44
  const headers = new Headers(params.request?.headers);
47
45
  if (params.body !== undefined)
48
46
  headers.set("Content-Type", "application/json");
49
- headers.set("sdk-version", MIDNIGHT_SDK_VERSION);
47
+ headers.set("sdk-version", version_generated_js_1.MIDNIGHT_SDK_VERSION);
50
48
  const init = {
51
49
  ...params.request,
52
50
  method: params.method,
@@ -0,0 +1,2 @@
1
+ /** @internal Installed Midnight SDK package version. */
2
+ export declare const MIDNIGHT_SDK_VERSION = "1.2.1";
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ // Generated by scripts/release/generate-midnight-package-version.mjs. Do not edit.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.MIDNIGHT_SDK_VERSION = void 0;
5
+ /** @internal Installed Midnight SDK package version. */
6
+ exports.MIDNIGHT_SDK_VERSION = "1.2.1";
@@ -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);
@@ -9,9 +9,9 @@ import { type IMarket, type IMarketParams, Market, MarketParams } from "../marke
9
9
  * take-side conversion helpers. Use {@link Offer.create} instead for new maker
10
10
  * offers so deterministic fields are validated before grouping or signing.
11
11
  * The `group` field is optional. When omitted, {@link Offer.group} lazily
12
- * derives the standalone group id from this offer hashed with the protocol
13
- * zero group id. `Group.create` copies offers into a shared group and overrides
14
- * this field on the copies it owns.
12
+ * derives the content-addressed singleton group id from this offer's
13
+ * zero-group hash. `Group.create` copies offers into a shared group and
14
+ * overrides this field on the copies it owns.
15
15
  *
16
16
  * @example
17
17
  * ```ts
@@ -64,7 +64,7 @@ export interface IOffer {
64
64
  readonly expiry: BigIntish;
65
65
  /** Midnight tick. */
66
66
  readonly tick: BigIntish;
67
- /** Consumption group id; defaults to the offer hash with the zero group id. */
67
+ /** Consumption group id; defaults to the content-addressed singleton group id. */
68
68
  readonly group?: Hash;
69
69
  /** Optional maker callback. */
70
70
  readonly callback: Address;
@@ -90,10 +90,11 @@ export interface IOffer {
90
90
  * `Group.create` for shared-consumption ladders or directly to `Tree.create`
91
91
  * for standalone offers. API/take-side code can convert a plain `IOffer`
92
92
  * into this class before ABI encoding. The class resolves the onchain `group`
93
- * field lazily. Standalone offers derive their group id from the offer hash
94
- * with the zero group id; `Group.create` copies offers and overrides the group
95
- * on those copies when several offers share one consumption bucket. Offers
96
- * hydrated from API or decoded data may also carry a known group id.
93
+ * field lazily. Standalone offers derive their content-addressed singleton
94
+ * group id from the offer's zero-group hash; `Group.create` copies offers and
95
+ * overrides the group on those copies when several offers share one
96
+ * consumption bucket. Offers hydrated from API or decoded data may also carry
97
+ * a known group id.
97
98
  *
98
99
  * @example
99
100
  * ```ts
@@ -220,8 +221,11 @@ export declare class Offer {
220
221
  * Consumption group id encoded into this offer.
221
222
  *
222
223
  * When no group id was provided, the value is computed on first access by
223
- * hashing this offer with the protocol zero group id. The computed value is
224
- * cached because offer hashing is resource-intensive.
224
+ * hashing this offer with the protocol zero group id, then hashing that
225
+ * result as a singleton group. The computed value is cached because offer
226
+ * hashing is resource-intensive. This is equivalent to
227
+ * `GroupUtils.hash([offer])`, but is computed directly to keep the offers
228
+ * layer from importing signature helpers and creating a circular dependency.
225
229
  *
226
230
  * @returns Consumption group id.
227
231
  * @example
@@ -11,10 +11,11 @@ const OfferUtils_js_1 = require("./OfferUtils.js");
11
11
  * `Group.create` for shared-consumption ladders or directly to `Tree.create`
12
12
  * for standalone offers. API/take-side code can convert a plain `IOffer`
13
13
  * into this class before ABI encoding. The class resolves the onchain `group`
14
- * field lazily. Standalone offers derive their group id from the offer hash
15
- * with the zero group id; `Group.create` copies offers and overrides the group
16
- * on those copies when several offers share one consumption bucket. Offers
17
- * hydrated from API or decoded data may also carry a known group id.
14
+ * field lazily. Standalone offers derive their content-addressed singleton
15
+ * group id from the offer's zero-group hash; `Group.create` copies offers and
16
+ * overrides the group on those copies when several offers share one
17
+ * consumption bucket. Offers hydrated from API or decoded data may also carry
18
+ * a known group id.
18
19
  *
19
20
  * @example
20
21
  * ```ts
@@ -162,8 +163,11 @@ class Offer {
162
163
  * Consumption group id encoded into this offer.
163
164
  *
164
165
  * When no group id was provided, the value is computed on first access by
165
- * hashing this offer with the protocol zero group id. The computed value is
166
- * cached because offer hashing is resource-intensive.
166
+ * hashing this offer with the protocol zero group id, then hashing that
167
+ * result as a singleton group. The computed value is cached because offer
168
+ * hashing is resource-intensive. This is equivalent to
169
+ * `GroupUtils.hash([offer])`, but is computed directly to keep the offers
170
+ * layer from importing signature helpers and creating a circular dependency.
167
171
  *
168
172
  * @returns Consumption group id.
169
173
  * @example
@@ -208,7 +212,7 @@ class Offer {
208
212
  * ```
209
213
  */
210
214
  get group() {
211
- this.cachedGroup ??= OfferUtils_js_1.OfferUtils.groupHash(this);
215
+ this.cachedGroup ??= (0, viem_1.keccak256)(OfferUtils_js_1.OfferUtils.groupHash(this));
212
216
  return this.cachedGroup;
213
217
  }
214
218
  /**
@@ -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
+ }