Skip to content

Pull main#9

Merged
avernikoz merged 100 commits into
mainfrom
pull-main
Mar 15, 2026
Merged

Pull main#9
avernikoz merged 100 commits into
mainfrom
pull-main

Conversation

@bathord

@bathord bathord commented Mar 14, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

tonylee08 and others added 30 commits January 13, 2026 12:45
* setup

* interest update

* supply amounts

* chart

* new rates

* update DEEP

* usdc interest

* maintainercap

* interest update

* update interest

* interest updates

* latest

* params

* docs

* min borrow
* deposit

* rate limit tests

* fees
* deepbook setup

* finish setup
* margin pre

* margin prep
* update rust + dependencies

* revert

* deps bump
* point deps to testnet
… tracking (MystenLabs#780)

* feat(indexer): Add ReferralFeeEvent handler for spot trading referral tracking

This adds support for indexing ReferralFeeEvent from the deepbook::pool module,
which has been emitting on-chain since September 2025 but was not being indexed.

The ReferralFeeEvent tracks referral fees for spot trades, enabling frontends to:
- Track trade attribution by referral_id (frontend identifier)
- Query accumulated fees per pool and referral
- Build points/rewards systems based on referral trading volume

Changes:
- Add referral_fee_events table migration with pool_id and referral_id indexes
- Add ReferralFeeEvent model to schema
- Add ReferralFeeEvent event struct to indexer models
- Create ReferralFeeEventHandler using define_handler! macro
- Register handler in main.rs under Deepbook package
- Add /referral_fee_events REST endpoint with pool_id and referral_id filters
* create liquidation vault

* cleanup

* action

* deposit into vault
* fund liquidation vault and deposit USDC

* supply 10k USDC

* supply 10k usdc
Ensure pool_id and asset_type labels are stored in canonical format (with 0x prefix and lowercase) to prevent duplicate Prometheus time series when the same addresses are stored differently in the database.

Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
* Add collateral and TPSL event handlers for margin indexing

This adds support for 6 new margin events:

Collateral Events:
- DepositCollateralEvent
- WithdrawCollateralEvent

TPSL (Take Profit / Stop Loss) Events:
- ConditionalOrderAddedEvent
- ConditionalOrderCancelledEvent
- ConditionalOrderExecutedEvent
- ConditionalOrderInsufficientFundsEvent

Changes:
- Added new handlers for each event type
- Added database migration for new tables
- Updated schema with new table definitions
- Added database models for the new events
- Added snapshot tests with mainnet checkpoint data
- Note: TPSL tests are marked #[ignore] pending BCS struct alignment fix

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

* Fix TPSL event BCS deserialization - PendingOrder Option types

The Move struct uses Option<T> for order_type, price, and expire_timestamp
fields, but the Rust struct was using plain T types. This caused BCS
deserialization to fail with "remaining input" error.

Changes:
- Updated PendingOrder struct to use Option<u8>, Option<u64>, Option<u64>
- Updated all 4 TPSL handlers to use unwrap_or(0) for database storage
- Enabled conditional_order_added and conditional_order_cancelled tests
- Added snapshots for the now-working TPSL tests

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

* consolidate tables

* fmt

* fix tests

* fix again

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Sam Barani <sam.barani@sui.io>
* testnet contract

* add collateral events endpoint
* update models

* CI

* withdraw tests
* funding

* action
* migration table

* diesel

* points endpoint

* amount = i64, make addresses optional

* fmt
* migration table

* diesel

* points endpoint

* amount = i64, make addresses optional

* fmt

* return 0 when no rows
* fund abyss vaults

* action
* update rates

* update charts

* new base rate

* action
* supply endpoint

* cargo fmt
tonylee08 and others added 29 commits February 19, 2026 10:11
* feat: Create new sandbox command

*Summary*
When we are running the indexer in a
sandbox environment we need to point
it to custom package ids for core and
margin during runtime.
This was not currently possible
since we used hard coded package ids for
mainnet and testnet.

We also needed a way to support localnet,
therefore using a local_ingestion_path
instead of remote_store_url.

These changes enable us to specify a new
`sandbox` command that expects the
package ids and the sandbox environment
as CLI arguments.

The package ids are overwritten during
runtime only when the sandbox command
is specified.

*Changes*
- Introduce new sandbox.rs module that
contains the core logic.
- Define different ingestion configs
at main.rs
- Modify lib.rs to use the sandbox package_ids
- Define separate Dockerfile for sandbox indexer

*Testing*
1. Use `cargo run -- sandbox --help` to learn more.
2. All existing tests should still pass.
3. Try running the indexer on localnet (the sandbox repo
can help you on this).

* Fix rust fmt errors

* Resolve issue with entry.sh args

* Address PR review feedback for sandbox command

Fix typo "Deebook" -> "DeepBook", use clap::Args instead of Parser
for SandboxArgs subcommand struct, and clarify comment on Mainnet
fallback in sandbox mode.

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

* Address remaining PR review feedback for sandbox command

Replace .expect() panics with .context()? for clean error messages,
map SandboxEnv to DeepbookEnv::Testnet instead of silent Mainnet
default, validate DEEPBOOK_PACKAGE_ID in entry.sh, and improve
comments for clarity.

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* readonly margin functions

* move test

* refactor
* improve ticker endpoint

* claude md
* add balance_manager missing events + test snapshots

* cargo format

* cargo format /schema

* add database indices + rename of balance_managers table to balance_manager_created
* feat: add portfolio endpoint for margin wallet overview

Adds GET /portfolio/:wallet_address endpoint that returns a comprehensive
margin portfolio view including positions, collateral, and LP balances
with real-time USD valuations.

* fix: address claude review comments on portfolio endpoint

- Add wallet address format validation (0x prefix + 64 hex chars)
- Increment db_requests_failed metric on query errors
- Fix LP query to filter on supplier (indexed) instead of sender (unindexed),
  using margin_manager_created join to resolve wallet -> balance_manager_id;
  uses existing idx_asset_supplied_supplier / idx_asset_withdrawn_supplier
- Remove spurious + 0.0 in PortfolioSummary construction
- Add comment explaining LIKE asset_id suffix matching pattern
- Consolidate row struct definitions above queries for readability

* fix: revert LP query sender->supplier change, document missing index

The supplier column in asset_supplied/asset_withdrawn stores the
SupplierCap NFT object ID, not the balance_manager_id. The join through
margin_manager_created.balance_manager_id produced zero results.

Revert to filtering on sender (wallet address) which is correct, and
add a comment noting that sender lacks an index — a follow-up migration
should add idx_asset_supplied_sender and idx_asset_withdrawn_sender.

* fix: address SQL linter findings in portfolio queries

- Guard am.decimals against NULL with COALESCE(am.decimals, 9) in
  collateral query division (linter: division without zero guard)
- Document intentional UNION dedup in asset_meta CTE (linter: UNION vs UNION ALL)

* fix: address PR review comments #1, #2, #5, #6

- Fix metrics accounting: increment per-query (3 succeeded on full
  success) instead of 1, consistent with rest of codebase
- Add hex character validation to wallet address check
- Add TODO for LIKE '%...' pattern preventing index use on balances.asset
- Move row structs (MarginPositionRow, CollateralRow, LpPositionRow) to
  module level, consistent with OhclvRow

* fix: remove lp.price_usd from GROUP BY, use short diesel type aliases

- Remove lp.price_usd from collateral query GROUP BY clause. It is
  functionally dependent on am.symbol (already in GROUP BY) and could
  produce phantom duplicate rows if latest_prices returns NULL.
- Switch row struct diesel annotations from fully qualified paths
  (diesel::sql_types::Text) to imported short aliases (Text, Double,
  BigInt) for consistency with OhclvRow.
The portfolio collateral query references lp.price_usd in the SELECT
but omits it from the GROUP BY clause, causing a PostgreSQL error:
"column lp.price_usd must appear in the GROUP BY clause or be used
in an aggregate function". This was introduced in MystenLabs#862 when a late
commit removed it.
…ystenLabs#872)

This workflow triggers a workflow dispatch
to sui-operations that publishes the
deepbookv3 (sandbox) indexer and server to docker
hub.
* prep new margin setup

* feeds

* XBTC pools

* formatting

* enable for loan

* new sdk

* cleanup

* enabling usde pairs
)

* feat: add pool_created and book_params_updated endpoints

Register PoolCreatedHandler and BookParamsUpdatedHandler indexer
pipelines, add book_params_updated DB table with migration, and
expose two new API endpoints:
- GET /pool_created returns all pool creation events
- GET /book_params_updated?pool_id=X returns latest params for a pool

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

* test: add book_params_updated snapshot test and fix rustfmt

Add snapshot test for BookParamsUpdatedHandler with checkpoint
144783257 and fix import ordering across main.rs, reader.rs,
and server.rs to pass rustfmt.

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

* fix: return 400 when pool_id is missing from book_params_updated

Previously defaulted to empty string and silently returned null
with HTTP 200. Now returns a proper 400 Bad Request error.

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

* test: add accepted snapshot for book_params_updated

The snapshot file was missing, causing insta to fail in CI.
Generated from checkpoint 144783257 output.

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* XBTC/USDC margin pool registration and enablement

Register and enable the XBTC_USDC deepbook pool for margin trading
with 5x leverage, and enable XBTC_USDC for loan from the XBTC margin pool.

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

* Bump @mysten/deepbook-v3 SDK to 1.1.3

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* Replace USDE with USDSUI in createPool and update SDK to 1.1.4

- Update @mysten/deepbook-v3 from 1.1.3 to 1.1.4
- Update @mysten/sui from 2.1.0 to 2.5.0 to satisfy peer dependency
- Replace USDE coin references with USDSUI in createPool.ts
- Add "update sdk" preference to scripts rules
- Add PR format instructions to CLAUDE.md

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

* Update PR branch convention to use generic <username>/<topic>

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

* Ask for branch name instead of assuming convention

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* feat: add SVI oracle for predict protocol

