@morpho-org/midnight-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/README.md +252 -0
  2. package/lib/cjs/abis.d.ts +3170 -0
  3. package/lib/cjs/abis.js +4073 -0
  4. package/lib/cjs/api/MidnightApi.d.ts +532 -0
  5. package/lib/cjs/api/MidnightApi.js +713 -0
  6. package/lib/cjs/api/helpers.d.ts +20 -0
  7. package/lib/cjs/api/helpers.js +189 -0
  8. package/lib/cjs/api/index.d.ts +2 -0
  9. package/lib/cjs/api/index.js +21 -0
  10. package/lib/cjs/api/types.d.ts +453 -0
  11. package/lib/cjs/api/types.js +2 -0
  12. package/lib/cjs/constants.d.ts +168 -0
  13. package/lib/cjs/constants.js +187 -0
  14. package/lib/cjs/errors.d.ts +253 -0
  15. package/lib/cjs/errors.js +309 -0
  16. package/lib/cjs/fetch/Market.d.ts +65 -0
  17. package/lib/cjs/fetch/Market.js +117 -0
  18. package/lib/cjs/fetch/Offer.d.ts +74 -0
  19. package/lib/cjs/fetch/Offer.js +119 -0
  20. package/lib/cjs/fetch/Position.d.ts +85 -0
  21. package/lib/cjs/fetch/Position.js +148 -0
  22. package/lib/cjs/fetch/Ratifier.d.ts +38 -0
  23. package/lib/cjs/fetch/Ratifier.js +57 -0
  24. package/lib/cjs/fetch/index.d.ts +5 -0
  25. package/lib/cjs/fetch/index.js +21 -0
  26. package/lib/cjs/fetch/types.d.ts +57 -0
  27. package/lib/cjs/fetch/types.js +2 -0
  28. package/lib/cjs/fetch/utils.d.ts +8 -0
  29. package/lib/cjs/fetch/utils.js +18 -0
  30. package/lib/cjs/index.d.ts +18 -0
  31. package/lib/cjs/index.js +57 -0
  32. package/lib/cjs/market/Market.d.ts +613 -0
  33. package/lib/cjs/market/Market.js +503 -0
  34. package/lib/cjs/market/MarketUtils.d.ts +189 -0
  35. package/lib/cjs/market/MarketUtils.js +303 -0
  36. package/lib/cjs/market/Position.d.ts +424 -0
  37. package/lib/cjs/market/Position.js +425 -0
  38. package/lib/cjs/market/PositionUtils.d.ts +115 -0
  39. package/lib/cjs/market/PositionUtils.js +194 -0
  40. package/lib/cjs/market/index.d.ts +4 -0
  41. package/lib/cjs/market/index.js +20 -0
  42. package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
  43. package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
  44. package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
  45. package/lib/cjs/math/TakeAmountsLib.js +199 -0
  46. package/lib/cjs/math/TickLib.d.ts +200 -0
  47. package/lib/cjs/math/TickLib.js +272 -0
  48. package/lib/cjs/math/index.d.ts +3 -0
  49. package/lib/cjs/math/index.js +19 -0
  50. package/lib/cjs/offers/Offer.d.ts +534 -0
  51. package/lib/cjs/offers/Offer.js +343 -0
  52. package/lib/cjs/offers/OfferUtils.d.ts +565 -0
  53. package/lib/cjs/offers/OfferUtils.js +659 -0
  54. package/lib/cjs/offers/index.d.ts +2 -0
  55. package/lib/cjs/offers/index.js +18 -0
  56. package/lib/cjs/package.json +1 -0
  57. package/lib/cjs/queries/GetPosition.d.ts +55 -0
  58. package/lib/cjs/queries/GetPosition.js +74 -0
  59. package/lib/cjs/queries/index.d.ts +1 -0
  60. package/lib/cjs/queries/index.js +37 -0
  61. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  62. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
  63. package/lib/cjs/signatures/Group.d.ts +178 -0
  64. package/lib/cjs/signatures/Group.js +193 -0
  65. package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
  66. package/lib/cjs/signatures/GroupUtils.js +127 -0
  67. package/lib/cjs/signatures/Payload.d.ts +283 -0
  68. package/lib/cjs/signatures/Payload.js +570 -0
  69. package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
  70. package/lib/cjs/signatures/RatifierUtils.js +76 -0
  71. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
  72. package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
  73. package/lib/cjs/signatures/Tree.d.ts +241 -0
  74. package/lib/cjs/signatures/Tree.js +263 -0
  75. package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
  76. package/lib/cjs/signatures/TreeUtils.js +461 -0
  77. package/lib/cjs/signatures/index.d.ts +7 -0
  78. package/lib/cjs/signatures/index.js +23 -0
  79. package/lib/esm/abis.d.ts +3170 -0
  80. package/lib/esm/abis.js +4070 -0
  81. package/lib/esm/api/MidnightApi.d.ts +532 -0
  82. package/lib/esm/api/MidnightApi.js +709 -0
  83. package/lib/esm/api/helpers.d.ts +20 -0
  84. package/lib/esm/api/helpers.js +180 -0
  85. package/lib/esm/api/index.d.ts +2 -0
  86. package/lib/esm/api/index.js +2 -0
  87. package/lib/esm/api/types.d.ts +453 -0
  88. package/lib/esm/api/types.js +1 -0
  89. package/lib/esm/constants.d.ts +168 -0
  90. package/lib/esm/constants.js +184 -0
  91. package/lib/esm/errors.d.ts +253 -0
  92. package/lib/esm/errors.js +291 -0
  93. package/lib/esm/fetch/Market.d.ts +65 -0
  94. package/lib/esm/fetch/Market.js +113 -0
  95. package/lib/esm/fetch/Offer.d.ts +74 -0
  96. package/lib/esm/fetch/Offer.js +116 -0
  97. package/lib/esm/fetch/Position.d.ts +85 -0
  98. package/lib/esm/fetch/Position.js +144 -0
  99. package/lib/esm/fetch/Ratifier.d.ts +38 -0
  100. package/lib/esm/fetch/Ratifier.js +54 -0
  101. package/lib/esm/fetch/index.d.ts +5 -0
  102. package/lib/esm/fetch/index.js +5 -0
  103. package/lib/esm/fetch/types.d.ts +57 -0
  104. package/lib/esm/fetch/types.js +1 -0
  105. package/lib/esm/fetch/utils.d.ts +8 -0
  106. package/lib/esm/fetch/utils.js +12 -0
  107. package/lib/esm/index.d.ts +18 -0
  108. package/lib/esm/index.js +18 -0
  109. package/lib/esm/market/Market.d.ts +613 -0
  110. package/lib/esm/market/Market.js +498 -0
  111. package/lib/esm/market/MarketUtils.d.ts +189 -0
  112. package/lib/esm/market/MarketUtils.js +300 -0
  113. package/lib/esm/market/Position.d.ts +424 -0
  114. package/lib/esm/market/Position.js +420 -0
  115. package/lib/esm/market/PositionUtils.d.ts +115 -0
  116. package/lib/esm/market/PositionUtils.js +191 -0
  117. package/lib/esm/market/index.d.ts +4 -0
  118. package/lib/esm/market/index.js +4 -0
  119. package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
  120. package/lib/esm/math/ConsumableUnitsLib.js +74 -0
  121. package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
  122. package/lib/esm/math/TakeAmountsLib.js +196 -0
  123. package/lib/esm/math/TickLib.d.ts +200 -0
  124. package/lib/esm/math/TickLib.js +269 -0
  125. package/lib/esm/math/index.d.ts +3 -0
  126. package/lib/esm/math/index.js +3 -0
  127. package/lib/esm/offers/Offer.d.ts +534 -0
  128. package/lib/esm/offers/Offer.js +339 -0
  129. package/lib/esm/offers/OfferUtils.d.ts +565 -0
  130. package/lib/esm/offers/OfferUtils.js +656 -0
  131. package/lib/esm/offers/index.d.ts +2 -0
  132. package/lib/esm/offers/index.js +2 -0
  133. package/lib/esm/package.json +1 -0
  134. package/lib/esm/queries/GetPosition.d.ts +55 -0
  135. package/lib/esm/queries/GetPosition.js +71 -0
  136. package/lib/esm/queries/index.d.ts +1 -0
  137. package/lib/esm/queries/index.js +1 -0
  138. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  139. package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
  140. package/lib/esm/signatures/Group.d.ts +178 -0
  141. package/lib/esm/signatures/Group.js +189 -0
  142. package/lib/esm/signatures/GroupUtils.d.ts +191 -0
  143. package/lib/esm/signatures/GroupUtils.js +124 -0
  144. package/lib/esm/signatures/Payload.d.ts +283 -0
  145. package/lib/esm/signatures/Payload.js +564 -0
  146. package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
  147. package/lib/esm/signatures/RatifierUtils.js +73 -0
  148. package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
  149. package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
  150. package/lib/esm/signatures/Tree.d.ts +241 -0
  151. package/lib/esm/signatures/Tree.js +259 -0
  152. package/lib/esm/signatures/TreeUtils.d.ts +573 -0
  153. package/lib/esm/signatures/TreeUtils.js +458 -0
  154. package/lib/esm/signatures/index.d.ts +7 -0
  155. package/lib/esm/signatures/index.js +7 -0
  156. package/package.json +69 -0
  157. package/src/index.ts +19 -0
