Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions crates/autopilot/src/database/onchain_order_events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ where
solver: ByteArray(*quote.data.solver.0),
verified: quote.data.verified,
metadata: quote.data.metadata.try_into()?,
auction_id: None,
}),
Err(err) => {
let err_label = err.to_metrics_label();
Expand Down Expand Up @@ -1315,6 +1316,7 @@ mod test {
solver: ByteArray(*quote.data.solver.0),
verified: quote.data.verified,
metadata: quote.data.metadata.try_into().unwrap(),
auction_id: None,
};
assert_eq!(result.1, vec![Some(expected_quote)]);
assert_eq!(
Expand Down
19 changes: 16 additions & 3 deletions crates/database/src/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
AppId,
OrderUid,
TransactionHash,
auction::AuctionId,
onchain_broadcasted_orders::OnchainOrderPlacementError,
order_events::{OrderEvent, OrderEventLabel, insert_order_event},
},
Expand Down Expand Up @@ -360,6 +361,7 @@ pub struct Quote {
pub solver: Address,
pub verified: bool,
pub metadata: serde_json::Value,
pub auction_id: Option<AuctionId>,
}

#[instrument(skip_all)]
Expand All @@ -380,9 +382,10 @@ INSERT INTO order_quotes (
buy_amount,
solver,
verified,
metadata
metadata,
auction_id
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"#;
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)"#;

#[instrument(skip_all)]
pub async fn insert_quote_and_update_on_conflict(
Expand All @@ -397,7 +400,8 @@ 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, verified = $8, metadata = $9
buy_amount = $6, verified = $8, metadata = $9,
auction_id = $10
Comment thread
MartinquaXD marked this conversation as resolved.
"
);
sqlx::query(QUERY)
Expand All @@ -410,6 +414,7 @@ buy_amount = $6, verified = $8, metadata = $9
.bind(quote.solver)
.bind(quote.verified)
.bind(&quote.metadata)
.bind(quote.auction_id)
.execute(ex)
.await?;
Ok(())
Expand All @@ -427,6 +432,7 @@ pub async fn insert_quote(ex: &mut PgConnection, quote: &Quote) -> Result<(), sq
.bind(quote.solver)
.bind(quote.verified)
.bind(&quote.metadata)
.bind(quote.auction_id)
.execute(ex)
.await?;
Ok(())
Expand Down Expand Up @@ -557,6 +563,7 @@ pub struct FullOrderWithQuote {
pub quote_verified: Option<bool>,
pub quote_metadata: Option<serde_json::Value>,
pub solver: Option<Address>,
pub quote_auction_id: Option<AuctionId>,
}

impl FullOrderWithQuote {
Expand Down Expand Up @@ -590,6 +597,7 @@ impl FullOrderWithQuote {
solver,
verified,
metadata,
auction_id: self.quote_auction_id,
}),
_ => None,
};
Expand Down Expand Up @@ -662,6 +670,7 @@ const FULL_ORDER_WITH_QUOTE: &str = const_format::concatcp!(
", o_quotes.verified as quote_verified",
", o_quotes.metadata as quote_metadata",
", o_quotes.solver as solver",
", o_quotes.auction_id as quote_auction_id",
" FROM ",
FROM,
" LEFT JOIN order_quotes o_quotes ON o.uid = o_quotes.order_uid",
Expand Down Expand Up @@ -1564,6 +1573,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: Default::default(),
auction_id: None,
};
insert_quote(&mut db, &quote).await.unwrap();
insert_quote_and_update_on_conflict(&mut db, &quote)
Expand Down Expand Up @@ -1640,6 +1650,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: true,
metadata,
auction_id: None,
};
insert_quote(&mut db, &quote).await.unwrap();
let quote_ = read_quote(&mut db, &quote.order_uid)
Expand Down Expand Up @@ -1668,6 +1679,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: Default::default(),
auction_id: None,
};
insert_quote(&mut db, &quote).await.unwrap();
let order_with_quote = single_full_order_with_quote(&mut db, &quote.order_uid)
Expand Down Expand Up @@ -2701,6 +2713,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: Default::default(),
auction_id: None,
};

