Validate order amounts#76
Open
fedgiac wants to merge 1 commit into
Open
Conversation
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.
After this PR, a user intent is fully respected in a settlement: fill-or-kill orders become actually fill-or-kill; the maximum sell/buy amount is respected. And accounting is done so that partial fills add up across settlements.
This concludes all checks needed to execute a basic settlement. This PR should be reviewed with the idea in mind that all protections for a user intent are in place. Any way to exploit user intents by solvers would be a security issue at this point for reviewing purposes.
Notably, DESIGN.md changes a bit: full-on snapshots aren't needed anymore, the cumulative amounts bought/sold are enough for everything we need to do on-chain. (@tilacog: the partial amount traded so far is only present in the PDA, should we emit something here for the back-end? In principle it can be derived from the past settlements though).
This allows us to simplify the finalize instruction: we don't need any further checks in
FinalizeSettle, we're doing everything we need inBeginSettlealready.The checks
For each order, after its pulls execute,
process_ordercomputesamount_in(total pulled) and hands it, withamount_out(the paired push), to a purecheck_order_amountshelper that enforces:amount_in <= sell_amountfor sell orders.amount_out <= buy_amountfor buy orders.partially_fillableorder's exact side must be filled completely (sell:amount_in == sell_amount; buy:amount_out == buy_amount).Notably, thanks to the way the math is structured, there's no place where we need to worry about rounding issues anywhere in the programs.
Tests
Added integration tests to
settle_limit_prices.rs.As before, some errors (notably overflows) are only checked in unit tests.