Skip to content

Commit

Permalink
Revert using external prices for conversion (#2891)
Browse files Browse the repository at this point in the history
# Description
Reverts #2863

[Failing e2e
test](https://github.com/cowprotocol/services/actions/runs/10358526203/job/28672946540?pr=2874)
showed that in cases when price improvements are significant during
auction run, using external prices (that were attached to Auction at the
auction building time) instead of uniform clearing prices can be very
inaccurate.

Closes #2874

## How to test
Existing tests.
  • Loading branch information
sunce86 authored Aug 15, 2024
1 parent ba79f70 commit 82d6346
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 39 deletions.
9 changes: 2 additions & 7 deletions crates/autopilot/src/domain/settlement/solution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Solution {
.iter()
.map(|trade| {
(*trade.order_uid(), {
let total = trade.total_fee_in_sell_token(&auction.prices);
let total = trade.total_fee_in_sell_token();
let protocol = trade.protocol_fees_in_sell_token(auction);
match (total, protocol) {
(Ok(total), Ok(protocol)) => {
Expand Down Expand Up @@ -333,15 +333,10 @@ mod tests {
eth::U256::from(52937525819789126u128)
);
// fee read from "executedSurplusFee" https://api.cow.fi/mainnet/api/v1/orders/0x10dab31217bb6cc2ace0fe601c15d342f7626a1ee5ef0495449800e73156998740a50cf069e992aa4536211b23f286ef88752187ffffffff
// "executedSurplusFee" and native fee are equal because the sell token is ETH
assert_eq!(
solution.native_fee(&auction.prices).0,
eth::U256::from(6752697350740628u128)
eth::U256::from(6890975030480504u128)
);
// fee read from "executedSurplusFee" https://api.cow.fi/mainnet/api/v1/orders/0x10dab31217bb6cc2ace0fe601c15d342f7626a1ee5ef0495449800e73156998740a50cf069e992aa4536211b23f286ef88752187ffffffff
let order_fees = solution.fees(&auction);
let order_fee = order_fees.get(&domain::OrderUid(hex!("10dab31217bb6cc2ace0fe601c15d342f7626a1ee5ef0495449800e73156998740a50cf069e992aa4536211b23f286ef88752187ffffffff"))).unwrap().clone().unwrap();
assert_eq!(order_fee.total().0, eth::U256::from(6752697350740628u128));
}

// https://etherscan.io/tx/0x688508eb59bd20dc8c0d7c0c0b01200865822c889f0fcef10113e28202783243
Expand Down
45 changes: 13 additions & 32 deletions crates/autopilot/src/domain/settlement/solution/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,33 +139,22 @@ impl Trade {
///
/// Denominated in NATIVE token
pub fn native_fee(&self, prices: &auction::Prices) -> Result<eth::Ether, Error> {
let fee = self.fee()?;
let total_fee = self.total_fee_in_sell_token()?;
let price = prices
.get(&fee.token)
.ok_or(Error::MissingPrice(fee.token))?;
Ok(price.in_eth(fee.amount))
.get(&self.sell.token)
.ok_or(Error::MissingPrice(self.sell.token))?;
Ok(price.in_eth(total_fee.into()))
}

/// Converts given surplus fee into sell token fee.
fn fee_into_sell_token(
&self,
fee: eth::TokenAmount,
prices: &auction::Prices,
) -> Result<eth::SellTokenAmount, Error> {
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 => {
let buy_price = prices
.get(&self.buy.token)
.ok_or(Error::MissingPrice(self.buy.token))?;
let sell_price = prices
.get(&self.sell.token)
.ok_or(Error::MissingPrice(self.sell.token))?;
fee.checked_mul(&buy_price.get().0.into())
.ok_or(error::Math::Overflow)?
.checked_div(&sell_price.get().0.into())
.ok_or(error::Math::DivisionByZero)?
}
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)
Expand All @@ -175,12 +164,9 @@ impl Trade {
/// before and after applying the fees.
///
/// Denominated in SELL token
pub fn total_fee_in_sell_token(
&self,
prices: &auction::Prices,
) -> Result<eth::SellTokenAmount, Error> {
pub fn total_fee_in_sell_token(&self) -> Result<eth::SellTokenAmount, Error> {
let fee = self.fee()?;
self.fee_into_sell_token(fee.amount, prices)
self.fee_into_sell_token(fee.amount)
}

/// Total fee (protocol fee + network fee). Equal to a surplus difference
Expand Down Expand Up @@ -208,12 +194,7 @@ impl Trade {
) -> Result<Vec<(eth::SellTokenAmount, fee::Policy)>, Error> {
self.protocol_fees(auction)?
.into_iter()
.map(|(fee, policy)| {
Ok((
self.fee_into_sell_token(fee.amount, &auction.prices)?,
policy,
))
})
.map(|(fee, policy)| Ok((self.fee_into_sell_token(fee.amount)?, policy)))
.collect()
}

Expand Down

0 comments on commit 82d6346

Please sign in to comment.