// insert quote with verified and metadata fields stored as NULL
Expand Down
17 changes: 14 additions & 3 deletions crates/database/src/quotes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{Address, orders::OrderKind},
crate::{Address, auction::AuctionId, orders::OrderKind},
bigdecimal::BigDecimal,
sqlx::{
PgConnection,
Expand Down Expand Up @@ -37,6 +37,7 @@ pub struct Quote {
pub solver: Address,
pub verified: bool,
pub metadata: serde_json::Value,
pub auction_id: Option<AuctionId>,
}

/// Stores the quote and returns the id. The id of the quote parameter is not
Expand All @@ -57,9 +58,10 @@ INSERT INTO quotes (
quote_kind,
solver,
verified,
metadata
metadata,
auction_id
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
RETURNING id
"#;
let (id,) = sqlx::query_as(QUERY)
Expand All @@ -76,6 +78,7 @@ RETURNING id
.bind(quote.solver)
.bind(quote.verified)
.bind(&quote.metadata)
.bind(quote.auction_id)
.fetch_one(ex)
.await?;
Ok(id)
Expand Down Expand Up @@ -199,6 +202,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: Default::default(),
auction_id: Some(12),
};
let id = save(&mut db, &quote).await.unwrap();
quote.id = id;
Expand Down Expand Up @@ -234,6 +238,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: Default::default(),
auction_id: None,
};

let token_b = ByteArray([2; 20]);
Expand All @@ -252,6 +257,7 @@ mod tests {
solver: ByteArray([2; 20]),
verified: false,
metadata: Default::default(),
auction_id: None,
};

// Save two measurements for token_a
Expand Down Expand Up @@ -425,6 +431,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: Default::default(),
auction_id: None,
};

// Highest absolute buy amount, but an expensive fee.
Expand Down Expand Up @@ -485,6 +492,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: Default::default(),
auction_id: None,
};

// Lowest absolute sell amount, but an expensive fee -> total spend 3000.
Expand Down Expand Up @@ -549,6 +557,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: Default::default(),
auction_id: None,
};

// Unverified but strictly better rate (more buy for the same sell).
Expand Down Expand Up @@ -610,6 +619,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: Default::default(),
auction_id: None,
};
let id = save(&mut db, &quote).await.unwrap();
quote.id = id;
Expand Down Expand Up @@ -668,6 +678,7 @@ mod tests {
solver: ByteArray([1; 20]),
verified: false,
metadata: metadata.clone(),
auction_id: None,
};
// store quote in database
let id = save(&mut db, &quote).await.unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/orderbook/src/database/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ async fn insert_order(order: &Order, ex: &mut PgConnection) -> Result<(), Insert
solver: ByteArray(quote.solver.0.0),
verified: quote.verified,
metadata: quote.metadata.clone(),
auction_id: None,
};
database::orders::insert_quote(ex, &db_quote)
.await
Expand Down
1 change: 1 addition & 0 deletions crates/shared/src/event_storing_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn create_quote_row(data: QuoteData) -> Result<DbQuote> {
solver: ByteArray(*data.solver.0),
verified: data.verified,
metadata: data.metadata.try_into()?,
auction_id: None,
})
}

Expand Down
2 changes: 2 additions & 0 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Quotes that an order was created with. These quotes get stored persistently and
verified | boolean | not null | information if quote was verified
metadata | json | not null | additional data associated with the quote in json format
creation\_timestamp | timestamptz | not null | when the entry was created (DEFAULT NOW() for new and 1970-01-01 for historical data)
auction\_id | bigint | nullable | the auction competition that was the basis for this quote

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly that this column will be populated only for the fast path quotes? If so, it is hard to understand from the naming and description. The same applies to the rust code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - only for fast path orders. I didn't want to codify this by calling it something like "fast_path_auction" or so in case we want to more generally migrate towards this system of storing quotes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but maybe a comment/description should explain that?


Indexes:
- PRIMARY KEY: btree(`order_uid`)
Expand Down Expand Up @@ -351,6 +352,7 @@ Stores quotes in order to determine whether it makes sense to allow a user to cr
solver | bytea | not null | public address of the solver that provided this quote
verified | boolean | not null | information if quote was verified
metadata | json | not null | additional data associated with the quote in json format
auction\_id | bigint | nullable | the auction competition that was the basis for this quote

Indexes:
- PRIMARY KEY: btree(`id`)
Expand Down
8 changes: 8 additions & 0 deletions database/sql/V120__add_auction_id_to_quotes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Links a stored quote to its solver competition ledger row in
-- `competition_auctions`. Nullable because only fast path quotes
-- need this link.
ALTER TABLE quotes ADD COLUMN auction_id bigint;

-- Copy of quotes.auction_id preserved when the quote is attached
-- to an order.
ALTER TABLE order_quotes ADD COLUMN auction_id bigint;
Comment thread
MartinquaXD marked this conversation as resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order_quotes.auction_id is added here but never populated, insert_order still sets it as None, and neither of the stacked follow-ups (#4713/#4714) touches this path; they only set quotes.auction_id. Is the order-creation copy meant for a a specifc later step?

Loading