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

Added QuoteMetadata to /orders/uid request #3222

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1ccbf34
Implementation
mstrug Jan 8, 2025
e6206ca
Moved quote metadata to order metadata
mstrug Jan 16, 2025
c7efc2d
Merge branch 'main' into api/quote-metadata-in-orders
mstrug Jan 16, 2025
a843053
Updated tests
mstrug Jan 17, 2025
10171e0
Merge branch 'main' into api/quote-metadata-in-orders
mstrug Jan 17, 2025
8808793
Merge branch 'main' into api/quote-metadata-in-orders
mstrug Jan 22, 2025
a7d3844
Changed QuoteMetadata type to json value
mstrug Jan 23, 2025
6b3840e
Fixed returning jit orders by uid
mstrug Jan 23, 2025
2f7915e
Small refactoring
mstrug Jan 23, 2025
c028e13
Merge branch 'main' into api/quote-metadata-in-orders
mstrug Feb 4, 2025
4094f95
New implementation
mstrug Feb 7, 2025
09f8f65
Merge branch 'main' into api/quote-metadata-in-orders
mstrug Feb 7, 2025
7424988
Fixed compilation
mstrug Feb 11, 2025
603401a
Removed OrderWithQuote
mstrug Feb 14, 2025
2c511f4
Merge branch 'main' into api/quote-metadata-in-orders
mstrug Feb 14, 2025
0e93b31
Update
mstrug Feb 17, 2025
89928c0
Moved function used only in tests
mstrug Feb 17, 2025
dc548ad
Fixed clippy warning
mstrug Feb 17, 2025
61de451
Removed set_order_quote() function
mstrug Feb 17, 2025
6930802
Renamed function
mstrug Feb 18, 2025
baeeed7
Replaced function in tests
mstrug Feb 18, 2025
77bdeec
Small refactoring
mstrug Feb 18, 2025
81c3029
Readability improvement
mstrug Feb 19, 2025
12e8c87
Merge branch 'main' into api/quote-metadata-in-orders
mstrug Feb 19, 2025
126a670
Update crates/model/src/order.rs
mstrug Feb 20, 2025
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
127 changes: 82 additions & 45 deletions crates/database/src/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,17 @@ pub struct FullOrder {
pub full_app_data: Option<Vec<u8>>,
}

impl FullOrder {
pub fn valid_to(&self) -> i64 {
if let Some((_, valid_to)) = self.ethflow_data {
// For ethflow orders, we always return the user valid_to,
// as the Eip1271 valid to is u32::max
return valid_to;
}
self.valid_to
}
}

