1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::all)]
use common::{DataFeed, OnNewSymbolsRelayed, Oracle, Rate};
use frame_support;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_std::{collections::btree_set::BTreeSet, vec::Vec};
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
mod benchmarking;
pub mod weights;
pub use weights::WeightInfo;
impl<T: Config> DataFeed<T::Symbol, Rate, u64> for Pallet<T> {
fn quote(symbol: &T::Symbol) -> Result<Option<Rate>, DispatchError> {
let enabled_oracles = Self::enabled_oracles();
Self::enabled_symbols(symbol)
.into_iter()
.filter(|oracle| enabled_oracles.contains(&oracle))
.map(|oracle| match oracle {
Oracle::BandChainFeed => T::BandChainOracle::quote(symbol),
})
.next()
.unwrap_or(Ok(None))
}
fn list_enabled_symbols() -> Result<Vec<(T::Symbol, u64)>, DispatchError> {
let symbols_rates = Self::enabled_oracles()
.into_iter()
.flat_map(|oracle| match oracle {
Oracle::BandChainFeed => match T::BandChainOracle::list_enabled_symbols() {
Ok(list) => list,
_ => Vec::new(),
},
})
.collect();
Ok(symbols_rates)
}
fn quote_unchecked(symbol: &T::Symbol) -> Option<Rate> {
let enabled_oracles = Self::enabled_oracles();
Self::enabled_symbols(symbol)
.into_iter()
.filter(|oracle| enabled_oracles.contains(&oracle))
.map(|oracle| match oracle {
Oracle::BandChainFeed => T::BandChainOracle::quote_unchecked(symbol),
})
.next()
.unwrap_or(None)
}
}
impl<T: Config> OnNewSymbolsRelayed<T::Symbol> for Pallet<T> {
fn on_new_symbols_relayed(
oracle_variant: Oracle,
symbols: BTreeSet<T::Symbol>,
) -> Result<(), DispatchError> {
symbols.into_iter().for_each(|symbol| {
SymbolProviders::<T>::set(symbol, Some(oracle_variant));
});
Ok(())
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::traits::StorageVersion;
use sp_std::collections::btree_set::BTreeSet;
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;
type Symbol: Parameter + Ord;
type BandChainOracle: DataFeed<Self::Symbol, Rate, u64>;
}
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::storage]
#[pallet::getter(fn enabled_oracles)]
pub type EnabledOracles<T: Config> = StorageValue<_, BTreeSet<Oracle>, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn enabled_symbols)]
pub type SymbolProviders<T: Config> = StorageMap<_, Blake2_128Concat, T::Symbol, Oracle>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
OracleEnabled(Oracle),
OracleDisabled(Oracle),
}
#[pallet::error]
pub enum Error<T> {
OracleAlreadyEnabled,
OracleAlreadyDisabled,
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::enable_oracle())]
pub fn enable_oracle(origin: OriginFor<T>, oracle: Oracle) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
ensure!(
!Self::enabled_oracles().contains(&oracle),
Error::<T>::OracleAlreadyEnabled
);
EnabledOracles::<T>::mutate(|enabled_oracles| enabled_oracles.insert(oracle.clone()));
Self::deposit_event(Event::OracleEnabled(oracle));
Ok(().into())
}
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::disable_oracle())]
pub fn disable_oracle(origin: OriginFor<T>, oracle: Oracle) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
ensure!(
Self::enabled_oracles().contains(&oracle),
Error::<T>::OracleAlreadyDisabled
);
EnabledOracles::<T>::mutate(|enabled_oracles| enabled_oracles.remove(&oracle));
Self::deposit_event(Event::OracleDisabled(oracle));
Ok(().into())
}
}
#[pallet::genesis_config]
pub struct GenesisConfig {
pub enabled_oracles: BTreeSet<Oracle>,
}
#[cfg(feature = "std")]
impl Default for GenesisConfig {
fn default() -> Self {
Self {
enabled_oracles: Default::default(),
}
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
EnabledOracles::<T>::put(&self.enabled_oracles);
}
}
}