Convert executed_fee_amount to surplus token #3224
Open
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.
Description
As part of the initiative to show all executed order fees in surplus tokens, we have to convert the
executed_fee_amount
from sell token to surplus token. This is needed by frontend and other integrators, so that ALL fees are in surplus token and easily summarized.As a reminder,
executed_fee_amount
represents executed version of a fixedfee_amount
fee that is part of the signed order and it's always assumed that it is expressed in sell token. But we can easily convert the EXECUTED REPRESENTATION of this fee into surplus token by following this math:This math is 100% equal to using uniform clearing prices to convert between fee in sell and buy tokens, as already used in the existing code:
https://github.com/cowprotocol/services/blob/main/crates/autopilot/src/domain/settlement/trade/math.rs#L126-L129 :
Note 1: This PR only changes how the executed version of this fee is represented over the API. It does not change anything in the database, so it also does not change our assumption of how the
fee_amount
is saved internally.Note 2: Only > ~2 year old orders are affected by this. All newer orders have
executed_fee_amount = 0
.Note 3: These old orders will have a bad total fee shown when this PR is merged. We need #3184 merged as well (and all consequent data migrations) so that all fees are finally shown in surplus tokens.
Changes
fee_amount
is now shown in surplus tokenHow to test
Confirmed on several examples of orders that have fee_amount > 0.