Skip to content

lukas/deploy scripts midprice#2163

Closed
LukasDeco wants to merge 17 commits intomasterfrom
lukas/deploy-scripts-midprice
Closed

lukas/deploy scripts midprice#2163
LukasDeco wants to merge 17 commits intomasterfrom
lukas/deploy-scripts-midprice

Conversation

@LukasDeco
Copy link
Copy Markdown
Collaborator

@LukasDeco LukasDeco commented Mar 25, 2026

Summary by CodeRabbit

  • New Features

    • Added PropAMM (Programmatic AMM) V1 integration with registry and on-chain validation.
    • Introduced oracle price cache system for improved fill routing and latency.
    • Added admin insurance fund withdrawal capability with event logging.
    • Enabled per-market control to disable formulaic K-factor updates.
    • New propamm-sdk with maker-bot and quoting examples.
    • Integrated Pyth Lazer oracle feed support.
  • Bug Fixes

    • Resolved users remaining in liquidation status after full position liquidation.
    • Fixed isolated positions incorrectly affecting cross-margin bankruptcy checks.
  • Documentation

    • Added PropAMM Interface V1, registry, and anti-spoofing specifications.
    • Documented oracle cache architecture and devnet rollout plan.

lowkeynicc and others added 17 commits March 2, 2026 14:16
* program: fix stuck in liquidation status

* add tests

* update changelog
* exclude isolated perp positions in is_cross_margin_bankrupt

* try to fix test

* update changelog
* filter out stink bids/asks from perp twap crank

* fix fmt

