Skip to main content

fynd_core/feed/
events.rs

1//! Market events for communication between the indexer and solvers.
2//!
3//! The indexer broadcasts these events when market data changes.
4//! Solvers subscribe to these events to keep their local graph in sync.
5
6use async_trait::async_trait;
7use rustc_hash::FxHashMap;
8use thiserror::Error;
9use tycho_simulation::tycho_common::models::Address;
10
11use crate::{graph::GraphError, types::ComponentId};
12
13/// Events broadcast by the indexer when market data changes.
14#[derive(Debug, Clone)]
15#[cfg_attr(test, derive(PartialEq))]
16pub enum MarketEvent {
17    /// Market was updated.
18    MarketUpdated {
19        /// Components added in this update, keyed by component ID.
20        added_components: FxHashMap<ComponentId, Vec<Address>>,
21        /// Component IDs that were removed.
22        removed_components: Vec<ComponentId>,
23        /// Component IDs whose state changed.
24        updated_components: Vec<ComponentId>,
25    },
26}
27
28/// Errors that can occur when handling market events.
29#[derive(Error, Debug)]
30pub enum EventError {
31    /// Graph-related errors
32    #[error("graph errors: {0:?}")]
33    GraphErrors(Vec<GraphError>),
34}
35
36/// Trait for components that can receive market events.
37#[async_trait]
38pub trait MarketEventHandler: Send {
39    /// Handle a market event.
40    ///
41    /// # Errors
42    ///
43    /// Returns an error if the event could not be processed.
44    async fn handle_event(&mut self, event: &MarketEvent) -> Result<(), EventError>;
45}