Introduces the oracle module as the first vertical slice of the predict
protocol: SVI volatility surface oracle, fixed-point math utilities,
and the constants they depend on.

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

* refactor: cache repeated calls and fix mul_signed_u64 negative zero

- Cache clock.timestamp_ms() in activate (was called twice)
- Cache oracle.id.to_inner() in update_prices (was called twice)
- Use local `now` in update_svi instead of reading oracle.timestamp back
- Normalize -0 to +0 in mul_signed_u64 (consistent with add/sub)
- Move new_price_data/new_svi_params to public fun section per style guide

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

* refactor: replace phantom Underlying with underlying_asset string

Remove generic type parameter from OracleSVI and all functions. Instead,
store the underlying asset as a String field, making the oracle
non-generic and simpler for downstream consumers.

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

* refactor: replace dynamic fields with VecSet for authorized caps

Replace oracle_cap_id + dynamic field AuthorizedCapKey pattern with a
single authorized_caps: VecSet<ID> field. Simpler, all state visible in
the struct, no dynamic field machinery needed.

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

* refactor: remove cap param from create_oracle

Registry gates oracle creation; cap registration is a separate step
via register_cap, same flow for all caps.

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

* predict oracle

* docs: update migration doc with PR 1 oracle changes

Reflect non-generic OracleSVI, VecSet authorized_caps, cap-less
create_oracle, and PR 1 status/link.

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

* Predict oracle review (MystenLabs#880)

* formatter for predict

* cleanup

* abort bug

* prettier

* fix: address PR review — bit shifts in exp(), block post-settlement updates

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Tony Lee <tony.lee@mystenlabs.com>
…Labs#884)

Comment out BalanceManagerEventHandler, DeepBookReferralCreatedEventHandler,
DeepBookReferralSetEventHandler, PoolCreatedHandler, and BookParamsUpdatedHandler
so the indexer can resume with the original ~39 pipelines and sync to chain tip
at ~250.5M checkpoints. Re-enable once caught up.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
…nLabs#885)

Backfill pipelines (containing @backfill) are inherently far behind
tip and were causing the /status endpoint to report UNHEALTHY even
when all live pipelines are current. They are now excluded from
max_checkpoint_lag, max_time_lag_seconds, and earliest_checkpoint
calculations while still appearing in the response with an
is_backfill flag for monitoring.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* feat(predict): add Vault and config structs (PR2)

Add protocol treasury (Vault) with per-oracle exposure tracking,
plus PricingConfig and RiskConfig with tunable defaults.

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

* address comments

* fix: address PR review feedback

- execute_mint/deposit take Balance instead of Coin (Coin conversion at entrypoint layer)
- Add bounds validation: base_spread and max_total_exposure_pct must be (0, FLOAT_SCALING]
- Fix withdraw underflow when max_liability > balance

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

* fix: regenerate deepbook_margin Move.lock with valid Sui rev

The testnet pinned rev (563c158...) no longer exists in the Sui repo,
causing CI to fail on git checkout during dependency resolution.

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* deposit usde

* action
MystenLabs#886)

* capture referralClaimed + EwmaUpdates + TakerFeePenalty events

* make order_id(u128) String
Indexer has caught up to tip, so re-enable the pipelines that were
temporarily disabled: BalanceManagerEvent, DeepBookReferralCreated,
DeepBookReferralSet, PoolCreated, BookParamsUpdated.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* feat(predict): add MarketKey and PredictManager modules (PR3)

MarketKey is a compact position identifier (oracle_id, expiry, strike, direction).
PredictManager wraps DeepBook BalanceManager with per-user position and collateral tracking.

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

* docs: update predict migration plan with PR1/PR2 merged status

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

* chore: trigger CI

* refactor(predict): use named fields for MarketKey struct

Address review feedback to use named fields instead of positional tuple
for better readability (key.oracle_id, key.expiry, key.strike, etc).

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Add USDSUI margin pool with same parameters as SUIUSDE pool.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
The margin poller inserts into margin_pool_snapshots every 30 seconds
but was using Db::for_read, which sets read-only transaction mode.
This caused "cannot execute INSERT in a read-only transaction" errors
across all server pods.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
…bs#895)

* feat(margin): enable 5x leverage for DEEP_USDC and WAL_USDC pools

Update risk params for DEEP_USDC and WAL_USDC margin pools to support
5x leverage with lower liquidation and borrow risk ratios.

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

* chore: remove tx-data.txt from git tracking

Already in .gitignore; remove the tracked copy so it stays untracked.

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

* Revert "chore: remove tx-data.txt from git tracking"

This reverts commit e7d6067.

* chore: reset tx-data.txt to empty

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
…et (MystenLabs#896)

The get_historical_volume_by_balance_manager_id_with_interval handler
was passing the outer start_time/end_time to get_order_fill_summary
instead of the per-bucket current_start/current_end, causing every
bucket to return the same full-range cumulative volume.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@avernikoz
avernikoz merged commit 03d2dfb into main Mar 15, 2026
5 checks passed
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.

9 participants