From a4aa3ed2365372ccd8aefb6294ea52918924cb06 Mon Sep 17 00:00:00 2001 From: Mateo Date: Mon, 15 Apr 2024 15:43:55 +0200 Subject: [PATCH] Include necessary data for simulations within the auction json sent to solvers --- Cargo.lock | 1 + crates/driver/src/boundary/settlement.rs | 24 +-------- .../src/domain/competition/order/signature.rs | 24 ++++++++- crates/driver/src/domain/eth/mod.rs | 10 ++++ crates/driver/src/infra/solver/dto/auction.rs | 49 ++++++++++++++++--- crates/solvers-dto/Cargo.toml | 3 +- crates/solvers-dto/src/auction.rs | 13 ++++- 7 files changed, 91 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 715fbbb567..207f15713e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4262,6 +4262,7 @@ dependencies = [ "bigdecimal", "chrono", "hex", + "model", "number", "serde", "serde_with", diff --git a/crates/driver/src/boundary/settlement.rs b/crates/driver/src/boundary/settlement.rs index f9d97de553..58cafd9e30 100644 --- a/crates/driver/src/boundary/settlement.rs +++ b/crates/driver/src/boundary/settlement.rs @@ -27,7 +27,6 @@ use { OrderUid, SellTokenSource, }, - signature::EcdsaSignature, DomainSeparator, }, shared::{ @@ -260,7 +259,7 @@ fn to_boundary_order(order: &competition::Order) -> Order { is_liquidity_order: order.is_liquidity(), full_app_data: Default::default(), }, - signature: to_boundary_signature(&order.signature), + signature: order.signature.to_boundary_signature(), interactions: Interactions { pre: order .pre_interactions @@ -321,7 +320,7 @@ fn to_boundary_jit_order(domain: &DomainSeparator, order: &order::Jit) -> Order // encoding, so we just use the default values. ..Default::default() }; - let signature = to_boundary_signature(&order.signature); + let signature = order.signature.to_boundary_signature(); Order { data, @@ -331,25 +330,6 @@ fn to_boundary_jit_order(domain: &DomainSeparator, order: &order::Jit) -> Order } } -fn to_boundary_signature(signature: &order::Signature) -> model::signature::Signature { - // TODO Different signing schemes imply different sizes of signature data, which - // indicates that I'm missing an invariant in my types and I need to fix - // that PreSign, for example, carries no data. Everything should be - // reflected in the types! - match signature.scheme { - order::signature::Scheme::Eip712 => model::signature::Signature::Eip712( - EcdsaSignature::from_bytes(signature.data.0.as_slice().try_into().unwrap()), - ), - order::signature::Scheme::EthSign => model::signature::Signature::EthSign( - EcdsaSignature::from_bytes(signature.data.0.as_slice().try_into().unwrap()), - ), - order::signature::Scheme::Eip1271 => { - model::signature::Signature::Eip1271(signature.data.clone().into()) - } - order::signature::Scheme::PreSign => model::signature::Signature::PreSign, - } -} - pub fn to_boundary_interaction( slippage_context: &SlippageContext, settlement_contract: eth::ContractAddress, diff --git a/crates/driver/src/domain/competition/order/signature.rs b/crates/driver/src/domain/competition/order/signature.rs index 09a586a61a..b77b8a3edd 100644 --- a/crates/driver/src/domain/competition/order/signature.rs +++ b/crates/driver/src/domain/competition/order/signature.rs @@ -1,4 +1,7 @@ -use crate::{domain::eth, util::Bytes}; +use { + crate::{domain::eth, util::Bytes}, + model::signature::EcdsaSignature, +}; /// Signature over the order data. #[derive(Debug, Clone)] @@ -9,6 +12,25 @@ pub struct Signature { pub signer: eth::Address, } +impl Signature { + pub fn to_boundary_signature(&self) -> model::signature::Signature { + // TODO Different signing schemes imply different sizes of signature data, which + // indicates that I'm missing an invariant in my types and I need to fix + // that PreSign, for example, carries no data. Everything should be + // reflected in the types! + match self.scheme { + Scheme::Eip712 => model::signature::Signature::Eip712(EcdsaSignature::from_bytes( + self.data.0.as_slice().try_into().unwrap(), + )), + Scheme::EthSign => model::signature::Signature::EthSign(EcdsaSignature::from_bytes( + self.data.0.as_slice().try_into().unwrap(), + )), + Scheme::Eip1271 => model::signature::Signature::Eip1271(self.data.clone().into()), + Scheme::PreSign => model::signature::Signature::PreSign, + } + } +} + /// 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. diff --git a/crates/driver/src/domain/eth/mod.rs b/crates/driver/src/domain/eth/mod.rs index 4442dd1a65..122f7e807d 100644 --- a/crates/driver/src/domain/eth/mod.rs +++ b/crates/driver/src/domain/eth/mod.rs @@ -359,6 +359,16 @@ pub struct Interaction { pub call_data: Bytes>, } +impl Into for Interaction { + fn into(self) -> model::interaction::InteractionData { + model::interaction::InteractionData { + target: self.target.into(), + value: self.value.into(), + call_data: self.call_data.into(), + } + } +} + /// A transaction ID, AKA transaction hash. #[derive(Clone, Debug)] pub struct TxId(pub H256); diff --git a/crates/driver/src/infra/solver/dto/auction.rs b/crates/driver/src/infra/solver/dto/auction.rs index 79525af94f..db50de916d 100644 --- a/crates/driver/src/infra/solver/dto/auction.rs +++ b/crates/driver/src/infra/solver/dto/auction.rs @@ -6,7 +6,7 @@ use { order, order::{fees, Side}, }, - eth, + eth::{self}, liquidity, }, infra::config::file::FeeHandler, @@ -16,6 +16,8 @@ use { }, }, indexmap::IndexMap, + model::{interaction::InteractionData, signature::Signature}, + primitive_types::H160, serde::Serialize, serde_with::serde_as, std::collections::{BTreeMap, HashMap}, @@ -113,16 +115,32 @@ impl Auction { buy_token: available.buy.token.into(), sell_amount: available.sell.amount.into(), buy_amount: available.buy.amount.into(), + full_sell_amount: order.sell.amount.into(), + full_buy_amount: order.buy.amount.into(), kind: match order.side { - competition::order::Side::Buy => Kind::Buy, - competition::order::Side::Sell => Kind::Sell, + Side::Buy => Kind::Buy, + Side::Sell => Kind::Sell, }, + receiver: order.receiver.map(Into::into), + owner: order.signature.signer.into(), partially_fillable: order.is_partial(), class: match order.kind { - competition::order::Kind::Market => Class::Market, - competition::order::Kind::Limit { .. } => Class::Limit, - competition::order::Kind::Liquidity => Class::Liquidity, + order::Kind::Market => Class::Market, + order::Kind::Limit { .. } => Class::Limit, + order::Kind::Liquidity => Class::Liquidity, }, + pre_interactions: order + .pre_interactions + .iter() + .cloned() + .map(Into::into) + .collect::>(), + post_interactions: order + .post_interactions + .iter() + .cloned() + .map(Into::into) + .collect::>(), fee_policies: (fee_handler == FeeHandler::Solver).then_some( order .protocol_fees @@ -131,6 +149,8 @@ impl Auction { .map(Into::into) .collect(), ), + signature: order.signature.to_boundary_signature(), + valid_to: order.valid_to.into(), } }) .collect(), @@ -298,13 +318,26 @@ struct Order { buy_token: eth::H160, #[serde_as(as = "serialize::U256")] sell_amount: eth::U256, + /// Original order `buy amount` + #[serde_as(as = "serialize::U256")] + full_buy_amount: eth::U256, #[serde_as(as = "serialize::U256")] buy_amount: eth::U256, + /// Original order `sell amount` + #[serde_as(as = "serialize::U256")] + full_sell_amount: eth::U256, + #[serde(skip_serializing_if = "Option::is_none")] + fee_policies: Option>, + valid_to: u32, kind: Kind, + receiver: Option, + owner: H160, partially_fillable: bool, + pre_interactions: Vec, + post_interactions: Vec, class: Class, - #[serde(skip_serializing_if = "Option::is_none")] - fee_policies: Option>, + #[serde(flatten)] + signature: Signature, } #[derive(Debug, Serialize)] diff --git a/crates/solvers-dto/Cargo.toml b/crates/solvers-dto/Cargo.toml index 931f536290..3967e337f2 100644 --- a/crates/solvers-dto/Cargo.toml +++ b/crates/solvers-dto/Cargo.toml @@ -6,10 +6,11 @@ edition = "2021" license = "MIT OR Apache-2.0" [dependencies] +model = { path = "../model" } bigdecimal = { workspace = true, features = ["serde"] } chrono = { workspace = true } hex = { workspace = true } -number = { path = "../number"} +number = { path = "../number" } serde = { workspace = true } serde_with = { workspace = true } web3 = { workspace = true } diff --git a/crates/solvers-dto/src/auction.rs b/crates/solvers-dto/src/auction.rs index dcd9b47e38..af322451c8 100644 --- a/crates/solvers-dto/src/auction.rs +++ b/crates/solvers-dto/src/auction.rs @@ -33,11 +33,22 @@ pub struct Order { #[serde_as(as = "HexOrDecimalU256")] pub sell_amount: U256, #[serde_as(as = "HexOrDecimalU256")] + pub full_sell_amount: U256, + #[serde_as(as = "HexOrDecimalU256")] pub buy_amount: U256, + #[serde_as(as = "HexOrDecimalU256")] + pub full_buy_amount: U256, + pub fee_policies: Option>, + pub valid_to: u32, pub kind: Kind, + pub receiver: Option, + pub owner: H160, pub partially_fillable: bool, + pub pre_interactions: Vec, + pub post_interactions: Vec, pub class: Class, - pub fee_policies: Option>, + #[serde(flatten)] + pub signature: model::signature::Signature, } #[derive(Debug, Deserialize)]