# @morpho-org/midnight-sdk

Viem-based package for Morpho Midnight, Morpho's fixed-rate lending protocol. It exports:

- Offer construction and signing: `Offer`, `Group`, `Tree`, `Payload` and the ratifier helpers: `PriceRatifierV1` and `RateRatifierV1` (the trees `morpho-sdk` maker flows accept), `EcrecoverRatifierUtils` and `SetterRatifierUtils`.
- Market and position entities, protocol math, constants and typed errors.
- Fetch helpers for on-chain market and position state, and the Midnight ABIs (including `midnightBundlesV2Abi`).
- Midnight HTTP API helpers (`MidnightApi`) from `@morpho-org/midnight-sdk/api`.

For ready-to-send transactions, use [`@morpho-org/morpho-sdk`](../morpho-sdk/) and
`client.morpho.midnight(chainId)`: each action's `getRequirements()` returns the approvals and
authorizations to submit before the final transaction.

Upgrading from v1? Read the [v1 → v2 migration guide](./MIGRATION-v1-to-v2.md).

## Installation

```bash
npm install @morpho-org/midnight-sdk @morpho-org/morpho-ts viem
```

```bash
yarn add @morpho-org/midnight-sdk @morpho-org/morpho-ts viem
```

## API Import Stability

Midnight HTTP API helpers are exported from `@morpho-org/midnight-sdk/api`. This import path is not stable and may not respect semver versioning.

## Making offers

The make-side starts with local offer construction, groups offers that should share consumption, and
commits grouped and standalone offers into one tree. Ratification inputs are optional for validation,
so validate the tree before asking the maker or an authorized signer to sign. After signing, pass
ratification inputs only when you also want to validate the final payload shape. Then encode the
ratified items into a mempool payload and submit the raw payload bytes onchain.

```ts
import { addresses } from "@morpho-org/morpho-ts";
import {
  EcrecoverRatifierUtils,
  Offer,
  Payload,
  Tree,
} from "@morpho-org/midnight-sdk";
import {
  parseUnits,
  zeroAddress,
  type Address,
  type Chain,
  type Transport,
  type WalletClient,
} from "viem";

const chainId = 8453;
const usdc = addresses[chainId].usdc!;
const weth = addresses[chainId].wNative!;
const midnight = addresses[chainId].midnight!;
const ecrecoverRatifier = addresses[chainId].ecrecoverRatifier!;
const midnightMempool = addresses[chainId].midnightMempool!;

export async function makeBaseUsdcWethOffers(params: {
  readonly walletClient: WalletClient<Transport, Chain>;
  readonly maker: Address;
  readonly wethUsdcOracle: Address;
}) {
  const market = {
    chainId,
    midnight,
    loanToken: usdc,
    collateralParams: [
      {
        token: weth,
        lltv: 770_000000000000000000n,
        liquidationCursor: 250_000000000000000000n,
        oracle: params.wethUsdcOracle,
      },
    ],
    maturity: 1_789_743_600n, // 2026-09-18 15:00:00 UTC.
    rcfThreshold: 0n,
    enterGate: zeroAddress,
    liquidatorGate: zeroAddress,
  };

  const commonOffer = {
    market,
    maker: params.maker,
    expiry: 1_789_741_800n,
    ratifier: ecrecoverRatifier,
  };

  const lend250kUsdc = Offer.create({
    ...commonOffer,
    buy: true,
    tick: 5_000n,
    maxAssets: parseUnits("250000", 6),
  });
  const lend100kUsdc = Offer.create({
    ...commonOffer,
    buy: true,
    tick: 5_012n,
    maxAssets: parseUnits("100000", 6),
  });

  const standaloneBorrowOffer = Offer.create({
    ...commonOffer,
    buy: false,
    tick: 5_024n,
    maxUnits: parseUnits("50", 18),
  });

  const tree = Tree.create([
    lend250kUsdc,
    lend100kUsdc,
    standaloneBorrowOffer,
  ]);

  // Ratification data is optional here. Omitting it catches API policy issues before asking the maker to sign.
  await tree.mempoolValidate({
    chainId,
  });

  // EcrecoverRatifierUtils derives the verifier from offer.ratifier and rejects mixed-ratifier trees.
  // The account may be the maker or an authorized signer for every maker in the tree.
  const signature = await EcrecoverRatifierUtils.sign({
    tree,
    client: params.walletClient,
    account: params.maker,
  });

  // Optional final-payload validation: include ratification data when validating post-signature bytes.
  await tree.mempoolValidate({
    chainId,
    ratification: { type: "ecrecover", account: params.maker, signature },
  });

  const items = await EcrecoverRatifierUtils.ratify({
    tree,
    account: params.maker,
    signature,
  });
  const payload = await Payload.encode(items);

  const transactionHash = await params.walletClient.sendTransaction({
    account: params.maker,
    to: midnightMempool,
    data: payload,
  });

  return { ok: true as const, transactionHash };
}
```

