Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrug committed Nov 25, 2024
1 parent 1868aae commit 507f6ae
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/autopilot/src/database/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl QuoteStoring for Postgres {
let row = create_quote_row(&data);
let id = database::quotes::save(&mut ex, &row).await?;
if !data.interactions.is_empty() {
let interactions = create_quote_interactions_insert_data(id, &data);
let interactions = create_quote_interactions_insert_data(id, &data)?;
database::quotes::insert_quote_interactions(&mut ex, &interactions).await?;
}
Ok(id)
Expand Down
14 changes: 7 additions & 7 deletions crates/database/src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ WHERE expiration_timestamp < $1
/// One row in the `quotes_interactions` table.
#[derive(Clone, Debug, PartialEq, sqlx::FromRow)]
pub struct QuoteInteraction {
pub id: QuoteId,
pub index: i64,
pub quote_id: QuoteId,
pub index: i32,
pub target: Address,
pub value: BigDecimal,
pub call_data: Vec<u8>,
Expand All @@ -164,7 +164,7 @@ pub async fn insert_quote_interaction(
) -> Result<(), sqlx::Error> {
const QUERY: &str = r#"
INSERT INTO quotes_interactions (
order_uid,
quote_id,
index,
target,
value,
Expand All @@ -173,7 +173,7 @@ INSERT INTO quotes_interactions (
VALUES ($1, $2, $3, $4, $5)
"#;
sqlx::query(QUERY)
.bind(quote_interaction.id)
.bind(quote_interaction.quote_id)
.bind(quote_interaction.index)
.bind(quote_interaction.target)
.bind(&quote_interaction.value)
Expand Down Expand Up @@ -484,7 +484,7 @@ mod tests {
crate::clear_DANGER_(&mut db).await.unwrap();

let quote_interaction = QuoteInteraction {
id: Default::default(),
quote_id: Default::default(),
index: Default::default(),
target: ByteArray([1; 20]),
value: 2.into(),
Expand All @@ -497,10 +497,10 @@ mod tests {
const QUERY: &str = r#"
SELECT *
FROM quotes_interactions
WHERE order_uid = $1
WHERE quote_id = $1
"#;
let interaction: Option<QuoteInteraction> = sqlx::query_as(QUERY)
.bind(quote_interaction.id)
.bind(quote_interaction.quote_id)
.fetch_optional(&mut db as &mut PgConnection)
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/orderbook/src/database/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl QuoteStoring for Postgres {
let row = create_quote_row(&data);
let id = database::quotes::save(&mut ex, &row).await?;
if !data.interactions.is_empty() {
let interactions = create_quote_interactions_insert_data(id, &data);
let interactions = create_quote_interactions_insert_data(id, &data)?;
database::quotes::insert_quote_interactions(&mut ex, &interactions).await?;
}
Ok(id)
Expand Down
17 changes: 10 additions & 7 deletions crates/shared/src/event_storing_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {
db_order_conversions::order_kind_into,
order_quoting::{quote_kind_from_signing_scheme, QuoteData, QuoteSearchParameters},
},
anyhow::Result,
chrono::{DateTime, Utc},
database::{
byte_array::ByteArray,
Expand Down Expand Up @@ -37,16 +38,18 @@ pub fn create_quote_row(data: &QuoteData) -> DbQuote {
pub fn create_quote_interactions_insert_data(
id: QuoteId,
data: &QuoteData,
) -> Vec<DbQuoteInteraction> {
) -> Result<Vec<DbQuoteInteraction>> {
data.interactions
.iter()
.enumerate()
.map(|(index, interaction)| DbQuoteInteraction {
id,
index: index.try_into().unwrap(),
target: ByteArray(interaction.target.0),
value: u256_to_big_decimal(&interaction.value),
call_data: interaction.call_data.clone(),
.map(|(index, interaction)| {
Ok(DbQuoteInteraction {
quote_id: id,
index: index.try_into()?,
target: ByteArray(interaction.target.0),
value: u256_to_big_decimal(&interaction.value),
call_data: interaction.call_data.clone(),
})
})
.collect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ ALTER TABLE order_quotes

-- Step 3: Create table with quote interactions
CREATE TABLE quotes_interactions (
order_uid bytea NOT NULL,
quote_id bigint NOT NULL,
index int NOT NULL,
target bytea NOT NULL,
value numeric(78,0) NOT NULL,
call_data bytea,

PRIMARY KEY (order_uid, index)
PRIMARY KEY (quote_id, index)
);

-- Get a specific quote's interactions.
CREATE INDEX quote_uid_interactions ON quotes_interactions USING HASH (order_uid);
CREATE INDEX quote_id_interactions ON quotes_interactions USING HASH (quote_id);

0 comments on commit 507f6ae

Please sign in to comment.