From 74c97c1fc9060bdd150ba04e5cf8ce09ce4db497 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Mon, 29 Mar 2021 13:45:03 +0200 Subject: [PATCH] WIP on answer handler --- pallet-chainlink-feed/src/lib.rs | 16 ++++++++++++++++ .../pallets/template/src/lib.rs | 12 +++++++++++- substrate-node-example/runtime/src/lib.rs | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pallet-chainlink-feed/src/lib.rs b/pallet-chainlink-feed/src/lib.rs index 0ccad4b..7d1b668 100644 --- a/pallet-chainlink-feed/src/lib.rs +++ b/pallet-chainlink-feed/src/lib.rs @@ -66,6 +66,8 @@ pub trait Trait: frame_system::Trait { /// Number of rounds to keep around per feed. type PruningWindow: Get; + type OnAnswerHandler: OnAnswerHandler; + /// The weight for this pallet's extrinsics. type WeightInfo: WeightInfo; } @@ -208,6 +210,16 @@ impl TryFrom> for RoundData { } } +pub trait OnAnswerHandler { + fn on_answer(feed: T::FeedId, new_data: RoundData); +} + +impl OnAnswerHandler for () { + fn on_answer(_feed: T::FeedId, _new_data: RoundData) { + // noop + } +} + /// Trait for interacting with the feeds in the pallet. pub trait FeedOracle { type FeedId: Parameter + BaseArithmetic; @@ -596,6 +608,7 @@ decl_module! { let updated_at = frame_system::Module::::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::::insert(feed_id, round_id, round); feed.config.latest_round = round_id; @@ -608,8 +621,11 @@ decl_module! { Details::::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 diff --git a/substrate-node-example/pallets/template/src/lib.rs b/substrate-node-example/pallets/template/src/lib.rs index 28bdb30..d0d7300 100644 --- a/substrate-node-example/pallets/template/src/lib.rs +++ b/substrate-node-example/pallets/template/src/lib.rs @@ -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; @@ -43,11 +43,13 @@ decl_event!( where AccountId = ::AccountId, Value = <<::Oracle as FeedOracle>::Feed as FeedInterface>::Value, + FeedValue = ::Value, { /// Event documentation should end with an array that provides descriptive names for event /// parameters. [something, who] SomethingStored(u32, AccountId), NewData(Value), + NewFeedValue(FeedValue), } ); @@ -122,3 +124,11 @@ decl_module! { } } } + +impl OnAnswerHandler for Module { + fn on_answer(feed: ::FeedId, round_data: RoundData::Value>) { + let RoundData { answer, .. } = round_data; + frame_support::debug::info!("feed: {:?}, new data: {:?}", feed, answer); + Self::deposit_event(RawEvent::NewData(answer)); + } +} diff --git a/substrate-node-example/runtime/src/lib.rs b/substrate-node-example/runtime/src/lib.rs index 983476f..21324b3 100644 --- a/substrate-node-example/runtime/src/lib.rs +++ b/substrate-node-example/runtime/src/lib.rs @@ -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; }