-
Notifications
You must be signed in to change notification settings - Fork 83
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
feat: Break down fees per policy #2862
Comments
Why does it need to be persisted? With the fee policies and the traded amounts don't we have everything to compute this number on the fly. The reason the frontend doesn't want to do it is because the logic is a big pita and it's easy to get it wrong. The autopilot already has logic to recover the fee amounts to compute the score. Can this be reused (and using one of the two available price vectors be converted into an approximate sell token amount)? |
Maybe we can reuse logic but code is not reusable - reimplementing it in the orderbook would be the third place where we calculate the same thing (we also have protocol fee calculation in the driver). Maybe it's time to extract the code? AFAIS we currently use uniform clearing prices to convert protocol fee to sell token for sell orders. This is not visible in orderbook.
You mean to use custom prices (that incorporate all fees) instead of uniform clearing prices for conversion to sell token? I don't think this would work for orders where fee represents a significant portion of the order executed amount, as approximate could be way off. |
It's slightly different in the driver and autopilot though. The driver has the "gross solution" without any fee applied and needs to compute the fee so that it can reduce its solution quality by it.
I think this makes sense (and can also allow for some nice tests that applying fees and recovering fees yield the same values). We need to check how we translate the different data-types though since this logic lives in the un-related domains of driver/autopilot respectively. Maybe creating own types in the shared module and having a thin shim in
I think we have two choices
I think the latter make slightly more sense as they are not subject to solver scrutiny. |
I was referring to
If so, then we need a separate PR to use external prices here as well?: https://github.com/cowprotocol/services/blob/main/crates/autopilot/src/domain/settlement/solution/trade.rs#L154-L165 |
If it has already been done using the other price vector I don't think it's worth the change. |
After working on it, I think it's pretty important we align this: #2863 Turned out the change is straight forward. |
# Description Related to discussion in the issue: #2862 (comment) Use external price vector to convert "fee in surplus token" into "fee in sell token" (instead of uniform clearing prices). This is important so that we make sure that it's always true: `total_fee = executed_surplus_fee + protocol fees` If we use uniform clearing prices for `executed_surplus_fee` and external prices for `protocol fees` I think above equation won't stand. ## How to test Existing unit test
There are two more arguments against calculating protocol fees JIT. Examples:
The only drawback of persisting executed protocol fee in database is memory space, if I am not wrong. |
|
ok, then I'm convinced. Where would you store those? One the trades table? |
I suggested putting it into |
If I understand correctly, the plan is to store one value per fee policy. So if there are two protocol fees attached to a trade, there would be two values, one for each fee policy. (This is how it is specified in the issue description, but the general introduction only mentions totals.) |
the other option is to save it in it's original form (in SURPLUS token) to the database and then, on edit: After some thinking, I am leaning towards saving the executed protocol fees in surplus token (for start), but also save surplus token address. I think this is the most future proof solution. Edited the original post ☝️ |
# Description Extracts part of https://github.com/cowprotocol/services/pull/2861/files that can be merged now, and don't have to be part of that PR. This enables me to have a nice pub interface of `Settlement` and continue adding functions for #2862 and not wait for ☝️ PR to be merged. Also #2861 becomes smaller and easier to revert if we encounter some issue on weekly release.
# Description Implements task ~2~ 1 from #2862: > Extend order_execution with a field that contains a list of executed protocol fees in surplus token, together with the surplus token address `autopilot::domain::Settlement` now exposes breakdown of protocol fees in surplus token. Applies the same refactoring to driver, to keep the code 1:1 (and also to use driver tests to confirm no bugs were introduced). ## How to test Existing tests.
…2910) # Description Related to #2862 Exposes executed protocol fees from database on /get_trades endpoint. Database fee field is an array of <token, amount> so that we have the flexibility now (and also in the future) to save taken fee in whatever token we want. This was inspired by discussion where even for the same order we considered defining taken protocol fee in sell token for "surplus" variant, while, for "volume" variant it's more logical to define it in non-surplus token (sell token for sell order and buy token for buy order) so that the taken fee doesn't depend on the amount of surplus provided in a solution. # Changes <!-- List of detailed changes (how the change is accomplished) --> - [ ] Add new columns `protocol_fee_tokens` and `protocol_fee_amounts` to `order_execution` database table. - [ ] Expand `executedProtocolFees` field on `Trade` struct to return both executed protocol fees and fee policies. ## How to test Updated db test. Existing tests. e2e test updated to prove correctness.
Finished. |
Problem
Some time ago we expanded
get_trades
API to return fee policies: #2809Now, we also need, for each fee policy in
get_trades
response, to return executed fee (in sell token). This would allow frontend to summarize them intototal_protocol_fee
. Then, since frontend already hasexecuted_surplus_fee
, they could calculategas_fee
=executed_surplus_fee
-total_protocol_fee
. With that, complete breakdown of fees is provided.Suggested solution
order_execution
database table. We would need to add a new field. Important requirement is that ordering of executed protocol fees saved toorder_execution
needs to be aligned with protocol fees defined infee_policies
database table.domain::Settlement
code needs to be adjusted to not only return total fee but also protocol fee per fee policy.OnSettlementEventUpdater
saves executed protocol fees toorder_execution
.Tasks
order_execution
with a field that contains a list of executed protocol fees in sell tokenAcceptance criteria
For a trade that has fee policies attached, executed fees per fee policy should be returned as well.
The text was updated successfully, but these errors were encountered: