Skip to content

Feature/get quantity in#5

Closed
bathord wants to merge 2 commits into
mainfrom
feature/get-quantity-in
Closed

Feature/get quantity in#5
bathord wants to merge 2 commits into
mainfrom
feature/get-quantity-in

Conversation

@bathord

@bathord bathord commented Dec 3, 2025

Copy link
Copy Markdown
Collaborator

Description

Problem

Users currently cannot calculate the exact input needed for a fixed output (e.g., repaying exactly 100 USDC debt). get_quantity_out only supports fixed input.

Solution

Added get_quantity_in, a view function that calculates the required input to achieve a target output.

Details

  • Algorithm: Iterates the order book to accumulate input cost for a target output.
  • Safety: Rounds UP inputs to guarantee the user receives at least the requested amount (preventing dust shortfalls).
  • Fees: Returns (NetInput, ActualOutput, Fee) separately, allowing precise "Input + Fee" calculations for swaps.

Changes

  • book.move: Core implementation with reverse matching logic.
  • pool.move: Public wrappers get_quantity_in and get_quantity_in_input_fee.

@bathord

bathord commented Dec 9, 2025

Copy link
Copy Markdown
Collaborator Author

No need in this anymore, deepbook implemented their own get_quantity_in functions.

@bathord bathord closed this Dec 9, 2025
bathord pushed a commit that referenced this pull request Mar 14, 2026
* 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.
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