Skip to main content

tycho_simulation/evm/
decoder.rs

1use std::{
2    any::Any,
3    collections::{hash_map::Entry, HashMap, HashSet},
4    future::Future,
5    pin::Pin,
6    sync::Arc,
7};
8
9use alloy::primitives::{Address, U256};
10use thiserror::Error;
11use tokio::sync::{watch, RwLock, RwLockReadGuard};
12use tracing::{debug, error, info, warn};
13use tycho_client::feed::{synchronizer::ComponentWithState, BlockHeader, FeedMessage, HeaderLike};
14use tycho_common::{
15    dto::{ChangeType, ProtocolStateDelta},
16    models::{blockchain::BlockAggregatedChanges, token::Token, Chain},
17    simulation::protocol_sim::{Balances, BlockContext, ProtocolSim},
18    Bytes,
19};
20#[cfg(test)]
21use {
22    mockall::mock,
23    num_bigint::BigUint,
24    tycho_common::simulation::{
25        errors::{SimulationError, TransitionError},
26        protocol_sim::GetAmountOutResult,
27    },
28};
29
30use crate::{
31    evm::{
32        engine_db::{update_engine, SHARED_TYCHO_DB},
33        override_stream::{OverrideSnapshot, StateOverrideProvider},
34        protocol::{
35            utils::bytes_to_address,
36            vm::{constants::ERC20_PROXY_BYTECODE, erc20_token::IMPLEMENTATION_SLOT},
37        },
38        simulation::{BlockEnvOverrides, PendingOverrides},
39        tycho_models::{AccountUpdate, ResponseAccount},
40    },
41    protocol::{
42        errors::InvalidSnapshotError,
43        models::{DecoderContext, ProtocolComponent, TryFromWithBlock, Update},
44    },
45};
46
47#[derive(Error, Debug)]
48pub enum StreamDecodeError {
49    #[error("{0}")]
50    Fatal(String),
51}
52
53#[derive(Default)]
54struct DecoderState {
55    tokens: HashMap<Bytes, Token>,
56    states: HashMap<String, Box<dyn ProtocolSim>>,
57    components: HashMap<String, ProtocolComponent>,
58    // maps contract address to the pools they affect
59    contracts_map: HashMap<Bytes, HashSet<String>>,
60    // Maps original token address to their new proxy token address
61    proxy_token_addresses: HashMap<Address, Address>,
62    // Set of failed components, these are components that failed to decode and will not be emitted
63    // again TODO: handle more gracefully inside tycho-client. We could fetch the snapshot and
64    // try to decode it again.
65    failed_components: HashSet<String>,
66    // The block number of the last confirmed block decoded via `decode()`.
67    current_block_number: u64,
68}
69
70type DecodeFut =
71    Pin<Box<dyn Future<Output = Result<Box<dyn ProtocolSim>, InvalidSnapshotError>> + Send + Sync>>;
72type AccountBalances = HashMap<Bytes, HashMap<Bytes, Bytes>>;
73type RegistryFn<H> = dyn Fn(
74        ComponentWithState,
75        H,
76        AccountBalances,
77        Arc<RwLock<DecoderState>>,
78        Option<watch::Receiver<OverrideSnapshot>>,
79    ) -> DecodeFut
80    + Send
81    + Sync;
82type FilterFn = fn(&ComponentWithState) -> bool;
83
84/// A decoder to process raw messages.
85///
86/// This struct decodes incoming messages of type `FeedMessage` and converts it into the
87/// `BlockUpdate` struct.
88///
89/// # Important:
90/// - Supports registering exchanges and their associated filters for specific protocol components.
91/// - Allows the addition of client-side filters for custom conditions.
92///
93/// **Note:** Tokens provided via [`set_tokens`](Self::set_tokens) are used to decode startup
94/// snapshots and initialize protocol states. This is not an ongoing filter — components arriving
95/// after startup include their own token metadata.
96pub struct TychoStreamDecoder<H>
97where
98    H: HeaderLike,
99{
100    state: Arc<RwLock<DecoderState>>,
101    skip_state_decode_failures: bool,
102    min_token_quality: u32,
103    registry: HashMap<String, Box<RegistryFn<H>>>,
104    inclusion_filters: HashMap<String, Vec<FilterFn>>,
105    /// Protocols whose components drop an unknown token and keep the rest.
106    protocols_skip_unknown_tokens: HashSet<String>,
107    /// Live override providers keyed by `protocol_system`. A pool of that protocol subscribes to
108    /// its provider at creation time and reads fresh overrides on every simulation.
109    override_providers: HashMap<String, Arc<dyn StateOverrideProvider>>,
110    /// Seconds between blocks, used to project a confirmed block header onto the next block when
111    /// deriving the execution block for block-sensitive states.
112    block_time_secs: u64,
113    /// Chain every component this decoder handles lives on. Stamped into each registered
114    /// [`DecoderContext`] so chain-specific decoders cannot be pointed at the wrong chain.
115    chain: Chain,
116}
117
118/// Curve migrated from the generic VM adapter (`EVMPoolState`) to the native [`CurveState`]
119/// decoder. Returns true when `vm:curve` is registered with any other type — i.e. the deprecated
120/// VM-adapter path, still supported for a few releases before removal.
121fn is_deprecated_curve_registration<T: 'static>(exchange: &str) -> bool {
122    exchange == "vm:curve" &&
123        std::any::type_name::<T>() !=
124            std::any::type_name::<crate::evm::protocol::curve::CurveState>()
125}
126
127impl<H> TychoStreamDecoder<H>
128where
129    H: HeaderLike + Clone + Sync + Send + 'static + std::fmt::Debug,
130{
131    /// Creates a decoder for `chain`.
132    ///
133    /// # Panics
134    ///
135    /// Panics if `chain` is a custom chain with no registered config.
136    pub fn new(chain: Chain) -> Self {
137        Self {
138            state: Arc::new(RwLock::new(DecoderState::default())),
139            skip_state_decode_failures: false,
140            min_token_quality: 100,
141            registry: HashMap::new(),
142            inclusion_filters: HashMap::new(),
143            protocols_skip_unknown_tokens: HashSet::new(),
144            override_providers: HashMap::new(),
145            block_time_secs: chain.block_time_secs(),
146            chain,
147        }
148    }
149
150    /// The block a quote produced from `header` is expected to execute in.
151    ///
152    /// A partial (flashblock) header describes a block that is still open, so a quote can still
153    /// land in it. A confirmed header describes a closed block, so the quote targets the next one.
154    fn execution_block(&self, header: &BlockHeader) -> BlockContext {
155        if header.partial_block_index.is_some() {
156            BlockContext::new(header.number, header.timestamp)
157        } else {
158            BlockContext::new(header.number + 1, header.timestamp + self.block_time_secs)
159        }
160    }
161
162    /// Advances every state to `execution_block` via [`ProtocolSim::apply_block`].
163    ///
164    /// States already being emitted are advanced in place. Stored states absent from this
165    /// message are advanced in place under the write guard and cloned into `updated_states`
166    /// only when their quoting behavior changed — so an idle block-sensitive pool costs one
167    /// virtual call per message and zero clones, and consumers are only told about pools whose
168    /// quotes actually moved. Stored states of failed or removed components are left untouched,
169    /// so a component the consumer was told is gone is never re-emitted.
170    fn refresh_execution_block<C>(
171        updated_states: &mut HashMap<String, Box<dyn ProtocolSim>>,
172        stored_states: &mut HashMap<String, Box<dyn ProtocolSim>>,
173        failed_components: &HashSet<String>,
174        removed_components: &HashMap<String, C>,
175        execution_block: &BlockContext,
176    ) {
177        for state in updated_states.values_mut() {
178            state.apply_block(execution_block);
179        }
180        for (id, state) in stored_states.iter_mut() {
181            if failed_components.contains(id) ||
182                removed_components.contains_key(id) ||
183                updated_states.contains_key(id)
184            {
185                continue;
186            }
187            if state.apply_block(execution_block) {
188                updated_states.insert(id.clone(), state.clone_box());
189            }
190        }
191    }
192
193    /// Registers `provider` as the live override source for `protocol_system`.
194    ///
195    /// Pools of that protocol subscribe to it at creation time, so overrides apply from the first
196    /// simulation onward. A later call for the same `protocol_system` replaces the previous
197    /// provider.
198    pub fn set_override_provider(
199        &mut self,
200        protocol_system: String,
201        provider: Arc<dyn StateOverrideProvider>,
202    ) {
203        self.override_providers
204            .insert(protocol_system, provider);
205    }
206
207    /// Provides token metadata used to decode startup snapshots and initialize protocol states.
208    ///
209    /// This is not an ongoing stream filter. Components arriving after startup include their
210    /// own token metadata for decoding.
211    pub async fn set_tokens(&self, tokens: HashMap<Bytes, Token>) {
212        let mut guard = self.state.write().await;
213        guard.tokens = tokens;
214    }
215
216    pub fn skip_state_decode_failures(&mut self, skip: bool) {
217        self.skip_state_decode_failures = skip;
218    }
219
220    /// Sets the minimum token quality for decoding.
221    ///
222    /// Tokens arriving in stream deltas below this threshold are ignored. Defaults to 100.
223    /// Set this to the same value used in [`load_all_tokens()`](crate::utils::load_all_tokens) to
224    /// apply consistent filtering.
225    pub fn min_token_quality(&mut self, quality: u32) {
226        self.min_token_quality = quality;
227    }
228
229    /// Registers a decoder for a given exchange with a decoder context.
230    ///
231    /// This method maps an exchange identifier to a specific protocol simulation type.
232    /// The associated type must implement the `TryFromWithBlock` trait to enable decoding
233    /// of state updates from `ComponentWithState` objects. This allows the decoder to transform
234    /// the component data into the appropriate protocol simulation type based on the current
235    /// blockchain state and the provided block header.
236    /// For example, to register a decoder for the `uniswap_v2` exchange with an additional decoder
237    /// context, you must call this function with
238    /// `register_decoder_with_context::<UniswapV2State>("uniswap_v2", context)`.
239    /// This ensures that the exchange ID `uniswap_v2` is properly associated with the
240    /// `UniswapV2State` decoder for use in the protocol stream.
241    ///
242    /// The decoder's own chain is stamped into `context`, overriding any chain the caller set.
243    pub fn register_decoder_with_context<T>(&mut self, exchange: &str, mut context: DecoderContext)
244    where
245        T: ProtocolSim
246            + TryFromWithBlock<ComponentWithState, H, Error = InvalidSnapshotError>
247            + Send
248            + 'static,
249    {
250        if let Some(requested) = context
251            .chain
252            .filter(|requested| *requested != self.chain)
253        {
254            warn!(
255                exchange,
256                requested_chain = %requested,
257                decoder_chain = %self.chain,
258                "DecoderContext declares a different chain than the decoder; using the decoder's \
259                 chain"
260            );
261        }
262        context.chain = Some(self.chain);
263
264        if is_deprecated_curve_registration::<T>(exchange) {
265            warn!(
266                registered_type = std::any::type_name::<T>(),
267                "Registering \"vm:curve\" with the generic VM adapter is deprecated; register the \
268                 native `CurveState` decoder instead (`exchange::<CurveState>(\"vm:curve\", ...)`). \
269                 The VM-adapter path still works but will be removed in a future release."
270            );
271        }
272        let decoder = Box::new(
273            move |component: ComponentWithState,
274                  header: H,
275                  account_balances: AccountBalances,
276                  state: Arc<RwLock<DecoderState>>,
277                  live_override: Option<watch::Receiver<OverrideSnapshot>>| {
278                let mut context = context.clone();
279                context.live_override = live_override;
280                Box::pin(async move {
281                    let guard = state.read().await;
282                    T::try_from_with_header(
283                        component,
284                        header,
285                        &account_balances,
286                        &guard.tokens,
287                        &context,
288                    )
289                    .await
290                    .map(|c| Box::new(c) as Box<dyn ProtocolSim>)
291                }) as DecodeFut
292            },
293        );
294        self.registry
295            .insert(exchange.to_string(), decoder);
296    }
297
298    /// Registers a decoder for a given exchange.
299    ///
300    /// This method maps an exchange identifier to a specific protocol simulation type.
301    /// The associated type must implement the `TryFromWithBlock` trait to enable decoding
302    /// of state updates from `ComponentWithState` objects. This allows the decoder to transform
303    /// the component data into the appropriate protocol simulation type based on the current
304    /// blockchain state and the provided block header.
305    /// For example, to register a decoder for the `uniswap_v2` exchange, you must call
306    /// this function with `register_decoder::<UniswapV2State>("uniswap_v2", vm_attributes)`.
307    /// This ensures that the exchange ID `uniswap_v2` is properly associated with the
308    /// `UniswapV2State` decoder for use in the protocol stream.
309    pub fn register_decoder<T>(&mut self, exchange: &str)
310    where
311        T: ProtocolSim
312            + TryFromWithBlock<ComponentWithState, H, Error = InvalidSnapshotError>
313            + Send
314            + 'static,
315    {
316        let context = DecoderContext::new();
317        self.register_decoder_with_context::<T>(exchange, context);
318    }
319
320    /// Registers a decoder for `exchange` that drops a token it does not know, or whose address it
321    /// cannot decode, and keeps the component. Other registrations fail the component. Use it for
322    /// a component with a fixed id, such as an all-pairs RFQ component: a failed one stays failed.
323    pub fn register_decoder_dropping_unknown_tokens<T>(&mut self, exchange: &str)
324    where
325        T: ProtocolSim
326            + TryFromWithBlock<ComponentWithState, H, Error = InvalidSnapshotError>
327            + Send
328            + 'static,
329    {
330        self.register_decoder::<T>(exchange);
331        self.protocols_skip_unknown_tokens
332            .insert(exchange.to_string());
333    }
334
335    /// Registers a client-side filter function for a given exchange.
336    ///
337    /// Associates a filter function with an exchange ID, enabling custom filtering of protocol
338    /// components. The filter function is applied client-side to refine the data received from the
339    /// stream. It can be used to exclude certain components based on attributes or conditions that
340    /// are not supported by the server-side filtering logic. This is particularly useful for
341    /// implementing custom behaviors, such as:
342    /// - Filtering out pools with specific attributes (e.g., unsupported features).
343    /// - Blacklisting pools based on custom criteria.
344    /// - Excluding pools that do not meet certain requirements (e.g., token pairs or liquidity
345    ///   constraints).
346    ///
347    /// For example, you might use a filter to exclude pools that are not fully supported in the
348    /// protocol, or to ignore pools with certain attributes that are irrelevant to your
349    /// application.
350    ///
351    /// Filters accumulate: registering a second predicate for the same exchange keeps the first,
352    /// and a component is admitted only when every registered predicate accepts it.
353    pub fn register_filter(&mut self, exchange: &str, predicate: FilterFn) {
354        self.inclusion_filters
355            .entry(exchange.to_string())
356            .or_default()
357            .push(predicate);
358    }
359
360    /// Whether every filter registered for `exchange` accepts `snapshot`. An exchange with no
361    /// registered filter admits every component.
362    fn admits(&self, exchange: &str, snapshot: &ComponentWithState) -> bool {
363        let Some(predicates) = self.inclusion_filters.get(exchange) else { return true };
364        predicates
365            .iter()
366            .all(|predicate| predicate(snapshot))
367    }
368
369    /// Decodes a `FeedMessage` into a `BlockUpdate` containing the updated states of protocol
370    /// components
371    pub async fn decode(&self, msg: &FeedMessage<H>) -> Result<Update, StreamDecodeError> {
372        // stores all states updated in this tick/msg
373        let mut updated_states = HashMap::new();
374        let mut new_pairs = HashMap::new();
375        let mut removed_pairs = HashMap::new();
376        let mut contracts_map = HashMap::new();
377        let mut msg_failed_components = HashSet::new();
378
379        let header = msg
380            .state_msgs
381            .values()
382            .next()
383            .ok_or_else(|| StreamDecodeError::Fatal("Missing block!".into()))?
384            .header
385            .clone();
386
387        let block_number = header
388            .clone()
389            .block_number_or_timestamp();
390        let current_block = header.clone().block();
391        let is_partial = current_block
392            .as_ref()
393            .map(|h| h.partial_block_index.is_some())
394            .unwrap_or(false);
395
396        for (protocol, protocol_msg) in msg.state_msgs.iter() {
397            let drops_unknown_tokens = self
398                .protocols_skip_unknown_tokens
399                .contains(protocol.as_str());
400            // Add any new tokens
401            if let Some(deltas) = protocol_msg.deltas.as_ref() {
402                let mut state_guard = self.state.write().await;
403
404                let new_tokens = deltas
405                    .new_tokens
406                    .iter()
407                    .filter(|(addr, t)| {
408                        t.quality >= self.min_token_quality &&
409                            !state_guard.tokens.contains_key(*addr)
410                    })
411                    .map(|(addr, t)| (addr.clone(), t.clone()))
412                    .collect::<HashMap<Bytes, Token>>();
413
414                if !new_tokens.is_empty() {
415                    debug!(n = new_tokens.len(), "NewTokens");
416                    state_guard.tokens.extend(new_tokens);
417                }
418            }
419
420            // Remove untracked components
421            {
422                let mut state_guard = self.state.write().await;
423                let removed_components: Vec<(String, ProtocolComponent)> = protocol_msg
424                    .removed_components
425                    .iter()
426                    .map(|(id, comp)| {
427                        if *id != comp.id {
428                            error!(
429                                "Component id mismatch in removed components {id} != {}",
430                                comp.id
431                            );
432                            return Err(StreamDecodeError::Fatal("Component id mismatch".into()));
433                        }
434
435                        let tokens = comp
436                            .tokens
437                            .iter()
438                            .flat_map(|addr| state_guard.tokens.get(addr).cloned())
439                            .collect::<Vec<_>>();
440
441                        if tokens.len() == comp.tokens.len() {
442                            Ok(Some((
443                                id.clone(),
444                                ProtocolComponent::from_with_tokens(comp.clone(), tokens),
445                            )))
446                        } else {
447                            Ok(None)
448                        }
449                    })
450                    .collect::<Result<Vec<Option<(String, ProtocolComponent)>>, StreamDecodeError>>(
451                    )?
452                    .into_iter()
453                    .flatten()
454                    .collect();
455
456                // Remove components from state and add to removed_pairs
457                for (id, component) in removed_components {
458                    state_guard.components.remove(&id);
459                    state_guard.states.remove(&id);
460                    removed_pairs.insert(id, component);
461                }
462
463                // UPDATE VM STORAGE
464                info!(
465                    "Processing {} contracts from snapshots",
466                    protocol_msg
467                        .snapshots
468                        .get_vm_storage()
469                        .len()
470                );
471
472                let mut proxy_token_accounts: HashMap<Address, AccountUpdate> = HashMap::new();
473                let mut storage_by_address: HashMap<Address, ResponseAccount> = HashMap::new();
474                for (key, value) in protocol_msg
475                    .snapshots
476                    .get_vm_storage()
477                    .iter()
478                {
479                    let account: ResponseAccount = value.clone().into();
480
481                    if state_guard.tokens.contains_key(key) {
482                        let original_address = account.address;
483                        // To work with Tycho's token overwrites system, if we get account
484                        // snapshots for a token we must handle them with a proxy/wrapper
485                        // contract.
486                        // Note: storage for the original contract must be set at the proxy
487                        // contract address. This is because the proxy contract uses
488                        // delegatecall to the original (implementation) contract.
489
490                        // Handle proxy token accounts
491                        let (impl_addr, proxy_state) = match state_guard
492                            .proxy_token_addresses
493                            .get(&original_address)
494                        {
495                            Some(impl_addr) => {
496                                // Token already has a proxy contract, simply update it.
497
498                                // Note: we apply the snapshot as an update. This is to cover the
499                                // case where a contract may be stale as it stopped being tracked
500                                // for some reason (e.g. due to a drop in tvl) and is now being
501                                // tracked again.
502                                let proxy_state = AccountUpdate::new(
503                                    original_address,
504                                    value.chain,
505                                    account.slots.clone(),
506                                    Some(account.native_balance),
507                                    None,
508                                    ChangeType::Update,
509                                );
510                                (*impl_addr, proxy_state)
511                            }
512                            None => {
513                                // Token does not have a proxy contract yet, create one
514
515                                // Assign original token contract to new address
516                                let impl_addr = generate_proxy_token_address(
517                                    state_guard.proxy_token_addresses.len(),
518                                )?;
519                                state_guard
520                                    .proxy_token_addresses
521                                    .insert(original_address, impl_addr);
522
523                                // Add proxy token contract at original token address
524                                let proxy_state = create_proxy_token_account(
525                                    original_address,
526                                    Some(impl_addr),
527                                    &account.slots,
528                                    value.chain,
529                                    Some(account.native_balance),
530                                );
531
532                                (impl_addr, proxy_state)
533                            }
534                        };
535
536                        proxy_token_accounts.insert(original_address, proxy_state);
537
538                        // Assign original token contract to the implementation address
539                        let impl_update = ResponseAccount {
540                            address: impl_addr,
541                            slots: HashMap::new(),
542                            ..account.clone()
543                        };
544                        storage_by_address.insert(impl_addr, impl_update);
545                    } else {
546                        // Not a token, apply snapshot to the account at its original address
547                        storage_by_address.insert(account.address, account);
548                    }
549                }
550
551                // Split proxy accounts by change type:
552                // - Creation: new proxies that must overwrite any existing placeholder
553                // - Update: existing proxies whose storage is being refreshed (handled normally)
554                let mut proxy_creates: Vec<AccountUpdate> = Vec::new();
555                let mut proxy_updates: HashMap<Address, AccountUpdate> = HashMap::new();
556                for (addr, update) in proxy_token_accounts {
557                    if matches!(update.change, ChangeType::Creation) {
558                        proxy_creates.push(update);
559                    } else {
560                        proxy_updates.insert(addr, update);
561                    }
562                }
563
564                info!("Updating engine with {} contracts from snapshots", storage_by_address.len());
565                update_engine(
566                    SHARED_TYCHO_DB.clone(),
567                    header.clone().block(),
568                    Some(storage_by_address),
569                    proxy_updates,
570                )
571                .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
572
573                // Force-overwrite new proxy token accounts so that authoritative vm_storage data
574                // always wins over any empty placeholder previously inserted by engine setup
575                // (which uses init_account / init-if-not-exists).
576                if !proxy_creates.is_empty() {
577                    SHARED_TYCHO_DB
578                        .force_update_accounts(proxy_creates)
579                        .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
580                }
581                info!("Engine updated");
582                drop(state_guard);
583            }
584
585            // Construct a contract to token balances map: HashMap<ContractAddress,
586            // HashMap<TokenAddress, Balance>>
587            let account_balances = protocol_msg
588                .clone()
589                .snapshots
590                .get_vm_storage()
591                .iter()
592                .filter_map(|(addr, acc)| {
593                    if acc.token_balances.is_empty() {
594                        return None;
595                    }
596                    let balances = acc
597                        .token_balances
598                        .iter()
599                        .map(|(token_addr, ab)| (token_addr.clone(), ab.balance.clone()))
600                        .collect::<HashMap<Bytes, Bytes>>();
601                    Some((addr.clone(), balances))
602                })
603                .collect::<AccountBalances>();
604
605            let mut new_components = HashMap::new();
606            let mut count_token_skips = 0;
607            let mut components_to_store = HashMap::new();
608            {
609                let state_guard = self.state.read().await;
610
611                // PROCESS SNAPSHOTS
612                'snapshot_loop: for (id, snapshot) in protocol_msg
613                    .snapshots
614                    .get_states()
615                    .clone()
616                {
617                    // Skip any unsupported pools
618                    if !self.admits(protocol.as_str(), &snapshot) {
619                        continue;
620                    }
621
622                    // Construct component from snapshot
623                    let mut component_tokens = Vec::new();
624                    let mut new_tokens_accounts = HashMap::new();
625                    for token in snapshot.component.tokens.clone() {
626                        match state_guard.tokens.get(&token) {
627                            Some(token) => {
628                                // If the token is not an existing proxy token, we need to add it to
629                                // the simulation engine
630                                let token_address = match bytes_to_address(&token.address) {
631                                    Ok(addr) => addr,
632                                    Err(_) if drops_unknown_tokens => {
633                                        warn!(
634                                            "Token address could not be decoded {}, ignoring the token in component {:x?}",
635                                            token.address, id
636                                        );
637                                        continue;
638                                    }
639                                    Err(_) => {
640                                        count_token_skips += 1;
641                                        msg_failed_components.insert(id.clone());
642                                        warn!(
643                                            "Token address could not be decoded {}, ignoring pool {:x?}",
644                                            token.address, id
645                                        );
646                                        continue 'snapshot_loop;
647                                    }
648                                };
649                                component_tokens.push(token.clone());
650                                // Deploy a proxy account without an implementation set
651                                if !state_guard
652                                    .proxy_token_addresses
653                                    .contains_key(&token_address)
654                                {
655                                    new_tokens_accounts.insert(
656                                        token_address,
657                                        create_proxy_token_account(
658                                            token_address,
659                                            None,
660                                            &HashMap::new(),
661                                            snapshot.component.chain,
662                                            None,
663                                        ),
664                                    );
665                                }
666                            }
667                            None if drops_unknown_tokens => {
668                                warn!(
669                                    "Token not found {}, ignoring the token in component {:x?}",
670                                    token, id
671                                );
672                            }
673                            None => {
674                                count_token_skips += 1;
675                                msg_failed_components.insert(id.clone());
676                                debug!("Token not found {}, ignoring pool {:x?}", token, id);
677                                continue 'snapshot_loop;
678                            }
679                        }
680                    }
681                    let component = ProtocolComponent::from_with_tokens(
682                        snapshot.component.clone(),
683                        component_tokens,
684                    );
685
686                    // Add new tokens to the simulation engine
687                    if !new_tokens_accounts.is_empty() {
688                        update_engine(
689                            SHARED_TYCHO_DB.clone(),
690                            header.clone().block(),
691                            None,
692                            new_tokens_accounts,
693                        )
694                        .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
695                    }
696
697                    // collect contracts:ids mapping for states that should update on contract
698                    // changes (non-manual updates)
699                    if !component
700                        .static_attributes
701                        .contains_key("manual_updates")
702                    {
703                        for contract in &component.contract_ids {
704                            contracts_map
705                                .entry(contract.clone())
706                                .or_insert_with(HashSet::new)
707                                .insert(id.clone());
708                        }
709                        // Add DCI contracts so changes to these contracts trigger
710                        // an update
711                        for (_, tracing) in snapshot.entrypoints.iter() {
712                            for contract in tracing.accessed_slots.keys().cloned() {
713                                contracts_map
714                                    .entry(contract)
715                                    .or_insert_with(HashSet::new)
716                                    .insert(id.clone());
717                            }
718                        }
719                    }
720
721                    // Collect new pairs (components)
722                    new_pairs.insert(id.clone(), component.clone());
723
724                    // Store component for later batch insertion
725                    components_to_store.insert(id.clone(), component);
726
727                    // Construct state from snapshot
728                    if let Some(state_decode_f) = self.registry.get(protocol.as_str()) {
729                        let live_override = self
730                            .override_providers
731                            .get(protocol.as_str())
732                            .and_then(|provider| provider.subscribe(protocol.as_str()));
733                        match state_decode_f(
734                            snapshot,
735                            header.clone(),
736                            account_balances.clone(),
737                            self.state.clone(),
738                            live_override,
739                        )
740                        .await
741                        {
742                            Ok(state) => {
743                                new_components.insert(id.clone(), state);
744                            }
745                            Err(e) => {
746                                if self.skip_state_decode_failures {
747                                    warn!(pool = id, error = %e, "StateDecodingFailure");
748                                    msg_failed_components.insert(id.clone());
749                                    continue 'snapshot_loop;
750                                }
751                                error!(pool = id, error = %e, "StateDecodingFailure");
752                                return Err(StreamDecodeError::Fatal(format!("{e}")));
753                            }
754                        }
755                    } else if self.skip_state_decode_failures {
756                        warn!(pool = id, "MissingDecoderRegistration");
757                        msg_failed_components.insert(id.clone());
758                    } else {
759                        error!(pool = id, "MissingDecoderRegistration");
760                        return Err(StreamDecodeError::Fatal(format!(
761                            "Missing decoder registration for: {id}"
762                        )));
763                    }
764                }
765            }
766
767            // Batch insert components into state
768            if !components_to_store.is_empty() {
769                let mut state_guard = self.state.write().await;
770                for (id, component) in components_to_store {
771                    state_guard
772                        .components
773                        .insert(id, component);
774                }
775            }
776
777            if !protocol_msg.snapshots.states.is_empty() {
778                info!("Decoded {} snapshots for protocol {protocol}", new_components.len());
779            }
780            if count_token_skips > 0 {
781                info!("Skipped {count_token_skips} pools due to missing tokens");
782            }
783
784            //TODO: should we remove failed components for new_components?
785            updated_states.extend(new_components);
786
787            // PROCESS DELTAS
788            if let Some(deltas) = protocol_msg.deltas.clone() {
789                // Update engine with account changes
790                let mut state_guard = self.state.write().await;
791
792                let mut account_update_by_address: HashMap<Address, AccountUpdate> = HashMap::new();
793                // New proxy token accounts that must overwrite any existing placeholder.
794                let mut new_proxy_accounts: Vec<AccountUpdate> = Vec::new();
795                for (key, value) in deltas.account_deltas.iter() {
796                    let mut update: AccountUpdate = value.clone().into();
797
798                    // TEMP PATCH (ENG-4993)
799                    //
800                    // The indexer may emit Creation deltas with no code for EOA addresses.
801                    // Treat them as EOAs (empty code) rather than downgrading to Update, which
802                    // would skip init_account and cause "uninitialized account" warnings.
803                    if update.code.is_none() && matches!(update.change, ChangeType::Creation) {
804                        error!(
805                            update = ?update,
806                            "FaultyCreationDelta"
807                        );
808                        update.code = Some(vec![]);
809                    }
810
811                    if state_guard.tokens.contains_key(key) {
812                        let original_address = update.address;
813                        // If the account is a token, we need to handle it with a proxy contract.
814                        // Storage updates apply to the proxy contract (at original address).
815                        // Code updates (if any) apply to the token implementation contract (at
816                        // impl_addr).
817
818                        // Handle proxy contract updates
819                        let impl_addr = match state_guard
820                            .proxy_token_addresses
821                            .get(&original_address)
822                        {
823                            Some(impl_addr) => {
824                                // Token already has a proxy contract.
825
826                                // The proxy account already exists, so this is always a plain
827                                // storage update regardless of the incoming change type.
828                                let proxy_update = AccountUpdate {
829                                    code: None,
830                                    change: ChangeType::Update,
831                                    ..update.clone()
832                                };
833                                account_update_by_address.insert(original_address, proxy_update);
834
835                                *impl_addr
836                            }
837                            None => {
838                                // Token does not have a proxy contract yet, create one
839
840                                // Assign original token (implementation) contract to new proxy
841                                // address
842                                let impl_addr = generate_proxy_token_address(
843                                    state_guard.proxy_token_addresses.len(),
844                                )?;
845                                state_guard
846                                    .proxy_token_addresses
847                                    .insert(original_address, impl_addr);
848
849                                // Create proxy token account with original account's storage (at
850                                // original address). Track it separately so it can be
851                                // force-overwritten and win over any placeholder that an engine
852                                // setup routine may have written earlier.
853                                let proxy_state = create_proxy_token_account(
854                                    original_address,
855                                    Some(impl_addr),
856                                    &update.slots,
857                                    update.chain,
858                                    update.balance,
859                                );
860                                new_proxy_accounts.push(proxy_state);
861
862                                impl_addr
863                            }
864                        };
865
866                        // Apply code update to token implementation contract
867                        if update.code.is_some() {
868                            let impl_update = AccountUpdate {
869                                address: impl_addr,
870                                slots: HashMap::new(),
871                                ..update.clone()
872                            };
873                            account_update_by_address.insert(impl_addr, impl_update);
874                        }
875                    } else {
876                        // Not a token, apply update to the account at its original address
877                        account_update_by_address.insert(update.address, update);
878                    }
879                }
880                drop(state_guard);
881
882                let state_guard = self.state.read().await;
883                info!("Updating engine with {} contract deltas", deltas.account_deltas.len());
884                update_engine(
885                    SHARED_TYCHO_DB.clone(),
886                    header.clone().block(),
887                    None,
888                    account_update_by_address,
889                )
890                .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
891
892                // Force-overwrite any newly-created proxy token accounts so they always win
893                // over placeholder entries inserted by engine setup.
894                if !new_proxy_accounts.is_empty() {
895                    SHARED_TYCHO_DB
896                        .force_update_accounts(new_proxy_accounts)
897                        .map_err(|e| StreamDecodeError::Fatal(e.to_string()))?;
898                }
899                info!("Engine updated");
900
901                // Collect all pools related to the updated accounts
902                let mut pools_to_update = HashSet::new();
903                for (account, _update) in deltas.account_deltas {
904                    // get new pools related to the account updated
905                    pools_to_update.extend(
906                        contracts_map
907                            .get(&account)
908                            .cloned()
909                            .unwrap_or_default(),
910                    );
911                    // get existing pools related to the account updated
912                    pools_to_update.extend(
913                        state_guard
914                            .contracts_map
915                            .get(&account)
916                            .cloned()
917                            .unwrap_or_default(),
918                    );
919                }
920
921                // Collect all balance changes this block
922                let all_balances = Balances {
923                    component_balances: deltas
924                        .component_balances
925                        .iter()
926                        .map(|(pool_id, bals)| {
927                            let mut balances = HashMap::new();
928                            for (t, b) in bals {
929                                balances.insert(t.clone(), b.balance.clone());
930                            }
931                            pools_to_update.insert(pool_id.clone());
932                            (pool_id.clone(), balances)
933                        })
934                        .collect(),
935                    account_balances: deltas
936                        .account_balances
937                        .iter()
938                        .map(|(account, bals)| {
939                            let mut balances = HashMap::new();
940                            for (t, b) in bals {
941                                balances.insert(t.clone(), b.balance.clone());
942                            }
943                            pools_to_update.extend(
944                                contracts_map
945                                    .get(account)
946                                    .cloned()
947                                    .unwrap_or_default(),
948                            );
949                            (account.clone(), balances)
950                        })
951                        .collect(),
952                };
953
954                // update states with protocol state deltas (attribute changes etc.)
955                for (id, update) in deltas.state_deltas {
956                    // TODO: is this needed?
957                    let update_with_block = Self::add_block_info_to_delta(
958                        ProtocolStateDelta::from(update),
959                        current_block.clone(),
960                    );
961                    match Self::apply_update(
962                        &id,
963                        update_with_block,
964                        &mut updated_states,
965                        &state_guard,
966                        &all_balances,
967                    ) {
968                        Ok(_) => {
969                            pools_to_update.remove(&id);
970                        }
971                        Err(e) => {
972                            if self.skip_state_decode_failures {
973                                warn!(pool = id, error = %e, "Failed to apply state update, marking component as removed");
974                                // Remove from updated_states if it was there
975                                updated_states.remove(&id);
976                                // Try to get component from new_pairs first, then from state
977                                if let Some(component) = new_pairs.remove(&id) {
978                                    removed_pairs.insert(id.clone(), component);
979                                } else if let Some(component) = state_guard.components.get(&id) {
980                                    removed_pairs.insert(id.clone(), component.clone());
981                                } else {
982                                    // Component not found in new_pairs or state, this shouldn't
983                                    // happen
984                                    warn!(pool = id, "Component not found in new_pairs or state, cannot add to removed_pairs");
985                                }
986                                pools_to_update.remove(&id);
987
988                                // Add to failed components
989                                msg_failed_components.insert(id.clone());
990                            } else {
991                                return Err(e);
992                            }
993                        }
994                    }
995                }
996
997                // update remaining pools linked to updated contracts/updated balances
998                for pool in pools_to_update {
999                    // TODO: is this needed?
1000                    let default_delta_with_block = Self::add_block_info_to_delta(
1001                        ProtocolStateDelta::default(),
1002                        current_block.clone(),
1003                    );
1004                    match Self::apply_update(
1005                        &pool,
1006                        default_delta_with_block,
1007                        &mut updated_states,
1008                        &state_guard,
1009                        &all_balances,
1010                    ) {
1011                        Ok(_) => {}
1012                        Err(e) => {
1013                            if self.skip_state_decode_failures {
1014                                warn!(pool = pool, error = %e, "Failed to apply contract/balance update, marking component as removed");
1015                                // Remove from updated_states if it was there
1016                                updated_states.remove(&pool);
1017                                // Try to get component from new_pairs first, then from state
1018                                if let Some(component) = new_pairs.remove(&pool) {
1019                                    removed_pairs.insert(pool.clone(), component);
1020                                } else if let Some(component) = state_guard.components.get(&pool) {
1021                                    removed_pairs.insert(pool.clone(), component.clone());
1022                                } else {
1023                                    // Component not found in new_pairs or state, this shouldn't
1024                                    // happen
1025                                    warn!(pool = pool, "Component not found in new_pairs or state, cannot add to removed_pairs");
1026                                }
1027
1028                                // Add to failed components
1029                                msg_failed_components.insert(pool.clone());
1030                            } else {
1031                                return Err(e);
1032                            }
1033                        }
1034                    }
1035                }
1036            };
1037        }
1038
1039        // Persist the newly added/updated states
1040        let mut state_guard = self.state.write().await;
1041
1042        // Update failed components with any new ones
1043        state_guard
1044            .failed_components
1045            .extend(msg_failed_components);
1046
1047        // Remove any failed components from Updates
1048        // Perf: we could do it directly in the decoder logic to avoid some steps, but this logic is
1049        // complex and this is more robust.
1050        updated_states.retain(|id, _| {
1051            !state_guard
1052                .failed_components
1053                .contains(id)
1054        });
1055        new_pairs.retain(|id, _| {
1056            !state_guard
1057                .failed_components
1058                .contains(id)
1059        });
1060
1061        if let Some(header) = current_block.as_ref() {
1062            let execution_block = self.execution_block(header);
1063            let decoder_state = &mut *state_guard;
1064            Self::refresh_execution_block(
1065                &mut updated_states,
1066                &mut decoder_state.states,
1067                &decoder_state.failed_components,
1068                &removed_pairs,
1069                &execution_block,
1070            );
1071        }
1072
1073        state_guard
1074            .states
1075            .extend(updated_states.clone());
1076
1077        state_guard.current_block_number = block_number;
1078
1079        // Add new components to persistent state
1080        for (id, component) in new_pairs.iter() {
1081            state_guard
1082                .components
1083                .insert(id.clone(), component.clone());
1084        }
1085
1086        // Remove components from persistent state
1087        for id in removed_pairs.keys() {
1088            state_guard.components.remove(id);
1089        }
1090
1091        for (key, values) in contracts_map {
1092            state_guard
1093                .contracts_map
1094                .entry(key)
1095                .or_insert_with(HashSet::new)
1096                .extend(values);
1097        }
1098
1099        // Send the tick with all updated states
1100        Ok(Update::new(block_number, updated_states, new_pairs)
1101            .set_is_partial(is_partial)
1102            .set_removed_pairs(removed_pairs)
1103            .set_sync_states(msg.sync_states.clone()))
1104    }
1105
1106    /// Applies pending deltas from one or more `TxDeltaIndexer`s against the current confirmed
1107    /// state and returns an ephemeral `Update`.
1108    ///
1109    /// This is the read-only counterpart of `decode()`. It clones pool states, applies the
1110    /// supplied `pending_deltas`, and returns the result — **without writing back** to
1111    /// `DecoderState`. Calling this method twice with the same input produces identical results.
1112    ///
1113    /// This method writes nothing to the VM database. A protocol decoding into the generic VM
1114    /// adapter therefore cannot take part: it re-reads pool state from that database, so its
1115    /// storage-derived values stay at the confirmed block — even though the delta's balance and
1116    /// block-environment attributes do get applied.
1117    ///
1118    /// # Parameters
1119    /// * `pending_deltas` — map from protocol system to the `BlockAggregatedChanges` that its
1120    ///   `TxDeltaIndexer::generate_deltas()` produced. The account deltas under each key reach only
1121    ///   pools of that protocol system, so that key's indexer must emit the writes to every account
1122    ///   those pools read.
1123    /// * `header` — the target block header. Its `block_number_or_timestamp()` is stamped on the
1124    ///   returned [`Update`]; its `block_number` and `block_timestamp` are injected into each state
1125    ///   delta so that protocols relying on block context (e.g. aerodrome slipstreams, etherfi)
1126    ///   receive correct values.
1127    pub async fn apply_deltas_ephemeral(
1128        &self,
1129        pending_deltas: &HashMap<String, BlockAggregatedChanges>,
1130        header: H,
1131    ) -> Result<Update, StreamDecodeError> {
1132        let block_number = header
1133            .clone()
1134            .block_number_or_timestamp();
1135        let current_block = header.block();
1136        let state_guard = self.state.read().await;
1137
1138        let mut updated_states: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1139        for (protocol_system, deltas) in pending_deltas {
1140            let changes = ExtractorChanges::new(protocol_system, deltas);
1141            let mut failed_pools = HashSet::new();
1142            for (id, state_delta) in &deltas.state_deltas {
1143                let dto_delta = Self::add_block_info_to_delta(
1144                    ProtocolStateDelta::from(state_delta.clone()),
1145                    current_block.clone(),
1146                );
1147                if let Err(e) = Self::apply_update(
1148                    id,
1149                    dto_delta,
1150                    &mut updated_states,
1151                    &state_guard,
1152                    &changes.balances,
1153                ) {
1154                    warn!(pool = id, error = %e, "EphemeralDeltaTransitionError");
1155                    failed_pools.insert(id);
1156                }
1157            }
1158            if !deltas.account_deltas.is_empty() {
1159                Self::apply_pending_overrides(
1160                    &changes,
1161                    &failed_pools,
1162                    current_block.as_ref(),
1163                    &mut updated_states,
1164                    &state_guard,
1165                )?;
1166            }
1167        }
1168
1169        // `header` is the block being built, so it already *is* the execution block — unlike
1170        // `decode`, there is nothing to project forward here. Only the delta-applied clones need
1171        // advancing: this path is read-only, and the stored states were already advanced to this
1172        // block by `decode()` on the confirmed stream.
1173        if let Some(header) = current_block.as_ref() {
1174            let execution_block = BlockContext::new(header.number, header.timestamp);
1175            for state in updated_states.values_mut() {
1176                state.apply_block(&execution_block);
1177            }
1178        }
1179
1180        Ok(Update::new(block_number, updated_states, HashMap::new()))
1181    }
1182
1183    /// Sets one extractor's account deltas as overrides on each pool that has the extractor's
1184    /// protocol system and is linked to a written account.
1185    ///
1186    /// A hooked pool prices from its hook's storage, which only the account deltas carry. An
1187    /// override for an account a hook never reads is inert.
1188    ///
1189    /// Skips pools of other protocol systems, even when they are linked to a written account such
1190    /// as a token contract. A pool holds one override set, so another extractor's set would
1191    /// replace the pool's own. Linking such a pool would also rebuild it from confirmed state with
1192    /// an empty delta. For a pool whose transition reads the VM, each rebuild costs one EVM call
1193    /// and changes nothing.
1194    ///
1195    /// Skips the pools in `failed_pools`, whose own state delta failed. A rebuild would pair the
1196    /// pool's confirmed state with the pending overrides and return the mix as pending. Left out,
1197    /// the pool stays at confirmed state for a consumer that overlays the update on it.
1198    ///
1199    /// # Errors
1200    /// Fails when a pool rejects the overrides' type, since its quotes would silently run
1201    /// against confirmed state.
1202    fn apply_pending_overrides(
1203        changes: &ExtractorChanges<'_>,
1204        failed_pools: &HashSet<&String>,
1205        current_block: Option<&BlockHeader>,
1206        updated_states: &mut HashMap<String, Box<dyn ProtocolSim>>,
1207        state_guard: &RwLockReadGuard<'_, DecoderState>,
1208    ) -> Result<(), StreamDecodeError> {
1209        let is_linkable = |id: &&String| {
1210            !failed_pools.contains(id) &&
1211                state_guard
1212                    .components
1213                    .get(*id)
1214                    .is_some_and(|component| {
1215                        component.protocol_system == changes.protocol_system
1216                    })
1217        };
1218        let mut linked_pools: HashSet<&String> = HashSet::new();
1219        for account in changes.deltas.account_deltas.keys() {
1220            if let Some(pools) = state_guard.contracts_map.get(account) {
1221                linked_pools.extend(pools.iter().filter(is_linkable));
1222            }
1223        }
1224        if linked_pools.is_empty() {
1225            return Ok(());
1226        }
1227
1228        let block = current_block
1229            .map(|h| BlockEnvOverrides { number: Some(h.number), timestamp: Some(h.timestamp) });
1230        let overrides: Arc<dyn Any + Send + Sync> =
1231            Arc::new(PendingOverrides::from_account_deltas(&changes.deltas.account_deltas, block));
1232        for id in linked_pools {
1233            if !updated_states.contains_key(id) {
1234                let empty_delta = Self::add_block_info_to_delta(
1235                    ProtocolStateDelta { component_id: id.clone(), ..Default::default() },
1236                    current_block.cloned(),
1237                );
1238                if let Err(e) = Self::apply_update(
1239                    id,
1240                    empty_delta,
1241                    updated_states,
1242                    state_guard,
1243                    &changes.balances,
1244                ) {
1245                    warn!(pool = id, error = %e, "EphemeralDeltaTransitionError");
1246                    continue;
1247                }
1248            }
1249            if let Some(state) = updated_states.get_mut(id) {
1250                state
1251                    .set_pending_overrides(Arc::clone(&overrides))
1252                    .map_err(|e| {
1253                        StreamDecodeError::Fatal(format!("Pending overrides for pool {id}: {e}"))
1254                    })?;
1255            }
1256        }
1257        Ok(())
1258    }
1259
1260    /// Add current block information (number and timestamp) to a ProtocolStateDelta.
1261    fn add_block_info_to_delta(
1262        mut delta: ProtocolStateDelta,
1263        block_header_opt: Option<BlockHeader>,
1264    ) -> ProtocolStateDelta {
1265        if let Some(header) = block_header_opt {
1266            // Add block_number and block_timestamp attributes to ensure pool states
1267            // receive current block information during delta_transition
1268            delta.updated_attributes.insert(
1269                "block_number".to_string(),
1270                Bytes::from(header.number.to_be_bytes().to_vec()),
1271            );
1272            delta.updated_attributes.insert(
1273                "block_timestamp".to_string(),
1274                Bytes::from(header.timestamp.to_be_bytes().to_vec()),
1275            );
1276        }
1277        delta
1278    }
1279
1280    fn apply_update(
1281        id: &String,
1282        update: ProtocolStateDelta,
1283        updated_states: &mut HashMap<String, Box<dyn ProtocolSim>>,
1284        state_guard: &RwLockReadGuard<'_, DecoderState>,
1285        all_balances: &Balances,
1286    ) -> Result<(), StreamDecodeError> {
1287        match updated_states.entry(id.clone()) {
1288            Entry::Occupied(mut entry) => {
1289                // If state exists in updated_states, apply the delta to it
1290                let state: &mut Box<dyn ProtocolSim> = entry.get_mut();
1291                state
1292                    .delta_transition(update, &state_guard.tokens, all_balances)
1293                    .map_err(|e| {
1294                        error!(pool = id, error = ?e, "DeltaTransitionError");
1295                        StreamDecodeError::Fatal(format!("TransitionFailure: {e:?}"))
1296                    })?;
1297            }
1298            Entry::Vacant(_) => {
1299                match state_guard.states.get(id) {
1300                    // If state does not exist in updated_states, apply the delta to the stored
1301                    // state
1302                    Some(stored_state) => {
1303                        let mut state = stored_state.clone();
1304                        state
1305                            .delta_transition(update, &state_guard.tokens, all_balances)
1306                            .map_err(|e| {
1307                                error!(pool = id, error = ?e, "DeltaTransitionError");
1308                                StreamDecodeError::Fatal(format!("TransitionFailure: {e:?}"))
1309                            })?;
1310                        updated_states.insert(id.clone(), state);
1311                    }
1312                    None => debug!(pool = id, reason = "MissingState", "DeltaTransitionError"),
1313                }
1314            }
1315        }
1316        Ok(())
1317    }
1318}
1319
1320/// One extractor's pending changes, with its `pending_deltas` key and its component balances.
1321struct ExtractorChanges<'a> {
1322    protocol_system: &'a str,
1323    deltas: &'a BlockAggregatedChanges,
1324    balances: Balances,
1325}
1326
1327impl<'a> ExtractorChanges<'a> {
1328    /// Collects the component balances in `deltas`, with empty account balances.
1329    fn new(protocol_system: &'a str, deltas: &'a BlockAggregatedChanges) -> Self {
1330        let mut all_component_balances = HashMap::with_capacity(deltas.component_balances.len());
1331        for (pool_id, pool_balances) in &deltas.component_balances {
1332            let pool_balances = pool_balances
1333                .iter()
1334                .map(|(token, balance)| (token.clone(), balance.balance.clone()))
1335                .collect();
1336            all_component_balances.insert(pool_id.clone(), pool_balances);
1337        }
1338        let balances = Balances {
1339            component_balances: all_component_balances,
1340            account_balances: HashMap::new(),
1341        };
1342        Self { protocol_system, deltas, balances }
1343    }
1344}
1345
1346/// Generate a proxy token address for a given token index
1347fn generate_proxy_token_address(idx: usize) -> Result<Address, StreamDecodeError> {
1348    let padded_idx = format!("{idx:x}");
1349    let padded_zeroes = "0".repeat(33 - padded_idx.len());
1350    let proxy_token_address = format!("{padded_zeroes}{padded_idx}BAdbaBe");
1351    let decoded = hex::decode(proxy_token_address).map_err(|e| {
1352        StreamDecodeError::Fatal(format!("Invalid proxy token address encoding: {e}"))
1353    })?;
1354
1355    const ADDRESS_LENGTH: usize = 20;
1356    if decoded.len() != ADDRESS_LENGTH {
1357        return Err(StreamDecodeError::Fatal(format!(
1358            "Invalid proxy token address length: expected {}, got {}",
1359            ADDRESS_LENGTH,
1360            decoded.len(),
1361        )));
1362    }
1363
1364    Ok(Address::from_slice(&decoded))
1365}
1366
1367/// Create a proxy token account for a token at a given address
1368///
1369/// The proxy token account is created at the original token address and points to the new token
1370/// address.
1371fn create_proxy_token_account(
1372    addr: Address,
1373    new_address: Option<Address>,
1374    storage: &HashMap<U256, U256>,
1375    chain: Chain,
1376    balance: Option<U256>,
1377) -> AccountUpdate {
1378    let mut slots = storage.clone();
1379    if let Some(new_address) = new_address {
1380        slots.insert(*IMPLEMENTATION_SLOT, U256::from_be_slice(new_address.as_slice()));
1381    }
1382
1383    AccountUpdate {
1384        address: addr,
1385        chain,
1386        slots,
1387        balance,
1388        code: Some(ERC20_PROXY_BYTECODE.to_vec()),
1389        change: ChangeType::Creation,
1390    }
1391}
1392
1393#[cfg(test)]
1394#[expect(
1395    clippy::same_name_method,
1396    reason = "mockall generates the inherent methods the ProtocolSim impl forwards to"
1397)]
1398mod protocol_sim_mock {
1399    use super::*;
1400
1401    mock! {
1402        #[derive(Debug)]
1403        pub ProtocolSim {
1404            pub fn fee(&self) -> f64;
1405            pub fn spot_price(&self, base: &Token, quote: &Token) -> Result<f64, SimulationError>;
1406            pub fn get_amount_out(
1407                &self,
1408                amount_in: BigUint,
1409                token_in: &Token,
1410                token_out: &Token,
1411            ) -> Result<GetAmountOutResult, SimulationError>;
1412            pub fn get_limits(
1413                &self,
1414                sell_token: Bytes,
1415                buy_token: Bytes,
1416            ) -> Result<(BigUint, BigUint), SimulationError>;
1417            pub fn delta_transition(
1418                &mut self,
1419                delta: ProtocolStateDelta,
1420                tokens: &HashMap<Bytes, Token>,
1421                balances: &Balances,
1422            ) -> Result<(), TransitionError>;
1423            pub fn clone_box(&self) -> Box<dyn ProtocolSim>;
1424            pub fn eq(&self, other: &dyn ProtocolSim) -> bool;
1425            pub fn base_token(&self) -> &Token;
1426            pub fn quote_token(&self) -> &Token;
1427            pub fn directions(&self) -> Vec<(Bytes, Bytes)>;
1428            pub fn set_pending_overrides(
1429                &mut self,
1430                overrides: Arc<dyn Any + Send + Sync>,
1431            ) -> Result<(), SimulationError>;
1432        }
1433    }
1434}
1435
1436#[cfg(test)]
1437pub use protocol_sim_mock::MockProtocolSim;
1438
1439#[cfg(test)]
1440crate::impl_non_serializable_protocol!(MockProtocolSim, "test protocol");
1441
1442#[cfg(test)]
1443impl ProtocolSim for MockProtocolSim {
1444    fn fee(&self) -> f64 {
1445        self.fee()
1446    }
1447
1448    fn spot_price(&self, base: &Token, quote: &Token) -> Result<f64, SimulationError> {
1449        self.spot_price(base, quote)
1450    }
1451
1452    fn get_amount_out(
1453        &self,
1454        amount_in: BigUint,
1455        token_in: &Token,
1456        token_out: &Token,
1457    ) -> Result<GetAmountOutResult, SimulationError> {
1458        self.get_amount_out(amount_in, token_in, token_out)
1459    }
1460
1461    fn get_limits(
1462        &self,
1463        sell_token: Bytes,
1464        buy_token: Bytes,
1465    ) -> Result<(BigUint, BigUint), SimulationError> {
1466        self.get_limits(sell_token, buy_token)
1467    }
1468
1469    fn delta_transition(
1470        &mut self,
1471        delta: ProtocolStateDelta,
1472        tokens: &HashMap<Bytes, Token>,
1473        balances: &Balances,
1474    ) -> Result<(), TransitionError> {
1475        self.delta_transition(delta, tokens, balances)
1476    }
1477
1478    fn clone_box(&self) -> Box<dyn ProtocolSim> {
1479        self.clone_box()
1480    }
1481
1482    fn set_pending_overrides(
1483        &mut self,
1484        overrides: Arc<dyn Any + Send + Sync>,
1485    ) -> Result<(), SimulationError> {
1486        self.set_pending_overrides(overrides)
1487    }
1488
1489    fn as_any(&self) -> &dyn Any {
1490        panic!("MockProtocolSim does not support as_any")
1491    }
1492
1493    fn as_any_mut(&mut self) -> &mut dyn Any {
1494        panic!("MockProtocolSim does not support as_any_mut")
1495    }
1496
1497    fn eq(&self, other: &dyn ProtocolSim) -> bool {
1498        self.eq(other)
1499    }
1500
1501    fn typetag_name(&self) -> &'static str {
1502        // typetag reads the tag before Serialize, so a panic here would hide Serialize's error.
1503        "MockProtocolSim"
1504    }
1505
1506    #[expect(clippy::unreachable, reason = "the mock is never deserialized through its typetag")]
1507    fn typetag_deserialize(&self) {
1508        unreachable!()
1509    }
1510}
1511
1512/// Lets the mock stand in for a book's state wherever one pair's tokens and directions are
1513/// what the code under test reads.
1514#[cfg(test)]
1515impl crate::book::component::PairState for MockProtocolSim {
1516    fn base_token(&self) -> &Token {
1517        self.base_token()
1518    }
1519
1520    fn quote_token(&self) -> &Token {
1521        self.quote_token()
1522    }
1523
1524    fn directions(&self) -> Vec<(Bytes, Bytes)> {
1525        self.directions()
1526    }
1527}
1528
1529#[cfg(test)]
1530mod tests {
1531    use std::str::FromStr;
1532
1533    use alloy::primitives::address;
1534    use mockall::predicate::*;
1535    use rstest::*;
1536    use tycho_client::feed::BlockHeader;
1537    use tycho_common::{models::Chain, Bytes};
1538
1539    use super::*;
1540
1541    fn header_at(number: u64, timestamp: u64, partial: Option<u32>) -> BlockHeader {
1542        BlockHeader {
1543            hash: Bytes::from([0u8; 32]),
1544            number,
1545            parent_hash: Bytes::from([0u8; 32]),
1546            revert: false,
1547            timestamp,
1548            partial_block_index: partial,
1549        }
1550    }
1551
1552    /// A block-sensitive state whose execution timestamp we can read back.
1553    fn block_sensitive_state() -> Box<dyn ProtocolSim> {
1554        use crate::evm::protocol::{
1555            aerodrome_slipstreams::state::AerodromeSlipstreamsState,
1556            utils::{
1557                slipstreams::{dynamic_fee_module::DynamicFeeConfig, observations::Observation},
1558                uniswap::{tick_list::TickInfo, tick_math::get_sqrt_ratio_at_tick},
1559            },
1560        };
1561
1562        Box::new(
1563            AerodromeSlipstreamsState::new(
1564                "block-sensitive".to_string(),
1565                0,
1566                1_000_000_000_000_000_000,
1567                get_sqrt_ratio_at_tick(0).unwrap(),
1568                0,
1569                1,
1570                3000,
1571                1,
1572                0,
1573                vec![TickInfo::new(-120, 0).unwrap(), TickInfo::new(120, 0).unwrap()],
1574                vec![Observation { block_timestamp: 500, initialized: true, ..Default::default() }],
1575                DynamicFeeConfig::new(2700, 30_000, 0, true, 750),
1576            )
1577            .expect("state should build")
1578            // These fixtures exercise the fee-flip path, which needs the optimistic mode:
1579            // under the worst-case default a flat-fee pool never flips.
1580            .with_position_assumption(crate::protocol::models::BlockPositionAssumption::First),
1581        )
1582    }
1583
1584    #[test]
1585    fn confirmed_header_targets_the_next_block() {
1586        let decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Base);
1587
1588        let execution_block = decoder.execution_block(&header_at(100, 1_000, None));
1589
1590        assert_eq!(execution_block.number(), 101);
1591        assert_eq!(execution_block.timestamp(), 1_000 + Chain::Base.block_time_secs());
1592    }
1593
1594    #[test]
1595    fn partial_header_targets_the_block_that_is_still_open() {
1596        let decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Ethereum);
1597
1598        let execution_block = decoder.execution_block(&header_at(100, 1_000, Some(3)));
1599
1600        assert_eq!(execution_block.number(), 100);
1601        assert_eq!(execution_block.timestamp(), 1_000);
1602    }
1603
1604    #[test]
1605    fn refresh_re_emits_a_state_whose_fee_flipped_without_a_delta() {
1606        // The pool traded in block 100 and has no delta afterwards. Crossing into block 101
1607        // flips its fee branch (dynamic -> initial), so the refresh must advance the stored
1608        // state in place and emit a copy to consumers.
1609        let mut stored = HashMap::from([("block-sensitive".to_string(), {
1610            let mut state = block_sensitive_state();
1611            state.apply_block(&BlockContext::new(100, 500));
1612            state
1613        })]);
1614        let mut updated: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1615
1616        TychoStreamDecoder::<BlockHeader>::refresh_execution_block(
1617            &mut updated,
1618            &mut stored,
1619            &HashSet::new(),
1620            &HashMap::<String, ()>::new(),
1621            &BlockContext::new(101, 502),
1622        );
1623
1624        let emitted = updated
1625            .get("block-sensitive")
1626            .expect("a fee flip must be emitted even without a delta");
1627        assert_eq!(emitted.fee(), 750.0 / 1_000_000.0);
1628        // The stored copy was advanced in place as well.
1629        assert_eq!(stored["block-sensitive"].fee(), 750.0 / 1_000_000.0);
1630    }
1631
1632    #[test]
1633    fn refresh_never_re_emits_failed_components() {
1634        // A failed component's stored state may linger; the sweep must not resurrect it for
1635        // consumers that were told the component was removed — even when its fee flipped.
1636        let mut stored = HashMap::from([("zombie".to_string(), {
1637            let mut state = block_sensitive_state();
1638            state.apply_block(&BlockContext::new(100, 500));
1639            state
1640        })]);
1641        let failed = HashSet::from(["zombie".to_string()]);
1642        let mut updated: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1643
1644        TychoStreamDecoder::<BlockHeader>::refresh_execution_block(
1645            &mut updated,
1646            &mut stored,
1647            &failed,
1648            &HashMap::<String, ()>::new(),
1649            &BlockContext::new(101, 502),
1650        );
1651
1652        assert!(updated.is_empty());
1653    }
1654
1655    #[test]
1656    fn refresh_never_re_emits_removed_components() {
1657        // The stored state of a component reported in `removed_pairs` stays in place; the sweep
1658        // must not surface it again, even when its fee flipped in the execution block.
1659        let mut stored = HashMap::from([("gone".to_string(), {
1660            let mut state = block_sensitive_state();
1661            state.apply_block(&BlockContext::new(100, 500));
1662            state
1663        })]);
1664        let removed = HashMap::from([("gone".to_string(), ())]);
1665        let mut updated: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1666
1667        TychoStreamDecoder::<BlockHeader>::refresh_execution_block(
1668            &mut updated,
1669            &mut stored,
1670            &HashSet::new(),
1671            &removed,
1672            &BlockContext::new(101, 502),
1673        );
1674
1675        assert!(updated.is_empty());
1676    }
1677
1678    #[test]
1679    fn refresh_stays_quiet_when_no_fee_changed() {
1680        // An idle block-sensitive pool (fee branch unchanged) and a block-insensitive pool:
1681        // neither must be re-emitted.
1682        let mut stored: HashMap<String, Box<dyn ProtocolSim>> = HashMap::from([
1683            ("idle-sensitive".to_string(), {
1684                let mut state = block_sensitive_state();
1685                state.apply_block(&BlockContext::new(101, 502));
1686                state
1687            }),
1688            (
1689                "univ2".to_string(),
1690                Box::new(crate::evm::protocol::uniswap_v2::state::UniswapV2State::new(
1691                    U256::from(1_000_000u64),
1692                    U256::from(1_000_000u64),
1693                )) as Box<dyn ProtocolSim>,
1694            ),
1695        ]);
1696        let mut updated: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
1697
1698        TychoStreamDecoder::<BlockHeader>::refresh_execution_block(
1699            &mut updated,
1700            &mut stored,
1701            &HashSet::new(),
1702            &HashMap::<String, ()>::new(),
1703            &BlockContext::new(102, 504),
1704        );
1705
1706        assert!(updated.is_empty());
1707    }
1708    use crate::evm::protocol::{curve::CurveState, uniswap_v2::state::UniswapV2State};
1709
1710    #[test]
1711    fn curve_vm_adapter_registration_flagged_deprecated() {
1712        // The native decoder is the supported path — not flagged.
1713        assert!(!is_deprecated_curve_registration::<CurveState>("vm:curve"));
1714        // Any other type for vm:curve is the deprecated VM-adapter path.
1715        assert!(is_deprecated_curve_registration::<UniswapV2State>("vm:curve"));
1716        // Other exchanges are unaffected.
1717        assert!(!is_deprecated_curve_registration::<UniswapV2State>("uniswap_v2"));
1718    }
1719
1720    async fn setup_decoder(set_tokens: bool) -> TychoStreamDecoder<BlockHeader> {
1721        let mut decoder = TychoStreamDecoder::new(Chain::Ethereum);
1722        decoder.register_decoder::<UniswapV2State>("uniswap_v2");
1723        if set_tokens {
1724            let tokens = [
1725                Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").lpad(20, 0),
1726                Bytes::from("0xdac17f958d2ee523a2206206994597c13d831ec7").lpad(20, 0),
1727            ]
1728            .iter()
1729            .map(|addr| {
1730                let addr_str = format!("{addr:x}");
1731                (
1732                    addr.clone(),
1733                    Token::new(addr, &addr_str, 18, 100, &[Some(100_000)], Chain::Ethereum, 100),
1734                )
1735            })
1736            .collect();
1737            decoder.set_tokens(tokens).await;
1738        }
1739        decoder
1740    }
1741
1742    fn load_test_msg(name: &str) -> FeedMessage<BlockHeader> {
1743        use std::{fs, path::Path};
1744
1745        use tycho_client::feed::dto;
1746        let project_root = env!("CARGO_MANIFEST_DIR");
1747        let asset_path = Path::new(project_root).join(format!("tests/assets/decoder/{name}.json"));
1748        let json_data = fs::read_to_string(asset_path).expect("Failed to read test asset");
1749        let feed_msg: dto::FeedMessage<BlockHeader> =
1750            serde_json::from_str(&json_data).expect("Failed to deserialize FeedMsg json!");
1751        FeedMessage::from(feed_msg)
1752    }
1753
1754    #[tokio::test]
1755    async fn test_decode() {
1756        let decoder = setup_decoder(true).await;
1757
1758        let msg = load_test_msg("uniswap_v2_snapshot");
1759        let res1 = decoder
1760            .decode(&msg)
1761            .await
1762            .expect("decode failure");
1763        let msg = load_test_msg("uniswap_v2_delta");
1764        let res2 = decoder
1765            .decode(&msg)
1766            .await
1767            .expect("decode failure");
1768
1769        assert_eq!(res1.states.len(), 1);
1770        assert_eq!(res2.states.len(), 1);
1771        assert_eq!(res1.sync_states.len(), 1);
1772        assert_eq!(res2.sync_states.len(), 1);
1773    }
1774
1775    #[tokio::test]
1776    async fn test_decode_token_creation_delta_with_existing_proxy() {
1777        let decoder = setup_decoder(true).await;
1778        let msg = load_test_msg("uniswap_v2_delta_token_creation");
1779
1780        // First decode: the token has no proxy yet, so the Creation delta takes the
1781        // proxy-creating branch.
1782        decoder
1783            .decode(&msg)
1784            .await
1785            .expect("first decode (proxy creation) failed");
1786
1787        // Second decode: the proxy exists, so the same Creation delta must decode as a
1788        // storage update on the proxy account — not a code-less Creation, which the
1789        // engine rejects as "MissingCode".
1790        decoder
1791            .decode(&msg)
1792            .await
1793            .expect("decode of a token Creation delta with an existing proxy failed");
1794    }
1795
1796    #[tokio::test]
1797    async fn test_decode_component_missing_token() {
1798        let decoder = setup_decoder(false).await;
1799        let tokens = [Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").lpad(20, 0)]
1800            .iter()
1801            .map(|addr| {
1802                let addr_str = format!("{addr:x}");
1803                (
1804                    addr.clone(),
1805                    Token::new(addr, &addr_str, 18, 100, &[Some(100_000)], Chain::Ethereum, 100),
1806                )
1807            })
1808            .collect();
1809        decoder.set_tokens(tokens).await;
1810
1811        let msg = load_test_msg("uniswap_v2_snapshot");
1812        let res1 = decoder
1813            .decode(&msg)
1814            .await
1815            .expect("decode failure");
1816
1817        assert_eq!(res1.states.len(), 0);
1818    }
1819
1820    #[rstest]
1821    #[case::unknown_token(false)]
1822    #[case::undecodable_address(true)]
1823    #[tokio::test]
1824    async fn test_dropping_decoder_keeps_component(#[case] bad_address: bool) {
1825        let weth = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").lpad(20, 0);
1826        // The unknown token is not in the decoder. The bad address is, but 21 bytes are no address.
1827        let other = match bad_address {
1828            true => Bytes::from([1u8; 21].to_vec()),
1829            false => Bytes::from("0xdac17f958d2ee523a2206206994597c13d831ec7").lpad(20, 0),
1830        };
1831        let mut decoder = TychoStreamDecoder::new(Chain::Ethereum);
1832        decoder.register_decoder_dropping_unknown_tokens::<UniswapV2State>("uniswap_v2");
1833        let token = |address: &Bytes, symbol| {
1834            let token =
1835                Token::new(address, symbol, 18, 100, &[Some(100_000)], Chain::Ethereum, 100);
1836            (address.clone(), token)
1837        };
1838        let mut known = HashMap::from([token(&weth, "WETH")]);
1839        if bad_address {
1840            known.extend([token(&other, "OTHER")]);
1841        }
1842        decoder.set_tokens(known).await;
1843        let mut msg = load_test_msg("uniswap_v2_snapshot");
1844        let (id, snapshot) = msg
1845            .state_msgs
1846            .get_mut("uniswap_v2")
1847            .unwrap()
1848            .snapshots
1849            .states
1850            .iter_mut()
1851            .next()
1852            .unwrap();
1853        snapshot.component.tokens = vec![weth, other];
1854        let id = id.clone();
1855
1856        let update = decoder.decode(&msg).await.unwrap();
1857
1858        let symbols: Vec<_> = update.new_pairs[&id]
1859            .tokens
1860            .iter()
1861            .map(|token| token.symbol.as_str())
1862            .collect();
1863        assert_eq!(symbols, ["WETH"]);
1864        assert_eq!(update.states.len(), 1);
1865    }
1866
1867    #[tokio::test]
1868    async fn test_decode_component_bad_id() {
1869        let decoder = setup_decoder(true).await;
1870        let msg = load_test_msg("uniswap_v2_snapshot_broken_id");
1871
1872        match decoder.decode(&msg).await {
1873            Err(StreamDecodeError::Fatal(msg)) => {
1874                assert_eq!(msg, "Component id mismatch");
1875            }
1876            Ok(_) => {
1877                panic!("Expected failures to be raised")
1878            }
1879        }
1880    }
1881
1882    #[rstest]
1883    #[case(true)]
1884    #[case(false)]
1885    #[tokio::test]
1886    async fn test_decode_component_bad_state(#[case] skip_failures: bool) {
1887        let mut decoder = setup_decoder(true).await;
1888        decoder.skip_state_decode_failures = skip_failures;
1889
1890        let msg = load_test_msg("uniswap_v2_snapshot_broken_state");
1891        match decoder.decode(&msg).await {
1892            Err(StreamDecodeError::Fatal(msg)) => {
1893                if !skip_failures {
1894                    assert_eq!(msg, "Missing attributes reserve0");
1895                } else {
1896                    panic!("Expected failures to be ignored. Err: {msg}")
1897                }
1898            }
1899            Ok(res) => {
1900                if !skip_failures {
1901                    panic!("Expected failures to be raised")
1902                } else {
1903                    assert_eq!(res.states.len(), 0);
1904                }
1905            }
1906        }
1907    }
1908
1909    #[tokio::test]
1910    async fn test_decode_updates_state_on_contract_change() {
1911        let decoder = setup_decoder(true).await;
1912
1913        // Create the mock instances
1914        let mut mock_state = MockProtocolSim::new();
1915
1916        mock_state
1917            .expect_clone_box()
1918            .times(1)
1919            .returning(|| {
1920                let mut cloned_mock_state = MockProtocolSim::new();
1921                // Expect `delta_transition` to be called once with any parameters
1922                cloned_mock_state
1923                    .expect_delta_transition()
1924                    .times(1)
1925                    .returning(|_, _, _| Ok(()));
1926                cloned_mock_state
1927                    .expect_clone_box()
1928                    .times(1)
1929                    .returning(|| Box::new(MockProtocolSim::new()));
1930                Box::new(cloned_mock_state)
1931            });
1932
1933        // Insert mock state into `updated_states`
1934        let pool_id =
1935            "0x93d199263632a4ef4bb438f1feb99e57b4b5f0bd0000000000000000000005c2".to_string();
1936        decoder
1937            .state
1938            .write()
1939            .await
1940            .states
1941            .insert(pool_id.clone(), Box::new(mock_state) as Box<dyn ProtocolSim>);
1942        decoder
1943            .state
1944            .write()
1945            .await
1946            .contracts_map
1947            .insert(
1948                Bytes::from("0xba12222222228d8ba445958a75a0704d566bf2c8").lpad(20, 0),
1949                HashSet::from([pool_id.clone()]),
1950            );
1951
1952        // Load a test message containing a contract update
1953        let msg = load_test_msg("balancer_v2_delta");
1954
1955        // Decode the message
1956        let _ = decoder
1957            .decode(&msg)
1958            .await
1959            .expect("decode failure");
1960
1961        // The mock framework will assert that `delta_transition` was called exactly once
1962    }
1963
1964    /// The pending block's account deltas must reach the clone of every pool linked to a written
1965    /// account, and never the stored confirmed state.
1966    #[tokio::test]
1967    async fn test_apply_deltas_ephemeral_sets_pending_overrides_on_the_clone_only() {
1968        use tycho_common::models::{
1969            blockchain::BlockAggregatedChanges, contract::AccountDelta,
1970            protocol::ProtocolComponentStateDelta, ChangeType as ModelChangeType,
1971        };
1972
1973        use crate::evm::protocol::uniswap_v4::state::{UniswapV4Fees, UniswapV4State};
1974
1975        let decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Ethereum);
1976        let pool_id = "0xhooked".to_string();
1977        let quiet_pool_id = "0xhooked-no-log".to_string();
1978        let pool = UniswapV4State::new(
1979            1000,
1980            U256::from(1u8) << 96,
1981            UniswapV4Fees::new(0, 0, 3000),
1982            0,
1983            60,
1984            vec![],
1985        )
1986        .unwrap();
1987        let hook = Bytes::from(vec![7u8; 20]);
1988        {
1989            let mut state = decoder.state.write().await;
1990            state
1991                .states
1992                .insert(pool_id.clone(), Box::new(pool.clone()));
1993            state
1994                .states
1995                .insert(quiet_pool_id.clone(), Box::new(pool));
1996            state
1997                .contracts_map
1998                .insert(hook.clone(), HashSet::from([pool_id.clone(), quiet_pool_id.clone()]));
1999            for id in [&pool_id, &quiet_pool_id] {
2000                state
2001                    .components
2002                    .insert(id.clone(), component_with_protocol_system(id, "uniswap_v4_hooks"));
2003            }
2004        }
2005
2006        let deltas = BlockAggregatedChanges {
2007            extractor: "uniswap_v4_hooks".to_string(),
2008            state_deltas: HashMap::from([(
2009                pool_id.clone(),
2010                ProtocolComponentStateDelta {
2011                    component_id: pool_id.clone(),
2012                    updated_attributes: HashMap::from([(
2013                        "liquidity".to_string(),
2014                        Bytes::from(2000_u64.to_be_bytes().to_vec()),
2015                    )]),
2016                    deleted_attributes: HashSet::new(),
2017                    created_attributes: HashSet::new(),
2018                },
2019            )]),
2020            account_deltas: HashMap::from([(
2021                hook.clone(),
2022                AccountDelta::new(
2023                    Chain::Ethereum,
2024                    hook.clone(),
2025                    HashMap::from([(Bytes::from(vec![0u8]), Some(Bytes::from(vec![9u8])))]),
2026                    None,
2027                    None,
2028                    ModelChangeType::Update,
2029                ),
2030            )]),
2031            ..Default::default()
2032        };
2033        let header = BlockHeader { number: 10, timestamp: 20, ..Default::default() };
2034
2035        let update = decoder
2036            .apply_deltas_ephemeral(
2037                &HashMap::from([("uniswap_v4_hooks".to_string(), deltas)]),
2038                header,
2039            )
2040            .await
2041            .unwrap();
2042
2043        let clone = update.states[&pool_id]
2044            .as_any()
2045            .downcast_ref::<UniswapV4State>()
2046            .unwrap();
2047        let pending = clone
2048            .pending_overrides()
2049            .expect("the clone carries the pending block's overrides");
2050        assert_eq!(
2051            pending.block,
2052            Some(BlockEnvOverrides { number: Some(10), timestamp: Some(20) })
2053        );
2054        assert_eq!(
2055            pending.storage.as_ref().unwrap()[&Address::from_slice(&hook)][&U256::ZERO],
2056            U256::from(9)
2057        );
2058        let quiet = update.states[&quiet_pool_id]
2059            .as_any()
2060            .downcast_ref::<UniswapV4State>()
2061            .expect("a pool whose hook was written is cloned without a delta of its own");
2062        assert!(quiet.pending_overrides().is_some());
2063
2064        let stored = decoder.state.read().await;
2065        for id in [&pool_id, &quiet_pool_id] {
2066            let stored = stored.states[id]
2067                .as_any()
2068                .downcast_ref::<UniswapV4State>()
2069                .unwrap();
2070            assert!(stored.pending_overrides().is_none(), "confirmed state is never overlaid");
2071        }
2072    }
2073
2074    fn component_with_protocol_system(id: &str, protocol_system: &str) -> ProtocolComponent {
2075        ProtocolComponent::new(
2076            Bytes::from(id.as_bytes().to_vec()),
2077            protocol_system.to_string(),
2078            "test".to_string(),
2079            Chain::Ethereum,
2080            vec![],
2081            vec![],
2082            HashMap::new(),
2083            Bytes::default(),
2084            chrono::NaiveDateTime::default(),
2085        )
2086    }
2087
2088    /// Builds account deltas that set slot 0 of `account` to `value`.
2089    fn account_write(
2090        account: &Bytes,
2091        value: u8,
2092    ) -> HashMap<Bytes, tycho_common::models::contract::AccountDelta> {
2093        use tycho_common::models::{contract::AccountDelta, ChangeType as ModelChangeType};
2094
2095        HashMap::from([(
2096            account.clone(),
2097            AccountDelta::new(
2098                Chain::Ethereum,
2099                account.clone(),
2100                HashMap::from([(Bytes::from(vec![0u8]), Some(Bytes::from(vec![value])))]),
2101                None,
2102                None,
2103                ModelChangeType::Update,
2104            ),
2105        )])
2106    }
2107
2108    /// Builds a state delta for `pool_id` that sets its `reserves` attribute.
2109    fn reserves_delta(
2110        pool_id: &str,
2111    ) -> HashMap<String, tycho_common::models::protocol::ProtocolComponentStateDelta> {
2112        use tycho_common::models::protocol::ProtocolComponentStateDelta;
2113
2114        HashMap::from([(
2115            pool_id.to_string(),
2116            ProtocolComponentStateDelta {
2117                component_id: pool_id.to_string(),
2118                updated_attributes: HashMap::from([(
2119                    "reserves".to_string(),
2120                    Bytes::from(vec![1u8]),
2121                )]),
2122                deleted_attributes: HashSet::new(),
2123                created_attributes: HashSet::new(),
2124            },
2125        )])
2126    }
2127
2128    /// Whether `overrides` are `PendingOverrides` that set slot 0 of `account` to `value`.
2129    fn sets_slot_zero(overrides: &Arc<dyn Any + Send + Sync>, account: &Bytes, value: u8) -> bool {
2130        overrides
2131            .downcast_ref::<PendingOverrides>()
2132            .and_then(|pending| pending.storage.as_ref())
2133            .and_then(|storage| storage.get(&Address::from_slice(account)))
2134            .and_then(|slots| slots.get(&U256::ZERO))
2135            .is_some_and(|slot| *slot == U256::from(value))
2136    }
2137
2138    /// Builds a decoder that stores `stored` as `pool_id` and links it to `account`. Adds a
2139    /// component of `protocol_system` for the pool only when `protocol_system` is `Some`.
2140    async fn decoder_with_linked_pool(
2141        pool_id: &str,
2142        stored: MockProtocolSim,
2143        account: &Bytes,
2144        protocol_system: Option<&str>,
2145    ) -> TychoStreamDecoder<BlockHeader> {
2146        let decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Ethereum);
2147        {
2148            let mut state = decoder.state.write().await;
2149            state
2150                .states
2151                .insert(pool_id.to_string(), Box::new(stored));
2152            state
2153                .contracts_map
2154                .insert(account.clone(), HashSet::from([pool_id.to_string()]));
2155            if let Some(protocol_system) = protocol_system {
2156                state.components.insert(
2157                    pool_id.to_string(),
2158                    component_with_protocol_system(pool_id, protocol_system),
2159                );
2160            }
2161        }
2162        decoder
2163    }
2164
2165    /// When one extractor has both a state delta for a pool and a write to an account linked to
2166    /// that pool, the decoder clones the pool once, applies the delta with the extractor's
2167    /// component balances, then sets the overrides on that clone.
2168    #[tokio::test]
2169    async fn test_apply_deltas_ephemeral_applies_state_delta_before_overrides() {
2170        use tycho_common::models::protocol::ComponentBalance;
2171
2172        let pool_id = "0xpool";
2173        let account = Bytes::from(vec![5u8; 20]);
2174        let token = Bytes::from(vec![3u8; 20]);
2175        let mut sequence = mockall::Sequence::new();
2176        let mut clone = MockProtocolSim::new();
2177        let expected_token = token.clone();
2178        clone
2179            .expect_delta_transition()
2180            .withf(move |delta, _, balances| {
2181                delta
2182                    .updated_attributes
2183                    .contains_key("reserves") &&
2184                    balances.component_balances[pool_id][&expected_token] == vec![7u8]
2185            })
2186            .times(1)
2187            .in_sequence(&mut sequence)
2188            .returning(|_, _, _| Ok(()));
2189        clone
2190            .expect_set_pending_overrides()
2191            .times(1)
2192            .in_sequence(&mut sequence)
2193            .returning(|_| Ok(()));
2194        let mut stored = MockProtocolSim::new();
2195        stored
2196            .expect_clone_box()
2197            .times(1)
2198            .return_once(move || Box::new(clone));
2199        let decoder =
2200            decoder_with_linked_pool(pool_id, stored, &account, Some("uniswap_v4_hooks")).await;
2201        let balance = ComponentBalance::new(
2202            token.clone(),
2203            Bytes::from(vec![7u8]),
2204            7.0,
2205            Bytes::default(),
2206            pool_id,
2207        );
2208        let pending = HashMap::from([(
2209            "uniswap_v4_hooks".to_string(),
2210            BlockAggregatedChanges {
2211                state_deltas: reserves_delta(pool_id),
2212                account_deltas: account_write(&account, 1),
2213                component_balances: HashMap::from([(
2214                    pool_id.to_string(),
2215                    HashMap::from([(token, balance)]),
2216                )]),
2217                ..Default::default()
2218            },
2219        )]);
2220
2221        let update = decoder
2222            .apply_deltas_ephemeral(&pending, BlockHeader { number: 10, ..Default::default() })
2223            .await
2224            .unwrap();
2225
2226        assert!(update.states.contains_key(pool_id));
2227    }
2228
2229    /// When an extractor writes an account linked to a pool of another protocol system, or to a
2230    /// pool with no component, the decoder neither clones the pool nor sets overrides on it.
2231    #[rstest]
2232    #[case::other_protocol_system(Some("fluid_v1"), "vm:curve")]
2233    #[case::no_component(None, "fluid_v1")]
2234    #[tokio::test]
2235    async fn test_apply_deltas_ephemeral_skips_pools_of_other_protocol_systems(
2236        #[case] pool_protocol_system: Option<&str>,
2237        #[case] extractor_key: &str,
2238    ) {
2239        let account = Bytes::from(vec![6u8; 20]);
2240        let mut stored = MockProtocolSim::new();
2241        stored.expect_clone_box().times(0);
2242        let decoder =
2243            decoder_with_linked_pool("0xpool", stored, &account, pool_protocol_system).await;
2244        let pending = HashMap::from([(
2245            extractor_key.to_string(),
2246            BlockAggregatedChanges {
2247                account_deltas: account_write(&account, 1),
2248                ..Default::default()
2249            },
2250        )]);
2251
2252        let update = decoder
2253            .apply_deltas_ephemeral(&pending, BlockHeader { number: 10, ..Default::default() })
2254            .await
2255            .unwrap();
2256
2257        assert!(update.states.is_empty());
2258    }
2259
2260    /// When two extractors write the same account, linked to one pool of each, each pool takes
2261    /// only its own extractor's overrides, whatever order the extractors run in.
2262    #[tokio::test]
2263    async fn test_apply_deltas_ephemeral_gives_each_pool_its_own_extractors_overrides() {
2264        let account = Bytes::from(vec![8u8; 20]);
2265        let pools = [("0xfluid", "fluid_v1", 1u8), ("0xhooked", "uniswap_v4_hooks", 2u8)];
2266        let decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Ethereum);
2267        {
2268            let mut state = decoder.state.write().await;
2269            for (pool_id, protocol_system, value) in pools {
2270                let expected_account = account.clone();
2271                let mut clone = MockProtocolSim::new();
2272                clone
2273                    .expect_delta_transition()
2274                    .returning(|_, _, _| Ok(()));
2275                clone
2276                    .expect_set_pending_overrides()
2277                    .withf(move |overrides| sets_slot_zero(overrides, &expected_account, value))
2278                    .times(1)
2279                    .returning(|_| Ok(()));
2280                let mut stored = MockProtocolSim::new();
2281                stored
2282                    .expect_clone_box()
2283                    .times(1)
2284                    .return_once(move || Box::new(clone));
2285                state
2286                    .states
2287                    .insert(pool_id.to_string(), Box::new(stored));
2288                state.components.insert(
2289                    pool_id.to_string(),
2290                    component_with_protocol_system(pool_id, protocol_system),
2291                );
2292            }
2293            state.contracts_map.insert(
2294                account.clone(),
2295                pools
2296                    .map(|(pool_id, ..)| pool_id.to_string())
2297                    .into(),
2298            );
2299        }
2300        let pending: HashMap<String, BlockAggregatedChanges> = pools
2301            .into_iter()
2302            .map(|(_, protocol_system, value)| {
2303                let changes = BlockAggregatedChanges {
2304                    account_deltas: account_write(&account, value),
2305                    ..Default::default()
2306                };
2307                (protocol_system.to_string(), changes)
2308            })
2309            .collect();
2310
2311        let update = decoder
2312            .apply_deltas_ephemeral(&pending, BlockHeader { number: 10, ..Default::default() })
2313            .await
2314            .unwrap();
2315
2316        assert_eq!(update.states.len(), 2);
2317    }
2318
2319    /// The decoder links pools by the `pending_deltas` key and ignores
2320    /// `BlockAggregatedChanges::extractor`.
2321    #[tokio::test]
2322    async fn test_apply_deltas_ephemeral_links_pools_by_pending_deltas_key() {
2323        let pool_id = "0xfluid";
2324        let account = Bytes::from(vec![6u8; 20]);
2325        let mut clone = MockProtocolSim::new();
2326        clone
2327            .expect_delta_transition()
2328            .returning(|_, _, _| Ok(()));
2329        clone
2330            .expect_set_pending_overrides()
2331            .times(1)
2332            .returning(|_| Ok(()));
2333        let mut stored = MockProtocolSim::new();
2334        stored
2335            .expect_clone_box()
2336            .times(1)
2337            .return_once(move || Box::new(clone));
2338        let decoder = decoder_with_linked_pool(pool_id, stored, &account, Some("fluid_v1")).await;
2339        let pending = HashMap::from([(
2340            "fluid_v1".to_string(),
2341            BlockAggregatedChanges {
2342                extractor: "fluid".to_string(),
2343                account_deltas: account_write(&account, 1),
2344                ..Default::default()
2345            },
2346        )]);
2347
2348        let update = decoder
2349            .apply_deltas_ephemeral(&pending, BlockHeader { number: 10, ..Default::default() })
2350            .await
2351            .unwrap();
2352
2353        assert!(update.states.contains_key(pool_id));
2354    }
2355
2356    /// When a pool's own state delta fails, the decoder leaves the pool out of the update, even
2357    /// when the same extractor writes an account linked to it.
2358    #[tokio::test]
2359    async fn test_apply_deltas_ephemeral_leaves_out_a_pool_whose_own_delta_failed() {
2360        let pool_id = "0xpool";
2361        let account = Bytes::from(vec![5u8; 20]);
2362        let mut stored = MockProtocolSim::new();
2363        stored
2364            .expect_clone_box()
2365            .times(1)
2366            .returning(|| {
2367                let mut clone = MockProtocolSim::new();
2368                clone
2369                    .expect_delta_transition()
2370                    .returning(|_, _, _| Err(TransitionError::DecodeError("reserves".to_string())));
2371                Box::new(clone)
2372            });
2373        let decoder = decoder_with_linked_pool(pool_id, stored, &account, Some("fluid_v1")).await;
2374        let pending = HashMap::from([(
2375            "fluid_v1".to_string(),
2376            BlockAggregatedChanges {
2377                state_deltas: reserves_delta(pool_id),
2378                account_deltas: account_write(&account, 1),
2379                ..Default::default()
2380            },
2381        )]);
2382
2383        let update = decoder
2384            .apply_deltas_ephemeral(&pending, BlockHeader { number: 10, ..Default::default() })
2385            .await
2386            .unwrap();
2387
2388        assert!(!update.states.contains_key(pool_id));
2389    }
2390
2391    #[test]
2392    fn test_generate_proxy_token_address() {
2393        let idx = 1;
2394        let generated_address =
2395            generate_proxy_token_address(idx).expect("proxy token address should be valid");
2396        assert_eq!(generated_address, address!("000000000000000000000000000000001badbabe"));
2397
2398        let idx = 123456;
2399        let generated_address =
2400            generate_proxy_token_address(idx).expect("proxy token address should be valid");
2401        assert_eq!(generated_address, address!("00000000000000000000000000001e240badbabe"));
2402    }
2403
2404    /// A single-block feed message holding one tokenless Uniswap V4 pool whose `hooks` attribute
2405    /// points at an address no native handler is registered for.
2406    ///
2407    /// The component has no tokens and the message no VM storage, so decoding it neither reads
2408    /// nor writes the process-wide simulation engine and cannot disturb other tests.
2409    fn hooked_v4_msg() -> FeedMessage<BlockHeader> {
2410        use tycho_client::feed::synchronizer::{Snapshot, StateSyncMessage};
2411        use tycho_common::models::protocol::{ProtocolComponent, ProtocolComponentState};
2412
2413        let id = "0x00000000000000000000000000000000000000000000000000000000000000c4";
2414        let static_attributes = HashMap::from([
2415            ("key_lp_fee".to_string(), Bytes::from(500_i32.to_be_bytes().to_vec())),
2416            ("tick_spacing".to_string(), Bytes::from(60_i32.to_be_bytes().to_vec())),
2417            (
2418                "hooks".to_string(),
2419                Bytes::from_str("0x00000000000000000000000000000000000000c4").unwrap(),
2420            ),
2421        ]);
2422        let attributes = HashMap::from([
2423            ("liquidity".to_string(), Bytes::from(100_u64.to_be_bytes().to_vec())),
2424            ("tick".to_string(), Bytes::from(300_i32.to_be_bytes().to_vec())),
2425            (
2426                "sqrt_price_x96".to_string(),
2427                Bytes::from(
2428                    79228162514264337593543950336_u128
2429                        .to_be_bytes()
2430                        .to_vec(),
2431                ),
2432            ),
2433            ("protocol_fees/zero2one".to_string(), Bytes::from(0_u32.to_be_bytes().to_vec())),
2434            ("protocol_fees/one2zero".to_string(), Bytes::from(0_u32.to_be_bytes().to_vec())),
2435            ("ticks/60/net_liquidity".to_string(), Bytes::from(400_i128.to_be_bytes().to_vec())),
2436        ]);
2437
2438        let snapshot = ComponentWithState {
2439            state: ProtocolComponentState::new(id, attributes, HashMap::new()),
2440            component: ProtocolComponent {
2441                id: id.to_string(),
2442                static_attributes,
2443                ..Default::default()
2444            },
2445            component_tvl: None,
2446            entrypoints: Vec::new(),
2447        };
2448
2449        FeedMessage {
2450            state_msgs: HashMap::from([(
2451                "uniswap_v4_hooks".to_string(),
2452                StateSyncMessage {
2453                    header: header_at(1, 1_000, None),
2454                    snapshots: Snapshot {
2455                        states: HashMap::from([(id.to_string(), snapshot)]),
2456                        vm_storage: HashMap::new(),
2457                    },
2458                    deltas: None,
2459                    removed_components: HashMap::new(),
2460                },
2461            )]),
2462            sync_states: HashMap::new(),
2463        }
2464    }
2465
2466    /// A decoder for `chain` that decodes [`hooked_v4_msg`].
2467    fn hooked_v4_decoder(chain: Chain, context: DecoderContext) -> TychoStreamDecoder<BlockHeader> {
2468        let mut decoder = TychoStreamDecoder::new(chain);
2469        decoder.register_decoder_with_context::<crate::evm::protocol::uniswap_v4::state::UniswapV4State>(
2470            "uniswap_v4_hooks", context
2471        );
2472        decoder
2473    }
2474
2475    #[tokio::test]
2476    async fn test_unregistered_hook_is_rejected_off_the_generic_vm_chains() {
2477        let decoder = hooked_v4_decoder(Chain::Robinhood, DecoderContext::new());
2478
2479        let Err(StreamDecodeError::Fatal(message)) = decoder.decode(&hooked_v4_msg()).await else {
2480            panic!("a hooked pool on robinhood must not decode");
2481        };
2482
2483        assert!(message.contains("unsupported uniswap v4 hook"), "{message}");
2484        assert!(message.contains("robinhood"), "{message}");
2485    }
2486
2487    #[tokio::test]
2488    async fn test_decoder_chain_overrides_the_registered_context_chain() {
2489        let decoder =
2490            hooked_v4_decoder(Chain::Robinhood, DecoderContext::new().chain(Chain::Ethereum));
2491
2492        let Err(StreamDecodeError::Fatal(message)) = decoder.decode(&hooked_v4_msg()).await else {
2493            panic!("the decoder's chain must win over the context's");
2494        };
2495
2496        assert!(message.contains("robinhood"), "{message}");
2497    }
2498
2499    #[tokio::test]
2500    async fn test_unregistered_hook_takes_the_generic_path_on_ethereum() {
2501        let decoder = hooked_v4_decoder(Chain::Ethereum, DecoderContext::new());
2502
2503        let Err(StreamDecodeError::Fatal(message)) = decoder.decode(&hooked_v4_msg()).await else {
2504            panic!("the generic VM creator has no balance_owner attribute to work from");
2505        };
2506
2507        assert!(!message.contains("unsupported uniswap v4 hook"), "{message}");
2508        assert!(message.contains("balance_owner"), "{message}");
2509    }
2510
2511    #[tokio::test(flavor = "multi_thread")]
2512    async fn test_euler_hook_low_pool_manager_balance() {
2513        let mut decoder = TychoStreamDecoder::new(Chain::Ethereum);
2514
2515        decoder.register_decoder_with_context::<crate::evm::protocol::uniswap_v4::state::UniswapV4State>(
2516            "uniswap_v4_hooks", DecoderContext::new().vm_traces(true)
2517        );
2518
2519        let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
2520        let teth = Bytes::from_str("0xd11c452fc99cf405034ee446803b6f6c1f6d5ed8").unwrap();
2521        let tokens = HashMap::from([
2522            (
2523                weth.clone(),
2524                Token::new(&weth, "WETH", 18, 100, &[Some(100_000)], Chain::Ethereum, 100),
2525            ),
2526            (
2527                teth.clone(),
2528                Token::new(&teth, "tETH", 18, 100, &[Some(100_000)], Chain::Ethereum, 100),
2529            ),
2530        ]);
2531
2532        decoder.set_tokens(tokens.clone()).await;
2533
2534        let msg = load_test_msg("euler_hook_snapshot");
2535        let res = decoder
2536            .decode(&msg)
2537            .await
2538            .expect("decode failure");
2539
2540        let pool_state = res
2541            .states
2542            .get("0xc70d7fbd7fcccdf726e02fed78548b40dc52502b097c7a1ee7d995f4d4396134")
2543            .expect("Couldn't find target pool");
2544        let amount_out = pool_state
2545            .get_amount_out(
2546                BigUint::from_str("1000000000000000000").unwrap(),
2547                tokens.get(&teth).unwrap(),
2548                tokens.get(&weth).unwrap(),
2549            )
2550            .expect("Get amount out failed");
2551
2552        assert_eq!(amount_out.amount, BigUint::from_str("1216190190361759119").unwrap());
2553    }
2554
2555    /// A single-block feed message carrying `snapshot` as a `uniswap_v4_hooks` component.
2556    fn pons_feed_message(snapshot: ComponentWithState) -> FeedMessage<BlockHeader> {
2557        use tycho_client::feed::synchronizer::{Snapshot, StateSyncMessage};
2558
2559        use crate::evm::protocol::uniswap_v4::pons_fixture;
2560
2561        FeedMessage {
2562            state_msgs: HashMap::from([(
2563                "uniswap_v4_hooks".to_string(),
2564                StateSyncMessage {
2565                    header: pons_fixture::header(),
2566                    snapshots: Snapshot {
2567                        states: HashMap::from([(pons_fixture::POOL_ID.to_string(), snapshot)]),
2568                        vm_storage: HashMap::new(),
2569                    },
2570                    deltas: None,
2571                    removed_components: HashMap::new(),
2572                },
2573            )]),
2574            sync_states: HashMap::new(),
2575        }
2576    }
2577
2578    /// A decoder for Robinhood that knows the fixture pool's two tokens.
2579    async fn pons_stream_decoder() -> TychoStreamDecoder<BlockHeader> {
2580        use crate::evm::protocol::uniswap_v4::{
2581            hooks::hook_handler_creator::initialize_hook_handlers, pons_fixture,
2582            state::UniswapV4State,
2583        };
2584
2585        initialize_hook_handlers().expect("hook handler registration should succeed");
2586        let mut decoder = TychoStreamDecoder::new(Chain::Robinhood);
2587        decoder.register_decoder::<UniswapV4State>("uniswap_v4_hooks");
2588        decoder
2589            .set_tokens(pons_fixture::tokens())
2590            .await;
2591        decoder
2592    }
2593
2594    /// The whole stream path on Robinhood: a Pons pool arrives in a snapshot, is emitted as a
2595    /// `UniswapV4State` carrying the native handler, and quotes the hookless output less the fee
2596    /// and the tax `registerPool` froze for it.
2597    #[tokio::test]
2598    async fn test_pons_pool_decodes_through_the_stream_decoder_on_robinhood() {
2599        use crate::evm::protocol::uniswap_v4::{
2600            hooks::pons_v2::hook_handler::PonsV2HookHandler, pons_fixture, state::UniswapV4State,
2601        };
2602
2603        let decoder = pons_stream_decoder().await;
2604
2605        let result = decoder
2606            .decode(&pons_feed_message(pons_fixture::snapshot()))
2607            .await
2608            .expect("decode failure");
2609
2610        let emitted = result
2611            .states
2612            .get(pons_fixture::POOL_ID)
2613            .expect("the Pons pool must reach consumers");
2614        let pool = emitted
2615            .as_any()
2616            .downcast_ref::<UniswapV4State>()
2617            .expect("a uniswap_v4_hooks component decodes into a UniswapV4State");
2618        let handler = pool
2619            .hook
2620            .as_ref()
2621            .expect("a Pons pool must carry a hook handler");
2622        let pons = handler
2623            .as_any()
2624            .downcast_ref::<PonsV2HookHandler>()
2625            .expect("the registry must hand back the native Pons handler");
2626        assert_eq!(u32::from(pons.hook_fee_bps()), pons_fixture::HOOK_FEE_BPS);
2627        assert_eq!(u32::from(pons.creator_tax_bps()), pons_fixture::CREATOR_TAX_BPS);
2628
2629        let core = pons_fixture::decode_on(pons_fixture::hookless_snapshot(), Chain::Robinhood)
2630            .await
2631            .expect("the same pool with no hook must decode too");
2632        let (xlg, nvda) = (pons_fixture::xlg(), pons_fixture::nvda());
2633        let amount_in = BigUint::from(10u64).pow(18);
2634        for (token_in, token_out) in [(&xlg, &nvda), (&nvda, &xlg)] {
2635            let core_out = core
2636                .get_amount_out(amount_in.clone(), token_in, token_out)
2637                .expect("the reference pool quotes one whole token")
2638                .amount;
2639            let hooked_out = emitted
2640                .get_amount_out(amount_in.clone(), token_in, token_out)
2641                .expect("the emitted pool quotes one whole token")
2642                .amount;
2643
2644            let expected = pons_fixture::net_of_hook_take(&core_out);
2645            assert!(expected < core_out, "the hook must take something out of {core_out}");
2646            assert_eq!(hooked_out, expected, "{} -> {}", token_in.symbol, token_out.symbol);
2647        }
2648    }
2649
2650    /// The same pool behind a hook with no native handler never reaches consumers, even when the
2651    /// stream is configured to carry on past a decode failure.
2652    #[tokio::test]
2653    async fn test_unknown_hook_component_is_not_emitted_on_robinhood() {
2654        use crate::evm::protocol::uniswap_v4::pons_fixture;
2655
2656        let mut decoder = pons_stream_decoder().await;
2657        decoder.skip_state_decode_failures(true);
2658
2659        let result = decoder
2660            .decode(&pons_feed_message(pons_fixture::unknown_hook_snapshot()))
2661            .await
2662            .expect("a skipped decode failure is not fatal");
2663
2664        assert!(
2665            !result
2666                .states
2667                .contains_key(pons_fixture::POOL_ID),
2668            "a pool whose hook cannot be modelled must not be quoted"
2669        );
2670        assert!(result.states.is_empty());
2671    }
2672
2673    fn component_with_id(id: &str) -> ComponentWithState {
2674        use tycho_common::models::protocol::{ProtocolComponent, ProtocolComponentState};
2675
2676        ComponentWithState {
2677            state: ProtocolComponentState::new(id, HashMap::new(), HashMap::new()),
2678            component: ProtocolComponent { id: id.to_string(), ..Default::default() },
2679            component_tvl: None,
2680            entrypoints: Vec::new(),
2681        }
2682    }
2683
2684    fn rejects_a(component: &ComponentWithState) -> bool {
2685        component.component.id != "a"
2686    }
2687
2688    fn rejects_b(component: &ComponentWithState) -> bool {
2689        component.component.id != "b"
2690    }
2691
2692    #[test]
2693    fn test_admits_requires_every_registered_filter() {
2694        // Two filters on one exchange both apply: the second registration does not replace the
2695        // first, and a component must pass both. An exchange without filters admits everything.
2696        let mut decoder = TychoStreamDecoder::<BlockHeader>::new(Chain::Ethereum);
2697        decoder.register_filter("x", rejects_a);
2698        decoder.register_filter("x", rejects_b);
2699
2700        assert!(!decoder.admits("x", &component_with_id("a")));
2701        assert!(!decoder.admits("x", &component_with_id("b")));
2702        assert!(decoder.admits("x", &component_with_id("c")));
2703        assert!(decoder.admits("y", &component_with_id("a")));
2704    }
2705}