diff --git a/contracts/price-aggregator/src/events.rs b/contracts/price-aggregator/src/events.rs index 15c0b645..50f369a2 100644 --- a/contracts/price-aggregator/src/events.rs +++ b/contracts/price-aggregator/src/events.rs @@ -13,6 +13,14 @@ pub struct NewRoundEvent { epoch: u64, } +#[type_abi] +#[derive(TopEncode)] +pub struct DiscardSubmissionEvent { + submission_timestamp: u64, + first_submission_timestamp: u64, + has_caller_already_submitted: bool, +} + #[multiversx_sc::module] pub trait EventsModule { fn emit_new_round_event( @@ -45,6 +53,35 @@ pub trait EventsModule { new_round_event: &NewRoundEvent, ); + fn emit_discard_submission_event( + &self, + token_pair: &TokenPair, + round_id: usize, + submission_timestamp: u64, + first_submission_timestamp: u64, + has_caller_already_submitted: bool, + ) { + self.discard_submission_event( + &token_pair.from.clone(), + &token_pair.to.clone(), + round_id, + &DiscardSubmissionEvent { + submission_timestamp, + first_submission_timestamp, + has_caller_already_submitted, + }, + ) + } + + #[event("discard_submission")] + fn discard_submission_event( + &self, + #[indexed] from: &ManagedBuffer, + #[indexed] to: &ManagedBuffer, + #[indexed] round: usize, + discard_submission_event: &DiscardSubmissionEvent, + ); + #[event("discard_round")] fn discard_round_event( &self, diff --git a/contracts/price-aggregator/src/lib.rs b/contracts/price-aggregator/src/lib.rs index e2936365..05698745 100644 --- a/contracts/price-aggregator/src/lib.rs +++ b/contracts/price-aggregator/src/lib.rs @@ -159,6 +159,12 @@ pub trait PriceAggregator: let first_sub_time_mapper = self.first_submission_timestamp(&token_pair); let last_sub_time_mapper = self.last_submission_timestamp(&token_pair); + let mut round_id = 0; + let wrapped_rounds = self.rounds().get(&token_pair); + if wrapped_rounds.is_some() { + round_id = wrapped_rounds.unwrap().len() + 1; + } + let current_timestamp = self.blockchain().get_block_timestamp(); let mut is_first_submission = false; let mut first_submission_timestamp = if submissions.is_empty() { @@ -182,20 +188,17 @@ pub trait PriceAggregator: first_submission_timestamp = current_timestamp; is_first_submission = true; + self.discard_round_event(&token_pair.from.clone(), &token_pair.to.clone(), round_id) } let caller = self.blockchain().get_caller(); - let accepted = !submissions.contains_key(&caller) + let has_caller_already_submitted = submissions.contains_key(&caller); + let accepted = !has_caller_already_submitted && (is_first_submission || submission_timestamp >= first_submission_timestamp); if accepted { submissions.insert(caller.clone(), price.clone()); last_sub_time_mapper.set(current_timestamp); - let mut round_id = 0; - let wrapped_rounds = self.rounds().get(&token_pair); - if wrapped_rounds.is_some() { - round_id = wrapped_rounds.unwrap().len() + 1; - } self.create_new_round(token_pair.clone(), round_id, submissions, decimals); self.add_submission_event( &token_pair.from.clone(), @@ -203,6 +206,14 @@ pub trait PriceAggregator: round_id, &price, ); + } else { + self.emit_discard_submission_event( + &token_pair, + round_id, + submission_timestamp, + first_submission_timestamp, + has_caller_already_submitted, + ); } self.oracle_status() @@ -299,8 +310,6 @@ pub trait PriceAggregator: .get() .push(&price_feed); self.emit_new_round_event(&token_pair, round_id, &price_feed); - } else { - self.discard_round_event(&token_pair.from.clone(), &token_pair.to.clone(), round_id); } }