Skip to content

Commit

Permalink
Make ws apis return the exact same result as the http apis
Browse files Browse the repository at this point in the history
  • Loading branch information
m30m committed Feb 29, 2024
1 parent af66e2b commit 6a07ab4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
9 changes: 6 additions & 3 deletions auction-server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
bid::BidResult,
liquidation::OpportunityParamsWithMetadata,
ws::{
APIResposne,
ClientMessage,
ClientRequest,
ServerResultMessage,
Expand Down Expand Up @@ -82,7 +83,7 @@ pub enum RestError {
}

impl RestError {
pub fn to_status_and_message(self) -> (StatusCode, String) {
pub fn to_status_and_message(&self) -> (StatusCode, String) {
match self {
RestError::BadParameters(msg) => {
(StatusCode::BAD_REQUEST, format!("Bad parameters: {}", msg))
Expand Down Expand Up @@ -134,17 +135,19 @@ pub async fn live() -> Response {


pub async fn start_api(run_options: RunOptions, store: Arc<Store>) -> Result<()> {
// Make sure functions included in the paths section have distinct names, otherwise some api generators will fail
#[derive(OpenApi)]
#[openapi(
paths(
bid::bid,
bid::bid_status,
liquidation::post_opportunity,
liquidation::bid,
liquidation::liquidation_bid,
liquidation::get_opportunities,
),
components(
schemas(
APIResposne,
Bid,
BidStatus,
BidResult,
Expand Down Expand Up @@ -188,7 +191,7 @@ pub async fn start_api(run_options: RunOptions, store: Arc<Store>) -> Result<()>
)
.route(
"/v1/liquidation/opportunities/:opportunity_id/bids",
post(liquidation::bid),
post(liquidation::liquidation_bid),
)
.route("/v1/ws", get(ws::ws_route_handler))
.route("/live", get(live))
Expand Down
8 changes: 3 additions & 5 deletions auction-server/src/api/bid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use {
},
Json,
},
ethers::signers::Signer,
serde::{
Deserialize,
Serialize,
Expand Down Expand Up @@ -56,11 +55,10 @@ pub async fn bid(
State(store): State<Arc<Store>>,
Json(bid): Json<Bid>,
) -> Result<Json<BidResult>, RestError> {
store
.chains
.get(&bid.chain_id)
.ok_or(RestError::InvalidChainId)?;
process_bid(store, bid).await
}

pub async fn process_bid(store: Arc<Store>, bid: Bid) -> Result<Json<BidResult>, RestError> {
match handle_bid(store, bid).await {
Ok(id) => Ok(BidResult {
status: "OK".to_string(),
Expand Down
12 changes: 10 additions & 2 deletions auction-server/src/api/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,20 @@ pub async fn get_opportunities(
(status = 400, response = ErrorBodyResponse),
(status = 404, description = "Opportunity or chain id was not found", body = ErrorBodyResponse),
),)]
pub async fn bid(
pub async fn liquidation_bid(
State(store): State<Arc<Store>>,
Path(opportunity_id): Path<OpportunityId>,
Json(opportunity_bid): Json<OpportunityBid>,
) -> Result<Json<BidResult>, RestError> {
match handle_liquidation_bid(store, opportunity_id, &opportunity_bid).await {
process_liquidation_bid(store, opportunity_id, &opportunity_bid).await
}

pub async fn process_liquidation_bid(
store: Arc<Store>,
opportunity_id: OpportunityId,
opportunity_bid: &OpportunityBid,
) -> Result<Json<BidResult>, RestError> {
match handle_liquidation_bid(store, opportunity_id, opportunity_bid).await {
Ok(id) => Ok(BidResult {
status: "OK".to_string(),
id,
Expand Down
58 changes: 38 additions & 20 deletions auction-server/src/api/ws.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use {
crate::{
api::liquidation::OpportunityParamsWithMetadata,
auction::{
handle_bid,
Bid,
api::{
bid::{
process_bid,
BidResult,
},
liquidation::{
process_liquidation_bid,
OpportunityParamsWithMetadata,
},
},
auction::Bid,
config::ChainId,
liquidation_adapter::{
handle_liquidation_bid,
OpportunityBid,
},
liquidation_adapter::OpportunityBid,
server::{
EXIT_CHECK_INTERVAL,
SHOULD_EXIT,
Expand Down Expand Up @@ -112,18 +115,23 @@ pub enum ServerUpdateResponse {
BidStatusUpdate { id: BidId, status: BidStatus },
}

#[derive(Serialize, Debug, Clone, ToSchema)]
#[derive(Serialize, Clone, ToSchema)]
#[serde(untagged)]
pub enum APIResposne {
BidResult(BidResult),
}
#[derive(Serialize, Clone, ToSchema)]
#[serde(tag = "status", content = "result")]
pub enum ServerResultMessage {
#[serde(rename = "success")]
Success,
Success(Option<APIResposne>),
#[serde(rename = "error")]
Err(String),
}

/// This enum is used to send the result for a specific client request with the same id
/// id is only None when the client message is invalid
#[derive(Serialize, Debug, Clone, ToSchema)]
#[derive(Serialize, Clone, ToSchema)]
pub struct ServerResultResponse {
id: Option<String>,
#[serde(flatten)]
Expand Down Expand Up @@ -297,7 +305,7 @@ impl Subscriber {
Ok(ClientRequest { msg, id }) => {
let ok_response = ServerResultResponse {
id: Some(id.clone()),
result: ServerResultMessage::Success,
result: ServerResultMessage::Success(None),
};
match msg {
ClientMessage::Subscribe { chain_ids } => {
Expand Down Expand Up @@ -329,10 +337,15 @@ impl Subscriber {
ok_response
}
ClientMessage::PostBid { bid } => {
match handle_bid(self.store.clone(), bid).await {
Ok(bid_id) => {
self.bid_ids.insert(bid_id);
ok_response
match process_bid(self.store.clone(), bid).await {
Ok(bid_result) => {
self.bid_ids.insert(bid_result.id);
ServerResultResponse {
id: Some(id.clone()),
result: ServerResultMessage::Success(Some(
APIResposne::BidResult(bid_result.0),
)),
}
}
Err(e) => ServerResultResponse {
id: Some(id),
Expand All @@ -344,16 +357,21 @@ impl Subscriber {
opportunity_bid,
opportunity_id,
} => {
match handle_liquidation_bid(
match process_liquidation_bid(
self.store.clone(),
opportunity_id,
&opportunity_bid,
)
.await
{
Ok(bid_id) => {
self.bid_ids.insert(bid_id);
ok_response
Ok(bid_result) => {
self.bid_ids.insert(bid_result.id);
ServerResultResponse {
id: Some(id.clone()),
result: ServerResultMessage::Success(Some(
APIResposne::BidResult(bid_result.0),
)),
}
}
Err(e) => ServerResultResponse {
id: Some(id),
Expand Down

0 comments on commit 6a07ab4

Please sign in to comment.