|
5 | 5 | database::{Address, TransactionHash, byte_array::ByteArray, order_events}, |
6 | 6 | e2e::setup::Db, |
7 | 7 | model::order::OrderUid, |
| 8 | + sqlx::PgConnection, |
8 | 9 | std::ops::DerefMut, |
9 | 10 | }; |
10 | 11 |
|
@@ -41,57 +42,36 @@ pub struct AuctionTransaction { |
41 | 42 | pub solution_uid: i64, |
42 | 43 | } |
43 | 44 |
|
44 | | -#[allow(dead_code)] |
45 | | -#[derive(Clone, Debug)] |
46 | | -pub struct Cip20Data { |
47 | | - pub txs: Vec<AuctionTransaction>, |
48 | | - pub participants: Vec<database::solver_competition_v2::AuctionParticipant>, |
49 | | - pub prices: Vec<database::auction_prices::AuctionPrice>, |
50 | | - pub reference_scores: Vec<database::reference_scores::Score>, |
51 | | - pub competition: serde_json::Value, |
| 45 | +pub async fn auction_participants( |
| 46 | + ex: &mut PgConnection, |
| 47 | + auction_id: i64, |
| 48 | +) -> anyhow::Result<Vec<Address>> { |
| 49 | + const QUERY: &str = r#" |
| 50 | + SELECT DISTINCT ps.solver |
| 51 | + FROM proposed_solutions ps |
| 52 | + WHERE ps.auction_id = $1 |
| 53 | + "#; |
| 54 | + Ok(sqlx::query_as(QUERY).bind(auction_id).fetch_all(ex).await?) |
52 | 55 | } |
53 | 56 |
|
54 | | -/// Returns `Some(data)` if the all the expected CIP-20 data has been indexed |
55 | | -/// for the most recent `auction_id` from `settlements` table. |
56 | | -pub async fn most_recent_cip_20_data(db: &Db) -> Option<Cip20Data> { |
57 | | - let mut db = db.acquire().await.unwrap(); |
58 | | - |
59 | | - const LAST_AUCTION_ID: &str = "SELECT auction_id FROM settlements WHERE auction_id IS NOT \ |
60 | | - NULL ORDER BY auction_id DESC LIMIT 1"; |
61 | | - let auction_id: i64 = sqlx::query_scalar(LAST_AUCTION_ID) |
62 | | - .fetch_optional(db.deref_mut()) |
63 | | - .await |
64 | | - .unwrap()?; |
65 | | - |
66 | | - const TX_QUERY: &str = r" |
67 | | -SELECT * FROM settlements WHERE auction_id = $1"; |
68 | | - |
69 | | - let txs: Vec<AuctionTransaction> = sqlx::query_as(TX_QUERY) |
70 | | - .bind(auction_id) |
71 | | - .fetch_all(db.deref_mut()) |
72 | | - .await |
73 | | - .ok()?; |
| 57 | +pub async fn auction_prices( |
| 58 | + ex: &mut PgConnection, |
| 59 | + auction_id: i64, |
| 60 | +) -> anyhow::Result<Vec<database::auction_prices::AuctionPrice>> { |
| 61 | + const QUERY: &str = "SELECT * FROM auction_prices WHERE auction_id = $1"; |
| 62 | + Ok(sqlx::query_as(QUERY).bind(auction_id).fetch_all(ex).await?) |
| 63 | +} |
74 | 64 |
|
75 | | - let participants = |
76 | | - database::solver_competition_v2::fetch_auction_participants(&mut db, auction_id) |
77 | | - .await |
78 | | - .unwrap(); |
79 | | - let prices = database::auction_prices::fetch(&mut db, auction_id) |
80 | | - .await |
81 | | - .unwrap(); |
82 | | - let reference_scores = database::reference_scores::fetch(&mut db, auction_id) |
83 | | - .await |
84 | | - .unwrap(); |
85 | | - let competition = database::solver_competition::load_by_id(&mut db, auction_id) |
86 | | - .await |
87 | | - .unwrap()? |
88 | | - .json; |
| 65 | +pub async fn reference_scores( |
| 66 | + ex: &mut PgConnection, |
| 67 | + auction_id: i64, |
| 68 | +) -> anyhow::Result<Vec<database::reference_scores::Score>> { |
| 69 | + const QUERY: &str = "SELECT * FROM reference_scores WHERE auction_id = $1"; |
| 70 | + Ok(sqlx::query_as(QUERY).bind(auction_id).fetch_all(ex).await?) |
| 71 | +} |
89 | 72 |
|
90 | | - Some(Cip20Data { |
91 | | - txs, |
92 | | - participants, |
93 | | - prices, |
94 | | - reference_scores, |
95 | | - competition, |
96 | | - }) |
| 73 | +pub async fn latest_auction_id(ex: &mut PgConnection) -> anyhow::Result<Option<i64>> { |
| 74 | + const QUERY: &str = "SELECT auction_id FROM settlements WHERE auction_id IS NOT NULL ORDER BY \ |
| 75 | + auction_id DESC LIMIT 1"; |
| 76 | + Ok(sqlx::query_scalar(QUERY).fetch_optional(ex).await?) |
97 | 77 | } |
0 commit comments