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

Revert using external prices for conversion #2891

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading