Skip to content

Commit

Permalink
Create auction orders table (#2748)
Browse files Browse the repository at this point in the history
# Description
Fixes #2745

This table is supposed to be populated for each `Auction`. All orders
that are part of the `Auction` will be saved to this table, during
competition.

This information is important so we can reconstruct the whole
competition environment needed for circuit breaker and
on_settlement_event actions.

## How to test
Added unit test. CI.
  • Loading branch information
sunce86 authored May 24, 2024
1 parent 40af2cf commit 13e7491
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/autopilot/src/database/competition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ impl super::Postgres {
.await
.context("settlement_call_data::insert")?;

database::auction_orders::insert(
&mut ex,
competition.auction_id,
competition
.competition_table
.auction
.orders
.iter()
.map(|order| ByteArray(order.0))
.collect::<Vec<_>>()
.as_slice(),
)
.await
.context("auction_orders::insert")?;

ex.commit().await.context("commit")
}

Expand Down
54 changes: 54 additions & 0 deletions crates/database/src/auction_orders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use {
crate::{auction::AuctionId, OrderUid},
sqlx::PgConnection,
};

pub async fn insert(
ex: &mut PgConnection,
auction_id: AuctionId,
orders: &[OrderUid],
) -> Result<(), sqlx::Error> {
const QUERY: &str = r#"INSERT INTO auction_orders (auction_id, order_uids) VALUES ($1, $2);"#;
sqlx::query(QUERY)
.bind(auction_id)
.bind(orders)
.execute(ex)
.await?;
Ok(())
}

pub async fn fetch(
ex: &mut PgConnection,
auction_id: AuctionId,
) -> Result<Option<Vec<OrderUid>>, sqlx::Error> {
const QUERY: &str = r#"SELECT order_uids FROM auction_orders WHERE auction_id = $1;"#;
let row = sqlx::query_scalar(QUERY)
.bind(auction_id)
.fetch_optional(ex)
.await?;
Ok(row)
}

#[cfg(test)]
mod tests {
use {super::*, crate::byte_array::ByteArray, sqlx::Connection};

#[tokio::test]
#[ignore]
async fn postgres_roundtrip() {
let mut db = PgConnection::connect("postgresql://").await.unwrap();
let mut db = db.begin().await.unwrap();
crate::clear_DANGER_(&mut db).await.unwrap();

let auction = vec![ByteArray([1; 56]), ByteArray([2; 56])];

insert(&mut db, 1, &auction).await.unwrap();

let output = fetch(&mut db, 1).await.unwrap();
assert_eq!(output, Some(auction));

// non-existent auction
let output = fetch(&mut db, 2).await.unwrap();
assert!(output.is_none());
}
}
1 change: 1 addition & 0 deletions crates/database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod app_data;
pub mod auction;
pub mod auction_orders;
pub mod auction_participants;
pub mod auction_prices;
pub mod byte_array;
Expand Down
12 changes: 12 additions & 0 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,18 @@ Indexes:
- PRIMARY KEY: btree(`block_number`, `log_index`)
- trade\_order\_uid: btree (`order_uid`, `block_number`, `log_index`)

### auction\_orders

Stores all orders that were included in a given auction. The same order can be included in multiple auctions.

Column | Type | Nullable | Details
------------|---------|----------|--------
auction\_id | bigint | not null | which auction this order was part of
order\_uids | bytea[] | not null | order uids that were included in the auction

Indexes:
- PRIMARY KEY: btree(`auction_uid`)

### Enums

#### executiontime
Expand Down
5 changes: 5 additions & 0 deletions database/sql/V065__auction_orders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- All orders that were part of an auction. An order can appear in multiple auctions.
CREATE TABLE auction_orders (
auction_id bigint PRIMARY KEY,
order_uids bytea[] NOT NULL
);

0 comments on commit 13e7491

Please sign in to comment.