From 0f04715b2e9f257a20c9990669561577d54e4ac8 Mon Sep 17 00:00:00 2001 From: Alin Cruceat Date: Fri, 4 Oct 2024 13:20:41 +0300 Subject: [PATCH 1/2] price aggregator changes from mx-sdk-rs --- contracts/price-aggregator/src/events.rs | 16 ++++++++++++++++ contracts/price-aggregator/src/lib.rs | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/contracts/price-aggregator/src/events.rs b/contracts/price-aggregator/src/events.rs index 1b25ec4b..387471ed 100644 --- a/contracts/price-aggregator/src/events.rs +++ b/contracts/price-aggregator/src/events.rs @@ -43,4 +43,20 @@ pub trait EventsModule { #[indexed] epoch: u64, new_round_event: &NewRoundEvent, ); + + #[event("discard_round")] + fn discard_round_event( + &self, + #[indexed] from: &ManagedBuffer, + #[indexed] to: &ManagedBuffer, + #[indexed] round: usize, + ); + + #[event("add_submission")] + fn add_submission_event( + &self, + #[indexed] caller: &ManagedAddress, + #[indexed] price: &BigUint, + #[indexed] round: usize, + ); } diff --git a/contracts/price-aggregator/src/lib.rs b/contracts/price-aggregator/src/lib.rs index 31a1b149..cf3d6333 100644 --- a/contracts/price-aggregator/src/lib.rs +++ b/contracts/price-aggregator/src/lib.rs @@ -183,10 +183,15 @@ pub trait PriceAggregator: let accepted = !submissions.contains_key(&caller) && (is_first_submission || submission_timestamp >= first_submission_timestamp); if accepted { - submissions.insert(caller, price); + submissions.insert(caller.clone(), price.clone()); last_sub_time_mapper.set(current_timestamp); - - self.create_new_round(token_pair, submissions, decimals); + self.create_new_round(token_pair.clone(), submissions, decimals); + let wrapped_rounds = self.rounds().get(&token_pair); + let mut round_id = 0; + if wrapped_rounds.is_some() { + round_id = wrapped_rounds.unwrap().len(); + } + self.add_submission_event(&caller, &price, round_id); } self.oracle_status() @@ -282,6 +287,13 @@ pub trait PriceAggregator: .get() .push(&price_feed); self.emit_new_round_event(&token_pair, &price_feed); + } else { + let wrapped_rounds = self.rounds().get(&token_pair); + let mut round_id = 0; + if wrapped_rounds.is_some() { + round_id = wrapped_rounds.unwrap().len(); + } + self.discard_round_event(&token_pair.from.clone(), &token_pair.to.clone(), round_id); } } From a75fc5629377d269d5f06e5add7bbe29f29d2aaf Mon Sep 17 00:00:00 2001 From: Alin Cruceat Date: Fri, 4 Oct 2024 13:33:04 +0300 Subject: [PATCH 2/2] more changes --- contracts/price-aggregator/src/events.rs | 14 ++++++++------ contracts/price-aggregator/src/lib.rs | 24 +++++++++++++----------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/contracts/price-aggregator/src/events.rs b/contracts/price-aggregator/src/events.rs index 387471ed..15c0b645 100644 --- a/contracts/price-aggregator/src/events.rs +++ b/contracts/price-aggregator/src/events.rs @@ -1,5 +1,5 @@ -use multiversx_sc::derive_imports::*; -use multiversx_sc::imports::*; +multiversx_sc::imports!(); +multiversx_sc::derive_imports!(); use crate::price_aggregator_data::{TimestampedPrice, TokenPair}; @@ -18,13 +18,14 @@ pub trait EventsModule { fn emit_new_round_event( &self, token_pair: &TokenPair, + round_id: usize, price_feed: &TimestampedPrice, ) { let epoch = self.blockchain().get_block_epoch(); self.new_round_event( &token_pair.from.clone(), &token_pair.to.clone(), - epoch, + round_id, &NewRoundEvent { price: price_feed.price.clone(), timestamp: price_feed.timestamp, @@ -40,7 +41,7 @@ pub trait EventsModule { &self, #[indexed] from: &ManagedBuffer, #[indexed] to: &ManagedBuffer, - #[indexed] epoch: u64, + #[indexed] round: usize, new_round_event: &NewRoundEvent, ); @@ -55,8 +56,9 @@ pub trait EventsModule { #[event("add_submission")] fn add_submission_event( &self, - #[indexed] caller: &ManagedAddress, - #[indexed] price: &BigUint, + #[indexed] from: &ManagedBuffer, + #[indexed] to: &ManagedBuffer, #[indexed] round: usize, + price: &BigUint, ); } diff --git a/contracts/price-aggregator/src/lib.rs b/contracts/price-aggregator/src/lib.rs index cf3d6333..1c502cfb 100644 --- a/contracts/price-aggregator/src/lib.rs +++ b/contracts/price-aggregator/src/lib.rs @@ -1,6 +1,6 @@ #![no_std] -use multiversx_sc::imports::*; +multiversx_sc::imports!(); mod events; pub mod median; @@ -185,13 +185,19 @@ pub trait PriceAggregator: if accepted { submissions.insert(caller.clone(), price.clone()); last_sub_time_mapper.set(current_timestamp); - self.create_new_round(token_pair.clone(), submissions, decimals); - let wrapped_rounds = self.rounds().get(&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(); + round_id = wrapped_rounds.unwrap().len() + 1; } - self.add_submission_event(&caller, &price, round_id); + 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, + ); } self.oracle_status() @@ -253,6 +259,7 @@ pub trait PriceAggregator: fn create_new_round( &self, token_pair: TokenPair, + round_id: usize, mut submissions: MapMapper, decimals: u8, ) { @@ -286,13 +293,8 @@ pub trait PriceAggregator: .or_default() .get() .push(&price_feed); - self.emit_new_round_event(&token_pair, &price_feed); + self.emit_new_round_event(&token_pair, round_id, &price_feed); } else { - let wrapped_rounds = self.rounds().get(&token_pair); - let mut round_id = 0; - if wrapped_rounds.is_some() { - round_id = wrapped_rounds.unwrap().len(); - } self.discard_round_event(&token_pair.from.clone(), &token_pair.to.clone(), round_id); } }