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

add more events to PriceAggregator #1804

Merged
merged 15 commits into from
Oct 9, 2024
22 changes: 20 additions & 2 deletions contracts/core/price-aggregator/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ pub trait EventsModule {
fn emit_new_round_event(
&self,
token_pair: &TokenPair<Self::Api>,
round_id: usize,
price_feed: &TimestampedPrice<Self::Api>,
) {
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,
Expand All @@ -40,7 +41,24 @@ pub trait EventsModule {
&self,
#[indexed] from: &ManagedBuffer,
#[indexed] to: &ManagedBuffer,
#[indexed] epoch: u64,
#[indexed] round: usize,
new_round_event: &NewRoundEvent<Self::Api>,
);

#[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] from: &ManagedBuffer,
#[indexed] to: &ManagedBuffer,
#[indexed] round: usize,
price: &BigUint,
);
}
33 changes: 27 additions & 6 deletions contracts/core/price-aggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub trait PriceAggregator:
self.set_paused(true);
}

#[upgrade]
fn upgrade(&self) {
self.set_paused(true);
}

#[only_owner]
#[endpoint(changeAmounts)]
fn change_amounts(&self, staking_amount: BigUint, slash_amount: BigUint) {
Expand Down Expand Up @@ -183,10 +188,21 @@ 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);
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,
);
}

self.oracle_status()
Expand Down Expand Up @@ -248,6 +264,7 @@ pub trait PriceAggregator:
fn create_new_round(
&self,
token_pair: TokenPair<Self::Api>,
round_id: usize,
mut submissions: MapMapper<ManagedAddress, BigUint>,
decimals: u8,
) {
Expand Down Expand Up @@ -281,7 +298,9 @@ 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 {
self.discard_round_event(&token_pair.from.clone(), &token_pair.to.clone(), round_id);
}
}

Expand Down Expand Up @@ -377,9 +396,11 @@ pub trait PriceAggregator:
#[only_owner]
#[endpoint(setPairDecimals)]
fn set_pair_decimals(&self, from: ManagedBuffer, to: ManagedBuffer, decimals: u8) {
self.require_paused();

self.pair_decimals(&from, &to).set(Some(decimals));
let pair_decimals_mapper = self.pair_decimals(&from, &to);
if !pair_decimals_mapper.is_empty() {
self.require_paused();
}
pair_decimals_mapper.set(Some(decimals));
let pair = TokenPair { from, to };
self.clear_submissions(&pair);
}
Expand Down
1 change: 0 additions & 1 deletion contracts/core/price-aggregator/src/median.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

/// Returns the sorted middle, or the average of the two middle indexed items if the
/// vector has an even number of elements.
Expand Down
19 changes: 19 additions & 0 deletions contracts/core/price-aggregator/tests/price_aggregator_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ where
}
}

#[rustfmt::skip]
impl<Env, From, To, Gas> PriceAggregatorProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn upgrade(
self,
) -> TxTypedUpgrade<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_upgrade()
.original_result()
}
}

#[rustfmt::skip]
impl<Env, From, To, Gas> PriceAggregatorProxyMethods<Env, From, To, Gas>
where
Expand Down
4 changes: 3 additions & 1 deletion contracts/core/price-aggregator/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
////////////////////////////////////////////////////

// Init: 1
// Upgrade: 1
// Endpoints: 21
// Async Callback (empty): 1
// Total number of exported functions: 23
// Total number of exported functions: 24

#![no_std]

Expand All @@ -18,6 +19,7 @@ multiversx_sc_wasm_adapter::endpoints! {
multiversx_price_aggregator_sc
(
init => init
upgrade => upgrade
changeAmounts => change_amounts
addOracles => add_oracles
removeOracles => remove_oracles
Expand Down
Loading