@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.
Files changed (118) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -46
  3. package/lib/cjs/abis.d.ts +1185 -996
  4. package/lib/cjs/abis.js +1445 -1211
  5. package/lib/cjs/api/MidnightApi.d.ts +22 -16
  6. package/lib/cjs/api/MidnightApi.js +22 -16
  7. package/lib/cjs/api/helpers.js +6 -3
  8. package/lib/cjs/api/types.d.ts +35 -9
  9. package/lib/cjs/constants.d.ts +12 -11
  10. package/lib/cjs/constants.js +14 -12
  11. package/lib/cjs/errors.d.ts +74 -1
  12. package/lib/cjs/errors.js +94 -3
  13. package/lib/cjs/fetch/Market.js +0 -1
  14. package/lib/cjs/fetch/index.d.ts +0 -1
  15. package/lib/cjs/fetch/index.js +0 -1
  16. package/lib/cjs/index.d.ts +1 -1
  17. package/lib/cjs/index.js +2 -23
  18. package/lib/cjs/market/Market.d.ts +99 -33
  19. package/lib/cjs/market/Market.js +137 -28
  20. package/lib/cjs/market/MarketUtils.d.ts +61 -41
  21. package/lib/cjs/market/MarketUtils.js +108 -48
  22. package/lib/cjs/market/Position.d.ts +16 -11
  23. package/lib/cjs/market/Position.js +18 -12
  24. package/lib/cjs/market/PositionUtils.d.ts +6 -4
  25. package/lib/cjs/market/PositionUtils.js +9 -6
  26. package/lib/cjs/math/TickLib.d.ts +22 -0
  27. package/lib/cjs/math/TickLib.js +30 -0
  28. package/lib/cjs/math/index.d.ts +0 -1
  29. package/lib/cjs/math/index.js +0 -1
  30. package/lib/cjs/offers/Offer.d.ts +217 -11
  31. package/lib/cjs/offers/Offer.js +215 -7
  32. package/lib/cjs/offers/OfferUtils.d.ts +245 -139
  33. package/lib/cjs/offers/OfferUtils.js +304 -36
  34. package/lib/cjs/queries/GetPosition.d.ts +1 -1
  35. package/lib/cjs/queries/GetPosition.js +1 -1
  36. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +66 -38
  37. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +147 -79
  38. package/lib/cjs/signatures/Group.d.ts +12 -4
  39. package/lib/cjs/signatures/Group.js +12 -4
  40. package/lib/cjs/signatures/GroupUtils.d.ts +12 -4
  41. package/lib/cjs/signatures/GroupUtils.js +6 -2
  42. package/lib/cjs/signatures/Payload.d.ts +277 -277
  43. package/lib/cjs/signatures/Payload.js +517 -534
  44. package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
  45. package/lib/cjs/signatures/RatifierUtils.js +94 -0
  46. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +21 -13
  47. package/lib/cjs/signatures/SetterRatifierUtils.js +25 -20
  48. package/lib/cjs/signatures/Tree.d.ts +28 -16
  49. package/lib/cjs/signatures/Tree.js +25 -13
  50. package/lib/cjs/signatures/TreeUtils.d.ts +181 -62
  51. package/lib/cjs/signatures/TreeUtils.js +73 -76
  52. package/lib/cjs/signatures/index.d.ts +1 -0
  53. package/lib/cjs/signatures/index.js +3 -0
  54. package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
  55. package/lib/cjs/signatures/offerStructInternal.js +72 -0
  56. package/lib/esm/abis.d.ts +1185 -996
  57. package/lib/esm/abis.js +1445 -1211
  58. package/lib/esm/api/MidnightApi.d.ts +22 -16
  59. package/lib/esm/api/MidnightApi.js +23 -17
  60. package/lib/esm/api/helpers.js +6 -3
  61. package/lib/esm/api/types.d.ts +35 -9
  62. package/lib/esm/constants.d.ts +12 -11
  63. package/lib/esm/constants.js +13 -11
  64. package/lib/esm/errors.d.ts +74 -1
  65. package/lib/esm/errors.js +89 -2
  66. package/lib/esm/fetch/Market.js +0 -1
  67. package/lib/esm/fetch/index.d.ts +0 -1
  68. package/lib/esm/fetch/index.js +0 -1
  69. package/lib/esm/index.d.ts +1 -1
  70. package/lib/esm/index.js +1 -1
  71. package/lib/esm/market/Market.d.ts +99 -33
  72. package/lib/esm/market/Market.js +137 -28
  73. package/lib/esm/market/MarketUtils.d.ts +61 -41
  74. package/lib/esm/market/MarketUtils.js +109 -49
  75. package/lib/esm/market/Position.d.ts +16 -11
  76. package/lib/esm/market/Position.js +19 -13
  77. package/lib/esm/market/PositionUtils.d.ts +6 -4
  78. package/lib/esm/market/PositionUtils.js +9 -6
  79. package/lib/esm/math/TickLib.d.ts +22 -0
  80. package/lib/esm/math/TickLib.js +31 -1
  81. package/lib/esm/math/index.d.ts +0 -1
  82. package/lib/esm/math/index.js +0 -1
  83. package/lib/esm/offers/Offer.d.ts +217 -11
  84. package/lib/esm/offers/Offer.js +216 -8
  85. package/lib/esm/offers/OfferUtils.d.ts +245 -139
  86. package/lib/esm/offers/OfferUtils.js +306 -38
  87. package/lib/esm/queries/GetPosition.d.ts +1 -1
  88. package/lib/esm/queries/GetPosition.js +1 -1
  89. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +66 -38
  90. package/lib/esm/signatures/EcrecoverRatifierUtils.js +149 -81
  91. package/lib/esm/signatures/Group.d.ts +12 -4
  92. package/lib/esm/signatures/Group.js +12 -4
  93. package/lib/esm/signatures/GroupUtils.d.ts +12 -4
  94. package/lib/esm/signatures/GroupUtils.js +6 -2
  95. package/lib/esm/signatures/Payload.d.ts +277 -277
  96. package/lib/esm/signatures/Payload.js +517 -531
  97. package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
  98. package/lib/esm/signatures/RatifierUtils.js +94 -0
  99. package/lib/esm/signatures/SetterRatifierUtils.d.ts +21 -13
  100. package/lib/esm/signatures/SetterRatifierUtils.js +25 -20
  101. package/lib/esm/signatures/Tree.d.ts +28 -16
  102. package/lib/esm/signatures/Tree.js +25 -13
  103. package/lib/esm/signatures/TreeUtils.d.ts +181 -62
  104. package/lib/esm/signatures/TreeUtils.js +75 -78
  105. package/lib/esm/signatures/index.d.ts +1 -0
  106. package/lib/esm/signatures/index.js +1 -0
  107. package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
  108. package/lib/esm/signatures/offerStructInternal.js +67 -0
  109. package/package.json +28 -32
  110. package/src/index.ts +1 -1
  111. package/lib/cjs/fetch/Offer.d.ts +0 -74
  112. package/lib/cjs/fetch/Offer.js +0 -119
  113. package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
  114. package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
  115. package/lib/esm/fetch/Offer.d.ts +0 -74
  116. package/lib/esm/fetch/Offer.js +0 -116
  117. package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
  118. package/lib/esm/math/ConsumableUnitsLib.js +0 -74
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Morpho Association
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -19,8 +19,9 @@ Midnight HTTP API helpers are exported from `@morpho-org/midnight-sdk/api`. This
19
19
  ## Making offers
