Skip to content

Commit

Permalink
Fix svm duplicate opportunity (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
danimhr authored Nov 20, 2024
1 parent 52abf57 commit 24b1ba6
Show file tree
Hide file tree
Showing 20 changed files with 211 additions and 268 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

12 changes: 12 additions & 0 deletions auction-server/src/opportunity/entities/opportunity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct OpportunityCoreFields<T: TokenAmount> {
pub sell_tokens: Vec<T>,
pub buy_tokens: Vec<T>,
pub creation_time: OffsetDateTime,
pub refresh_time: OffsetDateTime,
}

impl<T: TokenAmount> OpportunityCoreFields<T> {
Expand All @@ -46,6 +47,7 @@ impl<T: TokenAmount> OpportunityCoreFields<T> {
sell_tokens: val.sell_tokens,
buy_tokens: val.buy_tokens,
creation_time: OffsetDateTime::now_utc(),
refresh_time: OffsetDateTime::now_utc(),
}
}
}
Expand All @@ -58,6 +60,13 @@ pub struct OpportunityCoreFieldsCreate<T: TokenAmount> {
pub buy_tokens: Vec<T>,
}

#[derive(Debug, Clone)]
pub enum OpportunityComparison {
New,
Duplicate,
NeedsRefresh,
}

// TODO Think more about structure. Isn't it better to have a generic Opportunity struct with a field of type OpportunityParams?
pub trait Opportunity:
Debug
Expand All @@ -80,6 +89,9 @@ pub trait Opportunity:
fn get_key(&self) -> OpportunityKey {
OpportunityKey(self.chain_id.clone(), self.permission_key.clone())
}

fn compare(&self, other: &Self::OpportunityCreate) -> OpportunityComparison;
fn refresh(&mut self);
}

pub trait OpportunityCreate: Debug + Clone + From<Self::ApiOpportunityCreate> + PartialEq {
Expand Down
17 changes: 16 additions & 1 deletion auction-server/src/opportunity/entities/opportunity_evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
OpportunityCoreFields,
},
token_amount_evm::TokenAmountEvm,
OpportunityComparison,
OpportunityCoreFieldsCreate,
OpportunityCreate,
},
Expand All @@ -22,6 +23,7 @@ use {
U256,
},
std::ops::Deref,
time::OffsetDateTime,
};

// TODO revise the entities for opportunity, Maybe generic opportunity with params
Expand Down Expand Up @@ -71,6 +73,18 @@ impl Opportunity for OpportunityEvm {
chain_id: self.core_fields.chain_id.clone(),
}))
}

fn compare(&self, other: &Self::OpportunityCreate) -> super::OpportunityComparison {
if *other == self.clone().into() {
OpportunityComparison::Duplicate
} else {
OpportunityComparison::New
}
}

fn refresh(&mut self) {
self.core_fields.refresh_time = OffsetDateTime::now_utc();
}
}

impl OpportunityCreate for OpportunityCreateEvm {
Expand Down Expand Up @@ -170,7 +184,8 @@ impl TryFrom<repository::Opportunity<repository::OpportunityMetadataEvm>> for Op
Ok(OpportunityEvm {
core_fields: OpportunityCoreFields {
id: val.id,
creation_time: val.last_creation_time.assume_utc(),
creation_time: val.creation_time.assume_utc(),
refresh_time: val.creation_time.assume_utc(),
permission_key: PermissionKey::from(val.permission_key),
chain_id: val.chain_id,
sell_tokens,
Expand Down
29 changes: 28 additions & 1 deletion auction-server/src/opportunity/entities/opportunity_svm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
OpportunityCoreFields,
},
token_amount_svm::TokenAmountSvm,
OpportunityComparison,
OpportunityCoreFieldsCreate,
OpportunityCreate,
},
Expand All @@ -22,6 +23,10 @@ use {
pubkey::Pubkey,
},
std::ops::Deref,
time::{
Duration,
OffsetDateTime,
},
};

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -62,6 +67,9 @@ pub struct OpportunityCreateSvm {
pub slot: Slot,
}

// Opportunity can be refreshed after 10 seconds
const MIN_REFRESH_TIME: Duration = Duration::seconds(30);

impl Opportunity for OpportunitySvm {
type TokenAmount = TokenAmountSvm;
type ModelMetadata = repository::OpportunityMetadataSvm;
Expand Down Expand Up @@ -114,6 +122,24 @@ impl Opportunity for OpportunitySvm {
program: self.program.clone().into(),
}))
}

fn compare(&self, other: &Self::OpportunityCreate) -> super::OpportunityComparison {
let mut self_clone: OpportunityCreateSvm = self.clone().into();
self_clone.slot = other.slot;
if *other == self_clone {
if self.refresh_time + MIN_REFRESH_TIME < OffsetDateTime::now_utc() {
OpportunityComparison::NeedsRefresh
} else {
OpportunityComparison::Duplicate
}
} else {
OpportunityComparison::New
}
}

fn refresh(&mut self) {
self.core_fields.refresh_time = OffsetDateTime::now_utc();
}
}

impl OpportunityCreate for OpportunityCreateSvm {
Expand Down Expand Up @@ -226,7 +252,8 @@ impl TryFrom<repository::Opportunity<repository::OpportunityMetadataSvm>> for Op
Ok(OpportunitySvm {
core_fields: OpportunityCoreFields {
id: val.id,
creation_time: val.last_creation_time.assume_utc(),
creation_time: val.creation_time.assume_utc(),
refresh_time: val.creation_time.assume_utc(),
permission_key: PermissionKey::from(val.permission_key),
chain_id: val.chain_id,
sell_tokens,
Expand Down
Loading

0 comments on commit 24b1ba6

Please sign in to comment.