Skip to content

Commit

Permalink
Remove UNIT (#2499)
Browse files Browse the repository at this point in the history
# Description
Addresses
#2448 (comment)

Apply function needs to return the same denomination AFAIU.
  • Loading branch information
sunce86 authored Mar 8, 2024
1 parent 3aa3896 commit 4b4b77e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
21 changes: 19 additions & 2 deletions crates/driver/src/domain/competition/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ pub struct Token {
pub struct Price(eth::Ether);

impl Price {
/// The base Ether amount for pricing.
const BASE: u128 = 10_u128.pow(18);

pub fn new(value: eth::Ether) -> Result<Self, InvalidPrice> {
if value.0.is_zero() {
Err(InvalidPrice)
Expand All @@ -413,8 +416,22 @@ impl Price {
}

/// Apply this price to some token amount, converting that token into ETH.
pub fn apply(self, amount: eth::TokenAmount) -> eth::Ether {
(amount.0 * self.0 .0).into()
///
/// # Examples
///
/// Converting 1 ETH expressed in `eth::TokenAmount` into `eth::Ether`
///
/// ```
/// use driver::domain::{competition::auction::Price, eth};
///
/// let amount = eth::TokenAmount::from(eth::U256::exp10(18));
/// let price = Price::new(eth::Ether::from(eth::U256::exp10(18))).unwrap();
///
/// let eth = price.in_eth(amount);
/// assert_eq!(eth, eth::Ether::from(eth::U256::exp10(18)));
/// ```
pub fn in_eth(self, amount: eth::TokenAmount) -> eth::Ether {
(amount.0 * self.0 .0 / Self::BASE).into()
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/driver/src/domain/competition/order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ impl Order {
tokens.get(self.sell.token).price,
) {
(Some(buy_price), Some(sell_price)) => {
let buy = buy_price.apply(self.buy.amount);
let sell = sell_price.apply(self.sell.amount);
let buy = buy_price.in_eth(self.buy.amount);
let sell = sell_price.in_eth(self.sell.amount);
sell.0
.to_big_rational()
.checked_div(&buy.0.to_big_rational())
Expand Down
14 changes: 4 additions & 10 deletions crates/driver/src/domain/competition/solution/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,12 @@ impl Trade {
///
/// Denominated in NATIVE token
fn native_surplus(&self, prices: &auction::Prices) -> Result<eth::Ether, Error> {
let surplus = self.surplus_token_price(prices)?.apply(
let surplus = self.surplus_token_price(prices)?.in_eth(
self.surplus()
.ok_or(Error::Surplus(self.sell, self.buy))?
.amount,
);
// normalize
Ok((surplus.0 / *UNIT).into())
Ok(surplus)
}

/// Protocol fee is defined by fee policies attached to the order.
Expand Down Expand Up @@ -215,9 +214,8 @@ impl Trade {
fn native_protocol_fee(&self, prices: &auction::Prices) -> Result<eth::Ether, Error> {
let protocol_fee = self
.surplus_token_price(prices)?
.apply(self.protocol_fee()?.amount);
// normalize
Ok((protocol_fee.0 / *UNIT).into())
.in_eth(self.protocol_fee()?.amount);
Ok(protocol_fee)
}

fn surplus_token(&self) -> eth::TokenAddress {
Expand Down Expand Up @@ -263,7 +261,3 @@ pub enum Error {
#[error(transparent)]
Math(#[from] Math),
}

lazy_static::lazy_static! {
static ref UNIT: eth::U256 = eth::U256::from(1_000_000_000_000_000_000_u128);
}

0 comments on commit 4b4b77e

Please sign in to comment.