Skip to content
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

Fix quote fee handling in market price #2572

Merged
merged 15 commits into from
Mar 29, 2024
Merged

Fix quote fee handling in market price #2572

merged 15 commits into from
Mar 29, 2024

Conversation

sunce86
Copy link
Contributor

@sunce86 sunce86 commented Mar 28, 2024

Description

Fixes the way we scale quote sell and quote buy amounts to consider quote fee.

Quoting @fhenneke and his example:

Suppose there is a quote for an order selling 100 DAI to USDC. The quote might be something like sell amount: 100 DAI, buy amount 100 USDC, network fee 50 DAI.
The logic currently used by the solver team: the reference buy amount for computing price improvement is 50 USDC. This is because if a user sells 100 DAI and 50 DAI are for network fees and the exchange rate is 100->100, i.e., 1 -> 1, the expected buy amount is 50 DAI.
The logic currently used by the driver: the reference buy amount for computing price improvement is 66 USDC. This is because if the user payed additional 50DAI on top of the 100DAI they want to sell, they could get 100USDC. Scaling down this trade from 150DAI -> 100USDC gives 100DAI -> 66USDC.

Note that this is affecting only sell orders.

How to test

Fixed existing tests to be aligned with new logic.

@squadgazzz
Copy link
Contributor

Driver tests should probably tell whether the logic is legit.

// 294580438010728) GNO for 9871580343970612988 DAI, then scaled to sell amount
// gives 10000000000000000000 GNO for 9871289555090525964 DAI
// 294580438010728 GNO fee. Equivalent to: 10000000000000000000 GNO for
// 9871580343970612988 * ((10000000000000000000 - 294580438010728) /
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note how we no longer add fee on top of sell amount, but instead, we now reduce the buy amount by converted amount.

) -> bool {
match kind {
OrderKind::Buy => {
order.sell.full_mul(quote.buy) < (quote.sell + quote.fee).full_mul(order.buy)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buy order stayed the same as before, as we do need to add fee to sell amount in this case

order
.sell
.full_mul(quote.buy - quote.fee * quote.buy / quote.sell)
< quote.sell.full_mul(order.buy)
Copy link
Contributor Author

@sunce86 sunce86 Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For buy sell orders, we need to reduce the quote buy amount by converted fee amount

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you mean "sell" orders here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harisang Exactly, fixed the comment

@sunce86 sunce86 changed the title DRAFT: Fix quote fee handling in market price Fix quote fee handling in market price Mar 28, 2024
Copy link
Contributor

@fleupold fleupold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. Some safe math issues and comments on the readability/strictness of the tests.

crates/driver/src/domain/competition/solution/fee.rs Outdated Show resolved Hide resolved
crates/driver/src/domain/competition/solution/fee.rs Outdated Show resolved Hide resolved
crates/shared/src/order_validation.rs Outdated Show resolved Hide resolved
Comment on lines +404 to +405
205315427345679u128.into(),
202678773689953u128.into(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, very clear that this is now working as intended ;-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will be tackled in the #2561 🙂

Copy link
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The tests look valid.

@sunce86 sunce86 enabled auto-merge (squash) March 29, 2024 11:17
@sunce86 sunce86 merged commit e005371 into main Mar 29, 2024
9 checks passed
@sunce86 sunce86 deleted the quote-adjustment branch March 29, 2024 11:18
@github-actions github-actions bot locked and limited conversation to collaborators Mar 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants