Skip to content

Commit

Permalink
Serialize current liquidity and auction instead of creating specific
Browse files Browse the repository at this point in the history
  • Loading branch information
m-lord-renkse committed Apr 26, 2024
1 parent 19c0e03 commit bbc33dc
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 447 deletions.
22 changes: 16 additions & 6 deletions crates/driver/src/domain/competition/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ use {
domain::{
competition::{self, auction},
eth,
liquidity,
liquidity::{self},
time,
},
infra::{self, blockchain, observe, Ethereum},
util::{self},
},
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 @@ -22,7 +25,8 @@ 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(Debug)]
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Auction {
/// See the [`Self::id`] method.
id: Option<Id>,
Expand Down Expand Up @@ -346,7 +350,8 @@ impl AuctionProcessor {
}

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

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

#[derive(Debug, Clone)]
#[serde_as]
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
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)]
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(transparent)]
pub struct Price(eth::Ether);

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

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

impl Id {
Expand Down
11 changes: 8 additions & 3 deletions crates/driver/src/domain/competition/order/fees.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::domain::eth;
use {crate::domain::eth, serde_with::serde_derive::Serialize};

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
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 @@ -20,6 +22,7 @@ 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 @@ -39,14 +42,16 @@ 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)]
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Quote {
pub sell: eth::Asset,
pub buy: eth::Asset,
Expand Down
39 changes: 27 additions & 12 deletions crates/driver/src/domain/competition/order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ 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)]
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Order {
pub uid: Uid,
/// The user specified a custom address to receive the output of this order.
Expand Down Expand Up @@ -70,8 +74,10 @@ 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.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct TargetAmount(pub eth::U256);
#[serde_as]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(transparent)]
pub struct TargetAmount(#[serde_as(as = "HexOrDecimalU256")] pub eth::U256);

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

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
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)]
pub struct Uid(pub Bytes<[u8; UID_LEN]>);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(transparent)]
pub struct Uid(#[serde(with = "bytes_hex")] pub Bytes<[u8; UID_LEN]>);

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

// TODO These doc comments are incorrect for limit orders
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Side {
/// Buy an exact amount. The sell amount can vary due to e.g. partial fills
/// or slippage.
Expand Down Expand Up @@ -291,8 +302,9 @@ 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)]
pub struct AppData(pub Bytes<[u8; APP_DATA_LEN]>);
#[derive(Debug, Default, Clone, Copy, Serialize)]
#[serde(transparent)]
pub struct AppData(#[serde(with = "bytes_hex")] pub Bytes<[u8; APP_DATA_LEN]>);

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

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

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

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

/// Signature over the order data.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
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 @@ -34,7 +37,8 @@ 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)]
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Scheme {
Eip712,
EthSign,
Expand Down
13 changes: 9 additions & 4 deletions crates/driver/src/domain/eth/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ 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`].
#[derive(Debug, Default, Display, Clone, Copy, Ord, Eq, PartialOrd, PartialEq)]
pub struct Gas(pub U256);
#[serde_as]
#[derive(Debug, Default, Display, Clone, Copy, Ord, Eq, PartialOrd, PartialEq, Serialize)]
pub struct Gas(#[serde_as(as = "HexOrDecimalU256")] pub U256);

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

impl FeePerGas {
Expand Down
Loading

0 comments on commit bbc33dc

Please sign in to comment.