Skip to content

Commit 408c5e1

Browse files
authored
Merge pull request #111 from multiversx/price-aggregator-events-update
more event changes - Price Aggregator
2 parents 9ba2b0a + b876a3c commit 408c5e1

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

contracts/price-aggregator/src/events.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ pub struct NewRoundEvent<M: ManagedTypeApi> {
1313
epoch: u64,
1414
}
1515

16+
#[type_abi]
17+
#[derive(TopEncode)]
18+
pub struct DiscardSubmissionEvent {
19+
submission_timestamp: u64,
20+
first_submission_timestamp: u64,
21+
has_caller_already_submitted: bool,
22+
}
23+
1624
#[multiversx_sc::module]
1725
pub trait EventsModule {
1826
fn emit_new_round_event(
@@ -45,6 +53,35 @@ pub trait EventsModule {
4553
new_round_event: &NewRoundEvent<Self::Api>,
4654
);
4755

56+
fn emit_discard_submission_event(
57+
&self,
58+
token_pair: &TokenPair<Self::Api>,
59+
round_id: usize,
60+
submission_timestamp: u64,
61+
first_submission_timestamp: u64,
62+
has_caller_already_submitted: bool,
63+
) {
64+
self.discard_submission_event(
65+
&token_pair.from.clone(),
66+
&token_pair.to.clone(),
67+
round_id,
68+
&DiscardSubmissionEvent {
69+
submission_timestamp,
70+
first_submission_timestamp,
71+
has_caller_already_submitted,
72+
},
73+
)
74+
}
75+
76+
#[event("discard_submission")]
77+
fn discard_submission_event(
78+
&self,
79+
#[indexed] from: &ManagedBuffer,
80+
#[indexed] to: &ManagedBuffer,
81+
#[indexed] round: usize,
82+
discard_submission_event: &DiscardSubmissionEvent,
83+
);
84+
4885
#[event("discard_round")]
4986
fn discard_round_event(
5087
&self,

contracts/price-aggregator/src/lib.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ pub trait PriceAggregator:
159159
let first_sub_time_mapper = self.first_submission_timestamp(&token_pair);
160160
let last_sub_time_mapper = self.last_submission_timestamp(&token_pair);
161161

162+
let mut round_id = 0;
163+
let wrapped_rounds = self.rounds().get(&token_pair);
164+
if wrapped_rounds.is_some() {
165+
round_id = wrapped_rounds.unwrap().len() + 1;
166+
}
167+
162168
let current_timestamp = self.blockchain().get_block_timestamp();
163169
let mut is_first_submission = false;
164170
let mut first_submission_timestamp = if submissions.is_empty() {
@@ -182,27 +188,32 @@ pub trait PriceAggregator:
182188

183189
first_submission_timestamp = current_timestamp;
184190
is_first_submission = true;
191+
self.discard_round_event(&token_pair.from.clone(), &token_pair.to.clone(), round_id)
185192
}
186193

187194
let caller = self.blockchain().get_caller();
188-
let accepted = !submissions.contains_key(&caller)
195+
let has_caller_already_submitted = submissions.contains_key(&caller);
196+
let accepted = !has_caller_already_submitted
189197
&& (is_first_submission || submission_timestamp >= first_submission_timestamp);
190198
if accepted {
191199
submissions.insert(caller.clone(), price.clone());
192200
last_sub_time_mapper.set(current_timestamp);
193201

194-
let mut round_id = 0;
195-
let wrapped_rounds = self.rounds().get(&token_pair);
196-
if wrapped_rounds.is_some() {
197-
round_id = wrapped_rounds.unwrap().len() + 1;
198-
}
199202
self.create_new_round(token_pair.clone(), round_id, submissions, decimals);
200203
self.add_submission_event(
201204
&token_pair.from.clone(),
202205
&token_pair.to.clone(),
203206
round_id,
204207
&price,
205208
);
209+
} else {
210+
self.emit_discard_submission_event(
211+
&token_pair,
212+
round_id,
213+
submission_timestamp,
214+
first_submission_timestamp,
215+
has_caller_already_submitted,
216+
);
206217
}
207218

208219
self.oracle_status()
@@ -299,8 +310,6 @@ pub trait PriceAggregator:
299310
.get()
300311
.push(&price_feed);
301312
self.emit_new_round_event(&token_pair, round_id, &price_feed);
302-
} else {
303-
self.discard_round_event(&token_pair.from.clone(), &token_pair.to.clone(), round_id);
304313
}
305314
}
306315

0 commit comments

Comments
 (0)