1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::fmt::Debug;

use alloy_primitives::{Address, U256};
use clickhouse::Row;
use serde::{Deserialize, Serialize};

use super::accounting::{apply_delta, AddressDeltas, TokenAccounting};
pub use super::{Action, NormalizedSwap};
use crate::Protocol;

#[derive(Debug, Default, Serialize, Clone, Row, PartialEq, Eq, Deserialize)]
pub struct NormalizedBatch {
    pub protocol:            Protocol,
    pub trace_index:         u64,
    pub solver:              Address,
    pub settlement_contract: Address,
    pub user_swaps:          Vec<NormalizedSwap>,
    pub solver_swaps:        Option<Vec<NormalizedSwap>>,
    pub msg_value:           U256,
}

impl NormalizedBatch {
    pub fn fetch_underlying_actions(self) -> impl Iterator<Item = Action> {
        self.user_swaps
            .into_iter()
            .chain(self.solver_swaps.unwrap_or_default())
            .map(Action::from)
    }
}

impl TokenAccounting for NormalizedBatch {
    fn apply_token_deltas(&self, delta_map: &mut AddressDeltas) {
        self.user_swaps.iter().for_each(|swap| {
            apply_delta(self.solver, swap.token_in.address, swap.amount_in.clone(), delta_map);
            apply_delta(self.solver, swap.token_out.address, -swap.amount_out.clone(), delta_map);

            swap.apply_token_deltas(delta_map);
        });

        if let Some(swaps) = &self.solver_swaps {
            swaps
                .iter()
                .for_each(|swap| swap.apply_token_deltas(delta_map));
        }
    }
}