pallet-oracle 0.7.0

FRAME oracle pallet for off-chain data
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// This file is part of Substrate.

// Copyright (C) 2020-2025 Acala Foundation.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # Oracle
//!
//! A pallet that provides a decentralized and trustworthy way to bring external, off-chain data
//! onto the blockchain.
//!
//! ## Pallet API
//!
//! See the [`pallet`] module for more information about the interfaces this pallet exposes,
//! including its configuration trait, dispatchables, storage items, events and errors.
//!
//! ## Overview
//!
//! The Oracle pallet enables blockchain applications to access real-world data through a
//! decentralized network of trusted data providers. It's designed to be flexible and can handle
//! various types of external data such as cryptocurrency prices, weather data, sports scores, or
//! any other off-chain information that needs to be brought on-chain.
//!
//! The pallet operates on a permissioned model where only authorized oracle operators can submit
//! data. This ensures data quality and prevents spam while maintaining decentralization through
//! multiple independent operators. The system aggregates data from multiple sources using
//! configurable algorithms, typically taking the median to resist outliers and manipulation
//! attempts.
//!
//! ### Key Concepts
//!
//! * **Oracle Operators**: A set of trusted accounts authorized to submit data. Managed through the
//!   [`SortedMembers`] trait, allowing integration with membership pallets.
//! * **Data Feeds**: Key-value pairs where keys identify the data type (e.g., currency pair) and
//!   values contain the actual data (e.g., price).
//! * **Data Aggregation**: Configurable algorithms to combine multiple operator inputs into a
//!   single trusted value, with median aggregation provided by default.
//! * **Timestamped Data**: All submitted data includes timestamps for freshness tracking.
//!
//! ## Low Level / Implementation Details
//!
//! ### Design Goals
//!
//! The oracle system aims to provide:
//! - **Decentralization**: Multiple independent data providers prevent single points of failure
//! - **Data Quality**: Aggregation mechanisms filter out outliers and malicious data
//! - **Flexibility**: Configurable data types and aggregation strategies
//! - **Performance**: Efficient storage and retrieval of timestamped data
//! - **Security**: Permissioned access with cryptographic verification of data integrity
//!
//! ### Design
//!
//! The pallet uses a dual-storage approach:
//! - [`RawValues`]: Stores individual operator submissions with timestamps
//! - [`Values`]: Stores the final aggregated values after processing
//!
//! This design allows for:
//! - Historical tracking of individual operator submissions
//! - Efficient access to final aggregated values
//! - Clean separation between raw data and processed results
//! - Easy integration with data aggregation algorithms

#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode, MaxEncodedLen};

use serde::{Deserialize, Serialize};

use frame_support::{
	dispatch::Pays,
	ensure,
	pallet_prelude::*,
	traits::{ChangeMembers, Get, SortedMembers, Time},
	weights::Weight,
	PalletId, Parameter,
};
use frame_system::pallet_prelude::*;
use scale_info::TypeInfo;
use sp_runtime::{
	traits::{AccountIdConversion, Member},
	Debug, DispatchResult,
};
use sp_std::{prelude::*, vec};

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

mod default_combine_data;
pub use default_combine_data::DefaultCombineData;
pub mod traits;
pub use traits::{CombineData, DataFeeder, DataProvider, DataProviderExtended, OnNewData};
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod weights;

pub use pallet::*;
pub use weights::WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
/// Helper trait for benchmarking oracle operations.
pub trait BenchmarkHelper<OracleKey, OracleValue, L: Get<u32>> {
	/// Returns a list of `(oracle_key, oracle_value)` pairs to be used for
	/// benchmarking.
	///
	/// NOTE: User should ensure to at least submit two values, otherwise the
	/// benchmark linear analysis might fail.
	fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L>;
}

#[cfg(feature = "runtime-benchmarks")]
impl<OracleKey, OracleValue, L: Get<u32>> BenchmarkHelper<OracleKey, OracleValue, L> for () {
	fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L> {
		BoundedVec::default()
	}
}

#[frame_support::pallet]
pub mod pallet {
	use super::*;