20
20
 
21
21
  The make-side starts with local offer construction, groups offers that should share consumption, and
22
- commits grouped and standalone offers into one tree. Validate the tree before asking the maker or an
23
- authorized signer to sign, or before asking the maker contract to approve the root. Then encode the
22
+ commits grouped and standalone offers into one tree. Ratification inputs are optional for validation,
23
+ so validate the tree before asking the maker or an authorized signer to sign. After signing, pass
24
+ ratification inputs only when you also want to validate the final payload shape. Then encode the
24
25
  ratified items into a mempool payload and submit the raw payload bytes onchain.
25
26
 
26
27
  ```ts
@@ -43,6 +44,7 @@ import {
43
44
  const chainId = 8453;
44
45
  const usdc = addresses[chainId].usdc!;
45
46
  const weth = addresses[chainId].wNative!;
47
+ const midnight = addresses[chainId].midnight!;
46
48
  const ecrecoverRatifier = addresses[chainId].ecrecoverRatifier!;
47
49
  const midnightMempool = addresses[chainId].midnightMempool!;
48
50
 
@@ -52,12 +54,14 @@ export async function makeBaseUsdcWethOffers(params: {
52
54
  readonly wethUsdcOracle: Address;
53
55
  }) {
54
56
  const market = {
57
+ chainId,
58
+ midnight,
55
59
  loanToken: usdc,
56
60
  collateralParams: [
57
61
  {
58
62
  token: weth,
59
63
  lltv: 770_000000000000000000n,
60
- maxLif: 1_061007957559681697n,
64
+ liquidationCursor: 250_000000000000000000n,
61
65
  oracle: params.wethUsdcOracle,
62
66
  },
63
67
  ],
@@ -100,20 +104,30 @@ export async function makeBaseUsdcWethOffers(params: {
100
104
  standaloneBorrowOffer,
101
105
  ]);
102
106
 
103
- const treeValidation = await tree.mempoolValidate({
107
+ // Ratification data is optional here. Omitting it catches API policy issues before asking the maker to sign.
108
+ await tree.mempoolValidate({
104
109
  chainId,
105
110
  });
106
- if (!treeValidation.valid) {
107
- return { ok: false as const, issues: treeValidation.issues };
108
- }
109
111
 
110
112
  // EcrecoverRatifierUtils derives the verifier from offer.ratifier and rejects mixed-ratifier trees.
111
113
  // The account may be the maker or an authorized signer for every maker in the tree.
112
- const items = await EcrecoverRatifierUtils.ratify({
114
+ const signature = await EcrecoverRatifierUtils.sign({
113
115
  tree,
114
116
  client: params.walletClient,
115
117
  account: params.maker,
116
118
  });
119
+
120
+ // Optional final-payload validation: include ratification data when validating post-signature bytes.
121
+ await tree.mempoolValidate({
122
+ chainId,
123
+ ratification: { type: "ecrecover", account: params.maker, signature },
124
+ });
125
+
126
+ const items = await EcrecoverRatifierUtils.ratify({
127
+ tree,
128
+ account: params.maker,
129
+ signature,
130
+ });
117
131
  const payload = await Payload.encode(items);
118
132
 
119
133
  const transactionHash = await params.walletClient.sendTransaction({
@@ -136,13 +150,8 @@ The take-side starts from API book, quote, or maker takeable-offer responses. `M
136
150
  router response into ABI-ready take objects: each item has `offer`, `ratifierData`, and `units` for
