Skip to content

Commit

Permalink
Add ws support for liquidation bids
Browse files Browse the repository at this point in the history
  • Loading branch information
m30m committed Feb 27, 2024
1 parent d52fa4f commit 21687c6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
4 changes: 2 additions & 2 deletions auction-server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub async fn start_api(run_options: RunOptions, store: Arc<Store>) -> Result<()>
paths(
bid::bid,
liquidation::post_opportunity,
liquidation::post_bid,
liquidation::bid,
liquidation::get_opportunities,
),
components(
Expand Down Expand Up @@ -190,7 +190,7 @@ pub async fn start_api(run_options: RunOptions, store: Arc<Store>) -> Result<()>
)
.route(
"/v1/liquidation/opportunities/:opportunity_id/bids",
post(liquidation::post_bid),
post(liquidation::bid),
)
.route("/v1/ws", get(ws::ws_route_handler))
.route("/live", get(live))
Expand Down
29 changes: 21 additions & 8 deletions auction-server/src/api/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use {
},
state::{
LiquidationOpportunity,
OpportunityId,
OpportunityParams,
Store,
UnixTimestamp,
Expand Down Expand Up @@ -58,12 +59,13 @@ use {
uuid::Uuid,
};


/// Similar to OpportunityParams, but with the opportunity id included.
#[derive(Serialize, Deserialize, ToSchema, Clone, ToResponse)]
pub struct OpportunityParamsWithMetadata {
/// The opportunity unique id
#[schema(example = "f47ac10b-58cc-4372-a567-0e02b2c3d479", value_type=String)]
opportunity_id: Uuid,
opportunity_id: OpportunityId,
/// Creation time of the opportunity
#[schema(example = 1700000000, value_type=i64)]
creation_time: UnixTimestamp,
Expand Down Expand Up @@ -240,11 +242,26 @@ pub struct OpportunityBid {
(status = 400, response = ErrorBodyResponse),
(status = 404, description = "Opportunity or chain id was not found", body = ErrorBodyResponse),
),)]
pub async fn post_bid(
pub async fn bid(
State(store): State<Arc<Store>>,
Path(opportunity_id): Path<Uuid>,
Path(opportunity_id): Path<OpportunityId>,
Json(opportunity_bid): Json<OpportunityBid>,
) -> Result<Json<BidResult>, RestError> {
match handle_liquidation_bid(store, opportunity_id, &opportunity_bid).await {
Ok(id) => Ok(BidResult {
status: "OK".to_string(),
id,
}
.into()),
Err(e) => Err(e),
}
}

pub async fn handle_liquidation_bid(
store: Arc<Store>,
opportunity_id: OpportunityId,
opportunity_bid: &OpportunityBid,
) -> Result<Uuid, RestError> {
let opportunities = store
.liquidation_store
.opportunities
Expand Down Expand Up @@ -303,11 +320,7 @@ pub async fn post_bid(
.ok_or(RestError::OpportunityNotFound)?;
opportunity.bidders.insert(opportunity_bid.liquidator);
}
Ok(BidResult {
status: "OK".to_string(),
id,
}
.into())
Ok(id)
}
Err(e) => match e {
RestError::SimulationError { result, reason } => {
Expand Down
45 changes: 39 additions & 6 deletions auction-server/src/api/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use {
handle_bid,
Bid,
},
liquidation::OpportunityParamsWithMetadata,
liquidation::{
handle_liquidation_bid,
OpportunityBid,
OpportunityParamsWithMetadata,
},
},
config::ChainId,
server::{
Expand All @@ -15,6 +19,7 @@ use {
state::{
BidId,
BidStatus,
OpportunityId,
Store,
},
},
Expand Down Expand Up @@ -66,7 +71,7 @@ pub struct WsState {
pub broadcast_receiver: broadcast::Receiver<UpdateEvent>,
}

#[derive(Deserialize, Debug, Clone, ToSchema)]
#[derive(Deserialize, Clone, ToSchema)]
#[serde(tag = "method", content = "params")]
pub enum ClientMessage {
#[serde(rename = "subscribe")]
Expand All @@ -79,11 +84,18 @@ pub enum ClientMessage {
#[schema(value_type = Vec<String>)]
chain_ids: Vec<ChainId>,
},
#[serde(rename = "submit_bid")]
SubmitBid { bid: Bid },
#[serde(rename = "post_bid")]
PostBid { bid: Bid },

#[serde(rename = "post_liquidation_bid")]
PostLiquidationBid {
#[schema(value_type = String)]
opportunity_id: OpportunityId,
opportunity_bid: OpportunityBid,
},
}

#[derive(Deserialize, Debug, Clone, ToSchema)]
#[derive(Deserialize, Clone, ToSchema)]
pub struct ClientRequest {
id: String,
#[serde(flatten)]
Expand Down Expand Up @@ -318,7 +330,7 @@ impl Subscriber {
.retain(|chain_id| !chain_ids.contains(chain_id));
ok_response
}
ClientMessage::SubmitBid { bid } => {
ClientMessage::PostBid { bid } => {
match handle_bid(self.store.clone(), bid).await {
Ok(bid_id) => {
self.bid_ids.insert(bid_id);
Expand All @@ -330,6 +342,27 @@ impl Subscriber {
},
}
}
ClientMessage::PostLiquidationBid {
opportunity_bid,
opportunity_id,
} => {
match handle_liquidation_bid(
self.store.clone(),
opportunity_id,
&opportunity_bid,
)
.await
{
Ok(bid_id) => {
self.bid_ids.insert(bid_id);
ok_response
}
Err(e) => ServerResultResponse {
id: Some(id),
result: ServerResultMessage::Err(e.to_status_and_message().1),
},
}
}
}
}
};
Expand Down
3 changes: 2 additions & 1 deletion auction-server/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ pub enum OpportunityParams {
V1(OpportunityParamsV1),
}

pub type OpportunityId = Uuid;
#[derive(Clone, PartialEq)]
pub struct LiquidationOpportunity {
pub id: Uuid,
pub id: OpportunityId,
pub creation_time: UnixTimestamp,
pub params: OpportunityParams,
pub bidders: HashSet<Address>,
Expand Down

0 comments on commit 21687c6

Please sign in to comment.