	pub(crate) type MomentOf<T, I = ()> = <<T as Config<I>>::Time as Time>::Moment;
	pub(crate) type TimestampedValueOf<T, I = ()> =
		TimestampedValue<<T as Config<I>>::OracleValue, MomentOf<T, I>>;

	/// A wrapper for a value with a timestamp.
	#[derive(
		Encode,
		Decode,
		Debug,
		Eq,
		PartialEq,
		Clone,
		Copy,
		Ord,
		PartialOrd,
		TypeInfo,
		MaxEncodedLen,
		Serialize,
		Deserialize,
	)]
	pub struct TimestampedValue<Value, Moment> {
		/// The value.
		pub value: Value,
		/// The timestamp.
		pub timestamp: Moment,
	}

	#[pallet::config]
	pub trait Config<I: 'static = ()>: frame_system::Config {
		/// A hook to be called when new data is received.
		///
		/// This hook is triggered whenever an oracle operator successfully submits new data.
		/// It allows other pallets to react to oracle updates, enabling real-time responses to
		/// external data changes.
		type OnNewData: OnNewData<Self::AccountId, Self::OracleKey, Self::OracleValue>;

		/// The implementation to combine raw values into a single aggregated value.
		///
		/// This type defines how multiple oracle operator submissions are combined into a single
		/// trusted value. Common implementations include taking the median (to resist outliers)
		/// or weighted averages based on operator reputation.
		type CombineData: CombineData<Self::OracleKey, TimestampedValueOf<Self, I>>;

		/// The time provider for timestamping oracle data.
		///
		/// This type provides the current timestamp used to mark when oracle data was submitted.
		/// Timestamps are crucial for determining data freshness and preventing stale data usage.
		type Time: Time;

		/// The key type for identifying oracle data feeds.
		///
		/// This type is used to uniquely identify different types of oracle data (e.g., currency
		/// pairs, asset prices, weather data).
		type OracleKey: Parameter + Member + MaxEncodedLen;

		/// The value type for oracle data.
		///
		/// This type represents the actual data submitted by oracle operators (e.g., prices,
		/// temperatures, scores).
		type OracleValue: Parameter + Member + Ord + MaxEncodedLen;

		/// The pallet ID.
		///
		/// Will be used to derive the pallet's account, which is used as the oracle account
		/// when values are fed by root.
		#[pallet::constant]
		type PalletId: Get<PalletId>;

		/// The source of oracle members.
		///
		/// This type provides the set of accounts authorized to submit oracle data.
		/// Typically implemented by membership pallets to allow governance-controlled
		/// management of oracle operators.
		type Members: SortedMembers<Self::AccountId>;

		/// Weight information for extrinsics in this pallet.
		type WeightInfo: WeightInfo;

		/// The maximum number of oracle operators that can feed data in a single block.
		#[pallet::constant]
		type MaxHasDispatchedSize: Get<u32>;

		/// The maximum number of key-value pairs that can be submitted in a single extrinsic.
		#[pallet::constant]
		type MaxFeedValues: Get<u32>;

		/// A helper trait for benchmarking oracle operations.
		///
		/// Provides sample data for benchmarking the oracle pallet, allowing accurate
		/// weight calculations and performance testing.
		#[cfg(feature = "runtime-benchmarks")]
		type BenchmarkHelper: BenchmarkHelper<
			Self::OracleKey,
			Self::OracleValue,
			Self::MaxFeedValues,
		>;
	}

	#[pallet::error]
	pub enum Error<T, I = ()> {
		/// The sender is not a member of the oracle and does not have
		/// permission to feed data.
		NoPermission,
		/// The oracle member has already fed data in the current block.
		AlreadyFeeded,
		/// Exceeds the maximum number of `HasDispatched` size.
		ExceedsMaxHasDispatchedSize,
	}

	#[pallet::event]
	#[pallet::generate_deposit(pub(crate) fn deposit_event)]
	pub enum Event<T: Config<I>, I: 'static = ()> {
		/// New data has been fed into the oracle.
		NewFeedData {
			/// The account that fed the data.
			sender: T::AccountId,
			/// The key-value pairs of the data that was fed.
			values: Vec<(T::OracleKey, T::OracleValue)>,
		},
	}

	/// The raw values for each oracle operator.
	///
	/// Maps `(AccountId, OracleKey)` to `TimestampedValue` containing the operator's submitted
	/// value along with the timestamp when it was submitted. This storage maintains the complete
	/// history of individual operator submissions, allowing for data aggregation and audit trails.
	///
	/// ## Storage Economics
	///
	/// No storage deposits are required as this data is considered essential for the oracle's
	/// operation and data integrity. The storage cost is borne by the blockchain as part of the
	/// oracle infrastructure.
	#[pallet::storage]
	#[pallet::getter(fn raw_values)]
	pub type RawValues<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
		_,
		Twox64Concat,
		T::AccountId,
		Twox64Concat,
		T::OracleKey,
		TimestampedValueOf<T, I>,
	>;

	/// The aggregated values for each oracle key.
	///
	/// Maps `OracleKey` to `TimestampedValue`.
	#[pallet::storage]
	#[pallet::getter(fn values)]
	pub type Values<T: Config<I>, I: 'static = ()> =
		StorageMap<_, Twox64Concat, <T as Config<I>>::OracleKey, TimestampedValueOf<T, I>>;

	/// A set of accounts that have already fed data in the current block.
	///
	/// This storage item tracks which oracle operators have already submitted data in the
	/// current block to enforce the "one submission per block" rule. This prevents spam and
	/// ensures fair participation among oracle operators.
	///
	/// The storage is cleared at the end of each block in the `on_finalize` hook, resetting
	/// the state for the next block.
	#[pallet::storage]
	pub(crate) type HasDispatched<T: Config<I>, I: 'static = ()> =
		StorageValue<_, BoundedBTreeSet<T::AccountId, T::MaxHasDispatchedSize>, ValueQuery>;

	#[pallet::pallet]
	pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);

	#[pallet::hooks]
	impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
		/// `on_initialize` to return the weight used in `on_finalize`.
		fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
			T::WeightInfo::on_finalize()
		}

		fn on_finalize(_n: BlockNumberFor<T>) {
			// cleanup for next block
			<HasDispatched<T, I>>::kill();
		}
	}

	#[pallet::view_functions]
	impl<T: Config<I>, I: 'static> Pallet<T, I> {
		/// Retrieve the aggregated oracle value for a specific key, including its timestamp.
		pub fn get_value(key: T::OracleKey) -> Option<TimestampedValueOf<T, I>> {
			Self::get(&key)
		}

		/// Retrieve every aggregated oracle value tracked by the pallet.
		pub fn all_values() -> Vec<(T::OracleKey, TimestampedValueOf<T, I>)> {
			<Values<T, I>>::iter().collect()
		}
	}

	#[pallet::call]
	impl<T: Config<I>, I: 'static> Pallet<T, I> {
		/// Feeds external data values into the oracle system.
		///
		/// ## Dispatch Origin
		///
		/// The dispatch origin of this call must be a signed account that is either:
		/// - A member of the oracle operators set (managed by [`SortedMembers`])
		/// - The root origin
		///
		/// ## Details
		///
		/// This function allows authorized oracle operators to submit timestamped key-value pairs
		/// into the oracle system. Each submitted value is immediately timestamped with the current
		/// block time and stored in the [`RawValues`] storage. The system then attempts to
		/// aggregate all raw values for each key using the configured [`CombineData`] trait
		/// implementation, updating the final [`Values`] storage with the aggregated result.
		///
		/// Only one submission per oracle operator per block is allowed to prevent spam and ensure
		/// fair participation. The function also triggers the [`OnNewData`] hook for each submitted
		/// value, allowing other pallets to react to new oracle data.
		///
		/// ## Errors
		///
		/// - [`Error::NoPermission`]: The sender is not authorized to feed data
		/// - [`Error::AlreadyFeeded`]: The sender has already fed data in the current block
		/// - [`Error::ExceedsMaxHasDispatchedSize`]: Too many operators have fed data in this block
		///
		/// ## Events
		///
		/// - [`Event::NewFeedData`]: Emitted when data is successfully fed into the oracle
		#[pallet::call_index(0)]
		#[pallet::weight(T::WeightInfo::feed_values(values.len() as u32))]
		pub fn feed_values(
			origin: OriginFor<T>,
			values: BoundedVec<(T::OracleKey, T::OracleValue), T::MaxFeedValues>,
		) -> DispatchResultWithPostInfo {
			let feeder = ensure_signed_or_root(origin.clone())?;

			let who = Self::ensure_account(feeder)?;

			// ensure account hasn't dispatched an updated yet
			<HasDispatched<T, I>>::try_mutate(|set| {
				set.try_insert(who.clone())
					.map_err(|_| Error::<T, I>::ExceedsMaxHasDispatchedSize)?
					.then_some(())
					.ok_or(Error::<T, I>::AlreadyFeeded)
			})?;

			Self::do_feed_values(who, values.into());
			Ok(Pays::No.into())
		}
	}
}

