@morpho-org/midnight-sdk 0.1.0 → 1.1.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.
- 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 +89 -19
- package/lib/cjs/api/helpers.d.ts +11 -1
- package/lib/cjs/api/helpers.js +54 -4
- 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 +89 -1
- package/lib/cjs/errors.js +114 -3
- package/lib/cjs/fetch/Market.js +37 -3
- package/lib/cjs/fetch/index.d.ts +0 -1
- package/lib/cjs/fetch/index.js +0 -1
- package/lib/cjs/fetch/utils.d.ts +1 -1
- package/lib/cjs/fetch/utils.js +9 -2
- 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 +218 -12
- package/lib/cjs/offers/Offer.js +215 -7
- package/lib/cjs/offers/OfferUtils.d.ts +249 -143
- package/lib/cjs/offers/OfferUtils.js +326 -45
- package/lib/cjs/queries/GetPosition.d.ts +1 -1
- package/lib/cjs/queries/GetPosition.js +1 -1
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +184 -38
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +296 -80
- package/lib/cjs/signatures/Group.d.ts +12 -4
- package/lib/cjs/signatures/Group.js +17 -7
- package/lib/cjs/signatures/GroupUtils.d.ts +60 -4
- package/lib/cjs/signatures/GroupUtils.js +57 -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 +105 -0
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +52 -13
- package/lib/cjs/signatures/SetterRatifierUtils.js +62 -19
- package/lib/cjs/signatures/Tree.d.ts +28 -16
- package/lib/cjs/signatures/Tree.js +35 -15
- package/lib/cjs/signatures/TreeUtils.d.ts +185 -63
- package/lib/cjs/signatures/TreeUtils.js +89 -79
- package/lib/cjs/signatures/eip712.d.ts +22 -0
- package/lib/cjs/signatures/eip712.js +26 -0
- package/lib/cjs/signatures/index.d.ts +2 -0
- package/lib/cjs/signatures/index.js +4 -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 +91 -21
- package/lib/esm/api/helpers.d.ts +11 -1
- package/lib/esm/api/helpers.js +54 -5
- 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 +89 -1
- package/lib/esm/errors.js +108 -2
- package/lib/esm/fetch/Market.js +38 -4
- package/lib/esm/fetch/index.d.ts +0 -1
- package/lib/esm/fetch/index.js +0 -1
- package/lib/esm/fetch/utils.d.ts +1 -1
- package/lib/esm/fetch/utils.js +9 -2
- 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 +218 -12
- package/lib/esm/offers/Offer.js +216 -8
- package/lib/esm/offers/OfferUtils.d.ts +249 -143
- package/lib/esm/offers/OfferUtils.js +329 -48
- package/lib/esm/queries/GetPosition.d.ts +1 -1
- package/lib/esm/queries/GetPosition.js +1 -1
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +184 -38
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +298 -82
- package/lib/esm/signatures/Group.d.ts +12 -4
- package/lib/esm/signatures/Group.js +17 -7
- package/lib/esm/signatures/GroupUtils.d.ts +60 -4
- package/lib/esm/signatures/GroupUtils.js +57 -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 +105 -0
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +52 -13
- package/lib/esm/signatures/SetterRatifierUtils.js +62 -19
- package/lib/esm/signatures/Tree.d.ts +28 -16
- package/lib/esm/signatures/Tree.js +36 -16
- package/lib/esm/signatures/TreeUtils.d.ts +185 -63
- package/lib/esm/signatures/TreeUtils.js +91 -81
- package/lib/esm/signatures/eip712.d.ts +22 -0
- package/lib/esm/signatures/eip712.js +22 -0
- package/lib/esm/signatures/index.d.ts +2 -0
- package/lib/esm/signatures/index.js +2 -0
- package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
- package/lib/esm/signatures/offerStructInternal.js +67 -0
- package/package.json +31 -34
- 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
- package/src/index.ts +0 -19
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.
|
|
23
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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,
|
|
140
|
-
|
|
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,
|
|
174
|
+
async function buildAskQuoteTakes(marketId: Hash, targetUnits: bigint) {
|
|
171
175
|
const quote = await MidnightApi.fetchBookQuote({
|
|
172
176
|
marketId,
|
|
173
177
|
side: "asks",
|
|
174
|
-
|
|
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
|
|
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
|
|
210
|
-
const
|
|
211
|
-
const
|
|
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
|
|
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.
|
|
243
|
-
|
|
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
|
|