Skip to content

Commit

Permalink
Make all endpoints return json
Browse files Browse the repository at this point in the history
  • Loading branch information
m30m committed Feb 6, 2024
1 parent 21fde44 commit 1360094
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
10 changes: 8 additions & 2 deletions auction-server/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use {
crate::{
api::{
bid::Bid,
bid::{
Bid,
BidResult,
},
liquidation::{
OpportunityBid,
OpportunityParamsWithId,
Expand Down Expand Up @@ -172,8 +175,11 @@ pub async fn start_server(run_options: RunOptions) -> Result<()> {
schemas(OpportunityParams),
schemas(OpportunityParamsWithId),
schemas(TokenQty),
schemas(BidResult),
schemas(ErrorBodyResponse),
responses(ErrorBodyResponse)
responses(ErrorBodyResponse),
responses(OpportunityParamsWithId),
responses(BidResult)
),
tags(
(name = "PER Auction", description = "Pyth Express Relay Auction Server")
Expand Down
26 changes: 20 additions & 6 deletions auction-server/src/api/bid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use {
Serialize,
},
std::sync::Arc,
utoipa::ToSchema,
utoipa::{
ToResponse,
ToSchema,
},
};

#[derive(Serialize, Deserialize, ToSchema, Clone)]
Expand All @@ -55,7 +58,7 @@ pub struct Bid {
pub amount: U256,
}

pub async fn handle_bid(store: Arc<Store>, bid: Bid) -> Result<String, RestError> {
pub async fn handle_bid(store: Arc<Store>, bid: Bid) -> Result<(), RestError> {
let chain_store = store
.chains
.get(&bid.chain_id)
Expand Down Expand Up @@ -99,27 +102,38 @@ pub async fn handle_bid(store: Arc<Store>, bid: Bid) -> Result<String, RestError
calldata: bid.calldata.clone(),
bid: bid.amount,
});
Ok("OK".to_string())
Ok(())
}

#[derive(Serialize, Deserialize, ToResponse, ToSchema, Clone)]
pub struct BidResult {
pub status: String,
}

/// Bid on a specific permission key for a specific chain.
///
/// Your bid will be simulated and verified by the server. Depending on the outcome of the auction, a transaction
/// containing the contract call will be sent to the blockchain expecting the bid amount to be paid after the call.
#[utoipa::path(post, path = "/v1/bid", request_body = Bid, responses(
(status = 200, description = "Bid was placed succesfully", body = String),
(status = 200, description = "Bid was placed succesfully", body = BidResult, example = json!({"status": "OK"})),
(status = 400, response = ErrorBodyResponse),
(status = 404, description = "Chain id was not found", body = ErrorBodyResponse),
),)]
pub async fn bid(
State(store): State<Arc<Store>>,
Json(bid): Json<Bid>,
) -> Result<String, RestError> {
) -> Result<Json<BidResult>, RestError> {
let bid = bid.clone();
store
.chains
.get(&bid.chain_id)
.ok_or(RestError::InvalidChainId)?;

handle_bid(store, bid).await
match handle_bid(store, bid).await {
Ok(_) => Ok(BidResult {
status: "OK".to_string(),
}
.into()),
Err(e) => Err(e),
}
}
30 changes: 20 additions & 10 deletions auction-server/src/api/liquidation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use {
crate::{
api::{
bid::handle_bid,
bid::{
handle_bid,
BidResult,
},
ErrorBodyResponse,
RestError,
},
Expand Down Expand Up @@ -47,14 +50,15 @@ use {
},
utoipa::{
IntoParams,
ToResponse,
ToSchema,
},
uuid::Uuid,
};


/// Similar to OpportunityParams, but with the opportunity id included.
#[derive(Serialize, Deserialize, ToSchema, Clone)]
#[derive(Serialize, Deserialize, ToSchema, Clone, ToResponse)]
pub struct OpportunityParamsWithId {
/// The opportunity unique id
#[schema(example = "f47ac10b-58cc-4372-a567-0e02b2c3d479", value_type=String)]
Expand All @@ -69,14 +73,14 @@ pub struct OpportunityParamsWithId {
/// The opportunity will be verified by the server. If the opportunity is valid, it will be stored in the database
/// and will be available for bidding.
#[utoipa::path(post, path = "/v1/liquidation/opportunity", request_body = OpportunityParams, responses(
(status = 200, description = "Opportunity was stored succesfuly with the returned uuid", body = String),
(status = 200, description = "The created opportunity", body = OpportunityParamsWithId),
(status = 400, response = ErrorBodyResponse),
(status = 404, description = "Chain id was not found", body = ErrorBodyResponse),
),)]
pub async fn post_opportunity(
State(store): State<Arc<Store>>,
Json(versioned_params): Json<OpportunityParams>,
) -> Result<String, RestError> {
) -> Result<Json<OpportunityParamsWithId>, RestError> {
let params = match versioned_params.clone() {
OpportunityParams::V1(params) => params,
};
Expand All @@ -92,7 +96,7 @@ pub async fn post_opportunity(
.duration_since(UNIX_EPOCH)
.map_err(|_| RestError::BadParameters("Invalid system time".to_string()))?
.as_secs() as UnixTimestamp,
params: versioned_params,
params: versioned_params.clone(),
bidders: Default::default(),
};

Expand All @@ -107,8 +111,11 @@ pub async fn post_opportunity(
.await
.insert(params.permission_key.clone(), opportunity);

//TODO: return json
Ok(id.to_string())
Ok(OpportunityParamsWithId {
opportunity_id: id,
params: versioned_params,
}
.into())
}


Expand Down Expand Up @@ -184,14 +191,14 @@ pub struct OpportunityBid {

/// Bid on liquidation opportunity
#[utoipa::path(post, path = "/v1/liquidation/bid", request_body=OpportunityBid, responses(
(status = 200, description = "Bid Result", body = String),
(status = 200, description = "Bid Result", body = BidResult, example = json!({"status": "OK"})),
(status = 400, response = ErrorBodyResponse),
(status = 404, description = "Opportunity or chain id was not found", body = ErrorBodyResponse),
),)]
pub async fn post_bid(
State(store): State<Arc<Store>>,
Json(opportunity_bid): Json<OpportunityBid>,
) -> Result<String, RestError> {
) -> Result<Json<BidResult>, RestError> {
let opportunity = store
.liquidation_store
.opportunities
Expand Down Expand Up @@ -250,7 +257,10 @@ pub async fn post_bid(
if let Some(liquidation) = liquidation {
liquidation.bidders.insert(opportunity_bid.liquidator);
}
Ok("OK".to_string())
Ok(BidResult {
status: "OK".to_string(),
}
.into())
}
Err(e) => match e {
RestError::SimulationError { result, reason } => {
Expand Down

0 comments on commit 1360094

Please sign in to comment.