Skip to content

Commit 33afe6f

Browse files
committed
move HasDispatched check to the extrinsic
1 parent 055e7db commit 33afe6f

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

oracle/src/lib.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,16 @@ pub mod module {
163163
let feeder = ensure_signed(origin.clone())
164164
.map(Some)
165165
.or_else(|_| ensure_root(origin).map(|_| None))?;
166-
Self::do_feed_values(feeder, values)?;
166+
167+
let who = Self::ensure_account(feeder)?;
168+
169+
// ensure account hasn't dispatched an updated yet
170+
ensure!(
171+
HasDispatched::<T, I>::mutate(|set| set.insert(who.clone())),
172+
Error::<T, I>::AlreadyFeeded
173+
);
174+
175+
Self::do_feed_values(who, values)?;
167176
Ok(Pays::No.into())
168177
}
169178
}
@@ -193,21 +202,17 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
193202
T::CombineData::combine_data(key, values, Self::values(key))
194203
}
195204

196-
fn do_feed_values(who: Option<T::AccountId>, values: Vec<(T::OracleKey, T::OracleValue)>) -> DispatchResult {
205+
fn ensure_account(who: Option<T::AccountId>) -> Result<T::AccountId, DispatchError> {
197206
// ensure feeder is authorized
198-
let who = if let Some(who) = who {
207+
if let Some(who) = who {
199208
ensure!(T::Members::contains(&who), Error::<T, I>::NoPermission);
200-
who
209+
Ok(who)
201210
} else {
202-
T::RootOperatorAccountId::get()
203-
};
204-
205-
// ensure account hasn't dispatched an updated yet
206-
ensure!(
207-
HasDispatched::<T, I>::mutate(|set| set.insert(who.clone())),
208-
Error::<T, I>::AlreadyFeeded
209-
);
211+
Ok(T::RootOperatorAccountId::get())
212+
}
213+
}
210214

215+
fn do_feed_values(who: T::AccountId, values: Vec<(T::OracleKey, T::OracleValue)>) -> DispatchResult {
211216
let now = T::Time::now();
212217
for (key, value) in &values {
213218
let timestamped = TimestampedValue {
@@ -259,6 +264,6 @@ impl<T: Config<I>, I: 'static> DataProviderExtended<T::OracleKey, TimestampedVal
259264

260265
impl<T: Config<I>, I: 'static> DataFeeder<T::OracleKey, T::OracleValue, T::AccountId> for Pallet<T, I> {
261266
fn feed_value(who: T::AccountId, key: T::OracleKey, value: T::OracleValue) -> DispatchResult {
262-
Self::do_feed_values(Some(who), vec![(key, value)])
267+
Self::do_feed_values(Self::ensure_account(Some(who))?, vec![(key, value)])
263268
}
264269
}

0 commit comments

Comments
 (0)