Skip to content

Commit

Permalink
Merge branch 'main' into market_orders_deprecation_date-remove
Browse files Browse the repository at this point in the history
  • Loading branch information
sunce86 committed Mar 25, 2024
2 parents 1c37cd1 + 7bababd commit 15e1ffe
Show file tree
Hide file tree
Showing 109 changed files with 1,358 additions and 886 deletions.
24 changes: 21 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions crates/app-data/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "app-data"
version = "0.1.0"
edition = "2021"

[dependencies]
app-data-hash = { path = "../app-data-hash" }
anyhow = { workspace = true }
model = { path = "../model" }
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }
primitive-types = { workspace = true }
hex = "0.4.3"

[dev-dependencies]
ethcontract = { workspace = true }

[features]
test_helpers = []
23 changes: 14 additions & 9 deletions crates/shared/src/app_data.rs → crates/app-data/src/app_data.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use {
crate::Hooks,
anyhow::{anyhow, Context, Result},
model::{
app_data::AppDataHash,
order::{Hooks, OrderUid},
},
model::{app_data::AppDataHash, order::OrderUid},
primitive_types::H160,
serde::Deserialize,
};
Expand All @@ -13,33 +11,40 @@ mod compat;
/// The minimum valid empty app data JSON string.
pub const EMPTY: &str = "{}";

#[derive(Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct ValidatedAppData {
pub hash: AppDataHash,
pub document: String,
pub protocol: ProtocolAppData,
}

#[derive(Debug, Default, Deserialize, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolAppData {
#[serde(default)]
pub hooks: Hooks,
pub signer: Option<H160>,
pub replaced_order: Option<ReplacedOrder>,
pub partner_fee: Option<PartnerFee>,
}

#[derive(Debug, Default, Deserialize, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
pub struct ReplacedOrder {
pub uid: OrderUid,
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
pub struct PartnerFee {
pub bps: u64,
pub recipient: H160,
}

#[derive(Clone)]
pub struct Validator {
size_limit: usize,
}

#[cfg(test)]
#[cfg(any(test, feature = "test_helpers"))]
impl Default for Validator {
fn default() -> Self {
Self { size_limit: 8192 }
Expand Down Expand Up @@ -131,7 +136,7 @@ struct Root {

#[cfg(test)]
mod tests {
use {super::*, ethcontract::H160, model::order::Hook};
use {super::*, crate::Hook, ethcontract::H160};

macro_rules! assert_app_data {
($s:expr, $e:expr $(,)?) => {{
Expand Down
19 changes: 19 additions & 0 deletions crates/app-data/src/app_data/compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use {super::ProtocolAppData, crate::Hooks, serde::Deserialize};

/// The legacy `backend` app data object.
#[derive(Debug, Default, Deserialize)]
pub struct BackendAppData {
#[serde(default)]
pub hooks: Hooks,
}

impl From<BackendAppData> for ProtocolAppData {
fn from(value: BackendAppData) -> Self {
Self {
hooks: value.hooks,
signer: None,
replaced_order: None,
partner_fee: None,
}
}
}
58 changes: 58 additions & 0 deletions crates/app-data/src/hooks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use {
model::bytes_hex::BytesHex,
primitive_types::H160,
serde::{Deserialize, Serialize},
serde_with::{serde_as, DisplayFromStr},
std::{
fmt,
fmt::{Debug, Formatter},
},
};

/// Order hooks are user-specified Ethereum calls that get executed as part of
/// a pre- or post- interaction.
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct Hooks {
#[serde(default)]
pub pre: Vec<Hook>,
#[serde(default)]
pub post: Vec<Hook>,
}

impl Hooks {
pub fn is_empty(&self) -> bool {
self.pre.is_empty() && self.post.is_empty()
}

pub fn gas_limit(&self) -> u64 {
std::iter::empty()
.chain(&self.pre)
.chain(&self.post)
.fold(0_u64, |total, hook| total.saturating_add(hook.gas_limit))
}
}

/// A user-specified hook.
#[serde_as]
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Hook {
pub target: H160,
#[serde_as(as = "BytesHex")]
pub call_data: Vec<u8>,
#[serde_as(as = "DisplayFromStr")]
pub gas_limit: u64,
}

impl Debug for Hook {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("Hook")
.field("target", &self.target)
.field(
"call_data",
&format_args!("0x{}", hex::encode(&self.call_data)),
)
.field("gas_limit", &self.gas_limit)
.finish()
}
}
4 changes: 4 additions & 0 deletions crates/app-data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod app_data;
mod hooks;

pub use {app_data::*, hooks::*};
2 changes: 2 additions & 0 deletions crates/autopilot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ name = "autopilot"
path = "src/main.rs"

[dependencies]
app-data = { path = "../app-data" }
anyhow = { workspace = true }
async-trait = { workspace = true }
bigdecimal = { workspace = true }
Expand All @@ -23,6 +24,7 @@ clap = { workspace = true }
contracts = { path = "../contracts" }
database = { path = "../database" }
derivative = { workspace = true }
derive_more = "0.99.17"
ethcontract = { workspace = true }
ethrpc = { path = "../ethrpc" }
futures = { workspace = true }
Expand Down
Loading

0 comments on commit 15e1ffe

Please sign in to comment.