Skip to content

Commit

Permalink
Use DTO directly
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Oct 3, 2024
1 parent a2e6719 commit 3b245a3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 66 deletions.
6 changes: 3 additions & 3 deletions crates/shared/src/price_estimation/trade_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
code_simulation::CodeSimulating,
encoded_settlement::{encode_trade, EncodedSettlement},
interaction::EncodedInteraction,
trade_finding::{Interaction, Side, Trade},
trade_finding::{external::dto, Interaction, Trade},
},
anyhow::{Context, Result},
app_data::AppDataHash,
Expand Down Expand Up @@ -425,8 +425,8 @@ fn encode_settlement(
app_data: AppDataHash::from_str(&jit_order.app_data)?,
fee_amount: 0.into(),
kind: match &jit_order.side {
Side::Buy => OrderKind::Buy,
Side::Sell => OrderKind::Sell,
dto::Side::Buy => OrderKind::Buy,
dto::Side::Sell => OrderKind::Sell,
},
partially_fillable: false,
sell_token_balance: jit_order.sell_token_source,
Expand Down
37 changes: 6 additions & 31 deletions crates/shared/src/trade_finding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
pub mod external;

use {
crate::price_estimation::{PriceEstimationError, Query},
crate::{
price_estimation::{PriceEstimationError, Query},
trade_finding::external::dto,
},
anyhow::Result,
derivative::Derivative,
ethcontract::{contract::MethodBuilder, tokens::Tokenize, web3::Transport, Bytes, H160, U256},
model::{
interaction::InteractionData,
order::{BuyTokenDestination, SellTokenSource},
signature::SigningScheme,
},
model::interaction::InteractionData,
serde::Serialize,
thiserror::Error,
};
Expand Down Expand Up @@ -52,7 +51,7 @@ pub struct Trade {
/// If this is set the quote verification need to use this as the
/// `tx.origin` to make the quote pass the simulation.
pub tx_origin: Option<H160>,
pub jit_orders: Vec<JitOrder>,
pub jit_orders: Vec<dto::JitOrder>,
}

/// Data for a raw GPv2 interaction.
Expand Down Expand Up @@ -95,30 +94,6 @@ impl From<InteractionData> for Interaction {

pub type EncodedInteraction = (H160, U256, Bytes<Vec<u8>>);

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct JitOrder {
pub buy_token: H160,
pub sell_token: H160,
pub sell_amount: U256,
pub buy_amount: U256,
pub executed_amount: U256,
pub receiver: H160,
pub valid_to: u32,
pub app_data: String,
pub side: Side,
pub sell_token_source: SellTokenSource,
pub buy_token_destination: BuyTokenDestination,
pub signature: Vec<u8>,
pub signing_scheme: SigningScheme,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum Side {
#[default]
Buy,
Sell,
}

#[derive(Debug, Error)]
pub enum TradeError {
#[error("No liquidity")]
Expand Down
36 changes: 4 additions & 32 deletions crates/shared/src/trade_finding/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
crate::{
price_estimation::{PriceEstimationError, Query},
request_sharing::RequestSharing,
trade_finding::{Interaction, JitOrder, Quote, Side, Trade, TradeError, TradeFinding},
trade_finding::{Interaction, Quote, Trade, TradeError, TradeFinding},
},
anyhow::{anyhow, Context},
ethrpc::block_stream::CurrentBlockWatcher,
Expand Down Expand Up @@ -146,35 +146,6 @@ impl From<dto::Interaction> for Interaction {
}
}

impl From<dto::JitOrder> for JitOrder {
fn from(jit_order: dto::JitOrder) -> Self {
Self {
buy_token: jit_order.buy_token,
sell_token: jit_order.sell_token,
sell_amount: jit_order.sell_amount,
buy_amount: jit_order.buy_amount,
executed_amount: jit_order.executed_amount,
receiver: jit_order.receiver,
valid_to: jit_order.valid_to,
app_data: jit_order.app_data,
side: jit_order.side.into(),
sell_token_source: jit_order.sell_token_source,
buy_token_destination: jit_order.buy_token_destination,
signature: jit_order.signature,
signing_scheme: jit_order.signing_scheme,
}
}
}

impl From<dto::Side> for Side {
fn from(side: dto::Side) -> Self {
match side {
dto::Side::Buy => Self::Buy,
dto::Side::Sell => Self::Sell,
}
}
}

#[async_trait::async_trait]
impl TradeFinding for ExternalTradeFinder {
async fn get_quote(&self, query: &Query) -> Result<Quote, TradeError> {
Expand Down Expand Up @@ -251,7 +222,7 @@ pub(crate) mod dto {
}

#[serde_as]
#[derive(Clone, Debug, Deserialize)]
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(unused)]
pub struct JitOrder {
Expand Down Expand Up @@ -283,9 +254,10 @@ pub(crate) mod dto {
}

#[serde_as]
#[derive(Clone, Debug, Deserialize)]
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Side {
#[default]
Buy,
Sell,
}
Expand Down

0 comments on commit 3b245a3

Please sign in to comment.