Skip to content

feat: Aera money market (lend, borrow, collateral) — 8 tools - #12

Merged
fibanachos merged 4 commits into
mainfrom
feat/aera-lending
Oct 26, 2026
Merged

fibanachos merged 4 commits into
mainfrom
feat/aera-lending

Conversation

@fibanachos

Copy link
Copy Markdown
Collaborator

Adds the Aera overcollateralised COOK money market as a venue: 8 tools (65 total), library subpath cookie-mcp/lending.

Tools

  • Reads (no key): get_lending_markets, get_lending_position (accepts .cook names)
  • Writes: lend / withdraw_lend, deposit_collateral / withdraw_collateral, borrow / repay

How it is built

  • No IDL anywhere (the on-chain IDL account does not exist and the repo ships none), so src/core/lending/program.ts hand-encodes every instruction from the program source. Discriminators are Anchor's defaults and a golden test recomputes them from the names; account layouts are pinned against live accounts.
  • math.ts mirrors the program's accounting (index projection, share ⇄ liquidity, kinked rates, obligation health) for pre-flight checks and estimates. Rounding matches the handlers (user-favourable floors, owed ceils).
  • Every health-dependent write prepends the same-slot prelude the program demands (accrue, refresh_oracle with the stake-pool + ProgramData accounts, refresh_obligation with [reserve, oracle] pairs) in the same transaction.
  • Collateral is abCOOK shares: deposit_collateral supplies bCOOK and deposits the minted shares in one tx (creating the obligation on first use); withdraw_collateral reverses both. Agents only reason in bCOOK.
  • Native COOK moves through a throwaway token account per tx, so a wallet's existing wCOOK ATA is never closed or unwrapped.
  • borrow / withdraw_collateral refuse before signing when the loan-to-value limit would be exceeded or the health factor would fall under minHealthFactor (default 1.1; 1.0 = liquidation line).
  • Market pinned to id 0 ("Aera Core"); reserves are enumerated by getProgramAccounts, so a new reserve shows up without a release.

Verification

  • yarn test green: 805 unit tests (54 new), 65 tools in smoke.
  • scripts/verify-lending.ts (no key, nothing sent): reads, 7 guard refusals, then external-signer dry runs of every write flow — each builds its real transaction, simulates it on the RPC and stops at needs_signature. lend, withdraw_lend, borrow, repay (amount and all), withdraw_collateral (redeem and keepShares) simulated OK for a live borrower; deposit_collateral + lend bCOOK simulated OK for a bCOOK holder with no obligation. LTV-exceeding borrow and withdraw-all-with-debt refused before simulation.
  • LIVE=1 npx tsx scripts/verify-lending.ts runs a dust round trip (lend 1 COOK, withdraw, deposit 1 bCOOK, borrow 0.5, repay all, withdraw collateral). Not yet run.

Hand-encoded aera instructions (no IDL anywhere), per-action same-slot prelude, abCOOK-shares collateral handled in one tx, health-factor refusal with a named opt-out, throwaway native-COOK leg, external-signer dry-run verify script.
…al withdrawal

Mirror refresh_obligation: an obligation is stressed only when an oracle won't permit Borrow, and RateWarning does. borrow no longer refuses in rateWarning. withdraw_collateral refuses an amount above the deposit instead of clamping it. The position note uses DEFAULT_MIN_HEALTH_FACTOR.
# Conflicts:
#	src/core/config.ts
#	src/mcp/createServer.ts
@fibanachos
fibanachos merged commit d4bf366 into main Oct 26, 2026
1 check passed
@fibanachos
fibanachos deleted the feat/aera-lending branch October 26, 2026 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant