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

Treat ETH and WETH as single token in winners selection #3074

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions crates/autopilot/src/domain/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use {
derive_more::{Display, From, Into},
};

/// 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.
pub const ETH_TOKEN: TokenAddress = TokenAddress(H160([0xee; 20]));

/// An address. Can be an EOA or a smart contract address.
#[derive(
Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, From, Into, Display,
Expand Down Expand Up @@ -33,6 +39,27 @@ pub struct TxId(pub H256);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, From, Into)]
pub struct TokenAddress(pub H160);

impl TokenAddress {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is also in the driver, would it make sense to move it to the shared?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we went the opposite direction - moving away things from shared

/// If the token is ETH, return WETH, thereby converting it to erc20.
pub fn as_erc20(self, weth: WethAddress) -> Self {
if self == ETH_TOKEN {
weth.into()
} else {
self
}
}
}

/// The address of the WETH contract.
#[derive(Debug, Clone, Copy, From, Into)]
pub struct WethAddress(TokenAddress);

impl From<H160> for WethAddress {
fn from(value: H160) -> Self {
WethAddress(value.into())
}
}

/// An ERC20 token amount.
///
/// https://eips.ethereum.org/EIPS/eip-20
Expand Down
4 changes: 4 additions & 0 deletions crates/autopilot/src/infra/blockchain/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl Contracts {
&self.weth
}

pub fn weth_address(&self) -> domain::eth::WethAddress {
self.weth.address().into()
}

pub fn authenticator(&self) -> &contracts::GPv2AllowListAuthentication {
&self.authenticator
}
Expand Down
8 changes: 7 additions & 1 deletion crates/autopilot/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ impl RunLoop {
// until `max_winners_per_auction` are selected. The solution is a winner
// if it swaps tokens that are not yet swapped by any previously processed
// solution.
let weth = self.eth.contracts().weth_address();
let mut already_swapped_tokens = HashSet::new();
let mut winners = 0;
let solutions = solutions
Expand All @@ -575,7 +576,12 @@ impl RunLoop {
.solution()
.orders()
.iter()
.flat_map(|(_, order)| [order.sell.token, order.buy.token])
.flat_map(|(_, order)| {
[
order.sell.token.as_erc20(weth),
order.buy.token.as_erc20(weth),
]
})
.collect::<HashSet<_>>();

let is_winner = swapped_tokens.is_disjoint(&already_swapped_tokens)
Expand Down
2 changes: 1 addition & 1 deletion crates/driver/src/domain/competition/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Auction {
// Ensure that tokens are included for each order.
let weth = eth.contracts().weth_address();
if !orders.iter().all(|order| {
tokens.0.contains_key(&order.buy.token.wrap(weth))
tokens.0.contains_key(&order.buy.token.as_erc20(weth))
&& tokens.0.contains_key(&order.sell.token)
}) {
return Err(Error::InvalidTokens);
Expand Down
8 changes: 5 additions & 3 deletions crates/driver/src/domain/competition/solution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ impl Solution {
match &trade {
Trade::Fulfillment(fulfillment) => {
let prices = ClearingPrices {
sell: solution.prices[&fulfillment.order().sell.token.wrap(solution.weth)],
buy: solution.prices[&fulfillment.order().buy.token.wrap(solution.weth)],
sell: solution.prices
[&fulfillment.order().sell.token.as_erc20(solution.weth)],
buy: solution.prices
[&fulfillment.order().buy.token.as_erc20(solution.weth)],
};
let fulfillment = fulfillment.with_protocol_fees(prices)?;
trades.push(Trade::Fulfillment(fulfillment))
Expand Down Expand Up @@ -443,7 +445,7 @@ impl Solution {
/// Clearing price for the given token.
pub fn clearing_price(&self, token: eth::TokenAddress) -> Option<eth::U256> {
// The clearing price of ETH is equal to WETH.
let token = token.wrap(self.weth);
let token = token.as_erc20(self.weth);
self.prices.get(&token).map(ToOwned::to_owned)
}

Expand Down
5 changes: 3 additions & 2 deletions crates/driver/src/domain/competition/solution/settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ impl Settlement {
let order = match trade {
Trade::Fulfillment(_) => {
let prices = ClearingPrices {
sell: self.solution.prices[&trade.sell().token.wrap(self.solution.weth)],
buy: self.solution.prices[&trade.buy().token.wrap(self.solution.weth)],
sell: self.solution.prices
[&trade.sell().token.as_erc20(self.solution.weth)],
buy: self.solution.prices[&trade.buy().token.as_erc20(self.solution.weth)],
};
competition::Amounts {
side: trade.side(),
Expand Down
4 changes: 2 additions & 2 deletions crates/driver/src/domain/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl From<ContractAddress> for Address {
pub struct TokenAddress(pub ContractAddress);

impl TokenAddress {
/// If the token is ETH, return WETH, thereby "wrapping" it.
pub fn wrap(self, weth: WethAddress) -> Self {
/// If the token is ETH, return WETH, thereby converting it to erc20.
pub fn as_erc20(self, weth: WethAddress) -> Self {
if self == ETH_TOKEN {
weth.into()
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/driver/src/infra/solver/dto/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Auction {
let mut available = order.available();

if solver_native_token.wrap_address {
available.buy.token = available.buy.token.wrap(weth)
available.buy.token = available.buy.token.as_erc20(weth)
}
// In case of volume based fees, fee withheld by driver might be higher than the
// surplus of the solution. This would lead to violating limit prices when
Expand Down
Loading