feat(trading): implement slippage-protected multi-buy function (#717) - #759
Merged
Merged
Conversation
…slayerorg#717) Add a multi-buy endpoint that accepts a vector of purchase legs and executes them atomically with per-leg slippage protection. - POST /api/v1/trading/multi-buy endpoint - Pre-flight validation: empty legs, max 10 legs, duplicate creator, deadline check against current ledger, buyer balance vs worst-case cost - Per-leg bonding curve cost computed via computeBuyCost and compared against max_price * amount - Structured debug logs: KeyPurchased per leg, MultiBuyCompleted summary - Provider interfaces for ledger, balance, and supply to support testing - 6 unit tests covering all acceptance criteria
Member
|
The all-or-nothing rollback on slippage is exactly the right call here. One thing worth thinking about as usage grows is what happens when a partial failure lands near the deadline ledger and the buyer has to retry the whole bundle from scratch at new prices. A structured error response that tells the caller which leg triggered the slippage and what the actual cost was would make client side recovery a lot smoother without changing the core atomicity guarantee. |
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat(trading): implement slippage-protected multi-buy function (#717)
Summary
Closes #717
Investors seeking to diversify across multiple creators previously had to submit one transaction per creator, exposing them to price movements between submissions. This PR implements a multi-buy function that accepts a vector of
(creator, amount, max_price)legs and executes purchases atomically with all-or-nothing semantics and per-leg slippage protection.Changes
1. Endpoint & Schema (
src/modules/trading/multi-buy.schemas.ts)POST /api/v1/trading/multi-buyendpoint.legs: 1–10 entries (rejectslegs_emptyif empty,too_many_legsif > 10).amountandmax_price: positive integers.global_deadline_ledger: positive integer sequence number.2. Service Logic (
src/modules/trading/multi-buy.service.ts)duplicate_creator: panics if any creator appears more than once.deadline_passed: compares current ledger againstglobal_deadline_ledger.insufficient_funds: verifies buyer XLM balance covers worst-case total cost (computeBuyCost.slippage_exceededif leg cost exceedsmax_price * amount, rolling back all purchases.key_purchaseddebug log per leg and amulti_buy_completedsummary log upon completion.3. Controller & Routing (
src/modules/trading/multi-buy.controllers.ts,src/modules/trading/multi-buy.routes.ts)/api/v1/trading/multi-buyinsrc/modules/index.ts.Test Coverage
Unit tests in
src/modules/trading/multi-buy.service.test.ts(6 passed):max_priceand returns correct supplies and costs.max_price(slippage_exceeded).duplicate_creator).too_many_legs).global_deadline_ledgeris in the past (deadline_passed).insufficient_funds).