Skip to content

Commit

Permalink
fixes for discard round
Browse files Browse the repository at this point in the history
  • Loading branch information
dragos-rebegea committed Oct 4, 2024
1 parent c3c2b81 commit 6906540
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
37 changes: 37 additions & 0 deletions contracts/core/price-aggregator/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ pub struct NewRoundEvent<M: ManagedTypeApi> {
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(
Expand Down Expand Up @@ -45,6 +53,35 @@ pub trait EventsModule {
new_round_event: &NewRoundEvent<Self::Api>,
);

fn emit_discard_submission_event(
&self,
token_pair: &TokenPair<Self::Api>,
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,
Expand Down
25 changes: 17 additions & 8 deletions contracts/core/price-aggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -182,27 +188,32 @@ 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(),
&token_pair.to.clone(),
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()
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 6906540

Please sign in to comment.