diff --git a/event_feed/src/common/context.rs b/event_feed/src/common/context.rs index 1d8a4fd..6035156 100644 --- a/event_feed/src/common/context.rs +++ b/event_feed/src/common/context.rs @@ -1,14 +1,20 @@ -use std::sync::Arc; +use crate::IconFeed; +use crate::PolkadotFeed; +use subxt::PolkadotConfig; -use super::*; - -pub struct Context{ - pub feed_client: Arc, +pub enum Context { + PolkadotFeed(PolkadotFeed), + IconFeed(IconFeed), } -impl Context { - pub fn new(client: Arc) -> Self{ - Context { feed_client: client } +impl Context { + pub async fn feed_events(&self, cb: &dyn Fn(Vec)) { + match self { + Context::PolkadotFeed(feed) => feed.event_feed(cb).await, + Context::IconFeed(feed) => { + let _ =feed.event_feed(cb).await; + } + } } } diff --git a/event_feed/src/common/mod.rs b/event_feed/src/common/mod.rs index 36ff8ba..d15afb2 100644 --- a/event_feed/src/common/mod.rs +++ b/event_feed/src/common/mod.rs @@ -7,9 +7,7 @@ pub use kuska_client::*; mod context; pub use context::*; -pub trait EventFeeder: Send + 'static { - fn event_feeder(&self, cb: &dyn Fn(Vec)); -} +pub use super::*; #[derive(Deserialize, Debug, Clone)] pub struct ChainConfig { diff --git a/event_feed/src/icon/feeder.rs b/event_feed/src/icon/feeder.rs index 5fbc475..ae19174 100644 --- a/event_feed/src/icon/feeder.rs +++ b/event_feed/src/icon/feeder.rs @@ -1,5 +1,3 @@ -use crate::common::EventFeeder; - use super::*; pub struct IconFeed { @@ -128,13 +126,3 @@ impl IconFeed { } } } - -impl EventFeeder for IconFeed { - fn event_feeder(&self, cb: &dyn Fn(Vec)) { - let r = async { - // Simulate an async operation that resolves to a value - self.event_feed(cb).await.unwrap(); - }; - - } -} diff --git a/event_feed/src/main.rs b/event_feed/src/main.rs index d199140..0a058be 100644 --- a/event_feed/src/main.rs +++ b/event_feed/src/main.rs @@ -1,9 +1,6 @@ -use std::borrow::BorrowMut; -use std::sync::Arc; - use crate::common::Context; use crate::common::Producer; -use common::{ChainConfig, EventFeeder, ProducerConfig}; +use common::{ChainConfig, ProducerConfig}; use icon::IconFeed; use substrate::types::PolkadotFeed; use subxt::PolkadotConfig; @@ -23,56 +20,29 @@ async fn main() { let mut ssb_client = Producer::new(producer_config.clone()).await.unwrap(); ssb_client.accept_invite().await.unwrap(); - match chain_config.clone().chain.to_lowercase().as_str() { + let feed: Context = match chain_config.clone().chain.to_lowercase().as_str() { "substrate" => { - let s = Arc::new(PolkadotFeed::::new(chain_config.clone()).await); - let feed = Context::new(s); - feed.feed_client - .event_feed(&|e| { - for i in e { - tokio::spawn(async move { - let producer_config = envy::from_env::().unwrap(); - let mut ssb_client = Producer::new(producer_config.clone()).await.unwrap(); - ssb_client - .publish_feed(i) - .await - .expect("Failed to send event"); - }); - } - }) - .await; + let polkadot_client = PolkadotFeed::::new(chain_config.clone()).await; + Context::PolkadotFeed(polkadot_client) } "icon" => { - let s = Arc::new(IconFeed::new(chain_config.clone()).unwrap()); - let feed = Context::new(s); - feed.feed_client - .event_feed(&|e| { - for i in e { - tokio::spawn(async move { - let producer_config = envy::from_env::().unwrap(); - let mut ssb_client = Producer::new(producer_config.clone()).await.unwrap(); - ssb_client - .publish_feed(i) - .await - .expect("Failed to send event"); - }); - } - }) - .await.unwrap(); + let icon_client = IconFeed::new(chain_config.clone()).unwrap(); + Context::IconFeed(icon_client) } - _ => todo!(), + _ => panic!("Invalid Chain"), }; - let feed: Context = match chain_config.clone().chain.to_lowercase().as_str() { - "substrate" => { - let s = Arc::new(PolkadotFeed::::new(chain_config.clone()).await); - Context::new(s) - } - "icon" => { - let s = Arc::new(IconFeed::new(chain_config.clone()).unwrap()); - Context::new(s) + feed.feed_events(&|e| { + for i in e { + tokio::spawn(async move { + let producer_config = envy::from_env::().unwrap(); + let mut ssb_client = Producer::new(producer_config.clone()).await.unwrap(); + ssb_client + .publish_feed(i) + .await + .expect("Failed to send event"); + }); } - _ => todo!(), - }; - + }) + .await; } diff --git a/event_feed/src/substrate/mod.rs b/event_feed/src/substrate/mod.rs index e0a64bf..dcf8e4c 100644 --- a/event_feed/src/substrate/mod.rs +++ b/event_feed/src/substrate/mod.rs @@ -1,8 +1,8 @@ -pub mod types; -use types::*; +#[cfg(test)] mod tests; +pub mod types; use serde::{Deserialize, Serialize}; -use subxt::{Config, OnlineClient, PolkadotConfig}; +use subxt::{OnlineClient, PolkadotConfig}; #[subxt::subxt(runtime_metadata_path = "./src/common/utils/polkadot_metadata_full.scale")] pub mod polkadot {} diff --git a/event_feed/src/substrate/tests.rs b/event_feed/src/substrate/tests.rs index c64b90c..98108e2 100644 --- a/event_feed/src/substrate/tests.rs +++ b/event_feed/src/substrate/tests.rs @@ -1,32 +1,41 @@ +use tests::types::PolkadotFeed; + use super::*; +#[cfg(test)] use crate::ChainConfig; #[tokio::test] async fn test_filter_events() { let config = ChainConfig { - chain:"Susbtrate".to_string(), + chain: "Susbtrate".to_string(), node_url: "wss://rococo-rpc.polkadot.io".to_string(), event_filter: "balances=Transfer".to_string(), contract_filter: "".to_string(), }; let polkadot = PolkadotFeed::::new(config).await; - let result = polkadot - .split_filter(); + let result = polkadot.split_filter(); - assert_eq!(result, std::collections::HashMap::from([("balances".to_string(), vec!["Transfer"])])); + assert_eq!( + result, + std::collections::HashMap::from([("balances".to_string(), vec!["Transfer"])]) + ); } #[tokio::test] async fn test_filter_events_argument_with_no_method() { let config = ChainConfig { - chain:"Susbtrate".to_string(), + chain: "Susbtrate".to_string(), node_url: "wss://rococo-rpc.polkadot.io".to_string(), event_filter: "balances=Transfer;system".to_string(), contract_filter: "".to_string(), }; let polkadot = PolkadotFeed::::new(config).await; - let result = polkadot - .split_filter(); + let result = polkadot.split_filter(); - assert_eq!(result, std::collections::HashMap::from([("balances".to_string(), vec!["Transfer"]), ("system".to_string(), vec![])])); + assert_eq!( + result, + std::collections::HashMap::from([ + ("balances".to_string(), vec!["Transfer"]), + ("system".to_string(), vec![]) + ]) + ); } - diff --git a/event_feed/src/substrate/types.rs b/event_feed/src/substrate/types.rs index ba88f20..dc6ff4e 100644 --- a/event_feed/src/substrate/types.rs +++ b/event_feed/src/substrate/types.rs @@ -1,7 +1,7 @@ use super::*; -use crate::common::{ChainConfig, EventFeeder}; +use crate::common::ChainConfig; -#[derive(Debug,Clone)] +#[derive(Debug, Clone)] pub struct PolkadotFeed { chain_config: ChainConfig, @@ -26,7 +26,7 @@ impl PolkadotFeed { /// asynchronous function that takes a callback function `cb` as a parameter. This function /// subscribes to finalized blocks on the Polkadot chain and processes the extrinsics and events /// within those blocks. - pub async fn event_feed(&self, cb: &dyn Fn( Vec)) { + pub async fn event_feed(&self, cb: &dyn Fn(Vec)) { let mut blocks_sub = self.client.blocks().subscribe_finalized().await.unwrap(); // For each block, print a bunch of information about it: @@ -111,16 +111,3 @@ macro_rules! events { .collect::, _>>(); }; } - - -impl EventFeeder for PolkadotFeed{ - fn event_feeder(&self, cb: &dyn Fn( Vec)) { - let r = async { - // Simulate an async operation that resolves to a value - self.event_feed(cb).await; - - }; - } - - -} \ No newline at end of file