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

Convert executed_fee_amount to surplus token #3224

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

sunce86
Copy link
Contributor

@sunce86 sunce86 commented Jan 8, 2025

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 fixed fee_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:

let executedFeeAmountInSurplusToken = match order.kind {
    Buy => executedFeeAmount,
    Sell => executedFeeAmount * executedBuyAmount / executedSellAmountBeforeFees,
};

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 :

/// Converts given surplus fee into sell token fee.
    fn fee_into_sell_token(&self, fee: eth::TokenAmount) -> Result<eth::SellTokenAmount, Error> {
        let fee_in_sell_token = match self.side {
            order::Side::Buy => fee,
            order::Side::Sell => fee
                .checked_mul(&self.prices.uniform.buy.into())
                .ok_or(error::Math::Overflow)?
                .checked_div(&self.prices.uniform.sell.into())
                .ok_or(error::Math::DivisionByZero)?,
        }
        .into();
        Ok(fee_in_sell_token)
    }

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

  • Executed version of fee_amount is now shown in surplus token

How to test

Confirmed on several examples of orders that have fee_amount > 0.

@sunce86 sunce86 requested a review from a team as a code owner January 8, 2025 13:43
@sunce86 sunce86 self-assigned this Jan 8, 2025
Copy link

github-actions bot commented Jan 8, 2025

Reminder: Please consider backward compatibility when modifying the API specification.
If breaking changes are unavoidable, ensure:

  • You explicitly pointed out breaking changes.
  • You communicate the changes to affected teams.
  • You provide proper versioning and migration mechanisms.

Caused by:

@squadgazzz
Copy link
Contributor

The change looks trivial, but I don't have a full picture of these fee migrations and PRs ordering, so I can't think of any implications atm. I need to go over other PRs again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants