-
Notifications
You must be signed in to change notification settings - Fork 141
fix(stablecoin-exchange): calculate flip order amount from actual received funds #1815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…eived funds to prevent silent failures
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
…vent using just-received tokens
8d1bd8f to
64383d7
Compare
| // Ask filled: maker received quote tokens (rounded down) | ||
| // Flip creates bid: escrows quote tokens (rounded up) | ||
| // Calculate max base affordable with the quote received | ||
| let quote_received = | ||
| base_to_quote(fill_amount, order.tick(), RoundingDirection::Down) | ||
| .ok_or(TempoPrecompileError::under_overflow())?; | ||
|
|
||
| // Calculate how much base can be bought with quote_received at flip_tick | ||
| // Round down to ensure we don't exceed available quote | ||
| quote_to_base(quote_received, order.flip_tick(), RoundingDirection::Down) | ||
| .ok_or(TempoPrecompileError::under_overflow())? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, this implies that the flip order will (very) slowly decrease in quantity as it flips? It might be useful to document this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep done here 7abcf27
|
closing in favor of #1824 |
Closes CHAIN-387
Fixes flip order issues for both ask->bid and bid->ask scenarios.
When an ask flip order is filled, the maker receives quote tokens rounded down. The flip logic then tried to place a bid using the original base amount, which could require more quote for escrow than was actually received.
With the changes in this PR we apply consistent rounding when calculating the flip amount:
By rounding down consistently, the flip order is sized to what can actually be escrowed with received funds.
When a bid flip order is partially filled and the maker withdraws their internal balance, the subsequent fill would still create a flip order by using tokens just credited from the current fill. This violated the design intent that flip orders should only use pre-existing internal balance.
In this case we use
order.amount()instead offill_amount:This ensures the flip requires the original order amount for escrow, which won't be available if the maker withdrew after partial fills.