137
151
  `IMidnight.take`, plus `marketId` metadata from the API. Book `asks` are maker sell offers, while
138
152
  book `bids` are maker buy offers.
139
- For a quote that spans several offers, use `MidnightBundles`: it receives the returned take objects,
140
- calls `take` internally in order, and fills toward the target in one transaction.
141
- When executing offers one-by-one with `Midnight.take`, clamp each take to the remaining target instead
142
- of submitting every returned cap blindly.
143
- The no-permit bundle example assumes the taker has authorized `MidnightBundles` on Midnight and has
144
- approved the bundle contract to pull the market loan token. Set `minUnits` or `maxUnits` from your
145
- own price guard; the example uses `0n` to focus on the route mechanics.
153
+ For a quote that spans several offers, execute the returned takes in order with `Midnight.take` and
154
+ clamp each take to the remaining target instead of submitting every returned cap blindly.
146
155
 
147
156
  ```ts
148
157
  import { addresses } from "@morpho-org/morpho-ts";
@@ -150,10 +159,7 @@ import {
150
159
  MidnightApi,
151
160
  type MidnightApiTake,
152
161
  } from "@morpho-org/midnight-sdk/api";
153
- import {
154
- midnightAbi,
155
- midnightBundlesAbi,
156
- } from "@morpho-org/midnight-sdk";
162
+ import { midnightAbi } from "@morpho-org/midnight-sdk";
157
163
  import {
158
164
  parseUnits,
159
165
  zeroAddress,
@@ -164,14 +170,12 @@ import {
164
170
 
165
171
  const chainId = 8453;
166
172
  const midnight = addresses[chainId].midnight!;
167
- const midnightBundles = addresses[chainId].midnightBundles!;
168
- const tokenPermitNone = { kind: 0, data: "0x" } as const;
169
173
 
170
- async function buildAskQuoteTakes(marketId: Hash, targetBuyerAssets: bigint) {
174
+ async function buildAskQuoteTakes(marketId: Hash, targetUnits: bigint) {
171
175
  const quote = await MidnightApi.fetchBookQuote({
172
176
  marketId,
173
177
  side: "asks",
174
- assets: targetBuyerAssets,
178
+ units: targetUnits,
175
179
  slippage: "0.5",
176
180
  });
177
181
 
@@ -201,32 +205,35 @@ export async function takeOneOffer(params: {
201
205
  });
202
206
  }
203
207
 
204
- export async function takeAskQuoteWithBundle(params: {
208
+ export async function takeAskQuoteSequentially(params: {
205
209
  readonly walletClient: WalletClient;
206
210
  readonly taker: Address;
211
+ readonly receiverIfTakerIsSeller: Address;
207
212
  readonly marketId: Hash;
208
213
  }) {
209
- const targetBuyerAssets = parseUnits("10000", 6);
210
- const minUnits = 0n;
211
- const quote = await buildAskQuoteTakes(params.marketId, targetBuyerAssets);
214
+ const targetUnits = parseUnits("50", 18);
215
+ const quote = await buildAskQuoteTakes(params.marketId, targetUnits);
216
+ const transactionHashes: Hash[] = [];
217
+ let remainingUnits = targetUnits;
218
+
219
+ for (const take of quote.takeableOffers) {
220
+ if (remainingUnits === 0n) break;
221
+
222
+ const units = take.units < remainingUnits ? take.units : remainingUnits;
223
+ if (units === 0n) continue;
224
+
225
+ transactionHashes.push(
226
+ await takeOneOffer({
227
+ walletClient: params.walletClient,
228
+ taker: params.taker,
229
+ receiverIfTakerIsSeller: params.receiverIfTakerIsSeller,
230
+ take: { ...take, units },
231
+ }),
232
+ );
233
+ remainingUnits -= units;
234
+ }
212
235
 
213
- return params.walletClient.writeContract({
214
- account: params.taker,
215
- address: midnightBundles,
216
- abi: midnightBundlesAbi,
217
- functionName: "buyWithAssetsTargetAndWithdrawCollateral",
218
- args: [
219
- targetBuyerAssets,
220
- minUnits,
221
- params.taker,
222
- tokenPermitNone,
223
- quote.takeableOffers,
224
- [],
225
- params.taker,
226
- 0n,
227
- zeroAddress,
228
- ],
229
- });
236
+ return { remainingUnits, transactionHashes };
230
237
  }
231
238
  ```
232
239
 
@@ -239,8 +246,11 @@ normalization. Caller inputs and successful JSON output shapes are trusted at ru
239
246
  TypeScript types model the API contract.
240
247
 
241
248
  Use `tree.mempoolValidate({ chainId })` in normal make-side flows before the maker signs or approves
242
- the root. Pass `apiUrl` to that method when using a custom Midnight API URL. `MidnightApi` keeps the
243
- raw HTTP surface, including `validateMempoolPayload` for already encoded payload bytes.
249
+ the root. Ratification data is optional for validation; pass `ratification` only when validating
250
+ final payload bytes with real ratifier data after the signature or Setter proof is available. It
251
+ throws `MidnightMempoolValidationError` with the API issues when policy validation fails. Pass
252
+ `apiUrl` to that method when using a custom Midnight API URL. `MidnightApi` keeps the raw HTTP
253
+ surface, including non-throwing `validateMempoolPayload` results for already encoded payload bytes.
244
254
 
245
255
  ## Development
246
256