@morpho-org/midnight-sdk 1.8.0 → 2.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.
@@ -0,0 +1,6 @@
1
+ # Migrating from midnight-sdk v1 to v2
2
+
3
+ midnight-sdk v2 removes `midnightBundlesAbi` (MidnightBundles V1). Use `midnightBundlesV2Abi`
4
+ against the `midnightBundlesV2` address. V2 acts for `msg.sender` (no `taker` argument), takes no
5
+ inline token permits, and takes the `Market` struct as its first argument; the README's atomic-take
6
+ recipe shows the V2 call. Integrations that still call V1 must pin midnight-sdk v1.
package/README.md CHANGED
@@ -153,7 +153,7 @@ book `bids` are maker buy offers.
153
153
  The quote's `averageWorstPrice` is an **aggregate** guard over the requested target: a favorable
154
154
  offer can compensate for a worse one, and the worse offer does not satisfy the guard on its own.
155
155
  That guard only holds if every returned offer settles together, so execute a quote through
156
- `MidnightBundlesV1` in a single transaction. The bundle skips offers that became stale since the
156
+ `MidnightBundlesV2` in a single transaction. The bundle skips offers that became stale since the
157
157
  quote, reverts unless the exact target is reached, and enforces one aggregate consideration bound
158
158
  (`maxBuyerAssets` for asks, `minSellerAssets` for bids). Never split a quote into independent
159
159
  `Midnight.take` transactions: a stale first leg reverts but still consumes its nonce, so the
@@ -164,7 +164,7 @@ and resolve approvals for you; the recipe below shows the same route with this p
164
164
  ```ts
165
165
  import { getChainAddress, MathLib } from "@morpho-org/morpho-ts";
166
166
  import { MidnightApi } from "@morpho-org/midnight-sdk/api";
167
- import { midnightBundlesAbi } from "@morpho-org/midnight-sdk";
167
+ import { midnightBundlesV2Abi, type MarketParams } from "@morpho-org/midnight-sdk";
168
168
  import {
169
169
  maxUint256,
170
170
  parseUnits,
@@ -176,11 +176,14 @@ import {
176
176
  import { base } from "viem/chains";
177
177
 
178
178
  const chainId = base.id;
179
- const midnightBundles = getChainAddress(chainId, "midnightBundles");
179
+ // Throws `UnknownAddressError` until the chain's `midnightBundlesV2` is pinned in morpho-ts or
180
+ // registered with `registerCustomAddresses`.
181
+ const midnightBundlesV2 = getChainAddress(chainId, "midnightBundlesV2");
180
182
 
181
183
  export async function takeAskQuoteAtomically(params: {
182
184
  readonly walletClient: WalletClient;
183
185
  readonly taker: Address;
186
+ readonly market: MarketParams;
184
187
  readonly marketId: Hash;
185
188
  readonly deadline: bigint;
186
189
  }) {
@@ -197,24 +200,22 @@ export async function takeAskQuoteAtomically(params: {
197
200
  // not what the caller asked for.
198
201
  const maxBuyerAssets = MathLib.wMulUp(targetUnits, quote.data.averageWorstPrice);
199
202
 
200
- // Two confirmed prerequisites before this call: the taker has approved `midnightBundles`
201
- // to spend `maxBuyerAssets` of the loan token (or passes an ERC-2612/Permit2 payload as
202
- // `loanTokenPermit`), and has authorized the bundle on Midnight via
203
- // `Midnight.setIsAuthorized(midnightBundles, true, taker)` so it can act on the taker's
204
- // behalf (`@morpho-org/morpho-sdk`'s `getMidnightAuthorizationRequirement` resolves this
205
- // for you).
203
+ // Two confirmed prerequisites before this call: the taker has approved `midnightBundlesV2`
204
+ // to spend `maxBuyerAssets` of the loan token, and has authorized the bundle on Midnight via
205
+ // `Midnight.setIsAuthorized(midnightBundlesV2, true, taker)`. V2 acts for `msg.sender`, so the
206
+ // taker sends the transaction (`@morpho-org/morpho-sdk`'s entity `getRequirements()` resolves both).
206
207
  return params.walletClient.writeContract({
207
208
  account: params.taker,
208
209
  chain: base,
209
- address: midnightBundles,
210
- abi: midnightBundlesAbi,
211
- functionName: "midnightBundlesV1BuyWithUnitsTargetAndWithdrawCollateral",
210
+ address: midnightBundlesV2,
211
+ abi: midnightBundlesV2Abi,
212
+ functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
212
213
  args: [
214
+ params.market,
213
215
  targetUnits,
214
216
  maxBuyerAssets,
215
- params.taker,
216
217
  false, // reduceOnly
217
- { kind: 0, data: "0x" }, // loanTokenPermit: none
218
+ false, // repayEnabled
218
219
  quote.data.takeableOffers, // { offer, units, ratifierData }[] straight from the quote
219
220
  [], // collateralWithdrawals
220
221
  zeroAddress, // collateralReceiver
@@ -222,12 +223,13 @@ export async function takeAskQuoteAtomically(params: {
222
223
  zeroAddress, // referralFeeRecipient
223
224
  maxUint256, // maxContinuousFee: bound this to the fee the caller accepts
224
225
  params.deadline,
226
+ zeroAddress, // wrappedNative
225
227
  ],
226
228
  });
227
229
  }
228
230
  ```
229
231
 
230
- Bids mirror this with `midnightBundlesV1SupplyCollateralAndSellWithUnitsTarget` and a
232
+ Bids mirror this with `midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget` and a
231
233
  `minSellerAssets` floor derived from the same `averageWorstPrice`. Prefer the `*WithAssetsTarget*`
232
234
  variants when the caller fixes the asset amount instead of the unit amount.
233
235