Skip to content

Validate order limit prices#75

Open
fedgiac wants to merge 1 commit into
push-funds-to-user-processing-in-finalize-settlefrom
limit-price-check
Open

Validate order limit prices#75
fedgiac wants to merge 1 commit into
push-funds-to-user-processing-in-finalize-settlefrom
limit-price-check

Conversation

@fedgiac

@fedgiac fedgiac commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Make a user intent more meaningful by disallowing the contract to pull funds from the user if the price is worse than what the intent specifies.

Until now the program moved an order's sell tokens (pulls) and paid its buy tokens (pushes) without checking that the amounts respected the order. This adds the per-order checks that back DESIGN.md's user guarantee that the user receives at least the specified buy amount, proportional to how much of the sell amount was used.

This is the first of two PRs: it does the limit price only. The per-order sell/buy caps, fill-or-kill, and cumulative cross-settlement accounting follow separately.

Parsing the push amounts

BeginSettle now also parses the push amounts through introspection, similarly to what was done for the accounts before.
Consistency with parsing is validated by sharing helpers and a proptest.

The check

amount_out * sell_amount >= buy_amount * amount_in

Note that it doesn't matter for this check whether an order is sell or buy.

I run the check after the pulls rather than before to make the code simpler.
I'm a bit concerned for security once token2022 is introduced and CPI on transfer becomes a thing, but for now this code should be safe.

Tests

Added settle_limit_prices.rs with integration tests.
Some of the edge cases are only covered by unit tests, but by the way the code is structured I think it should be enough.

@fedgiac
fedgiac requested a review from a team as a code owner July 17, 2026 20:43
@fedgiac
fedgiac force-pushed the limit-price-check branch from 99a169c to b535496 Compare July 17, 2026 21:09
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