Skip to content

Commit

Permalink
Rework comments
Browse files Browse the repository at this point in the history
  • Loading branch information
m-lord-renkse committed Apr 29, 2024
1 parent d3624ac commit c2f4478
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 166 deletions.
20 changes: 5 additions & 15 deletions crates/driver/src/domain/competition/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use {
},
futures::future::{join_all, BoxFuture, FutureExt, Shared},
itertools::Itertools,
number::serialization::HexOrDecimalU256,
serde::Serialize,
serde_with::serde_as,
std::{
collections::{HashMap, HashSet},
sync::{Arc, Mutex},
Expand All @@ -25,8 +22,7 @@ use {
/// An auction is a set of orders that can be solved. The solvers calculate
/// [`super::solution::Solution`]s by picking subsets of these orders and
/// solving them.
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug)]
pub struct Auction {
/// See the [`Self::id`] method.
id: Option<Id>,
Expand Down Expand Up @@ -350,8 +346,7 @@ impl AuctionProcessor {
}

/// The tokens that are used in an auction.
#[derive(Debug, Default, Clone, Serialize)]
#[serde(transparent)]
#[derive(Debug, Default, Clone)]
pub struct Tokens(HashMap<eth::TokenAddress, Token>);

impl Tokens {
Expand All @@ -371,25 +366,21 @@ impl Tokens {
}
}

#[serde_as]
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone)]
pub struct Token {
pub decimals: Option<u8>,
pub symbol: Option<String>,
pub address: eth::TokenAddress,
pub price: Option<Price>,
/// The balance of this token available in our settlement contract.
#[serde_as(as = "HexOrDecimalU256")]
pub available_balance: eth::U256,
/// Is this token well-known and trusted by the protocol?
pub trusted: bool,
}

/// The price of a token in wei. This represents how much wei is needed to buy
/// 10**18 of another token.
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct Price(eth::Ether);