* fix fmt
…ance margin (#2130)

* feat: margin type config for override margin type behavior

* feat: tests for margin override

* feat: idl, anchor tests, more cargo tests

* feat: helpful comments

* fix: logic for user margin ratio evaluated on per position basis

* chore: use maintenance check on cross account place order req on iso margin calc

* feat: margin type config on transfer isolated + tests

* feat: transfer to isolated conditional margin overrides + fulfill_perp_order

* fix: recalc is high lev on each perp position calc margin req

* fix: tests and build errors

* fix: cargo fmt

* feat: additional test scenarios

* fix: using wrong margin type on fulfill perp order margin type config

* fix: anchor tests wrong oracle prices on order margin checks

* fix: bug with custom margin ratios

* fix: incorrect comments on anchor tests w oracle prices

* fix: another anchor test fix

* feat: isolated transfer anchor tests

* fix: anchor tests broken for iso transfer

* fix: fulfill order pr feedback how define is isolated flag

* fix: perp order fulfillment maker is isolated flag

* fix: tests for maker_fills map
… payload (#2133)

* feat: pyth lazer feed_update_timestamp + rm pyth pull + push

* feat: restore pyth pull and push branches on market init for now

* feat: update package.lock

* fix: cargo 2021 on pyth lazer

* fix: pyth lazer tests broken + mocha broken

* fix: pyth-lazer anchor tests feat mising

* fix: broken lp anchor test

* fix: more anchor tests broken from new SOL price

* fix: more tests broken

* fix: lint issues

* fix: todo on broken anchor test

* fix: it.todo not available

* fix: rm extra pyth lazer sdk code

* fix: broken tests pyth lazer

* refactor: cleaner comparison on pyth lazer oracle ix

* fix: backwards pyth timestamp logic
* fix: settle before full withdraw isolated position (#2129)

* sdk: release v2.158.0-beta.1

* fix: pnl settle will also clear out iso balance on market always

* feat: tests for settle pnl with iso withdrawal

* fix: broken pnl esttle iso clear out tests

* feat: add perp position validation after iso balance clear out

* feat: safer borrowing on pnl clear out

* feat: anchor test for pnl iso balance clear out

* fix: broken tests settle pnl iso pos

* feat: test improvements

---------

Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com>
* add bit flag

* add tests

* update idl + add sdk ix

* add helpers

* validate bit before set

* add bankrun

* update changelog

* add to test script

* minor fix

* use hot admin for fast disabling only

* skip in adjust_amm

* update test

* add normal condition to test

* minor fix

* improve tests

* clean up

* fix assertions in test

* update devnet idl
…if-vaults

Chakos/be 107 withdraw from if vaults
* spike: PropAMM poc

* wip

* hardening

* fixes

* updates

* fix

* revert: remove large-liq-safety changes from prop-amm branch

Liquidation size threshold feature (large_liq_notional_threshold,
large_liq_duration on PerpMarket, liquidation.rs slow-liq logic) is
unrelated to PropAMM. Moved to spike/large-liq-safety.

Also adds close_account and transfer_authority bankrun tests, and fixes
two pre-existing prettier/lint issues in the test file.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix tests

* add unified matching (vAMM + DLOB + PropAMM) and comprehensive margin tests

Extends prop_amm matching to support three fill sources with true price
priority: PropAMM book levels, DLOB limit orders, and vAMM. Adds 33 new
tests covering unified matching, security scenarios, and margin behavior
(taker margin type selection for open/close/reduce/flip/increase, maker
margin filter skip semantics with delta recalculation).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* add order versioning header, CU optimizations, and comprehensive test coverage

- Header v2: add order_entry_size (u16) at offset 112, 6 bytes reserved at 114,
  shifting ORDERS_DATA_OFFSET from 112 to 120. Runtime stride via detect_layout().
- CU optimizations: remove happy-path log, authority_matches_32 for stored authority,
  inline increment_sequence_number (skip MidpriceBookViewMut construction).
- Fix prop_amm.rs test helper missing ORDER_ENTRY_SIZE_OFFSET write (broke 20 tests).
- Add regression tests: reserved-byte passthrough, margin filter drops external_fills,
  partial fills, AMM zero-fill fallthrough, pro-rata allocation, short-side fills,
  taker position_before overflow, all-sources-above-limit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix bankrun tests and add mixed DLOB+vAMM fill tests

- Update MIDPRICE_ACCOUNT_MIN_LEN 112→120 for header v2
- Fix shared taker order: each test places its own via placeTakerLimitOrder()
- Fix DLOB maker setup: addUser() after init tx so client finds on-chain account
- Fix bankrunConnection.ts TS error (empty tuple type mismatch)
- Rebuild midprice_pino.so with latest header changes
- Add 5 new mixed fill source tests: vAMM-only, DLOB-only, PropAMM+DLOB,
  all sources combined, and 2+2 CU measurement
- 15/15 bankrun tests passing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* update bun.lock

* add market/oracle order support for PropAMM matching and signed msg e2e tests

- Use order.get_limit_price() instead of raw order.price for proper auction/oracle price resolution
- Accept Market and Oracle order types in prop_amm match validation
- Add atomic signed msg taker tests: limit, market, and oracle order variants
- Fix ts-mocha hanging on failure with --exit flag

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* add fee handling, order validation, and optional taker_order_id to PropAMM match

- Add taker fee calculation and settlement for all three fill sources
  (PropAMM makers, DLOB makers, vAMM) using standard drift fee functions
- Change taker_order_id from u32 to Option<u32>; None resolves to
  user.get_last_order_id(), eliminating race conditions in atomic
  place+fill transactions
- Add DLOB maker validation: is_resting_limit_order, max_ts expiry,
  reduce-only enforcement
- Add taker validation: max_ts expiry, reduce-only via existing position,
  MarketStatus::ReduceOnly enforcement
- Update TS tests to use null for atomic signed-msg place+fill cases

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* rename match_perp_order_via_prop_amm to fill_perp_order2

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* add correctness fixes to fill_perp_order2: margin simulation, preflight checks, oracle gating, funding settlement, order bookkeeping, post-fill finalization

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* refactors + fixes

* add oracle paging system for propamm fill efficiency

* use PropAMM fill event enum to distinguish

* add propamm-sdk: Rust SDK + maker bot for PropAMM quoting

Standalone workspace with:
- propamm-sdk crate: PDA derivations, instruction builders (midprice-pino native + Drift Anchor CPI),
  Pyth oracle parsing, async PropAmmClient, sequence/fill monitoring
- maker-bot example: full quoting loop with clap CLI + env var config
- one-shot-quote example: minimal single-quote demo
- rust-toolchain.toml pinned to stable (parent repo uses 1.76)

10/10 unit tests passing (instruction serialization, PDA determinism).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* oracle price cache: resizable entries, optional account, SDK admin methods

SetOracleCacheEntries now reallocs both buffers to fit the new entry count
(grow or shrink), with seed constraints to prevent cross-shard misuse.
Native handler updated for parity. Cache account is optional in
FillPerpOrder2 — pass drift program ID as sentinel to skip.

Added AdminClient methods: initializeOraclePriceCache, setOracleCacheEntries,
updateOracleCacheConfig, updateOraclePriceCache. PDA helper exported from SDK.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* PropAMM registry, oracle cache publish_ts, CI + SDK fixes

- PropAMM registry: approve/disable/remove instructions with hot-wallet
  authority support, SDK PDA helper, rollout doc
- Oracle cache: publish_ts-based staleness (prefer timestamp over slot),
  get_oracle_ts for Pyth Pull/Lazer sources, should_update_from method
- CI: new propamm-tests job (midprice_pino unit + propAmmCUs bankrun),
  oracle cache SDK tests in verify-sdk-configs
- SDK: export oracleCache module from index, fix build script echo

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix tests

* fix: remove duplicate import and clean up unused deps in test files

- isolated_transfer_tests.rs: fix duplicate create_account_info import
- user/tests.rs: remove unused imports and prefix unused variables
- liquidation/tests.rs: remove unused imports and unnecessary mut

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fmt

* fix error code order

* fix tests

* fix tests, optimize order layout

---------

Co-authored-by: jordy25519 <beauchjord@gmail.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: 0xahzam <ahzam.main@gmail.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 25, 2026

Caution

Review failed

Pull request was closed or merged during review

Walkthrough

The PR introduces comprehensive PropAMM (market maker program) integration with Drift, featuring a new midprice_pino Solana program, oracle price cache infrastructure, market configuration flags, refined isolated position handling, and migration from Pyth Pull to Pyth Lazer oracles.

Changes

Cohort / File(s) Summary
PropAMM Core Program
programs/midprice_pino/src/lib.rs, programs/midprice_pino/Cargo.toml, programs/midprice_pino/README.md, crates/midprice_book_view/...
New midprice_pino Solana program implementing externally-fillable orderbook accounts with standardized V1 header layout, instruction dispatch (initialize, update mid-price, set orders, apply fills, etc.), and midprice_book_view crate providing zero-copy layout definitions and immutable/mutable views for PropAMM account data.
PropAMM TypeScript SDK
propamm-sdk/crates/propamm-sdk/src/..., propamm-sdk/examples/..., propamm-sdk/Cargo.toml
New Rust/TypeScript SDK for PropAMM quoting (PropAmmClient, instruction builders, PDA derivation helpers, oracle parsing, sequence monitoring, example maker bot and one-shot quote programs).
PropAMM Registry & Admin
programs/drift/src/state/prop_amm_registry.rs, sdk/src/addresses/pda.ts, docs/propamm-registry.md, docs/propamm-interface-v1.md
PropAMM registry implementation for whitelisting approved PropAMM accounts (approve_prop_amms, disable_prop_amms, remove_prop_amms instructions), registry lookup helpers, and interface specification documentation.
Oracle Price Cache System
programs/drift/src/state/oracle_price_cache.rs, programs/drift/src/instructions/admin.rs, programs/drift/src/state/oracle_map.rs, sdk/src/oracles/oracleCache.ts
New on-chain oracle price cache account type with freshness tracking, instruction handlers for cache initialization/update/configuration, zero-copy serialization, and TypeScript utilities for refresh planning and candidate selection.
Market Configuration Flags
programs/drift/src/state/perp_market.rs, programs/drift/src/controller/amm.rs, programs/drift/src/math/repeg.rs, sdk/src/math/exchangeStatus.ts
New MarketConfigFlag enum and market_config: u8 field on PerpMarket with DisableFormulaicKUpdate flag; early-return in formulaic_update_k and adjust_amm when flag is set; admin instruction to update market config.
Isolated Position & Liquidation
programs/drift/src/controller/liquidation/tests.rs, programs/drift/src/state/liquidation_mode.rs, programs/drift/src/math/bankruptcy.rs, programs/drift/src/state/user/isolated_transfer_tests.rs
Skip isolated perp positions in cross-margin bankruptcy checks; fallback to CrossMarginLiquidatePerpMode when perp position missing; improved liquidation mode selection; new test coverage for isolated position handling.
Isolated Margin & Collateral
programs/drift/src/controller/isolated_position.rs, programs/drift/src/controller/orders.rs, programs/drift/src/state/margin_calculation.rs, programs/drift/src/state/user.rs
New MarginTypeConfig enum replacing direct MarginRequirementType for per-position isolated/cross-margin selection; meets_place_order_margin_requirement and margin context construction updated to support isolated overrides; maker fill tracking includes isolated flag.
Settlement & PnL
programs/drift/src/controller/pnl.rs, programs/drift/src/controller/pnl/tests.rs
Early-exit path in settle_pnl when unsettled PnL is zero, transferring isolated position deposits into quote spot; comprehensive test coverage for isolated PnL settlement scenarios.
Insurance Fund Withdrawal
programs/drift/src/instructions/if_staker.rs, sdk/src/adminClient.ts
New handle_admin_withdraw_from_insurance_fund_vault instruction with associated event; JavaScript SDK admin methods for withdrawal with share-price validation and optional transfer-hook support.
Pyth Lazer Integration
programs/pyth-lazer/src/..., programs/drift/src/instructions/pyth_lazer_oracle.rs, sdk/package.json
New pyth_lazer program library with message/payload/signature/storage/time modules, price/rate types, and JSON+binary serialization; Drift integration updated to use verify_message for signature verification and per-feed timestamp extraction.
Pyth Pull Removal
programs/drift/src/instructions/pyth_pull_oracle.rs, programs/drift/src/instructions/admin.rs
Complete removal of Pyth Pull oracle instructions (post_update, post_update_atomic, post_multi_updates_atomic, initialize_pyth_pull_oracle) and related PDA/CPI logic.
Error Codes & Events
programs/drift/src/error.rs, programs/drift/src/state/events.rs
New error variants: InvalidPerpMarketConfig, InvalidInsuranceFundWithdrawalRecipient, PropAMMQuoteExpired, InvalidPropAmmMatcherAccount, InvalidPropAmmAccountLayout, InvalidPropAMMAccount, PropAMMMakerUserMismatch, OraclePriceCacheStale, OraclePriceCacheFull; new event struct AdminWithdrawFromInsuranceFundRecord and OrderFilledWithPropAMM explanation variant.
Bid/Ask Filtering
programs/drift/src/math/orders.rs, programs/drift/src/math/orders/tests.rs, programs/drift/src/instructions/keeper.rs, programs/drift/src/math/constants.rs
New BID_ASK_TWAP_MAX_ORACLE_DIVERGENCE_PERCENT constant (15) and filter_bids_asks_by_oracle_divergence function for filtering maker bids/asks by oracle proximity; keeper TWAP updates apply this filter.
SDK Types & IDL
sdk/src/types.ts, sdk/src/idl/drift.json, sdk/src/adminClient.ts, sdk/src/index.ts
TypeScript types updated with MarketConfigFlag, MarginTypeConfig, new perp market marketConfig field, ORDER_FILLED_WITH_PROP_AMM explanation; IDL synchronized; SDK admin client methods for all new instructions.
Constants & Addresses
sdk/src/constants/perpMarkets.ts, sdk/src/addresses/pda.ts, propamm-sdk/crates/propamm-sdk/src/constants.rs
New Nvidia (NVDA-PERP) devnet perp market with Pyth Lazer oracle; new PDA helpers for PropAMM matcher, registry, and oracle cache; propamm-sdk precision and opcode constants.
Pyth Lazer SDK
sdk/src/pyth/pythLazerSubscriber.ts
Pyth Lazer subscription now requests feedUpdateTimestamp property in addition to existing price/bid/ask/exponent fields.
Build & Deployment
.github/workflows/main.yml, Anchor.toml, Cargo.toml, deploy-scripts/midprice_pino/...
CI workflow updated with pinned ts-mocha and new propamm-tests job; workspace exclusion of programs/midprice_pino; new Makefile and Bash scripts for building/deploying midprice_pino to devnet.
Documentation
docs/anti-spoofing.md, docs/oracle-cache.md, docs/propamm-interface-v1.md, docs/propamm-registry.md, docs/rollout.md, propamm-sdk/README.md
Comprehensive documentation covering PropAMM interface specification, registry structure, oracle cache architecture, anti-spoofing detection/enforcement, and devnet rollout procedures.
Configuration & Version
README.md, .gitignore, CHANGELOG.md, sdk/VERSION, sdk/package.json, propamm-sdk/rust-toolchain.toml
Version bumped to 2.158.0-beta.1; @pythnetwork/pyth-lazer-sdk updated to 6.0.0; Rust toolchain set to stable; Makefile and deployment scripts added; documentation updated with tool version requirements.
Test Utilities & Fixtures
programs/drift/src/test_utils.rs, sdk/tests/dlob/helpers.ts, sdk/tests/oracles/oracleCache.ts
New create_executable_program_account_info helper; mock perp markets gain marketConfig: 0 field; comprehensive oracle cache keeper test suite covering freshness, refresh logic, and prewarming.

Estimated code review effort

🎯 5 (Critical) | ⏱️ ~120+ minutes

Possibly related PRs

Poem

🐰 Hops through PropAMM marketplaces with glee,
Midprice books and oracle caches now free,
Isolated margins dance, lazers shine bright,
Insurance fund flows and config flags set right!

✨ Finishing Touches
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch lukas/deploy-scripts-midprice

@LukasDeco LukasDeco closed this Mar 25, 2026
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.

3 participants