impl<T: Config<I>, I: 'static> Pallet<T, I> {
	fn get_pallet_account() -> T::AccountId {
		T::PalletId::get().into_account_truncating()
	}

	/// Reads the raw values for a given key from all oracle members.
	pub fn read_raw_values(key: &T::OracleKey) -> Vec<TimestampedValueOf<T, I>> {
		T::Members::sorted_members()
			.iter()
			.chain([Self::get_pallet_account()].iter())
			.filter_map(|x| Self::raw_values(x, key))
			.collect()
	}

	/// Returns the aggregated and timestamped value for a given key.
	pub fn get(key: &T::OracleKey) -> Option<TimestampedValueOf<T, I>> {
		Self::values(key)
	}

	fn combined(key: &T::OracleKey) -> Option<TimestampedValueOf<T, I>> {
		let values = Self::read_raw_values(key);
		T::CombineData::combine_data(key, values, Self::values(key))
	}

	fn ensure_account(who: Option<T::AccountId>) -> Result<T::AccountId, DispatchError> {
		// ensure feeder is authorized
		if let Some(who) = who {
			ensure!(T::Members::contains(&who), Error::<T, I>::NoPermission);
			Ok(who)
		} else {
			Ok(Self::get_pallet_account())
		}
	}

	fn do_feed_values(who: T::AccountId, values: Vec<(T::OracleKey, T::OracleValue)>) {
		let now = T::Time::now();
		for (key, value) in &values {
			let timestamped = TimestampedValue { value: value.clone(), timestamp: now };
			RawValues::<T, I>::insert(&who, key, timestamped);

			// Update `Values` storage if `combined` yielded result.
			if let Some(combined) = Self::combined(key) {
				<Values<T, I>>::insert(key, combined);
			}

			T::OnNewData::on_new_data(&who, key, value);
		}
		Self::deposit_event(Event::NewFeedData { sender: who, values });
	}
}

