Skip to content
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

more event changes - Price Aggregator #111

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions contracts/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/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
Loading