Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Incomplete Callback Handler Spike #51

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pallet-chainlink-feed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub trait Trait: frame_system::Trait {
/// Number of rounds to keep around per feed.
type PruningWindow: Get<RoundId>;

type OnAnswerHandler: OnAnswerHandler<Self>;

/// The weight for this pallet's extrinsics.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -208,6 +210,16 @@ impl<B, V> TryFrom<Round<B, V>> for RoundData<B, V> {
}
}

pub trait OnAnswerHandler<T: Trait> {
fn on_answer(feed: T::FeedId, new_data: RoundData<T::BlockNumber, T::Value>);
}

impl<T: Trait> OnAnswerHandler<T> for () {
fn on_answer(_feed: T::FeedId, _new_data: RoundData<T::BlockNumber, T::Value>) {
// noop
}
}

/// Trait for interacting with the feeds in the pallet.
pub trait FeedOracle {
type FeedId: Parameter + BaseArithmetic;
Expand Down Expand Up @@ -596,6 +608,7 @@ decl_module! {
let updated_at = frame_system::Module::<T>::block_number();
round.updated_at = Some(updated_at);
round.answered_in_round = Some(round_id);
let round_data = round.clone().try_into().expect("round does not have empty fields and will therefore convert; qed");
Rounds::<T>::insert(feed_id, round_id, round);

feed.config.latest_round = round_id;
Expand All @@ -608,8 +621,11 @@ decl_module! {
Details::<T>::remove(feed_id, prev_round_id);
}

T::OnAnswerHandler::on_answer(feed_id, round_data);

Self::deposit_event(RawEvent::AnswerUpdated(
feed_id, round_id, new_answer, updated_at));

}

// update oracle rewards and try to reserve them
Expand Down
12 changes: 11 additions & 1 deletion substrate-node-example/pallets/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use frame_support::{decl_module, decl_storage, decl_event, decl_error, dispatch, traits::Get};
use frame_system::ensure_signed;

use pallet_chainlink_feed::{FeedInterface, FeedOracle, RoundData};
use pallet_chainlink_feed::{FeedInterface, FeedOracle, OnAnswerHandler, RoundData, Trait as FeedTrait};

#[cfg(test)]
mod mock;
Expand Down Expand Up @@ -43,11 +43,13 @@ decl_event!(
where
AccountId = <T as frame_system::Trait>::AccountId,
Value = <<<T as Trait>::Oracle as FeedOracle>::Feed as FeedInterface>::Value,
FeedValue = <T as FeedTrait>::Value,
{
/// Event documentation should end with an array that provides descriptive names for event
/// parameters. [something, who]
SomethingStored(u32, AccountId),
NewData(Value),
NewFeedValue(FeedValue),
}
);

Expand Down Expand Up @@ -122,3 +124,11 @@ decl_module! {
}
}
}

impl<T: Trait + FeedTrait> OnAnswerHandler<T> for Module<T> {
fn on_answer(feed: <T as FeedTrait>::FeedId, round_data: RoundData<T::BlockNumber, <T as FeedTrait>::Value>) {
let RoundData { answer, .. } = round_data;
frame_support::debug::info!("feed: {:?}, new data: {:?}", feed, answer);
Self::deposit_event(RawEvent::NewData(answer));
}
}
1 change: 1 addition & 0 deletions substrate-node-example/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ impl pallet_chainlink_feed::Trait for Runtime {
type OracleCountLimit = OracleCountLimit;
type FeedLimit = FeedLimit;
type PruningWindow = PruningWindow;
type OnAnswerHandler = TemplateModule;
type WeightInfo = ChainlinkWeightInfo;
}

Expand Down