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

Reduce Try/TryFrom implementations for general types #3130

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions crates/autopilot/src/domain/fee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ pub struct Quote {
pub solver: H160,
}

impl From<domain::Quote> for Quote {
fn from(value: domain::Quote) -> Self {
impl Quote {
fn from_domain(value: &domain::Quote) -> Self {
Self {
sell_amount: value.sell_amount.into(),
buy_amount: value.buy_amount.into(),
Expand Down
7 changes: 5 additions & 2 deletions crates/autopilot/src/domain/fee/policy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{
arguments,
boundary,
domain::{self, fee::FeeFactor},
domain::{
self,
fee::{FeeFactor, Quote},
},
};

pub enum Policy {
Expand Down Expand Up @@ -74,7 +77,7 @@ impl PriceImprovement {
boundary::OrderClass::Limit => Some(domain::fee::Policy::PriceImprovement {
factor: self.factor,
max_volume_factor: self.max_volume_factor,
quote: quote.clone().into(),
quote: Quote::from_domain(quote),
}),
}
}
Expand Down
34 changes: 16 additions & 18 deletions crates/autopilot/src/infra/persistence/dto/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,36 @@ pub struct RawAuctionData {

pub type AuctionId = i64;

impl TryFrom<Auction> for domain::Auction {
type Error = anyhow::Error;
#[serde_as]
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Auction {
pub id: AuctionId,
#[serde(flatten)]
pub auction: RawAuctionData,
}

fn try_from(dto: Auction) -> anyhow::Result<Self> {
impl Auction {
pub fn try_into_domain(self) -> anyhow::Result<domain::Auction> {
Ok(domain::Auction {
id: dto.id,
block: dto.auction.block,
latest_settlement_block: dto.auction.latest_settlement_block,
orders: dto
id: self.id,
block: self.auction.block,
latest_settlement_block: self.auction.latest_settlement_block,
orders: self
.auction
.orders
.into_iter()
.map(super::order::to_domain)
.collect(),
prices: dto
prices: self
.auction
.prices
.into_iter()
.map(|(key, value)| {
Price::new(value.into()).map(|price| (eth::TokenAddress(key), price))
})
.collect::<Result<_, _>>()?,
surplus_capturing_jit_order_owners: dto
surplus_capturing_jit_order_owners: self
.auction
.surplus_capturing_jit_order_owners
.into_iter()
Expand All @@ -79,12 +86,3 @@ impl TryFrom<Auction> for domain::Auction {
})
}
}

#[serde_as]
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Auction {
pub id: AuctionId,
#[serde(flatten)]
pub auction: RawAuctionData,
}
104 changes: 54 additions & 50 deletions crates/autopilot/src/infra/persistence/dto/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ pub fn from_domain(order: domain::Order) -> Order {
buy_token: order.buy.token.into(),
sell_amount: order.sell.amount.into(),
buy_amount: order.buy.amount.into(),
protocol_fees: order.protocol_fees.into_iter().map(Into::into).collect(),
protocol_fees: order
.protocol_fees
.into_iter()
.map(FeePolicy::from_domain)
.collect(),
created: order.created,
valid_to: order.valid_to,
kind: order.side.into(),
Expand All @@ -68,7 +72,7 @@ pub fn from_domain(order: domain::Order) -> Order {
class: boundary::OrderClass::Limit,
app_data: order.app_data.into(),
signature: order.signature.into(),
quote: order.quote.map(Into::into),
quote: order.quote.map(Quote::from_domain),
}
}

Expand All @@ -83,7 +87,11 @@ pub fn to_domain(order: Order) -> domain::Order {
token: order.buy_token.into(),
amount: order.buy_amount.into(),
},
protocol_fees: order.protocol_fees.into_iter().map(Into::into).collect(),
protocol_fees: order
.protocol_fees
.into_iter()
.map(|fee_policy| fee_policy.into_domain())
squadgazzz marked this conversation as resolved.
Show resolved Hide resolved
.collect(),
created: order.created,
valid_to: order.valid_to,
side: order.kind.into(),
Expand Down Expand Up @@ -262,44 +270,8 @@ pub enum FeePolicy {
Volume { factor: f64 },
}

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Quote {
#[serde_as(as = "HexOrDecimalU256")]
pub sell_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
pub buy_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
pub fee: U256,
pub solver: H160,
}

impl Quote {
pub fn to_domain(&self, order_uid: OrderUid) -> domain::Quote {
domain::Quote {
order_uid,
sell_amount: self.sell_amount.into(),
buy_amount: self.buy_amount.into(),
fee: self.fee.into(),
solver: self.solver.into(),
}
}
}

impl From<domain::Quote> for Quote {
fn from(quote: domain::Quote) -> Self {
Quote {
sell_amount: quote.sell_amount.0,
buy_amount: quote.buy_amount.0,
fee: quote.fee.0,
solver: quote.solver.0,
}
}
}

impl From<domain::fee::Policy> for FeePolicy {
fn from(policy: domain::fee::Policy) -> Self {
impl FeePolicy {
pub fn from_domain(policy: domain::fee::Policy) -> Self {
match policy {
domain::fee::Policy::Surplus {
factor,
Expand Down Expand Up @@ -327,23 +299,21 @@ impl From<domain::fee::Policy> for FeePolicy {
},
}
}
}

impl From<FeePolicy> for domain::fee::Policy {
fn from(policy: FeePolicy) -> Self {
match policy {
FeePolicy::Surplus {
pub fn into_domain(self) -> domain::fee::Policy {
match self {
Self::Surplus {
factor,
max_volume_factor,
} => Self::Surplus {
} => domain::fee::Policy::Surplus {
factor: FeeFactor::try_from(factor).unwrap(),
max_volume_factor: FeeFactor::try_from(max_volume_factor).unwrap(),
},
FeePolicy::PriceImprovement {
Self::PriceImprovement {
factor,
max_volume_factor,
quote,
} => Self::PriceImprovement {
} => domain::fee::Policy::PriceImprovement {
factor: FeeFactor::try_from(factor).unwrap(),
max_volume_factor: FeeFactor::try_from(max_volume_factor).unwrap(),
quote: domain::fee::Quote {
Expand All @@ -353,13 +323,47 @@ impl From<FeePolicy> for domain::fee::Policy {
solver: quote.solver,
},
},
FeePolicy::Volume { factor } => Self::Volume {
Self::Volume { factor } => domain::fee::Policy::Volume {
factor: FeeFactor::try_from(factor).unwrap(),
},
}
}
}

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Quote {
#[serde_as(as = "HexOrDecimalU256")]
pub sell_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
pub buy_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
pub fee: U256,
pub solver: H160,
}

impl Quote {
fn from_domain(quote: domain::Quote) -> Self {
Quote {
sell_amount: quote.sell_amount.0,
buy_amount: quote.buy_amount.0,
fee: quote.fee.0,
solver: quote.solver.0,
}
}

pub fn to_domain(&self, order_uid: OrderUid) -> domain::Quote {
domain::Quote {
order_uid,
sell_amount: self.sell_amount.into(),
buy_amount: self.buy_amount.into(),
fee: self.fee.into(),
solver: self.solver.into(),
}
}
}

impl From<domain::auction::order::Side> for database::orders::OrderKind {
fn from(side: domain::auction::order::Side) -> Self {
match side {
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/infra/shadow/orderbook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Orderbook {
.error_for_status()?
.json::<dto::Auction>()
.await
.map(TryInto::try_into)
.map(dto::Auction::try_into_domain)
.map_err(Into::<anyhow::Error>::into)?
}
}
6 changes: 3 additions & 3 deletions crates/driver/src/infra/solver/dto/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Auction {
.protocol_fees
.iter()
.cloned()
.map(Into::into)
.map(FeePolicy::from_domain)
.collect(),
),
app_data: AppDataHash(order.app_data.0.into()),
Expand Down Expand Up @@ -407,8 +407,8 @@ pub enum FeePolicy {
Volume { factor: f64 },
}

impl From<fees::FeePolicy> for FeePolicy {
fn from(value: order::FeePolicy) -> Self {
impl FeePolicy {
pub fn from_domain(value: fees::FeePolicy) -> FeePolicy {
match value {
order::FeePolicy::Surplus {
factor,
Expand Down
Loading