impl Price {
Expand Down Expand Up @@ -439,8 +430,7 @@ impl From<eth::U256> for Price {
/// All auction prices
pub type Prices = HashMap<eth::TokenAddress, Price>;

#[derive(Debug, Clone, Copy, Serialize)]
#[serde(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct Id(pub i64);

impl Id {
Expand Down
13 changes: 7 additions & 6 deletions crates/driver/src/domain/competition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
Mempools,
},
crate::{
domain::{competition::solution::Settlement, eth},
domain::{competition::solution::Settlement, eth, Liquidity},
infra::{
self,
blockchain::Ethereum,
Expand Down Expand Up @@ -55,7 +55,10 @@ pub struct Competition {

impl Competition {
/// Solve an auction as part of this competition.
pub async fn solve(&self, auction: &Auction) -> Result<Option<Solved>, Error> {
pub async fn solve(
&self,
auction: &Auction,
) -> Result<(Option<Solved>, Vec<Liquidity>), Error> {
let liquidity = match self.solver.liquidity() {
solver::Liquidity::Fetch => {
self.liquidity
Expand All @@ -79,8 +82,6 @@ impl Competition {
}
})?;

self.solver
.store_auction_with_liquidity(auction, &liquidity);
observe::postprocessing(&solutions, auction.deadline().driver());

// Discard solutions that don't have unique ID.
Expand Down Expand Up @@ -207,7 +208,7 @@ impl Competition {
let settlement = match settlement {
Some(settlement) => settlement,
// Don't wait for the deadline because we can't produce a solution anyway.
None => return Ok(score),
None => return Ok((score, liquidity)),
};

// Re-simulate the solution on every new block until the deadline ends to make
Expand Down Expand Up @@ -239,7 +240,7 @@ impl Competition {
let _ = tokio::time::timeout(remaining, simulate_on_new_blocks).await;
}

Ok(score)
Ok((score, liquidity))
}

pub async fn reveal(&self) -> Result<Revealed, Error> {
Expand Down
11 changes: 3 additions & 8 deletions crates/driver/src/domain/competition/order/fees.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use {crate::domain::eth, serde_with::serde_derive::Serialize};
use crate::domain::eth;

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Clone, Debug)]
pub enum FeePolicy {
/// If the order receives more than limit price, take the protocol fee as a
/// percentage of the difference. The fee is taken in `sell` token for
/// `buy` orders and in `buy` token for `sell` orders.
#[serde(rename_all = "camelCase")]
Surplus {
/// Factor of surplus the protocol charges as a fee.
/// Surplus is the difference between executed price and limit price
Expand All @@ -22,7 +20,6 @@ pub enum FeePolicy {
/// A price improvement corresponds to a situation where the order is
/// executed at a better price than the top quote. The protocol fee in such
/// case is calculated as a percentage of this price improvement.
#[serde(rename_all = "camelCase")]
PriceImprovement {
/// Price improvement is the difference between executed price and the
/// best quote or limit price, whichever is better for the user.
Expand All @@ -42,16 +39,14 @@ pub enum FeePolicy {
/// How much of the order's volume should be taken as a protocol fee.
/// The fee is taken in `sell` token for `sell` orders and in `buy`
/// token for `buy` orders.
#[serde(rename_all = "camelCase")]
Volume {
/// Percentage of the order's volume should be taken as a protocol
/// fee.
factor: f64,
},
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Clone, Debug)]
pub struct Quote {
pub sell: eth::Asset,
pub buy: eth::Asset,
Expand Down
39 changes: 12 additions & 27 deletions crates/driver/src/domain/competition/order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ use {
bigdecimal::Zero,
model::order::{BuyTokenDestination, SellTokenSource},
num::CheckedDiv,
number::serialization::HexOrDecimalU256,
serde::Serialize,
serde_with::serde_as,
};
pub use {fees::FeePolicy, signature::Signature};

pub mod fees;
pub mod signature;

/// An order in the auction.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone)]
pub struct Order {
pub uid: Uid,
/// The user specified a custom address to receive the output of this order.
Expand Down Expand Up @@ -74,10 +70,8 @@ impl From<SellAmount> for eth::U256 {

/// An amount denominated in the sell token for [`Side::Sell`] [`Order`]s, or in
/// the buy token for [`Side::Buy`] [`Order`]s.
#[serde_as]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(transparent)]
pub struct TargetAmount(#[serde_as(as = "HexOrDecimalU256")] pub eth::U256);
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct TargetAmount(pub eth::U256);

impl From<eth::U256> for TargetAmount {
fn from(value: eth::U256) -> Self {
Expand Down Expand Up @@ -234,31 +228,27 @@ impl Available {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Partial {
/// A partially order doesn't require the full amount to be traded.
/// E.g. only 10% of the requested amount may be traded, if this leads
/// to the most optimal solution.
#[serde(rename_all = "camelCase")]
Yes {
/// The available amount that can be used from the order.
///
/// This amount considers both how much of the order has already been
/// executed as well as the trader's balance.
available: TargetAmount,
},
#[serde(rename_all = "camelCase")]
No,
}

/// The length of an order UID in bytes.
pub const UID_LEN: usize = 56;

/// UID of an order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(transparent)]
pub struct Uid(#[serde(with = "bytes_hex")] pub Bytes<[u8; UID_LEN]>);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Uid(pub Bytes<[u8; UID_LEN]>);

impl Default for Uid {
fn default() -> Self {
Expand All @@ -273,8 +263,7 @@ impl PartialEq<[u8; UID_LEN]> for Uid {
}

// TODO These doc comments are incorrect for limit orders
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
/// Buy an exact amount. The sell amount can vary due to e.g. partial fills
/// or slippage.
Expand Down Expand Up @@ -302,9 +291,8 @@ pub const APP_DATA_LEN: usize = 32;
/// This is a hash allowing arbitrary user data to be associated with an order.
/// While this type holds the hash, the data itself is uploaded to IPFS. This
/// hash is signed along with the order.
#[derive(Debug, Default, Clone, Copy, Serialize)]
#[serde(transparent)]
pub struct AppData(#[serde(with = "bytes_hex")] pub Bytes<[u8; APP_DATA_LEN]>);
#[derive(Debug, Default, Clone, Copy)]
pub struct AppData(pub Bytes<[u8; APP_DATA_LEN]>);

impl From<[u8; APP_DATA_LEN]> for AppData {
fn from(inner: [u8; APP_DATA_LEN]) -> Self {
Expand All @@ -318,8 +306,7 @@ impl From<AppData> for [u8; APP_DATA_LEN] {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
/// Order intended to be immediately executed. This is the "regular" type of
/// order.
Expand All @@ -340,8 +327,7 @@ pub enum Kind {
}

/// [Balancer V2](https://docs.balancer.fi/) integration, used for settlement encoding.
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum SellTokenBalance {
Erc20,
Internal,
Expand Down Expand Up @@ -371,8 +357,7 @@ impl SellTokenBalance {
}

/// [Balancer V2](https://docs.balancer.fi/) integration, used for settlement encoding.
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum BuyTokenBalance {
Erc20,
Internal,
Expand Down
8 changes: 2 additions & 6 deletions crates/driver/src/domain/competition/order/signature.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use {
crate::{domain::eth, util::Bytes},
model::signature::EcdsaSignature,
serde::Serialize,
};

/// Signature over the order data.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone)]
pub struct Signature {
pub scheme: Scheme,
#[serde(with = "bytes_hex")]
pub data: Bytes<Vec<u8>>,
/// The address used to sign and place this order.
pub signer: eth::Address,
Expand Down Expand Up @@ -37,8 +34,7 @@ impl Signature {
/// The scheme used for signing the order. This is used by the solver and
/// the protocol, the driver does not care about the details of signature
/// verification.
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "lowercase")]
#[derive(Debug, Clone, Copy)]
pub enum Scheme {
Eip712,
EthSign,
Expand Down
13 changes: 4 additions & 9 deletions crates/driver/src/domain/eth/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ use {
super::{Ether, U256},
bigdecimal::Zero,
derive_more::Display,
number::serialization::HexOrDecimalU256,
serde::Serialize,
serde_with::serde_as,
std::{ops, ops::Add},
};

/// Gas amount in gas units.
///
/// The amount of Ether that is paid in transaction fees is proportional to this
/// amount as well as the transaction's [`EffectiveGasPrice`].
#[serde_as]
#[derive(Debug, Default, Display, Clone, Copy, Ord, Eq, PartialOrd, PartialEq, Serialize)]
pub struct Gas(#[serde_as(as = "HexOrDecimalU256")] pub U256);
#[derive(Debug, Default, Display, Clone, Copy, Ord, Eq, PartialOrd, PartialEq)]
pub struct Gas(pub U256);

impl From<U256> for Gas {
fn from(value: U256) -> Self {
Expand Down Expand Up @@ -55,7 +51,7 @@ impl Zero for Gas {
/// An EIP-1559 gas price estimate.
///
/// https://eips.ethereum.org/EIPS/eip-1559#specification
#[derive(Debug, Clone, Copy, Serialize)]
#[derive(Debug, Clone, Copy)]
pub struct GasPrice {
/// The maximum total fee that should be charged.
max: FeePerGas,
Expand Down Expand Up @@ -134,8 +130,7 @@ impl From<EffectiveGasPrice> for GasPrice {
/// `{max,max_priority,base}_fee_per_gas` as defined by EIP-1559.
///
/// https://eips.ethereum.org/EIPS/eip-1559#specification
#[derive(Debug, Clone, Copy, Ord, Eq, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
#[derive(Debug, Clone, Copy, Ord, Eq, PartialEq, PartialOrd)]
pub struct FeePerGas(pub Ether);

impl FeePerGas {
Expand Down
Loading

0 comments on commit c2f4478

Please sign in to comment.