Use `SetterRatifierUtils` instead when a contract maker approves the tree root onchain. In that
route, build the `Tree`, validate it, submit the root approval transaction for every maker in the
tree, then call `SetterRatifierUtils.ratify({ tree })` before `Payload.encode(...)`.

## Taking offers

The take-side starts from API book, quote, or maker takeable-offer responses. `MidnightApi` maps the
router response into ABI-ready take objects: each item has `offer`, `ratifierData`, and `units` for
`IMidnight.take`, plus `marketId` and `validUntil` metadata from the API. `validUntil` is the
inclusive Unix-second execution bound as a `bigint`. Book `asks` are maker sell offers, while
book `bids` are maker buy offers.
The quote's `averageWorstPrice` is an **aggregate** guard over the requested target: a favorable
offer can compensate for a worse one, and the worse offer does not satisfy the guard on its own.
That guard only holds if every returned offer settles together, so execute a quote through
`MidnightBundlesV2` in a single transaction. The bundle skips offers that became stale since the
quote, reverts unless the exact target is reached, and enforces one aggregate consideration bound
(`maxBuyerAssets` for asks, `minSellerAssets` for bids). Never split a quote into independent
`Midnight.take` transactions: a stale first leg reverts but still consumes its nonce, so the
remaining legs settle at a price the quote never authorized. The high-level
`@morpho-org/morpho-sdk` `client.morpho.midnight(chainId)` flows already route through the bundle
and resolve approvals for you; the recipe below shows the same route with this package's raw ABI.

Slippage quotes return **fee-excluded**, WAD-scaled prices. Deriving a bundle's asset limit
directly from those prices is only safe on fee-free markets. For asks, add the market's current
WAD-scaled `settlementFee` to the raw average to get the buyer price, then multiply by target
units and round up. For bids, subtract the fee to get the seller price, then multiply by target
units and round down. The results are loan-token base units, not WAD-scaled prices. Read the fee
for the market's current time to maturity and refresh the quote and fee before execution; a fee
change can invalidate the bound. These examples use no referral fee; nonzero referral fees also
need to be included in the bundle's spend/proceeds limits.

```ts
import { getChainAddress, MathLib } from "@morpho-org/morpho-ts";
import { MidnightApi } from "@morpho-org/midnight-sdk/api";
import { midnightBundlesV2Abi, type MarketParams } from "@morpho-org/midnight-sdk";
import {
  maxUint256,
  parseUnits,
  zeroAddress,
  type Address,
  type Hash,
  type WalletClient,
} from "viem";
import { base } from "viem/chains";

const chainId = base.id;
// Throws `UnknownAddressError` until the chain's `midnightBundlesV2` is pinned in morpho-ts or
// registered with `registerCustomAddresses`.
const midnightBundlesV2 = getChainAddress(chainId, "midnightBundlesV2");

export async function takeAskQuoteAtomically(params: {
  readonly walletClient: WalletClient;
  readonly taker: Address;
  readonly market: MarketParams;
  readonly marketId: Hash;
  readonly settlementFee: bigint;
  readonly deadline: bigint;
  readonly minimumDeadline?: bigint;
}) {
  const targetUnits = parseUnits("50", 18);
  const quote = await MidnightApi.fetchBookQuote({
    marketId: params.marketId,
    side: "asks",
    taker: params.taker,
    units: targetUnits,
    slippage: "0.5",
    settlementFee: params.settlementFee,
  });

  const deadline = quote.data.takeableOffers.reduce(
    (bound, take) => take.validUntil < bound ? take.validUntil : bound,
    params.deadline,
  );
  if (
    deadline < BigInt(Math.floor(Date.now() / 1_000)) ||
    deadline < (params.minimumDeadline ?? 0n)
  ) {
    throw new Error("Quote expires too soon; fetch a fresh quote.");
  }

  // Keep the caller's target and the quote's aggregate guard as two independent inputs.
  // Do not substitute `quote.data.availableUnits` for the target: it is fallback capacity,
  // not what the caller asked for.
  const buyerPrice = BigInt(quote.data.averageWorstPrice) + params.settlementFee;
  const maxBuyerAssets = MathLib.wMulUp(targetUnits, buyerPrice);

  // Two confirmed prerequisites before this call: the taker has approved `midnightBundlesV2`
  // to spend `maxBuyerAssets` of the loan token, and has authorized the bundle on Midnight via
  // `Midnight.setIsAuthorized(midnightBundlesV2, true, taker)`. V2 acts for `msg.sender`, so the
  // taker sends the transaction (`@morpho-org/morpho-sdk`'s entity `getRequirements()` resolves both).
  return params.walletClient.writeContract({
    account: params.taker,
    chain: base,
    address: midnightBundlesV2,
    abi: midnightBundlesV2Abi,
    functionName: "midnightBundlesV2BuyWithUnitsTargetAndWithdrawCollateral",
    args: [
      params.market,
      targetUnits,
      maxBuyerAssets,
      false, // reduceOnly
      false, // repayEnabled
      quote.data.takeableOffers, // { offer, units, ratifierData }[] straight from the quote
      [], // collateralWithdrawals
      zeroAddress, // collateralReceiver
      0n, // referralFeePct
      zeroAddress, // referralFeeRecipient
      maxUint256, // maxContinuousFee: bound this to the fee the caller accepts
      deadline,
      zeroAddress, // wrappedNative
    ],
  });
}
```

