nocturne
Rust SDK for building, signing, publishing, discovering, simulating, and taking Morpho Midnight offers.
Install
The crates.io package is nocturne-midnight; Rust imports it as nocturne:
[]
= { = "nocturne-midnight", = "0.1.0" }
Add features = ["alloy-wallet"] when converting publication requests directly into Alloy
transactions.
Quickstart
Build one lend offer, sign its Merkle tree, and verify it locally:
use *;
Run the same example from this repository:
The quickstart is offline and uses dummy addresses and key material.
Complete flows
Publish maker offers
- Build checked markets and offers with
MarketBuilderandOfferBuilder. - Create canonical groups, leaves, and proofs with
OfferGroupandOfferTree. - Sign with
LocalSignerorExternalSigner, or approve the root through SetterRatifier. - Encode
PayloadItemvalues withPayload::encode. - Optionally validate the exact payload with
MidnightApi::validate_payload. - Build the wallet request with
mempool_submissionand submit it.
publish_offer.rs
runs this entire flow through an Alloy wallet.
Find and take offers
- Fetch a book or executable quote with
MidnightApi. - Size and locally check the fill with
get_consumable_units,take_amounts, orsimulate_take. - Build
takecalldata withencode_take_calldata. - Submit the calldata through the Rust wallet client of your choice.
take_offer.rs
shows validation, sizing, simulation, and encoding.
Manage a position
Use supply_collateral, take_lend, take_borrow, supply_collateral_take_borrow,
repay_withdraw_collateral, and redeem to build wallet-ready transactions. Before bundle calls,
build a RequirementPlan and call discover_requirements to resolve only the ERC-20 approvals and
Midnight authorization that are still missing. This RPC-backed helper requires the alloy-wallet
feature; the transaction builders themselves are wallet-agnostic.
Main APIs
| Need | Start with |
|---|---|
| Markets and offers | MarketBuilder, OfferBuilder |
| Groups, roots, and proofs | OfferGroup, OfferTree |
| Local or external signing | LocalSigner, ExternalSigner |
| Books, quotes, and API validation | MidnightApi |
| Publication payloads | Payload, mempool_submission |
| Taking offers and managing positions | take_lend, take_borrow, supply_collateral, repay_withdraw_collateral, redeem |
| Required approvals and authorization | RequirementPlan, discover_requirements |
| Capacity and notional sizing | get_consumable_units, buyer_assets_to_units, seller_assets_to_units |
| Prices, APRs, fees, and simulation | tick_to_price, tick_to_apr, take_amounts, simulate_take |
| Calldata and state codecs | encode_*, decode_* |
| Delegated signing | Authorization, authorization_digest |
The complete API is organized into modules by role. Raw hashing, EIP-712, ABI, conversion, and recovery functions remain available for custom infrastructure.
Examples
| Example | Purpose |
|---|---|
quickstart |
Build, sign, and verify one offer |
publish_offer |
Publish through an Alloy wallet |
quote_book |
Build and re-quote an APR ladder |
take_offer |
Validate, size, simulate, and encode a take |
read_state |
Decode production calldata into typed views |
bench |
Measure the local re-quote pipeline |
The examples index contains exact commands and network requirements.
Security
Nocturne can produce signatures and calldata that move real value. It is an early v0.1.0 release and has not been independently audited. Review wallet requests, keep mutable chain state current, and read the repository's security policy.
License
MIT OR Apache-2.0.