Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions crates/autopilot/src/boundary/order.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::domain::{self, eth},
ethrpc::alloy::conversions::IntoLegacy,
ethrpc::alloy::conversions::IntoAlloy,
shared::remaining_amounts,
};

Expand All @@ -15,12 +15,12 @@ pub fn to_domain(
domain::Order {
uid: order.metadata.uid.into(),
sell: eth::Asset {
token: order.data.sell_token.into_legacy().into(),
amount: order.data.sell_amount.into_legacy().into(),
token: order.data.sell_token.into(),
amount: order.data.sell_amount.into(),
},
buy: eth::Asset {
token: order.data.buy_token.into_legacy().into(),
amount: order.data.buy_amount.into_legacy().into(),
token: order.data.buy_token.into(),
amount: order.data.buy_amount.into(),
},
protocol_fees,
created: u32::try_from(order.metadata.creation_date.timestamp()).unwrap_or(u32::MIN),
Expand All @@ -29,7 +29,7 @@ pub fn to_domain(
receiver: order.data.receiver,
owner: order.metadata.owner,
partially_fillable: order.data.partially_fillable,
executed: remaining_order.executed_amount.into(),
executed: remaining_order.executed_amount.into_alloy().into(),
pre_interactions: if order_is_untouched {
order.interactions.pre.into_iter().map(Into::into).collect()
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/database/competition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl super::Postgres {
.map(|(solver, score)| database::reference_scores::Score {
auction_id: competition.auction_id,
solver: ByteArray(solver.0.0),
reference_score: u256_to_big_decimal(&score.get().0),
reference_score: number::conversions::alloy::u256_to_big_decimal(&score.get().0),
})
.collect();

Expand Down
16 changes: 11 additions & 5 deletions crates/autopilot/src/domain/auction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
super::{Order, eth},
alloy::primitives::Address,
alloy::primitives::{Address, U256},
std::collections::HashMap,
};

Expand Down Expand Up @@ -70,14 +70,20 @@ impl Price {
/// ```
/// use autopilot::domain::{auction::Price, eth};
///
/// let amount = eth::TokenAmount::from(eth::U256::exp10(18));
/// let price = Price::try_new(eth::Ether::from(eth::U256::exp10(15))).unwrap(); // 0.001 ETH
/// let amount = eth::TokenAmount::from(eth::U256::from(10).pow(eth::U256::from(18)));
/// let price = Price::try_new(eth::Ether::from(
/// eth::U256::from(10).pow(eth::U256::from(15)), // 0.001 ETH
/// ))
/// .unwrap();
///
/// let eth = price.in_eth(amount);
/// assert_eq!(eth, eth::Ether::from(eth::U256::exp10(15)));
/// assert_eq!(
/// eth,
/// eth::Ether::from(eth::U256::from(10).pow(eth::U256::from(15)))
/// );
/// ```
pub fn in_eth(self, amount: eth::TokenAmount) -> eth::Ether {
(amount.0 * self.0.0 / Self::BASE).into()
(amount.0 * self.0.0 / U256::from(Self::BASE)).into()
}
}

Expand Down
19 changes: 10 additions & 9 deletions crates/autopilot/src/domain/competition/winner_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ mod tests {
},
infra::Driver,
},
alloy::primitives::{U256, address},
ethcontract::H160,
ethrpc::alloy::conversions::{IntoAlloy, IntoLegacy},
hex_literal::hex,
Expand Down Expand Up @@ -1157,7 +1158,7 @@ mod tests {
prices
.iter()
.map(|(token_id, price)| {
let token_address = TokenAddress(*token_map.get(token_id).unwrap());
let token_address = token_map.get(token_id).unwrap().into_alloy().into();
let price = create_price(*price);
(token_address, price)
})
Expand Down Expand Up @@ -1274,7 +1275,7 @@ mod tests {
fn create_test_arbitrator() -> super::Arbitrator {
super::Arbitrator {
max_winners: 10,
weth: H160::from_slice(&hex!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")).into(),
weth: address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").into(),
}
}

Expand All @@ -1294,18 +1295,18 @@ mod tests {
uid: create_order_uid(uid),
sell: eth::Asset {
amount: sell_amount.into(),
token: sell_token.into(),
token: sell_token.into_alloy().into(),
},
buy: eth::Asset {
amount: buy_amount.into(),
token: buy_token.into(),
token: buy_token.into_alloy().into(),
},
protocol_fees: vec![],
side,
receiver: None,
owner: Default::default(),
partially_fillable: false,
executed: eth::U256::zero().into(),
executed: eth::U256::ZERO.into(),
pre_interactions: vec![],
post_interactions: vec![],
sell_token_balance: order::SellTokenSource::Erc20,
Expand Down Expand Up @@ -1338,7 +1339,7 @@ mod tests {
) -> Auction {
// Initialize the prices of the tokens if they are not specified
let prices = prices.unwrap_or({
let default_price = create_price(DEFAULT_TOKEN_PRICE.into());
let default_price = create_price(U256::from(DEFAULT_TOKEN_PRICE));
let mut res = HashMap::new();
for order in &orders {
res.insert(order.buy.token, default_price);
Expand Down Expand Up @@ -1381,8 +1382,8 @@ mod tests {
let prices = prices.unwrap_or({
let mut res = HashMap::new();
for (_, trade) in &trades {
res.insert(trade.buy.token, create_price(eth::U256::one()));
res.insert(trade.sell.token, create_price(eth::U256::one()));
res.insert(trade.buy.token, create_price(eth::U256::ONE));
res.insert(trade.sell.token, create_price(eth::U256::ONE));
}
res
});
Expand All @@ -1394,7 +1395,7 @@ mod tests {
solution_id,
solver_address,
// provided score does not matter as it's computed automatically by the arbitrator
Score(eth::Ether(eth::U256::zero())),
Score(eth::Ether(eth::U256::ZERO)),
trade_order_map,
prices,
);
Expand Down
21 changes: 9 additions & 12 deletions crates/autopilot/src/domain/eth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
pub use alloy::primitives::{Address, B256, U256};
use {
crate::{domain, util::conv::U256Ext},
derive_more::{Display, From, Into},
};
pub use {
alloy::primitives::Address,
primitive_types::{H160, H256, U256},
};

/// ERC20 token address for ETH. In reality, ETH is not an ERC20 token because
/// it does not implement the ERC20 interface, but this address is used by
/// convention across the Ethereum ecosystem whenever ETH is treated like an
/// ERC20 token.
/// Same address is also used for XDAI on Gnosis Chain.
pub const NATIVE_TOKEN: TokenAddress = TokenAddress(H160([0xee; 20]));
pub const NATIVE_TOKEN: TokenAddress = TokenAddress(Address::repeat_byte(0xee));

/// Block number.
#[derive(Debug, Copy, Clone, From, PartialEq, PartialOrd, Default)]
Expand All @@ -29,13 +26,13 @@ impl std::ops::Add<u64> for BlockNo {

/// A transaction ID, AKA transaction hash.
#[derive(Debug, Copy, Clone, From, Default)]
pub struct TxId(pub H256);
pub struct TxId(pub B256);

/// An ERC20 token address.
///
/// https://eips.ethereum.org/EIPS/eip-20
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, From, Into)]
pub struct TokenAddress(pub H160);
pub struct TokenAddress(pub Address);

impl TokenAddress {
/// If the token is ETH/XDAI, return WETH/WXDAI, thereby converting it to
Expand All @@ -54,8 +51,8 @@ impl TokenAddress {
#[derive(Debug, Clone, Copy, From, Into)]
pub struct WrappedNativeToken(TokenAddress);

impl From<H160> for WrappedNativeToken {
fn from(value: H160) -> Self {
impl From<Address> for WrappedNativeToken {
fn from(value: Address) -> Self {
WrappedNativeToken(value.into())
}
}
Expand Down Expand Up @@ -106,7 +103,7 @@ impl std::ops::Add for SellTokenAmount {

impl num::Zero for SellTokenAmount {
fn zero() -> Self {
Self(U256::zero())
Self(U256::ZERO)
}

fn is_zero(&self) -> bool {
Expand Down Expand Up @@ -232,7 +229,7 @@ impl std::ops::AddAssign for TokenAmount {

impl num::Zero for TokenAmount {
fn zero() -> Self {
Self(U256::zero())
Self(U256::ZERO)
}

fn is_zero(&self) -> bool {
Expand Down Expand Up @@ -284,7 +281,7 @@ impl num::CheckedSub for Ether {

impl num::Zero for Ether {
fn zero() -> Self {
Self(U256::zero())
Self(U256::ZERO)
}

fn is_zero(&self) -> bool {
Expand Down
19 changes: 9 additions & 10 deletions crates/autopilot/src/domain/fee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use {
app_data::Validator,
chrono::{DateTime, Utc},
derive_more::Into,
ethrpc::alloy::conversions::{IntoAlloy, IntoLegacy},
primitive_types::H160,
rust_decimal::Decimal,
std::{collections::HashSet, str::FromStr},
Expand Down Expand Up @@ -207,9 +206,9 @@ impl ProtocolFees {
factor,
max_volume_factor,
quote: Quote {
sell_amount: quote.sell_amount.0.into_alloy(),
buy_amount: quote.buy_amount.0.into_alloy(),
fee: quote.fee.0.into_alloy(),
sell_amount: quote.sell_amount.0,
buy_amount: quote.buy_amount.0,
fee: quote.fee.0,
solver: quote.solver,
},
}
Expand All @@ -231,9 +230,9 @@ impl ProtocolFees {
// being considered out of market price.
let reference_quote = quote.clone().unwrap_or(domain::Quote {
order_uid: order.metadata.uid.into(),
sell_amount: order.data.sell_amount.into_legacy().into(),
buy_amount: U256::ZERO.into_legacy().into(),
fee: order.data.fee_amount.into_legacy().into(),
sell_amount: order.data.sell_amount.into(),
buy_amount: U256::ZERO.into(),
fee: order.data.fee_amount.into(),
solver: Address::ZERO,
});

Expand Down Expand Up @@ -372,9 +371,9 @@ pub struct Quote {
impl Quote {
fn from_domain(value: &domain::Quote) -> Self {
Self {
sell_amount: value.sell_amount.0.into_alloy(),
buy_amount: value.buy_amount.0.into_alloy(),
fee: value.fee.0.into_alloy(),
sell_amount: value.sell_amount.0,
buy_amount: value.buy_amount.0,
fee: value.fee.0,
solver: value.solver,
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/autopilot/src/domain/quote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use {
super::OrderUid,
crate::{boundary::Amounts, domain::eth},
alloy::primitives::Address,
ethrpc::alloy::conversions::IntoAlloy,
};

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -17,9 +16,9 @@ pub struct Quote {
impl From<&Quote> for Amounts {
fn from(quote: &Quote) -> Self {
Self {
sell: quote.sell_amount.0.into_alloy(),
buy: quote.buy_amount.0.into_alloy(),
fee: quote.fee.0.into_alloy(),
sell: quote.sell_amount.0,
buy: quote.buy_amount.0,
fee: quote.fee.0,
}
}
}
Expand Down
Loading
Loading