Skip to content

Commit 5d70e15

Browse files
authored
Update the bids state which are not submitted to chain for an auction (#108)
1 parent 6ddd024 commit 5d70e15

8 files changed

+120
-46
lines changed

auction-server/.sqlx/query-6ffd6feaa2f4f9163a37c6fe7ec414372506acd0ff572594702a491c17171cda.json renamed to auction-server/.sqlx/query-1a07b19befac7096f62aa512f2c33f6282c0565025ba9ede3325705de09ea145.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auction-server/.sqlx/query-33f3dda525887ab92aef0d37e718b87f933cb30d568abbc87b8112e7ab30c1bf.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auction-server/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auction-server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "auction-server"
3-
version = "0.9.3"
3+
version = "0.9.4"
44
edition = "2021"
55
license-file = "license.txt"
66

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX bid_creation_time_idx;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX bid_creation_time_idx ON bid (creation_time);

auction-server/src/auction.rs

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,52 @@ async fn conclude_submitted_auctions(store: Arc<Store>, chain_id: String) {
323323
}
324324
}
325325

326+
async fn broadcast_submitted_bids(
327+
store: Arc<Store>,
328+
bids: Vec<SimulatedBid>,
329+
tx_hash: H256,
330+
auction: models::Auction,
331+
) {
332+
join_all(bids.iter().enumerate().map(|(i, bid)| {
333+
store.broadcast_bid_status_and_update(
334+
bid.to_owned(),
335+
BidStatus::Submitted {
336+
result: tx_hash,
337+
index: i as u32,
338+
},
339+
Some(&auction),
340+
)
341+
}))
342+
.await;
343+
}
344+
345+
async fn broadcast_lost_bids(
346+
store: Arc<Store>,
347+
bids: Vec<SimulatedBid>,
348+
submitted_bids: Vec<SimulatedBid>,
349+
tx_hash: Option<H256>,
350+
auction: Option<&models::Auction>,
351+
) {
352+
join_all(bids.iter().filter_map(|bid| {
353+
if submitted_bids
354+
.iter()
355+
.any(|submitted_bid| bid.id == submitted_bid.id)
356+
{
357+
return None;
358+
}
359+
360+
Some(store.broadcast_bid_status_and_update(
361+
bid.clone(),
362+
BidStatus::Lost {
363+
result: tx_hash,
364+
index: None,
365+
},
366+
auction,
367+
))
368+
}))
369+
.await;
370+
}
371+
326372
async fn submit_auction_for_bids<'a>(
327373
bids: Vec<SimulatedBid>,
328374
bid_collection_time: OffsetDateTime,
@@ -349,18 +395,7 @@ async fn submit_auction_for_bids<'a>(
349395
let winner_bids =
350396
get_winner_bids(&bids, permission_key.clone(), store.clone(), chain_store).await?;
351397
if winner_bids.is_empty() {
352-
for bid in bids.iter() {
353-
store
354-
.broadcast_bid_status_and_update(
355-
bid.clone(),
356-
BidStatus::Lost {
357-
result: None,
358-
index: None,
359-
},
360-
None,
361-
)
362-
.await?;
363-
}
398+
broadcast_lost_bids(store.clone(), bids, winner_bids, None, None).await;
364399
return Ok(());
365400
}
366401

@@ -388,24 +423,21 @@ async fn submit_auction_for_bids<'a>(
388423
Ok(tx_hash) => {
389424
tracing::debug!("Submitted transaction: {:?}", tx_hash);
390425
auction = store.submit_auction(auction, tx_hash).await?;
391-
join_all(winner_bids.iter().enumerate().map(|(i, bid)| {
392-
// TODO update the status of bids to lost for those that are not going to be submitted to the chain for this auction
393-
let (index, store, bid, auction) =
394-
(i as u32, store.clone(), bid.clone(), auction.clone());
395-
async move {
396-
store
397-
.broadcast_bid_status_and_update(
398-
bid,
399-
BidStatus::Submitted {
400-
result: tx_hash,
401-
index,
402-
},
403-
Some(&auction),
404-
)
405-
.await
406-
}
407-
}))
408-
.await;
426+
tokio::join!(
427+
broadcast_submitted_bids(
428+
store.clone(),
429+
winner_bids.clone(),
430+
tx_hash,
431+
auction.clone()
432+
),
433+
broadcast_lost_bids(
434+
store.clone(),
435+
bids,
436+
winner_bids,
437+
Some(tx_hash),
438+
Some(&auction)
439+
),
440+
);
409441
}
410442
Err(err) => {
411443
tracing::error!("Transaction failed to submit: {:?}", err);

auction-server/src/state.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -747,20 +747,33 @@ impl Store {
747747
}
748748
BidStatus::Lost { result: _, index } => {
749749
if let Some(auction) = auction {
750-
query_result = sqlx::query!(
751-
"UPDATE bid SET status = $1, bundle_index = $2, auction_id = $3 WHERE id = $4 AND status = 'submitted'",
752-
updated_status as _,
753-
index.map(|i| i as i32),
754-
auction.id,
755-
bid.id
756-
)
757-
.execute(&self.db)
758-
.await?;
750+
match index {
751+
Some(index) => {
752+
query_result = sqlx::query!(
753+
"UPDATE bid SET status = $1, bundle_index = $2, auction_id = $3 WHERE id = $4 AND status = 'submitted'",
754+
updated_status as _,
755+
index as i32,
756+
auction.id,
757+
bid.id
758+
)
759+
.execute(&self.db)
760+
.await?;
761+
}
762+
None => {
763+
query_result = sqlx::query!(
764+
"UPDATE bid SET status = $1, auction_id = $2 WHERE id = $3 AND status = 'pending'",
765+
updated_status as _,
766+
auction.id,
767+
bid.id
768+
)
769+
.execute(&self.db)
770+
.await?;
771+
}
772+
}
759773
} else {
760774
query_result = sqlx::query!(
761-
"UPDATE bid SET status = $1, bundle_index = $2 WHERE id = $3 AND status = 'pending'",
775+
"UPDATE bid SET status = $1 WHERE id = $2 AND status = 'pending'",
762776
updated_status as _,
763-
index.map(|i| i as i32),
764777
bid.id
765778
)
766779
.execute(&self.db)

0 commit comments

Comments
 (0)