Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Anirudh Suresh authored and Anirudh Suresh committed Feb 13, 2024
1 parent 7b2b62b commit 7a97205
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
21 changes: 9 additions & 12 deletions auction-server/src/api/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,17 @@ pub async fn post_opportunity(

let mut write_lock = store.liquidation_store.opportunities.write().await;

if write_lock.contains_key(&params.permission_key) {
let opportunities_existing = &write_lock[&params.permission_key];
if let Some(opportunities_existing) = write_lock.get_mut(&params.permission_key) {
// check if same opportunity exists in the vector
for opportunity_existing in opportunities_existing {
if *opportunity_existing == opportunity {
for opportunity_existing in opportunities_existing.clone() {
if opportunity_existing == opportunity {
return Err(RestError::BadParameters(
"Duplicate opportunity submission".to_string(),
));
}
}
}

if let Some(x) = write_lock.get_mut(&params.permission_key) {
x.push(opportunity.clone());
opportunities_existing.push(opportunity);
} else {
write_lock.insert(params.permission_key.clone(), vec![opportunity]);
}
Expand Down Expand Up @@ -240,19 +237,19 @@ pub async fn post_bid(
.ok_or(RestError::OpportunityNotFound)?
.clone();

let liquidation = opportunities
let opportunity = opportunities
.iter()
.find(|o| o.id == opportunity_id)
.ok_or(RestError::OpportunityNotFound)?;

// TODO: move this logic to searcher side
if liquidation.bidders.contains(&opportunity_bid.liquidator) {
if opportunity.bidders.contains(&opportunity_bid.liquidator) {
return Err(RestError::BadParameters(
"Liquidator already bid on this opportunity".to_string(),
));
}

let params = match &liquidation.params {
let params = match &opportunity.params {
OpportunityParams::V1(params) => params,
};

Expand Down Expand Up @@ -285,11 +282,11 @@ pub async fn post_bid(
let mut write_guard = store.liquidation_store.opportunities.write().await;
let opportunities = write_guard.get_mut(&opportunity_bid.permission_key);
if let Some(opportunities) = opportunities {
let liquidation = opportunities
let opportunity = opportunities
.iter_mut()
.find(|o| o.id == opportunity_id)
.ok_or(RestError::OpportunityNotFound)?;
liquidation.bidders.insert(opportunity_bid.liquidator);
opportunity.bidders.insert(opportunity_bid.liquidator);
}
Ok(BidResult {
status: "OK".to_string(),
Expand Down
18 changes: 5 additions & 13 deletions auction-server/src/liquidation_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,19 +357,11 @@ pub async fn run_verification_loop(store: Arc<Store>) -> Result<()> {
// set write lock to remove all these opportunities
let mut write_lock = store.liquidation_store.opportunities.write().await;

for id_opp in opps_to_remove {
write_lock
.get_mut(permission_key)
.ok_or(anyhow!("Permission key not found"))?
.retain(|x| x.id != id_opp);
}

if write_lock
.get(permission_key)
.ok_or(anyhow!("Permission key not found"))?
.is_empty()
{
write_lock.remove(permission_key);
if let Some(opportunities) = write_lock.get_mut(permission_key) {
opportunities.retain(|x| !opps_to_remove.contains(&x.id));
if opportunities.is_empty() {
write_lock.remove(permission_key);
}
}

// release the write lock
Expand Down

0 comments on commit 7a97205

Please sign in to comment.