-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Websocket support #18
Changes from all commits
6181113
fc6b871
105f9a6
c94169b
89f1747
20fbd4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ use { | |
handle_bid, | ||
BidResult, | ||
}, | ||
ws::UpdateEvent::NewOpportunity, | ||
ErrorBodyResponse, | ||
RestError, | ||
}, | ||
|
@@ -57,22 +58,33 @@ use { | |
uuid::Uuid, | ||
}; | ||
|
||
|
||
/// Similar to OpportunityParams, but with the opportunity id included. | ||
#[derive(Serialize, Deserialize, ToSchema, Clone, ToResponse)] | ||
pub struct OpportunityParamsWithId { | ||
pub struct OpportunityParamsWithMetadata { | ||
/// The opportunity unique id | ||
#[schema(example = "f47ac10b-58cc-4372-a567-0e02b2c3d479", value_type=String)] | ||
opportunity_id: Uuid, | ||
/// Creation time of the opportunity | ||
#[schema(example = "1700000000")] | ||
creation_time: UnixTimestamp, | ||
/// opportunity data | ||
#[serde(flatten)] | ||
params: OpportunityParams, | ||
} | ||
|
||
impl Into<OpportunityParamsWithId> for LiquidationOpportunity { | ||
fn into(self) -> OpportunityParamsWithId { | ||
OpportunityParamsWithId { | ||
impl OpportunityParamsWithMetadata { | ||
pub fn get_chain_id(&self) -> &ChainId { | ||
match &self.params { | ||
OpportunityParams::V1(params) => ¶ms.chain_id, | ||
} | ||
} | ||
} | ||
|
||
impl Into<OpportunityParamsWithMetadata> for LiquidationOpportunity { | ||
fn into(self) -> OpportunityParamsWithMetadata { | ||
OpportunityParamsWithMetadata { | ||
opportunity_id: self.id, | ||
creation_time: self.creation_time, | ||
params: self.params, | ||
} | ||
} | ||
|
@@ -90,7 +102,7 @@ impl Into<OpportunityParamsWithId> for LiquidationOpportunity { | |
pub async fn post_opportunity( | ||
State(store): State<Arc<Store>>, | ||
Json(versioned_params): Json<OpportunityParams>, | ||
) -> Result<Json<OpportunityParamsWithId>, RestError> { | ||
) -> Result<Json<OpportunityParamsWithMetadata>, RestError> { | ||
let params = match versioned_params.clone() { | ||
OpportunityParams::V1(params) => params, | ||
}; | ||
|
@@ -119,30 +131,37 @@ pub async fn post_opportunity( | |
|
||
if let Some(opportunities_existing) = write_lock.get_mut(¶ms.permission_key) { | ||
// check if same opportunity exists in the vector | ||
for opportunity_existing in opportunities_existing.clone() { | ||
if opportunity_existing == opportunity { | ||
for opportunity_existing in opportunities_existing.iter() { | ||
if opportunity_existing == &opportunity { | ||
return Err(RestError::BadParameters( | ||
"Duplicate opportunity submission".to_string(), | ||
)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The clone in iteration above seems a bit off. I don't know whether it's unnecessary but if you use a map then |
||
} | ||
|
||
opportunities_existing.push(opportunity); | ||
opportunities_existing.push(opportunity.clone()); | ||
} else { | ||
write_lock.insert(params.permission_key.clone(), vec![opportunity]); | ||
write_lock.insert(params.permission_key.clone(), vec![opportunity.clone()]); | ||
} | ||
|
||
store | ||
.ws | ||
.broadcast_sender | ||
.send(NewOpportunity(opportunity.clone().into())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of cloning multiple times, maybe you could push opportunity into opportunities_existing, and then call
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also don't need to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the first comment, we technically need opportunity in both places and both structs need to own them in the end so we need to clone. I didn't understand the second comment, we implement the Into trait so we don't have to do this here. |
||
.map_err(|e| { | ||
tracing::error!("Failed to send update: {}", e); | ||
RestError::TemporarilyUnavailable | ||
})?; | ||
|
||
tracing::debug!("number of permission keys: {}", write_lock.len()); | ||
tracing::debug!( | ||
"number of opportunities for key: {}", | ||
write_lock[¶ms.permission_key].len() | ||
); | ||
|
||
Ok(OpportunityParamsWithId { | ||
opportunity_id: id, | ||
params: versioned_params, | ||
} | ||
.into()) | ||
let opportunity_with_metadata: OpportunityParamsWithMetadata = opportunity.into(); | ||
|
||
Ok(opportunity_with_metadata.into()) | ||
} | ||
|
||
|
||
|
@@ -162,8 +181,8 @@ params(ChainIdQueryParams))] | |
pub async fn get_opportunities( | ||
State(store): State<Arc<Store>>, | ||
query_params: Query<ChainIdQueryParams>, | ||
) -> Result<axum::Json<Vec<OpportunityParamsWithId>>, RestError> { | ||
let opportunities: Vec<OpportunityParamsWithId> = store | ||
) -> Result<axum::Json<Vec<OpportunityParamsWithMetadata>>, RestError> { | ||
let opportunities: Vec<OpportunityParamsWithMetadata> = store | ||
.liquidation_store | ||
.opportunities | ||
.read() | ||
|
@@ -177,7 +196,7 @@ pub async fn get_opportunities( | |
.clone() | ||
.into() | ||
}) | ||
.filter(|params_with_id: &OpportunityParamsWithId| { | ||
.filter(|params_with_id: &OpportunityParamsWithMetadata| { | ||
let params = match ¶ms_with_id.params { | ||
OpportunityParams::V1(params) => params, | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump the auction-server version as well?