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

Storing call_data in quotes and order_quotes tables #3124

Open
wants to merge 53 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
f46b82d
Implementation
mstrug Nov 14, 2024
d9eb189
Added call_data to quotes table
mstrug Nov 15, 2024
2c0e92e
Updated order_quotes table
mstrug Nov 15, 2024
006a873
Added comments
mstrug Nov 15, 2024
7d2bb01
Added db migration
mstrug Nov 15, 2024
9947257
Fixed formatting
mstrug Nov 15, 2024
692d94e
Fixed sql query
mstrug Nov 15, 2024
6fb1ae7
Updated db querying for order with quote
mstrug Nov 15, 2024
a8f349f
Added test
mstrug Nov 15, 2024
eaf09d2
Merge branch 'main' into feature/storing-call-data-in-quotes
mstrug Nov 15, 2024
67c20ff
Fixed formatting
mstrug Nov 15, 2024
e9026a4
Updated tests
mstrug Nov 15, 2024
6671767
Added verified column to quotes table
mstrug Nov 21, 2024
086e579
Added quotes_interactions table
mstrug Nov 23, 2024
bca89ac
Fixed tests compilation
mstrug Nov 23, 2024
7c6983c
Fixed formatting
mstrug Nov 23, 2024
a847647
Updated orders database tables
mstrug Nov 23, 2024
1868aae
Fixed sql
mstrug Nov 25, 2024
507f6ae
Update
mstrug Nov 25, 2024
aff0468
Added removing quote_interactions on quote remove
mstrug Nov 25, 2024
189222b
Added order_quotes_interactions table
mstrug Nov 26, 2024
7a0cdc3
Added filling order quote interaction table
mstrug Nov 26, 2024
ae1115a
Fixed clippy warning
mstrug Nov 26, 2024
33a4cc4
Fixed missing variant
mstrug Nov 26, 2024
c62ac2d
Returning quote interactions in QuoteStoring implementation
mstrug Nov 27, 2024
dc36269
Added waiting for db migrations before tests
mstrug Nov 27, 2024
888e576
Added db dependency to migrations docker container
mstrug Nov 27, 2024
ec9eae0
Temporarily disabled restarts on migration
mstrug Nov 27, 2024
0210d3c
Reverted yaml file changes
mstrug Nov 27, 2024
77fc406
Fixed migration script
mstrug Nov 27, 2024
69dd7fa
Fixed test
mstrug Nov 27, 2024
828ec19
Merge branch 'main' into feature/storing-call-data-in-quotes
mstrug Nov 27, 2024
757029a
Updated new table names
mstrug Nov 28, 2024
085c530
Updated db readme & migration script
mstrug Nov 28, 2024
1e91d31
Merge branch 'main' into feature/storing-call-data-in-quotes
mstrug Nov 28, 2024
0921924
Merge branch 'main' into feature/storing-call-data-in-quotes
mstrug Nov 28, 2024
67cd65c
Code cleanup
mstrug Nov 28, 2024
39cade5
Updated tests
mstrug Nov 28, 2024
5239901
Added transaction for storing interactions
mstrug Nov 29, 2024
5aac1dc
Updated removing of expired quote interactions
mstrug Nov 29, 2024
23b2182
Removed updated on conflict from insert order quote interaction
mstrug Nov 29, 2024
f4f040c
Updated test
mstrug Nov 29, 2024
8848b73
Updated db readme
mstrug Nov 29, 2024
0afb9ad
Ensuring repeated db inserts uses transaction
mstrug Nov 29, 2024
2f610ae
Fixed tests
mstrug Nov 29, 2024
9979645
Optimized db queries using join
mstrug Nov 29, 2024
9a7f510
Added test
mstrug Dec 2, 2024
d8ff00b
Small refactorings
mstrug Dec 2, 2024
726376a
Function rename
mstrug Dec 2, 2024
1e23f0d
Using array_agg function for getting quote with interactions from db
mstrug Dec 2, 2024
f5cbc3e
Removed new error variant, fixed comment in sql script
mstrug Dec 2, 2024
59e39a2
Merge branch 'main' into feature/storing-call-data-in-quotes
mstrug Dec 2, 2024
ddaf4b2
Updated table descriptions
mstrug Dec 3, 2024
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
7 changes: 7 additions & 0 deletions crates/autopilot/src/database/onchain_order_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ async fn parse_general_onchain_order_placement_data<'a>(
sell_amount: u256_to_big_decimal(&quote.sell_amount),
buy_amount: u256_to_big_decimal(&quote.buy_amount),
solver: ByteArray(quote.data.solver.0),
call_data: order_data.1.call_data.clone(),
verified: quote.data.verified,
}),
Err(err) => {
let err_label = err.to_metrics_label();
Expand Down Expand Up @@ -620,6 +622,7 @@ fn convert_onchain_order_placement(
true => OrderClass::Limit,
false => OrderClass::Market,
},
call_data: String::new(),
};
let onchain_order_placement_event = OnchainOrderPlacement {
order_uid: ByteArray(order_uid.0),
Expand Down Expand Up @@ -925,6 +928,7 @@ mod test {
buy_token_balance: buy_token_destination_into(expected_order_data.buy_token_balance),
full_fee_amount: u256_to_big_decimal(&expected_order_data.fee_amount),
cancellation_timestamp: None,
call_data: String::new(),
};
assert_eq!(onchain_order_placement, expected_onchain_order_placement);
assert_eq!(order, expected_order);
Expand Down Expand Up @@ -1036,6 +1040,7 @@ mod test {
buy_token_balance: buy_token_destination_into(expected_order_data.buy_token_balance),
full_fee_amount: u256_to_big_decimal(&U256::zero()),
cancellation_timestamp: None,
call_data: String::new(),
};
assert_eq!(onchain_order_placement, expected_onchain_order_placement);
assert_eq!(order, expected_order);
Expand Down Expand Up @@ -1187,6 +1192,8 @@ mod test {
sell_amount: u256_to_big_decimal(&quote.sell_amount),
buy_amount: u256_to_big_decimal(&quote.buy_amount),
solver: ByteArray(quote.data.solver.0),
call_data: String::new(),
verified: quote.data.verified,
};
assert_eq!(result.1, vec![Some(expected_quote)]);
assert_eq!(
Expand Down
32 changes: 26 additions & 6 deletions crates/database/src/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub struct Order {
pub full_fee_amount: BigDecimal,
pub cancellation_timestamp: Option<DateTime<Utc>>,
pub class: OrderClass,
pub call_data: String,
}

pub async fn insert_orders_and_ignore_conflicts(
Expand Down Expand Up @@ -142,9 +143,10 @@ INSERT INTO orders (
buy_token_balance,
full_fee_amount,
cancellation_timestamp,
class
class,
call_data
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22)
"#;

pub async fn insert_order_and_ignore_conflicts(
Expand Down Expand Up @@ -186,6 +188,7 @@ async fn insert_order_execute_sqlx(
.bind(&order.full_fee_amount)
.bind(order.cancellation_timestamp)
.bind(order.class)
.bind(&order.call_data)
.execute(ex)
.await?;
Ok(())
Expand Down Expand Up @@ -329,6 +332,8 @@ pub struct Quote {
pub sell_amount: BigDecimal,
pub buy_amount: BigDecimal,
pub solver: Address,
pub call_data: String,
pub verified: bool,
m-lord-renkse marked this conversation as resolved.
Show resolved Hide resolved
}

pub async fn insert_quotes(ex: &mut PgConnection, quotes: &[Quote]) -> Result<(), sqlx::Error> {
Expand All @@ -346,9 +351,11 @@ INSERT INTO order_quotes (
sell_token_price,
sell_amount,
buy_amount,
solver
solver,
call_data,
verified,
)
VALUES ($1, $2, $3, $4, $5, $6, $7)"#;
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"#;

pub async fn insert_quote_and_update_on_conflict(
ex: &mut PgConnection,
Expand All @@ -362,7 +369,7 @@ pub async fn insert_quote_and_update_on_conflict(
" ON CONFLICT (order_uid) DO UPDATE
SET gas_amount = $2, gas_price = $3,
sell_token_price = $4, sell_amount = $5,
buy_amount = $6
buy_amount = $6, call_data = $8, verified = $9
"
);
sqlx::query(QUERY)
Expand All @@ -373,6 +380,8 @@ buy_amount = $6
.bind(&quote.sell_amount)
.bind(&quote.buy_amount)
.bind(quote.solver)
.bind(&quote.call_data)
.bind(quote.verified)
.execute(ex)
.await?;
Ok(())
Expand All @@ -387,6 +396,8 @@ pub async fn insert_quote(ex: &mut PgConnection, quote: &Quote) -> Result<(), sq
.bind(&quote.sell_amount)
.bind(&quote.buy_amount)
.bind(quote.solver)
.bind(&quote.call_data)
.bind(quote.verified)
.execute(ex)
.await?;
Ok(())
Expand Down Expand Up @@ -487,6 +498,7 @@ pub struct FullOrder {
pub onchain_placement_error: Option<OnchainOrderPlacementError>,
pub executed_surplus_fee: BigDecimal,
pub full_app_data: Option<Vec<u8>>,
pub call_data: Option<String>,
}

#[derive(Debug, sqlx::FromRow)]
Expand All @@ -498,6 +510,7 @@ pub struct FullOrderWithQuote {
pub quote_gas_amount: Option<f64>,
pub quote_gas_price: Option<f64>,
pub quote_sell_token_price: Option<f64>,
pub quote_verified: Option<bool>,
pub solver: Option<Address>,
}

Expand Down Expand Up @@ -538,7 +551,7 @@ pub const SELECT: &str = r#"
o.uid, o.owner, o.creation_timestamp, o.sell_token, o.buy_token, o.sell_amount, o.buy_amount,
o.valid_to, o.app_data, o.fee_amount, o.full_fee_amount, o.kind, o.partially_fillable, o.signature,
o.receiver, o.signing_scheme, o.settlement_contract, o.sell_token_balance, o.buy_token_balance,
o.class,
o.class, o.call_data,
(SELECT COALESCE(SUM(t.buy_amount), 0) FROM trades t WHERE t.order_uid = o.uid) AS sum_buy,
(SELECT COALESCE(SUM(t.sell_amount), 0) FROM trades t WHERE t.order_uid = o.uid) AS sum_sell,
(SELECT COALESCE(SUM(t.fee_amount), 0) FROM trades t WHERE t.order_uid = o.uid) AS sum_fee,
Expand Down Expand Up @@ -591,6 +604,7 @@ pub async fn single_full_order_with_quote(
", o_quotes.gas_amount as quote_gas_amount",
", o_quotes.gas_price as quote_gas_price",
", o_quotes.sell_token_price as quote_sell_token_price",
", o_quotes.verified as quote_verified",
", o_quotes.solver as solver",
" FROM ", FROM,
" LEFT JOIN order_quotes o_quotes ON o.uid = o_quotes.order_uid",
Expand Down Expand Up @@ -1201,6 +1215,8 @@ mod tests {
sell_amount: 4.into(),
buy_amount: 5.into(),
solver: ByteArray([1; 20]),
call_data: String::new(),
verified: false,
};
insert_quote(&mut db, &quote).await.unwrap();
insert_quote_and_update_on_conflict(&mut db, &quote)
Expand Down Expand Up @@ -1261,6 +1277,8 @@ mod tests {
sell_amount: 4.into(),
buy_amount: 5.into(),
solver: ByteArray([1; 20]),
call_data: String::new(),
verified: false,
};
insert_quote(&mut db, &quote).await.unwrap();
let quote_ = read_quote(&mut db, &quote.order_uid)
Expand All @@ -1287,6 +1305,8 @@ mod tests {
sell_amount: 4.into(),
buy_amount: 5.into(),
solver: ByteArray([1; 20]),
call_data: String::new(),
verified: false,
};
insert_quote(&mut db, &quote).await.unwrap();
let order_with_quote = single_full_order_with_quote(&mut db, &quote.order_uid)
Expand Down
19 changes: 15 additions & 4 deletions crates/orderbook/src/database/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ impl OrderWithQuote {
sell_amount: u256_to_big_decimal(&quote.sell_amount),
buy_amount: u256_to_big_decimal(&quote.buy_amount),
solver: ByteArray(quote.data.solver.0),
call_data: order.metadata.full_app_data.clone().unwrap_or_default(),
verified: quote.data.verified,
}),
order,
}
Expand Down Expand Up @@ -202,6 +204,7 @@ async fn insert_order(order: &Order, ex: &mut PgConnection) -> Result<(), Insert
buy_token_balance: buy_token_destination_into(order.data.buy_token_balance),
full_fee_amount: u256_to_big_decimal(&order.metadata.full_fee_amount),
cancellation_timestamp: None,
call_data: order.metadata.full_app_data.clone().unwrap_or_default(),
};

database::orders::insert_order(ex, &order)
Expand All @@ -221,18 +224,20 @@ async fn insert_order(order: &Order, ex: &mut PgConnection) -> Result<(), Insert
}

async fn insert_quote(
uid: &OrderUid,
order: &Order,
quote: &Quote,
ex: &mut PgConnection,
) -> Result<(), InsertionError> {
let quote = database::orders::Quote {
order_uid: ByteArray(uid.0),
order_uid: ByteArray(order.metadata.uid.0),
gas_amount: quote.data.fee_parameters.gas_amount,
gas_price: quote.data.fee_parameters.gas_price,
sell_token_price: quote.data.fee_parameters.sell_token_price,
sell_amount: u256_to_big_decimal(&quote.sell_amount),
buy_amount: u256_to_big_decimal(&quote.buy_amount),
solver: ByteArray(quote.data.solver.0),
call_data: order.metadata.full_app_data.clone().unwrap_or_default(),
mstrug marked this conversation as resolved.
Show resolved Hide resolved
verified: quote.data.verified,
};
database::orders::insert_quote(ex, &quote)
.await
Expand All @@ -258,7 +263,7 @@ impl OrderStoring for Postgres {

insert_order(&order, &mut ex).await?;
if let Some(quote) = quote {
insert_quote(&order.metadata.uid, &quote, &mut ex).await?;
insert_quote(&order, &quote, &mut ex).await?;
}
Self::insert_order_app_data(&order, &mut ex).await?;

Expand Down Expand Up @@ -318,7 +323,7 @@ impl OrderStoring for Postgres {
.await?;
insert_order(&new_order, ex).await?;
if let Some(quote) = new_quote {
insert_quote(&new_order.metadata.uid, &quote, ex).await?;
insert_quote(&new_order, &quote, ex).await?;
}
Self::insert_order_app_data(&new_order, ex).await?;

Expand Down Expand Up @@ -356,12 +361,14 @@ impl OrderStoring for Postgres {
let order = orders::single_full_order_with_quote(&mut ex, &ByteArray(uid.0)).await?;
order
.map(|order_with_quote| {
let call_data = order_with_quote.full_order.call_data.clone().unwrap_or_default();
let quote = match (
order_with_quote.quote_buy_amount,
order_with_quote.quote_sell_amount,
order_with_quote.quote_gas_amount,
order_with_quote.quote_gas_price,
order_with_quote.quote_sell_token_price,
order_with_quote.quote_verified,
order_with_quote.solver,
) {
(
Expand All @@ -370,6 +377,7 @@ impl OrderStoring for Postgres {
Some(gas_amount),
Some(gas_price),
Some(sell_token_price),
Some(verified),
Some(solver),
) => Some(orders::Quote {
order_uid: order_with_quote.full_order.uid,
Expand All @@ -379,6 +387,8 @@ impl OrderStoring for Postgres {
sell_amount,
buy_amount,
solver,
call_data,
verified
}),
_ => None,
};
Expand Down Expand Up @@ -708,6 +718,7 @@ mod tests {
onchain_placement_error: None,
executed_surplus_fee: Default::default(),
full_app_data: Default::default(),
call_data: None,
};

// Open - sell (filled - 0%)
Expand Down
Loading