@@ -0,0 +1,85 @@
1
+ import type { Address, Client, Hash } from "viem";
2
+ import { AccrualPosition, Position } from "../market/index.js";
3
+ import type { MidnightFetchParams } from "./types.js";
4
+ /**
5
+ * Fetches a Midnight position by id and user.
6
+ *
7
+ * The Solidity storage getter does not return the fixed collateral array, so
8
+ * this helper reads each collateral slot before returning the position.
9
+ * The deployless path is used by default. If deployless is disabled or
10
+ * unavailable, the direct fallback can fan out to 130 RPC calls unless the viem
11
+ * client has `batch.multicall` enabled.
12
+ *
13
+ * Reads `eth_chainId` only when the viem client has no configured chain id. In
14
+ * deployless mode, reads the deployless `GetPosition.query(midnight, marketId,
15
+ * user)` helper. If deployless is disabled or falls back, reads
16
+ * `Midnight.position(marketId, user)` and each
17
+ * `Midnight.collateral(marketId, user, index)` slot for `index` 0 through 127.
18
+ *
19
+ * @param client - Viem client used for the reads.
20
+ * @param params.marketId - Market id whose position to read.
21
+ * @param params.user - Position owner address.
22
+ * @param params.deployless - Optional deployless mode; defaults to `true` with fallback, or `"force"` without fallback.
23
+ * @param params.account - Optional account used as the `from` field for the reads.
24
+ * @param params.blockNumber - Optional block number used for the reads.
25
+ * @param params.blockTag - Optional block tag used for the reads.
26
+ * @param params.stateOverride - Optional state override set used for the reads.
27
+ * @returns Normalized position object.
28
+ * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
29
+ * @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
30
+ * @example
31
+ * ```ts
32
+ * import { fetchPosition } from "@morpho-org/midnight-sdk";
33
+ * import { createPublicClient, http } from "viem";
34
+ * import { base } from "viem/chains";
35
+ *
36
+ * const client = createPublicClient({ chain: base, transport: http() });
37
+ * const position = await fetchPosition(client, {
38
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
39
+ * user: "0x0000000000000000000000000000000000009000",
40
+ * });
41
+ * console.log(position.debt);
42
+ * ```
43
+ */
44
+ export declare function fetchPosition(client: Client, params: MidnightFetchParams & {
45
+ readonly marketId: Hash;
46
+ readonly user: Address;
47
+ }): Promise<Position>;
48
+ /**
49
+ * Fetches a Midnight position paired with its hydrated market.
50
+ *
51
+ * Reads the full inventory of `fetchPosition` and `fetchMarket` at the same
52
+ * call parameters: `eth_chainId` only when needed,
53
+ * `Midnight.toMarket(marketId)`, `Midnight.marketState(marketId)`, and either
54
+ * deployless `GetPosition.query(midnight, marketId, user)` or direct
55
+ * `Midnight.position(marketId, user)` plus all collateral slots.
56
+ *
57
+ * @param client - Viem client used for the reads.
58
+ * @param params.marketId - Market id whose position and market state to read.
59
+ * @param params.user - Position owner address.
60
+ * @param params.deployless - Optional deployless mode for the position read; defaults to `true` with fallback, or `"force"` without fallback.
61
+ * @param params.account - Optional account used as the `from` field for the reads.
62
+ * @param params.blockNumber - Optional block number used for the reads.
63
+ * @param params.blockTag - Optional block tag used for the reads.
64
+ * @param params.stateOverride - Optional state override set used for the reads.
65
+ * @returns Accrual position instance.
66
+ * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
67
+ * @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
68
+ * @example
69
+ * ```ts
70
+ * import { fetchAccrualPosition } from "@morpho-org/midnight-sdk";
71
+ * import { createPublicClient, http } from "viem";
72
+ * import { base } from "viem/chains";
73
+ *
74
+ * const client = createPublicClient({ chain: base, transport: http() });
75
+ * const position = await fetchAccrualPosition(client, {
76
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
77
+ * user: "0x0000000000000000000000000000000000009000",
78
+ * });
79
+ * console.log(position.market.id);
80
+ * ```
81
+ */
82
+ export declare function fetchAccrualPosition(client: Client, params: MidnightFetchParams & {
83
+ readonly marketId: Hash;
84
+ readonly user: Address;
85
+ }): Promise<AccrualPosition>;
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchPosition = fetchPosition;
4
+ exports.fetchAccrualPosition = fetchAccrualPosition;
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const actions_1 = require("viem/actions");
7
+ const abis_js_1 = require("../abis.js");
8
+ const constants_js_1 = require("../constants.js");
9
+ const index_js_1 = require("../market/index.js");
10
+ const GetPosition_js_1 = require("../queries/GetPosition.js");
11
+ const Market_js_1 = require("./Market.js");
12
+ const utils_js_1 = require("./utils.js");
13
+ /**
14
+ * Fetches a Midnight position by id and user.
15
+ *
16
+ * The Solidity storage getter does not return the fixed collateral array, so
17
+ * this helper reads each collateral slot before returning the position.
18
+ * The deployless path is used by default. If deployless is disabled or
19
+ * unavailable, the direct fallback can fan out to 130 RPC calls unless the viem
20
+ * client has `batch.multicall` enabled.
21
+ *
22
+ * Reads `eth_chainId` only when the viem client has no configured chain id. In
23
+ * deployless mode, reads the deployless `GetPosition.query(midnight, marketId,
24
+ * user)` helper. If deployless is disabled or falls back, reads
25
+ * `Midnight.position(marketId, user)` and each
26
+ * `Midnight.collateral(marketId, user, index)` slot for `index` 0 through 127.
27
+ *
28
+ * @param client - Viem client used for the reads.
29
+ * @param params.marketId - Market id whose position to read.
30
+ * @param params.user - Position owner address.
31
+ * @param params.deployless - Optional deployless mode; defaults to `true` with fallback, or `"force"` without fallback.
32
+ * @param params.account - Optional account used as the `from` field for the reads.
33
+ * @param params.blockNumber - Optional block number used for the reads.
34
+ * @param params.blockTag - Optional block tag used for the reads.
35
+ * @param params.stateOverride - Optional state override set used for the reads.
36
+ * @returns Normalized position object.
37
+ * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
38
+ * @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
39
+ * @example
40
+ * ```ts
41
+ * import { fetchPosition } from "@morpho-org/midnight-sdk";
42
+ * import { createPublicClient, http } from "viem";
43
+ * import { base } from "viem/chains";
44
+ *
45
+ * const client = createPublicClient({ chain: base, transport: http() });
46
+ * const position = await fetchPosition(client, {
47
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
48
+ * user: "0x0000000000000000000000000000000000009000",
49
+ * });
50
+ * console.log(position.debt);
51
+ * ```
52
+ */
53
+ async function fetchPosition(client, params) {
54
+ const chainId = await (0, utils_js_1.resolveChainId)(client);
55
+ const midnight = (0, morpho_ts_1.getChainAddress)(chainId, "midnight");
56
+ if ((0, utils_js_1.shouldUseDeployless)(params)) {
57
+ try {
58
+ const position = await (0, actions_1.readContract)(client, {
59
+ ...(0, utils_js_1.callParameters)(params),
60
+ abi: GetPosition_js_1.abi,
61
+ code: GetPosition_js_1.code,
62
+ functionName: "query",
63
+ args: [midnight, params.marketId, params.user],
64
+ });
65
+ const collateral = position.collateral;
66
+ return new index_js_1.Position({
67
+ credit: position.credit,
68
+ pendingFee: position.pendingFee,
69
+ lastLossFactor: position.lastLossFactor,
70
+ lastAccrual: position.lastAccrual,
71
+ debt: position.debt,
72
+ collateralBitmap: position.collateralBitmap,
73
+ collateral: collateral.map((assets) => BigInt(assets)),
74
+ });
75
+ }
76
+ catch (error) {
77
+ if (params.deployless === "force")
78
+ throw error;
79
+ // Fallback to direct reads if deployless execution is unavailable.
80
+ }
81
+ }
82
+ const position = await (0, actions_1.readContract)(client, {
83
+ ...(0, utils_js_1.callParameters)(params),
84
+ address: midnight,
85
+ abi: abis_js_1.midnightAbi,
86
+ functionName: "position",
87
+ args: [params.marketId, params.user],
88
+ });
89
+ const collateral = await Promise.all(Array.from({ length: Number(constants_js_1.MAX_COLLATERALS) }, (_, collateralIndex) => {
90
+ return (0, actions_1.readContract)(client, {
91
+ ...(0, utils_js_1.callParameters)(params),
92
+ address: midnight,
93
+ abi: abis_js_1.midnightAbi,
94
+ functionName: "collateral",
95
+ args: [params.marketId, params.user, BigInt(collateralIndex)],
96
+ });
97
+ }));
98
+ return new index_js_1.Position({
99
+ credit: position[0],
100
+ pendingFee: position[1],
101
+ lastLossFactor: position[2],
102
+ lastAccrual: position[3],
103
+ debt: position[4],
104
+ collateralBitmap: position[5],
105
+ collateral: [...collateral],
106
+ });
107
+ }
108
+ /**
109
+ * Fetches a Midnight position paired with its hydrated market.
110
+ *
111
+ * Reads the full inventory of `fetchPosition` and `fetchMarket` at the same
112
+ * call parameters: `eth_chainId` only when needed,
113
+ * `Midnight.toMarket(marketId)`, `Midnight.marketState(marketId)`, and either
114
+ * deployless `GetPosition.query(midnight, marketId, user)` or direct
115
+ * `Midnight.position(marketId, user)` plus all collateral slots.
116
+ *
117
+ * @param client - Viem client used for the reads.
118
+ * @param params.marketId - Market id whose position and market state to read.
119
+ * @param params.user - Position owner address.
120
+ * @param params.deployless - Optional deployless mode for the position read; defaults to `true` with fallback, or `"force"` without fallback.
121
+ * @param params.account - Optional account used as the `from` field for the reads.
122
+ * @param params.blockNumber - Optional block number used for the reads.
123
+ * @param params.blockTag - Optional block tag used for the reads.
124
+ * @param params.stateOverride - Optional state override set used for the reads.
125
+ * @returns Accrual position instance.
126
+ * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
127
+ * @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
128
+ * @example
129
+ * ```ts
130
+ * import { fetchAccrualPosition } from "@morpho-org/midnight-sdk";
131
+ * import { createPublicClient, http } from "viem";
132
+ * import { base } from "viem/chains";
133
+ *
134
+ * const client = createPublicClient({ chain: base, transport: http() });
135
+ * const position = await fetchAccrualPosition(client, {
136
+ * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
137
+ * user: "0x0000000000000000000000000000000000009000",
138
+ * });
139
+ * console.log(position.market.id);
140
+ * ```
141
+ */
142
+ async function fetchAccrualPosition(client, params) {
143
+ const [position, market] = await Promise.all([
144
+ fetchPosition(client, params),
145
+ (0, Market_js_1.fetchMarket)(client, params),
146
+ ]);
147
+ return new index_js_1.AccrualPosition(position, market);
148
+ }
@@ -0,0 +1,38 @@
1
+ import type { Address, Client } from "viem";
2
+ import { type RatifierInfo } from "../signatures/RatifierUtils.js";
3
+ import type { MidnightCallParameters } from "./types.js";
4
+ /**
5
+ * Fetches maker bytecode and classifies the ratifier route for new offers.
6
+ *
7
+ * The `bytecode` read is the result of `eth_getCode` for `params.maker` at the
8
+ * requested block. It is not returned to callers; it only tells the SDK whether
9
+ * the maker is an EOA or EIP-7702 account that can use Ecrecover signatures, or
10
+ * a deployed-code account that should use Setter root approval. Put the returned
11
+ * `ratifier` address on `Offer.create`, then use the returned `type` to choose
12
+ * `EcrecoverRatifierUtils.ratify` or `SetterRatifierUtils.ratify` after the tree
13
+ * has been built.
14
+ *
15
+ * Reads `eth_chainId` only when the viem client has no configured chain id,
16
+ * then reads `eth_getCode` for `params.maker`.
17
+ *
18
+ * @param client - Viem client used for the bytecode read.
19
+ * @param params.maker - Maker address whose bytecode determines the ratifier route.
20
+ * @param params.blockNumber - Optional block number used for the bytecode read.
21
+ * @param params.blockTag - Optional block tag used for the bytecode read.
22
+ * @returns Ratifier information.
23
+ * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
24
+ * @throws {UnknownAddressError} when the registry has no configured ratifier address for the client chain id.
25
+ * @example
26
+ * ```ts
27
+ * import { fetchRatifierInfo } from "@morpho-org/midnight-sdk";
28
+ *
29
+ * const maker = "0x7b093658BE7f90B63D7c359e8f408e503c2D9401";
30
+ * const info = await fetchRatifierInfo(client, { maker });
31
+ * // Pass info.ratifier to Offer.create(...), then use info.type after
32
+ * // Tree.create([...]) to choose the Ecrecover or Setter ratifier flow.
33
+ * console.log(info.type, info.ratifier);
34
+ * ```
35
+ */
36
+ export declare function fetchRatifierInfo(client: Client, params: {
37
+ readonly maker: Address;
38
+ } & MidnightCallParameters): Promise<RatifierInfo>;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchRatifierInfo = fetchRatifierInfo;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const actions_1 = require("viem/actions");
6
+ const RatifierUtils_js_1 = require("../signatures/RatifierUtils.js");
7
+ const utils_js_1 = require("./utils.js");
8
+ /**
9
+ * Fetches maker bytecode and classifies the ratifier route for new offers.
10
+ *
11
+ * The `bytecode` read is the result of `eth_getCode` for `params.maker` at the
12
+ * requested block. It is not returned to callers; it only tells the SDK whether
13
+ * the maker is an EOA or EIP-7702 account that can use Ecrecover signatures, or
14
+ * a deployed-code account that should use Setter root approval. Put the returned
15
+ * `ratifier` address on `Offer.create`, then use the returned `type` to choose
16
+ * `EcrecoverRatifierUtils.ratify` or `SetterRatifierUtils.ratify` after the tree
17
+ * has been built.
18
+ *
19
+ * Reads `eth_chainId` only when the viem client has no configured chain id,
20
+ * then reads `eth_getCode` for `params.maker`.
21
+ *
22
+ * @param client - Viem client used for the bytecode read.
23
+ * @param params.maker - Maker address whose bytecode determines the ratifier route.
24
+ * @param params.blockNumber - Optional block number used for the bytecode read.
25
+ * @param params.blockTag - Optional block tag used for the bytecode read.
26
+ * @returns Ratifier information.
27
+ * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
28
+ * @throws {UnknownAddressError} when the registry has no configured ratifier address for the client chain id.
29
+ * @example
30
+ * ```ts
31
+ * import { fetchRatifierInfo } from "@morpho-org/midnight-sdk";
32
+ *
33
+ * const maker = "0x7b093658BE7f90B63D7c359e8f408e503c2D9401";
34
+ * const info = await fetchRatifierInfo(client, { maker });
35
+ * // Pass info.ratifier to Offer.create(...), then use info.type after
36
+ * // Tree.create([...]) to choose the Ecrecover or Setter ratifier flow.
37
+ * console.log(info.type, info.ratifier);
38
+ * ```
39
+ */
40
+ async function fetchRatifierInfo(client, params) {
41
+ const chainId = await (0, utils_js_1.resolveChainId)(client);
42
+ const ecrecoverRatifier = (0, morpho_ts_1.getChainAddress)(chainId, "ecrecoverRatifier");
43
+ const setterRatifier = (0, morpho_ts_1.getChainAddress)(chainId, "setterRatifier");
44
+ const bytecode = await (0, actions_1.getBytecode)(client, {
45
+ ...(params.blockNumber != null
46
+ ? { blockNumber: params.blockNumber }
47
+ : params.blockTag != null
48
+ ? { blockTag: params.blockTag }
49
+ : {}),
50
+ address: params.maker,
51
+ });
52
+ return RatifierUtils_js_1.RatifierUtils.getRatifierInfo({
53
+ bytecode,
54
+ ecrecoverRatifier,
55
+ setterRatifier,
56
+ });
57
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./Market.js";
2
+ export * from "./Offer.js";
3
+ export * from "./Position.js";
4
+ export * from "./Ratifier.js";
5
+ export * from "./types.js";
@@ -0,0 +1,21 @@
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("./Market.js"), exports);
18
+ __exportStar(require("./Offer.js"), exports);
19
+ __exportStar(require("./Position.js"), exports);
20
+ __exportStar(require("./Ratifier.js"), exports);
21
+ __exportStar(require("./types.js"), exports);
@@ -0,0 +1,57 @@
1
+ import type { Account, Address, BlockTag, StateOverride } from "viem";
2
+ /**
3
+ * Shared viem call parameters accepted by Midnight fetch helpers.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import type { MidnightCallParameters } from "@morpho-org/midnight-sdk";
8
+ *
9
+ * const params: MidnightCallParameters = { blockTag: "latest" };
10
+ * console.log(params.blockTag);
11
+ * ```
12
+ */
13
+ export interface MidnightCallParameters {
14
+ /** Account used as the `from` field for the read. */
15
+ readonly account?: Account | Address;
16
+ /** Block number used for the read. */
17
+ readonly blockNumber?: bigint;
18
+ /** Block tag used for the read. */
19
+ readonly blockTag?: BlockTag;
20
+ /** State override set used for the read. */
21
+ readonly stateOverride?: StateOverride;
22
+ }
23
+ /**
24
+ * Deployless read mode accepted by deployless-capable Midnight fetch helpers.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import type { DeploylessFetchParameters } from "@morpho-org/midnight-sdk";
29
+ *
30
+ * const params: DeploylessFetchParameters = { deployless: "force" };
31
+ * console.log(params.deployless);
32
+ * ```
33
+ */
34
+ export interface DeploylessFetchParameters extends MidnightCallParameters {
35
+ /**
36
+ * If `true`, deployless-capable fetchers use deployless reads and fall back to direct reads if they fail.
37
+ *
38
+ * If `"force"`, deployless-capable fetchers use deployless reads without fallback.
39
+ *
40
+ * If `false`, deployless-capable fetchers use direct reads.
41
+ *
42
+ * Default is `true` for fetchers that implement deployless reads.
43
+ */
44
+ readonly deployless?: boolean | "force";
45
+ }
46
+ /**
47
+ * Shared viem fetch parameters for Midnight helpers.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import type { MidnightFetchParams } from "@morpho-org/midnight-sdk";
52
+ *
53
+ * const params: MidnightFetchParams = { blockTag: "latest" };
54
+ * console.log(params.blockTag);
55
+ * ```
56
+ */
57
+ export type MidnightFetchParams = DeploylessFetchParameters;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import type { Client } from "viem";
2
+ import type { DeploylessFetchParameters, MidnightCallParameters } from "./types.js";
3
+ /** @internal Extracts viem call parameters from Midnight fetch inputs. */
4
+ export declare const callParameters: (params: MidnightCallParameters) => MidnightCallParameters;
5
+ /** @internal Returns the configured deployless mode, defaulting to enabled. */
6
+ export declare const shouldUseDeployless: (params: DeploylessFetchParameters) => boolean | "force";
7
+ /** @internal Returns the configured viem client chain id or fetches it from RPC. */
8
+ export declare const resolveChainId: (client: Client) => Promise<number>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveChainId = exports.shouldUseDeployless = exports.callParameters = void 0;
4
+ const actions_1 = require("viem/actions");
5
+ /** @internal Extracts viem call parameters from Midnight fetch inputs. */
6
+ const callParameters = (params) => ({
7
+ account: params.account,
8
+ blockNumber: params.blockNumber,
9
+ blockTag: params.blockTag,
10
+ stateOverride: params.stateOverride,
11
+ });
12
+ exports.callParameters = callParameters;
13
+ /** @internal Returns the configured deployless mode, defaulting to enabled. */
14
+ const shouldUseDeployless = (params) => params.deployless ?? true;
15
+ exports.shouldUseDeployless = shouldUseDeployless;
16
+ /** @internal Returns the configured viem client chain id or fetches it from RPC. */
17
+ const resolveChainId = async (client) => client.chain?.id ?? (await (0, actions_1.getChainId)(client));
18
+ exports.resolveChainId = resolveChainId;
@@ -0,0 +1,18 @@
1
+ export * from "./abis.js";
2
+ export * from "./constants.js";
3
+ export * from "./errors.js";
4
+ export * from "./fetch/index.js";
5
+ export * from "./market/index.js";
6
+ export * from "./math/index.js";
7
+ export * from "./offers/index.js";
8
+ export * from "./signatures/index.js";
9
+ /**
10
+ * Namespace for Midnight mempool payload utilities.
11
+ *
12
+ * Use `Payload.encode` to turn ratified offer items into wire bytes for mempool
13
+ * publication or raw payload API validation, and `Payload.decode` to inspect
14
+ * published maker offers, ratifier data, or attribution-tagged payloads.
15
+ * Encoded payloads are always `Hex` strings ready to be included in onchain
16
+ * mempool submission calldata.
17
+ */
18
+ export * as Payload from "./signatures/Payload.js";
@@ -0,0 +1,57 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.Payload = void 0;
40
+ __exportStar(require("./abis.js"), exports);
41
+ __exportStar(require("./constants.js"), exports);
42
+ __exportStar(require("./errors.js"), exports);
43
+ __exportStar(require("./fetch/index.js"), exports);
44
+ __exportStar(require("./market/index.js"), exports);
45
+ __exportStar(require("./math/index.js"), exports);
46
+ __exportStar(require("./offers/index.js"), exports);
47
+ __exportStar(require("./signatures/index.js"), exports);
48
+ /**
49
+ * Namespace for Midnight mempool payload utilities.
50
+ *
51
+ * Use `Payload.encode` to turn ratified offer items into wire bytes for mempool
52
+ * publication or raw payload API validation, and `Payload.decode` to inspect
53
+ * published maker offers, ratifier data, or attribution-tagged payloads.
54
+ * Encoded payloads are always `Hex` strings ready to be included in onchain
55
+ * mempool submission calldata.
56
+ */
57
+ exports.Payload = __importStar(require("./signatures/Payload.js"));