Skip to content

Commit

Permalink
move HasDispatched check to the extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed May 31, 2023
1 parent 055e7db commit 33afe6f
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,16 @@ pub mod module {
let feeder = ensure_signed(origin.clone())
.map(Some)
.or_else(|_| ensure_root(origin).map(|_| None))?;
Self::do_feed_values(feeder, values)?;

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

// ensure account hasn't dispatched an updated yet
ensure!(
HasDispatched::<T, I>::mutate(|set| set.insert(who.clone())),
Error::<T, I>::AlreadyFeeded
);

Self::do_feed_values(who, values)?;
Ok(Pays::No.into())
}
}
Expand Down Expand Up @@ -193,21 +202,17 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
T::CombineData::combine_data(key, values, Self::values(key))
}

fn do_feed_values(who: Option<T::AccountId>, values: Vec<(T::OracleKey, T::OracleValue)>) -> DispatchResult {
fn ensure_account(who: Option<T::AccountId>) -> Result<T::AccountId, DispatchError> {
// ensure feeder is authorized
let who = if let Some(who) = who {
if let Some(who) = who {
ensure!(T::Members::contains(&who), Error::<T, I>::NoPermission);
who
Ok(who)
} else {
T::RootOperatorAccountId::get()
};

// ensure account hasn't dispatched an updated yet
ensure!(
HasDispatched::<T, I>::mutate(|set| set.insert(who.clone())),
Error::<T, I>::AlreadyFeeded
);
Ok(T::RootOperatorAccountId::get())
}
}

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

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

0 comments on commit 33afe6f

Please sign in to comment.