Moonglade

The 1inch Aqua AMM for Morpho Midnight: lend, borrow, or exit fixed-rate loans in one swap.

Moonglade

Created At

ETHGlobal Mumbai

Winner of

1inch

1inch - Build an Aqua App 2nd place

Project Description

Moonglade makes fixed-rate credit tradable through swaps. Built on 1inch Aqua, SwapVM, and Morpho Midnight, it lets users lend, borrow against collateral, and exit lending positions through the same AMM interface.

Morpho Midnight provides fixed-rate, fixed-term loans through an order book. Trading depends on finding an offer at the right size and rate. Lenders may struggle to exit before maturity, borrowers may wait for offers to fill, and makers need infrastructure to keep orders updated. Moonglade adds an AMM alongside that order book, giving users executable quotes from maker-run bond desks.

The core asset is mgNOTE, an ERC-20 share of a Midnight market’s net credit. Each market has its own note contract. This turns a credit position into something users can hold, transfer, and trade through standard token interfaces.

Users have four main actions:

  • Lend: Swap the market’s loan asset for mgNOTE. The purchase price determines the return available from holding the credit to maturity, assuming repayment. Missing note inventory is created during settlement.
  • Borrow: Supply collateral to your own Midnight position and approve a bounded borrowing allowance. A swap then mints the necessary note shortfall and sells it for the loan asset. The resulting debt belongs to your account, backed by your collateral.
  • Exit early: Sell existing mgNOTE back to a desk while it is trading and has available liquidity. You receive the current market quote, allowing you to exit without waiting for maturity. The exit price can differ from your entry price.
  • Redeem: After maturity, burn mgNOTE for its proportional share of repaid Midnight credit as repayments become available.

Makers operate these desks from their own wallets. Aqua lets multiple strategies share the same wallet balances, so idle capital does not need to be deposited into separate liquidity pools. Makers select a pricing curve, set their trading fee, and configure debt and collateral limits.

Moonglade’s settlement hooks create inventory only when a trade needs it. When a lender buys notes, the desk can originate Midnight credit to supply them. When notes flow back, the desk can retain them as inventory or burn them to cancel maker debt and release surplus collateral.

The default YieldSpace curve prices remaining maturity and inventory imbalance. Trading changes the quoted rate, encouraging offsetting flow between lenders and borrowers. Makers earn fees in both directions, while their exposure depends on the balance of that flow. Wrapping and unwrapping Midnight offers also lets arbitrageurs trade price differences between desks and the order book.

The protocol is designed around Midnight markets rather than one specific asset pair. The current Base mainnet deployment demonstrates a USDC desk with WETH collateral. Real lending, borrowing, and early-exit transactions have completed on mainnet; separate fork demonstrations cover maturity redemption and shared liquidity across strategies.

How it's Made

I built Moonglade in Solidity with Foundry, combining Morpho Midnight’s fixed-rate credit markets with 1inch Aqua’s shared wallet liquidity and a custom SwapVM router.

The tokenization layer is mgNOTE, an ERC-20 share of a Midnight market’s net credit. A factory deploys one note per market, while a shared ratifier authorizes only the note’s active settlement offer. Notes can also wrap and unwrap Midnight order-book offers, connecting the AMM to the existing credit market.

On SwapVM, I added a custom MoongladeSwap opcode (0xd0). It queries a maker-selected pricing contract, snaps the quote to Midnight’s tick grid with directional rounding, and validates the final amounts. The default YieldSpace curve prices remaining maturity and inventory imbalance, with maker-configurable fees embedded in both bid and ask prices.

Aqua makes the desk capital-efficient: makers register virtual budgets while idle funds remain in their wallets, available across strategies. The unusual part is inventory creation during settlement. preTransferOut mints missing maker inventory; preTransferIn mints a borrower’s note shortfall under a bounded, expiring approval. Borrowing becomes a note sale that opens a real Midnight loan on the borrower’s own collateral. After incoming notes settle, postTransferIn can burn them to cancel maker debt and release surplus collateral.

One practical hack was handling incompatible compiler pins: Midnight requires Solidity 0.8.34, while SwapVM pins 0.8.30. I compiled separate dependency graphs and used vm.getCode with assembly CREATE to deploy their artifacts from one Foundry script.

The frontend is client-only React, TypeScript, and Vite, using RainbowKit, wagmi, and viem for wallets, live quotes, simulations, and transactions, hosted on Vercel. The protocol runs on Base against the official Aqua and Midnight deployments. Lending, borrowing, and early exits have executed with real mainnet funds; fork scripts demonstrate the wider lifecycle, including maturity redemption and shared liquidity.

background image mobile

Join the mailing list

Get the latest news and updates