Skip to content
Open
9 changes: 9 additions & 0 deletions crates/autopilot/src/infra/persistence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,15 @@ impl Persistence {
)
.await?;

database::settlements::update_settlement_gas(
&mut ex,
block_number,
log_index,
u256_to_big_decimal(&gas.0),
u256_to_big_decimal(&gas_price.0.0),
)
.await?;

store_order_events(
&mut ex,
fee_breakdown.keys().cloned(),
Expand Down
12 changes: 10 additions & 2 deletions crates/database/src/jit_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub async fn get_by_id(
const QUERY: &str = const_format::concatcp!(
"SELECT ",
SELECT,
crate::trades::ORDER_GAS_COST_COLUMN,
" FROM ", FROM,
" WHERE o.uid = $1 ",
);
Expand All @@ -59,8 +60,14 @@ pub async fn get_many_by_uid<'a>(
ex: &'a mut PgConnection,
order_uids: &'a [OrderUid],
) -> Result<Vec<orders::FullOrder>, sqlx::Error> {
const QUERY: &str =
const_format::concatcp!("SELECT ", SELECT, " FROM ", FROM, " WHERE o.uid = ANY($1)");
const QUERY: &str = const_format::concatcp!(
"SELECT ",
SELECT,
crate::trades::ORDER_GAS_COST_COLUMN,
" FROM ",
FROM,
" WHERE o.uid = ANY($1)"
);
sqlx::query_as(QUERY).bind(order_uids).fetch_all(ex).await
}

Expand All @@ -73,6 +80,7 @@ pub async fn get_by_tx(
orders::SETTLEMENT_LOG_INDICES,
"SELECT ",
SELECT,
crate::trades::ORDER_GAS_COST_COLUMN,
" FROM ",
FROM,
" JOIN trades t ON t.order_uid = o.uid",

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.

Worth double-checking the semantics here: ORDER_GAS_COST sums the order's gas across all of its fills globally, but get_by_tx (feeding /transactions/{tx}/orders) is a per-transaction view. For a partially-fillable order settled across multiple transactions, this endpoint will report the order's lifetime gas cost, not the portion attributable to $tx. A frontend summing gasCost over the orders in a tx to get a "transaction gas total" would over-count. Consistent with the OrderMetadata.gas_cost field definition (it's an order-level property), so likely acceptable — just flagging in case the per-tx endpoint is expected to be tx-scoped.

Expand Down
4 changes: 2 additions & 2 deletions crates/database/src/order_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ pub fn user_orders<'a>(
") ",
// Phase 2: fetch full rows for the relevant UIDs only
" (",
" SELECT ", orders::SELECT,
" SELECT ", orders::SELECT, crate::trades::ORDER_GAS_COST_COLUMN,
" FROM ", orders::FROM,
" WHERE o.uid IN (SELECT uid FROM page_uids)",
" )",
" UNION ALL",
" (",
" SELECT ", jit_orders::SELECT,
" SELECT ", jit_orders::SELECT, crate::trades::ORDER_GAS_COST_COLUMN,
" FROM ", jit_orders::FROM,
" WHERE o.uid IN (SELECT uid FROM page_uids)",
// despite already handling duplicates in phase 1 we need to handle
Expand Down
10 changes: 9 additions & 1 deletion crates/database/src/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ pub struct FullOrder {
pub executed_fee: BigDecimal,
pub executed_fee_token: Address,
pub full_app_data: Option<Vec<u8>>,
/// Total on-chain gas cost (native token wei) attributed to the order,
/// summed across its fills. Populated by the order-detail queries that
/// select it (see [`crate::trades::ORDER_GAS_COST`]); `None` for queries
/// that don't, such as the solvable-orders queries.
#[sqlx(default)]
pub gas_cost: Option<BigDecimal>,
}

impl FullOrder {
Expand Down Expand Up @@ -648,6 +654,7 @@ pub const FROM: &str = "orders o";
const FULL_ORDER_WITH_QUOTE: &str = const_format::concatcp!(
"SELECT ",
SELECT,
crate::trades::ORDER_GAS_COST_COLUMN,
", o_quotes.sell_amount as quote_sell_amount",
", o_quotes.buy_amount as quote_buy_amount",
", o_quotes.gas_amount as quote_gas_amount",
Expand Down Expand Up @@ -713,10 +720,11 @@ pub fn full_orders_in_tx<'a>(
ex: &'a mut PgConnection,
tx_hash: &'a TransactionHash,
) -> BoxStream<'a, Result<FullOrder, sqlx::Error>> {
use crate::trades::ORDER_GAS_COST_COLUMN;
const QUERY: &str = const_format::formatcp!(
r#"
{SETTLEMENT_LOG_INDICES}
SELECT {SELECT}
SELECT {SELECT}{ORDER_GAS_COST_COLUMN}
FROM {FROM}
JOIN trades t ON t.order_uid = o.uid
WHERE
Expand Down
26 changes: 26 additions & 0 deletions crates/database/src/settlements.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
crate::{Address, PgTransaction, TransactionHash},
bigdecimal::BigDecimal,
sqlx::{Executor, PgConnection},
tracing::instrument,
};
Expand Down Expand Up @@ -89,6 +90,31 @@ WHERE block_number = $3 AND log_index = $4
.map(|_| ())
}

/// Stores the actual gas cost (read from the transaction receipt) of a settled
/// transaction. See migration `V115`.
#[instrument(skip_all)]
pub async fn update_settlement_gas(
ex: &mut PgConnection,
block_number: i64,
log_index: i64,
gas_used: BigDecimal,
effective_gas_price: BigDecimal,
) -> Result<(), sqlx::Error> {
const QUERY: &str = r#"
UPDATE settlements
SET gas_used = $1, effective_gas_price = $2
WHERE block_number = $3 AND log_index = $4
;"#;
sqlx::query(QUERY)
.bind(gas_used)
.bind(effective_gas_price)
.bind(block_number)
.bind(log_index)
.execute(ex)
.await?;
Ok(())
}

/// Deletes all database data that referenced the deleted settlement events.
#[instrument(skip_all)]
pub async fn delete(
Expand Down
Loading
Loading