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