Bids use `midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget`. Compute its
`minSellerAssets` floor from a bid quote as follows (with zero referral fee):

```ts
import { MathLib } from "@morpho-org/morpho-ts";
import { MidnightApi } from "@morpho-org/midnight-sdk/api";
import type { Address, Hash } from "viem";

export async function quoteBidLimit(params: {
  readonly marketId: Hash;
  readonly taker: Address;
  readonly targetUnits: bigint;
  readonly settlementFee: bigint;
}) {
  const quote = await MidnightApi.fetchBookQuote({
    marketId: params.marketId,
    taker: params.taker,
    side: "bids",
    units: params.targetUnits,
    slippage: "0.5",
    settlementFee: params.settlementFee,
  });
  const sellerPrice = MathLib.zeroFloorSub(
    quote.data.averageWorstPrice,
    params.settlementFee,
  );
  const minSellerAssets = MathLib.wMulDown(params.targetUnits, sellerPrice);
  return { quote, minSellerAssets };
}
```

The zero floor prevents a negative unsigned limit when the raw aggregate is below the fee;
the SDK still rejects the quote if any bid it uses to fill the target cannot cover the settlement
fee. Bids returned in `takeableOffers` beyond the target are not checked against the fee, so do
not fall back to them without your own fee check; the bundle's onchain limits still apply. If a
zero proceeds floor is unacceptable, reject it and request a quote with a stricter limit.

Alternatively, pass a separate fee-adjusted `averageWorstPrice` **instead of** `slippage`,
together with the real `settlementFee`: use a fee-inclusive buyer-price ceiling for asks or a
seller-price floor net of the fee for bids. In that mode, the returned `averageWorstPrice` is
already fee-adjusted: apply `MathLib.wMulUp` (asks) or `MathLib.wMulDown` (bids) to it and the
target units without adding or subtracting the fee again. Keep the user's asset limit as an
independent execution bound; per-offer rounding or changed liquidity can still cause a revert.
Prefer the `*WithAssetsTarget*` variants when the caller fixes the asset amount instead of the
unit amount.

If you must submit `Midnight.take` directly (for example from a contract that performs its own
atomic price check), treat each transaction as an independent fill: await and validate its receipt
before reducing the remaining target, stop on any revert, refresh offer consumption, and re-quote
the remaining target so a fresh aggregate guard covers what is left. An aggregate guard for the
full quote does not apply to any subset of it.

## Midnight API

Instantiate `MidnightApi` when an integration makes more than one Midnight API call or needs shared
request options. The instance keeps `baseUrl`, `fetch`, headers, credentials, and abort signals in
one place, while the SDK still owns endpoint paths, HTTP methods, request bodies, and response
normalization. Caller inputs and successful JSON output shapes are trusted at runtime; returned
TypeScript types model the API contract.

Use `tree.mempoolValidate({ chainId })` in normal make-side flows before the maker signs or approves
the root. Ratification data is optional for validation; pass `ratification` only when validating
final payload bytes with real ratifier data after the signature or Setter proof is available. It
throws `MidnightMempoolValidationError` with the API issues when policy validation fails. Pass
`apiUrl` to that method when using a custom Midnight API URL. `MidnightApi` keeps the raw HTTP
surface, including non-throwing `validateMempoolPayload` results for already encoded payload bytes.

## Development

Contribute from the monorepo root. See [CONTRIBUTING.md](../../CONTRIBUTING.md) for setup, checks,
and package workflow.

## License

MIT. See [LICENSE](../../LICENSE).