#[derive(Debug, sqlx::FromRow)]
pub struct FullOrderWithQuote {
#[sqlx(flatten)]
Expand All @@ -508,14 +519,41 @@ pub struct FullOrderWithQuote {
pub solver: Option<Address>,
}

impl FullOrder {
pub fn valid_to(&self) -> i64 {
if let Some((_, valid_to)) = self.ethflow_data {
// For ethflow orders, we always return the user valid_to,
// as the Eip1271 valid to is u32::max
return valid_to;
}
self.valid_to
impl FullOrderWithQuote {
pub fn into_order_and_quote(self) -> (FullOrder, Option<Quote>) {
let quote = match (
self.quote_buy_amount,
self.quote_sell_amount,
self.quote_gas_amount,
self.quote_gas_price,
self.quote_sell_token_price,
self.quote_verified,
self.quote_metadata,
self.solver,
) {
(
Some(buy_amount),
Some(sell_amount),
Some(gas_amount),
Some(gas_price),
Some(sell_token_price),
Some(verified),
Some(metadata),
Some(solver),
) => Some(Quote {
order_uid: self.full_order.uid,
gas_amount,
gas_price,
sell_token_price,
sell_amount,
buy_amount,
solver,
verified,
metadata,
}),
_ => None,
};
(self.full_order, quote)
}
}

Expand Down Expand Up @@ -574,19 +612,6 @@ COALESCE((SELECT executed_fee_token FROM order_execution oe WHERE oe.order_uid =

pub const FROM: &str = "orders o";

pub async fn single_full_order(
ex: &mut PgConnection,
uid: &OrderUid,
) -> Result<Option<FullOrder>, sqlx::Error> {
#[rustfmt::skip]
const QUERY: &str = const_format::concatcp!(
"SELECT ", SELECT,
" FROM ", FROM,
" WHERE o.uid = $1 ",
);
sqlx::query_as(QUERY).bind(uid).fetch_optional(ex).await
}

pub async fn single_full_order_with_quote(
ex: &mut PgConnection,
uid: &OrderUid,
Expand Down Expand Up @@ -861,10 +886,11 @@ mod tests {
let order_ = read_order(&mut db, &order.uid).await.unwrap().unwrap();
assert_eq!(order, order_);

let full_order = single_full_order(&mut db, &order.uid)
let full_order = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;

assert_eq!(order.uid, full_order.uid);
assert_eq!(order.owner, full_order.owner);
Expand Down Expand Up @@ -923,10 +949,11 @@ mod tests {
.await
.unwrap();
insert_order(&mut db, &order).await.unwrap();
let order_ = single_full_order(&mut db, &order.uid)
let order_ = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert_eq!(Some(sender), order_.onchain_user);
}

Expand Down Expand Up @@ -958,10 +985,11 @@ mod tests {
)
.await
.unwrap();
let order_ = single_full_order(&mut db, &order.uid)
let order_ = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert_eq!(
Some((Some(Default::default()), user_valid_to)),
order_.ethflow_data
Expand Down Expand Up @@ -994,10 +1022,11 @@ mod tests {
insert_or_overwrite_interaction(&mut db, &post_interaction_1, &order.uid)
.await
.unwrap();
let order_ = single_full_order(&mut db, &order.uid)
let order_ = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert_eq!(
vec![ByteArray::default(), ByteArray([1; 20])],
order_
Expand Down Expand Up @@ -1084,10 +1113,11 @@ mod tests {
insert_or_overwrite_interaction(&mut db, &pre_interaction_1, &order.uid)
.await
.unwrap();
let order_ = single_full_order(&mut db, &order.uid)
let order_ = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert_eq!(
vec![ByteArray::default(), ByteArray([1; 20])],
order_
Expand Down Expand Up @@ -1458,10 +1488,11 @@ mod tests {
.unwrap();
}

let order = single_full_order(&mut db, &order.uid)
let order = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;

let expected_sell_amount_including_fees: BigInt = sell_amount_including_fee * 16;
assert!(expected_sell_amount_including_fees > u256_max);
Expand Down Expand Up @@ -1558,18 +1589,20 @@ mod tests {
..Default::default()
};
insert_order(&mut db, &order).await.unwrap();
let result = single_full_order(&mut db, &order.uid)
let result = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert!(!result.invalidated);
insert_onchain_invalidation(&mut db, &EventIndex::default(), &order.uid)
.await
.unwrap();
let result = single_full_order(&mut db, &order.uid)
let result = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert!(result.invalidated);
}

Expand Down Expand Up @@ -1907,10 +1940,11 @@ mod tests {
.unwrap();

let fee: BigDecimal = 0.into();
let order = single_full_order(&mut db, &order_uid)
let order = single_full_order_with_quote(&mut db, &order_uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert_eq!(order.executed_fee, fee);

let fee: BigDecimal = 1.into();
Expand All @@ -1928,10 +1962,11 @@ mod tests {
.await
.unwrap();

let order = single_full_order(&mut db, &order_uid)
let order = single_full_order_with_quote(&mut db, &order_uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert_eq!(order.executed_fee, fee);
}

Expand All @@ -1946,19 +1981,21 @@ mod tests {
..Default::default()
};
insert_order(&mut db, &order).await.unwrap();
let full_order = single_full_order(&mut db, &order.uid)
let full_order = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert!(full_order.full_app_data.is_none());
let full_app_data = vec![0u8, 1, 2];
crate::app_data::insert(&mut db, &order.app_data, &full_app_data)
.await
.unwrap();
let full_order = single_full_order(&mut db, &order.uid)
let full_order = single_full_order_with_quote(&mut db, &order.uid)
.await
.unwrap()
.unwrap();
.unwrap()
.full_order;
assert_eq!(full_order.full_app_data, Some(full_app_data));
}

Expand Down
2 changes: 1 addition & 1 deletion crates/model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ num = { workspace = true }
primitive-types = { workspace = true }
secp256k1 = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }
strum = { workspace = true }
web3 = { workspace = true, features = ["signing"] }

[dev-dependencies]
serde_json = { workspace = true }
maplit = { workspace = true }
testlib = { path = "../testlib" }

Expand Down
25 changes: 25 additions & 0 deletions crates/model/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
},
anyhow::{anyhow, Result},
app_data::{hash_full_app_data, AppDataHash},
bigdecimal::BigDecimal,
chrono::{offset::Utc, DateTime},
derive_more::Debug as DeriveDebug,
hex_literal::hex,
Expand Down Expand Up @@ -691,6 +692,10 @@ pub struct OrderMetadata {
/// Full app data that `OrderData::app_data` is a hash of. Can be None if
/// the backend doesn't know about the full app data.
pub full_app_data: Option<String>,
/// If the order was created with a quote, then this field contains that
/// quote data for reference.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub quote: Option<OrderQuote>,
}

// uid as 56 bytes: 32 for orderDigest, 20 for ownerAddress and 4 for validTo
Expand Down Expand Up @@ -964,6 +969,26 @@ impl BuyTokenDestination {
}
}

/// A quote from which particular order was created.
#[serde_as]
#[derive(Eq, PartialEq, Clone, Default, Deserialize, Serialize, DeriveDebug)]
#[serde(rename_all = "camelCase")]
pub struct OrderQuote {
#[serde_as(as = "DisplayFromStr")]
pub gas_amount: BigDecimal,
#[serde_as(as = "DisplayFromStr")]
pub gas_price: BigDecimal,
#[serde_as(as = "DisplayFromStr")]
pub sell_token_price: BigDecimal,
#[serde_as(as = "HexOrDecimalU256")]
pub sell_amount: U256,
#[serde_as(as = "HexOrDecimalU256")]
pub buy_amount: U256,
pub solver: H160,
pub verified: bool,
pub metadata: serde_json::Value,
}

#[cfg(test)]
mod tests {
use {
Expand Down
Loading
Loading