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

Domain autopilot fee #2701

Merged
merged 5 commits into from
May 8, 2024
Merged

Domain autopilot fee #2701

merged 5 commits into from
May 8, 2024

Conversation

sunce86
Copy link
Contributor

@sunce86 sunce86 commented May 7, 2024

Description

Part 5 of #2436

Adds protocol fee calculation.
Adds native fee calculation (total fee = protocol fee + network fee).
Adds score calculation.

Most of the code is taken from the driver with some refactoring since here we need to calculate the native fee (total fee) as a surplus difference.

How to test

Updated unit test

@sunce86 sunce86 requested a review from a team as a code owner May 7, 2024 20:18
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 ok, quite a shame that we have to duplicate so much non trivial math. I wonder if we could create a create just for the fee computation (which would define its own types) and use this in the two places.

Comment on lines +97 to +103
let bought = self
.executed
.0
.checked_mul(prices.sell)
.ok_or(error::Math::Overflow)?
.checked_ceil_div(&prices.buy)
.ok_or(error::Math::DivisionByZero)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Being able to convert amounts from one side of the trade to another side of the trade (e.g. some amount in buy tokens to that amount in sell tokens) seems to be quite a common operation here (I see it at least 3 times). Maybe we can expose this in a more easy to use way that can be shared?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried but not happy with what I ended up with, particularly because types are not identical in these code repetitions.

Comment on lines +398 to +399
order::Side::Sell => factor / (1.0 - factor),
order::Side::Buy => factor / (1.0 + factor),
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this isn't possible in the current code path, but should we be defensive here and return math errors in case we divide by 0 (we just had a panic in prod this week)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it would result in f64::NaN and be handled without panic. https://users.rust-lang.org/t/how-should-i-handle-f64-division-by-zero-panic-elegantly/32485/2

Unless you are worried we might in the future add some more code right after this line which might have assumptions that f64 is not nan?

Copy link
Contributor

Choose a reason for hiding this comment

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

In this case apply_factor will return 0 if I'm not mistake (from_f64_lossy for NaN yields 0), which is wrong (volume fee should be large in that case not 0) and going to be gnarly to debug. Why can't we simply return a Math::DivisionByZero error in this case (which intuitively is the correct thing to do here)?

.amount
.checked_sub(&self.surplus_over_limit_price()?.amount)
.ok_or(error::Math::Negative)?;
// We don't have to convert the fee to the sell token, we can just use the fee
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment only applies to the order::Side::Buy case, right? Because in the other case we compute the fee in buy token.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. But the comment stays above the whole line since if we want to drop this conversion, then we remove the whole statement.

@sunce86
Copy link
Contributor Author

sunce86 commented May 8, 2024

Looks ok, quite a shame that we have to duplicate so much non trivial math. I wonder if we could create a create just for the fee computation (which would define its own types) and use this in the two places.

Yeah, that would be the ultimate idea. Extracting surplus/fee/score calculation on the encoded format of the settlement can be definitely shared but requires non-trivial effort. Might be a good task for an external contributor to tackle though.

@sunce86 sunce86 enabled auto-merge (squash) May 8, 2024 18:30
@sunce86 sunce86 merged commit 3e9a4d4 into main May 8, 2024
10 checks passed
@sunce86 sunce86 deleted the domain-autopilot-fee branch May 8, 2024 18:34
@github-actions github-actions bot locked and limited conversation to collaborators May 8, 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