Skip to content

Commit

Permalink
add PoolRegistered event
Browse files Browse the repository at this point in the history
  • Loading branch information
Roznovjak committed Aug 19, 2021
1 parent 6487d35 commit 7ca20ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pallets/price-oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ pub mod pallet {
}

#[pallet::event]
pub enum Event<T: Config> {}
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// Pool was registered. [AssetPair]
PoolRegistered(AssetPair),
}

/// The number of assets registered and handled by this pallet.
#[pallet::storage]
Expand Down Expand Up @@ -127,11 +131,14 @@ impl<T: Config> Pallet<T> {
let incremented_asset_count = if let Some(count) = Self::num_of_assets().checked_add(1) {
count
} else {
// We don't want to throw an error here because this method is used in different extrinsics.
// We also do not expect to have more than 2^32 assets registered.
return
};
<NumOfTrackedAssets<T>>::put(incremented_asset_count);

PriceDataTen::<T>::append((asset_pair.name(), BucketQueue::default()));
Self::deposit_event(Event::PoolRegistered(asset_pair));
}
}

Expand All @@ -143,6 +150,7 @@ impl<T: Config> Pallet<T> {
PriceEntry::default()
};

// Invalid values are ignored and not added to the queue.
if let Some(new_entry) = previous_entry.calculate_new_price_entry(&price_entry) {
PriceDataAccumulator::<T>::insert(asset_pair.name(), new_entry);
}
Expand Down Expand Up @@ -197,11 +205,13 @@ impl<T: Config> AMMHandlers<T::AccountId, AssetId, AssetPair, Balance> for Price
let (price, amount) = if let Some(price_tuple) = amm_transfer.normalize_price() {
price_tuple
} else {
// We don't want to throw an error here because this method is used in different extrinsics.
// Invalid prices are ignored and not added to the queue.
return;
};

// we assume that zero prices are not valid
// zero values are ignored and not added to the queue
// We assume that zero values are not valid.
// Zero values are ignored and not added to the queue.
if price.is_zero() || amount.is_zero() || liq_amount.is_zero() {
return;
}
Expand Down
16 changes: 16 additions & 0 deletions pallets/price-oracle/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
ext
}

fn last_events(n: usize) -> Vec<TestEvent> {
frame_system::Pallet::<Test>::events()
.into_iter()
.rev()
.take(n)
.rev()
.map(|e| e.event)
.collect()
}

fn expect_events(e: Vec<TestEvent>) {
assert_eq!(last_events(e.len()), e);
}

#[test]
fn add_new_asset_pair_should_work() {
new_test_ext().execute_with(|| {
Expand All @@ -42,6 +56,7 @@ fn add_new_asset_pair_should_work() {
<PriceDataTen<Test>>::get().contains(&(ASSET_PAIR_A.name(), BucketQueue::default())),
true
);
expect_events(vec![Event::PoolRegistered(ASSET_PAIR_A).into()]);
});
}

Expand All @@ -54,6 +69,7 @@ fn add_existing_asset_pair_should_not_work() {
);
PriceOracle::on_create_pool(ASSET_PAIR_A);
assert_storage_noop!(PriceOracle::on_create_pool(ASSET_PAIR_A));
expect_events(vec![Event::PoolRegistered(ASSET_PAIR_A).into()]);
});
}

Expand Down

0 comments on commit 7ca20ef

Please sign in to comment.