impl<T: Config<I>, I: 'static> ChangeMembers<T::AccountId> for Pallet<T, I> {
	fn change_members_sorted(
		_incoming: &[T::AccountId],
		outgoing: &[T::AccountId],
		_new: &[T::AccountId],
	) {
		// remove values
		for removed in outgoing {
			let _ = RawValues::<T, I>::clear_prefix(removed, u32::MAX, None);
		}
	}

	fn set_prime(_prime: Option<T::AccountId>) {
		// nothing
	}
}

impl<T: Config<I>, I: 'static> DataProvider<T::OracleKey, T::OracleValue> for Pallet<T, I> {
	fn get(key: &T::OracleKey) -> Option<T::OracleValue> {
		Self::get(key).map(|timestamped_value| timestamped_value.value)
	}
}
impl<T: Config<I>, I: 'static> DataProviderExtended<T::OracleKey, TimestampedValueOf<T, I>>
	for Pallet<T, I>
{
	fn get_all_values() -> impl Iterator<Item = (T::OracleKey, Option<TimestampedValueOf<T, I>>)> {
		<Values<T, I>>::iter().map(|(k, v)| (k, Some(v)))
	}
}

impl<T: Config<I>, I: 'static> DataFeeder<T::OracleKey, T::OracleValue, T::AccountId>
	for Pallet<T, I>
{
	fn feed_value(
		who: Option<T::AccountId>,
		key: T::OracleKey,
		value: T::OracleValue,
	) -> DispatchResult {
		Self::do_feed_values(Self::ensure_account(who)?, vec![(key, value)]);
		Ok(())
	}
}