-
Notifications
You must be signed in to change notification settings - Fork 77
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
feat: manual oracle activation #782
Changes from 16 commits
ed0cb7a
74c7bda
644a614
e802938
4e3067b
01c948d
f38be99
4d38a39
84b22f7
37a090c
1497db0
cfedd19
0b2ceb5
50a81f0
a4e35c0
1177201
1fd258e
781a31d
5d6eda8
1a6c318
e4b3176
4e48e74
249a797
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,8 +115,8 @@ impl BenchmarkHelper<AssetId> for () { | |
#[frame_support::pallet] | ||
pub mod pallet { | ||
use super::*; | ||
use frame_support::BoundedBTreeMap; | ||
use frame_system::pallet_prelude::BlockNumberFor; | ||
use frame_support::{BoundedBTreeMap, BoundedBTreeSet}; | ||
use frame_system::pallet_prelude::{BlockNumberFor, OriginFor}; | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
@@ -128,6 +128,9 @@ pub mod pallet { | |
/// Weight information for the extrinsics. | ||
type WeightInfo: WeightInfo; | ||
|
||
/// Origin that can enable oracle for assets that would be rejected by `OracleWhitelist` otherwise. | ||
type AuthorityOrigin: EnsureOrigin<Self::RuntimeOrigin>; | ||
|
||
/// Provider for the current block number. | ||
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>; | ||
|
||
|
@@ -149,10 +152,17 @@ pub mod pallet { | |
pub enum Error<T> { | ||
TooManyUniqueEntries, | ||
OnTradeValueZero, | ||
OracleNotFound, | ||
} | ||
|
||
#[pallet::event] | ||
pub enum Event<T: Config> {} | ||
#[pallet::generate_deposit(pub(crate) fn deposit_event)] | ||
pub enum Event<T: Config> { | ||
/// Oracle was added to the whitelist. | ||
OracleAdded { source: Source, assets: (AssetId, AssetId) }, | ||
/// Oracle was removed from the whitelist. | ||
OracleRemoved { source: Source, assets: (AssetId, AssetId) }, | ||
} | ||
|
||
/// Accumulator for oracle data in current block that will be recorded at the end of the block. | ||
#[pallet::storage] | ||
|
@@ -163,7 +173,7 @@ pub mod pallet { | |
ValueQuery, | ||
>; | ||
|
||
/// Orace storage keyed by data source, involved asset ids and the period length of the oracle. | ||
/// Oracle storage keyed by data source, involved asset ids and the period length of the oracle. | ||
/// | ||
/// Stores the data entry as well as the block number when the oracle was first initialized. | ||
#[pallet::storage] | ||
|
@@ -179,6 +189,11 @@ pub mod pallet { | |
OptionQuery, | ||
>; | ||
|
||
/// Assets that are whitelisted and tracked by the pallet. | ||
#[pallet::storage] | ||
pub type WhitelistedAssets<T: Config> = | ||
StorageValue<_, BoundedBTreeSet<(Source, (AssetId, AssetId)), T::MaxUniqueEntries>, ValueQuery>; | ||
|
||
#[pallet::genesis_config] | ||
#[derive(frame_support::DefaultNoBound)] | ||
pub struct GenesisConfig<T: Config> { | ||
|
@@ -232,7 +247,43 @@ pub mod pallet { | |
} | ||
|
||
#[pallet::call] | ||
impl<T: Config> Pallet<T> {} | ||
impl<T: Config> Pallet<T> { | ||
#[pallet::call_index(0)] | ||
#[pallet::weight(<T as Config>::WeightInfo::add_oracle())] | ||
pub fn add_oracle(origin: OriginFor<T>, source: Source, assets: (AssetId, AssetId)) -> DispatchResult { | ||
T::AuthorityOrigin::ensure_origin(origin)?; | ||
|
||
let assets = ordered_pair(assets.0, assets.1); | ||
|
||
WhitelistedAssets::<T>::mutate(|list| { | ||
list.try_insert((source, (assets))) | ||
.map_err(|_| Error::<T>::TooManyUniqueEntries) | ||
})?; | ||
|
||
Self::deposit_event(Event::OracleAdded { source, assets }); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[pallet::call_index(1)] | ||
#[pallet::weight(<T as Config>::WeightInfo::remove_oracle())] | ||
pub fn remove_oracle(origin: OriginFor<T>, source: Source, assets: (AssetId, AssetId)) -> DispatchResult { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would expect this function would remove actual oracle as well, not just item from whitelist |
||
T::AuthorityOrigin::ensure_origin(origin)?; | ||
|
||
let assets = ordered_pair(assets.0, assets.1); | ||
|
||
WhitelistedAssets::<T>::mutate(|list| { | ||
let was_removed = list.remove(&(source, (assets))); | ||
ensure!(was_removed, Error::<T>::OracleNotFound); | ||
|
||
Ok::<(), DispatchError>(()) | ||
})?; | ||
|
||
Self::deposit_event(Event::OracleRemoved { source, assets }); | ||
|
||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
impl<T: Config> Pallet<T> { | ||
|
@@ -243,7 +294,10 @@ impl<T: Config> Pallet<T> { | |
assets: (AssetId, AssetId), | ||
oracle_entry: OracleEntry<BlockNumberFor<T>>, | ||
) -> Result<(), ()> { | ||
if !T::OracleWhitelist::contains(&(src, assets.0, assets.1)) { | ||
if !(T::OracleWhitelist::contains(&(src, assets.0, assets.1)) | ||
|| WhitelistedAssets::<T>::get().contains(&(src, (assets.0, assets.1)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont like the way this is tacked on, from configuration of the pallet i would expect that only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so for our case you would basically implement extension in the runtime config (extend sufficient asset filter), not other way around |
||
{ | ||
// if we don't track oracle for given asset pair, don't throw error | ||
return Ok(()); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when you add event like this, i would expect it being fired every time oracle is created, not just when you modify whitelist