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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]

use std::{
    fmt::Debug,
    sync::{Arc, OnceLock},
};

use alloy_primitives::{Address, Bytes};
use brontes_database::libmdbx::{DBWriter, LibmdbxReader};
use brontes_metrics::classifier::ClassificationMetrics;
use brontes_pricing::types::DexPriceMsg;
use brontes_types::{
    normalized_actions::pool::NormalizedNewPool, structured_trace::CallFrameInfo,
    traits::TracingProvider,
};
use futures::Future;

pub mod tree_builder;
pub use tree_builder::Classifier;
pub mod discovery_only;
pub mod multi_frame_classification;

#[cfg(feature = "tests")]
pub mod test_utils;

mod classifiers;
use alloy_sol_types::sol;
use brontes_types::normalized_actions::Action;
pub use classifiers::*;

// Actions
sol!(UniswapV2, "./classifier-abis/UniswapV2.json");
sol!(SushiSwapV2, "./classifier-abis/SushiSwapV2.json");
sol!(UniswapV3, "./classifier-abis/UniswapV3.json");
sol!(SushiSwapV3, "./classifier-abis/SushiSwapV3.json");
sol!(PancakeSwapV2, "./classifier-abis/PancakeSwapV2.json");
sol!(PancakeSwapV3, "./classifier-abis/PancakeSwapV3.json");
sol!(CurveBase2, "./classifier-abis/CurveBase2.json");
//sol!(CurveLido2, "./classifier-abis/CurveBase2Lido.json");
sol!(CurveBase3, "./classifier-abis/CurveBase3.json");
sol!(CurveBase4, "./classifier-abis/CurveBase4.json");
sol!(CurveV1MetapoolImpl, "./classifier-abis/CurveV1MetapoolImpl.json");
sol!(CurveV2MetapoolImpl, "./classifier-abis/CurveV2MetapoolImpl.json");
sol!(CurveV2PlainImpl, "./classifier-abis/CurveV2PlainImpl.json");
sol!(CurvecrvUSDPlainImpl, "./classifier-abis/CurvecrvUSDPlainImpl.json");
sol!(CurveCryptoSwap, "./classifier-abis/CurveCryptoSwap.json");
sol!(BalancerV1, "./classifier-abis/balancer/BalancerV1Pool.json");
sol!(BalancerV2Vault, "./classifier-abis/balancer/BalancerV2Vault.json");
sol!(AaveV2, "./classifier-abis/AaveV2Pool.json");
sol!(AaveV3, "./classifier-abis/AaveV3Pool.json");
sol!(UniswapX, "./classifier-abis/UniswapXExclusiveDutchOrderReactor.json");
sol!(MakerPSM, "./classifier-abis/maker/MakerPSM.json");
sol!(MakerDssFlash, "./classifier-abis/maker/MakerDssFlash.json");
sol!(CompoundV2CToken, "./classifier-abis/CompoundV2CToken.json");
sol!(OneInchAggregationRouterV5, "./classifier-abis/OneInchAggregationRouterV5.json");
sol!(OneInchFusionSettlement, "./classifier-abis/OneInchFusionSettlement.json");
sol!(ClipperExchange, "./classifier-abis/ClipperExchange.json");
sol!(CowswapGPv2Settlement, "./classifier-abis/cowswap/GPv2Settlement.json");
sol!(ZeroXUniswapFeaure, "./classifier-abis/zero-x/ZeroXUniswapFeature.json");
sol!(ZeroXUniswapV3Feature, "./classifier-abis/zero-x/ZeroXUniswapV3Feature.json");
sol!(ZeroXTransformERC20Feature, "./classifier-abis/zero-x/ZeroXTransformERC20Feature.json");
sol!(ZeroXPancakeSwapFeature, "./classifier-abis/zero-x/ZeroXPancakeSwapFeature.json");
sol!(ZeroXOtcOrdersFeature, "./classifier-abis/zero-x/ZeroXOtcOrdersFeature.json");
sol!(ZeroXLiquidityProviderFeature, "./classifier-abis/zero-x/ZeroXLiquidityProviderFeature.json");
sol!(ZeroXInterface, "./classifier-abis/zero-x/ZeroXInterface.json");
sol!(DodoDPPPool, "./classifier-abis/dodo/DPPPool.json");
sol!(DodoDSPPool, "./classifier-abis/dodo/DSPPool.json");

// Discovery
sol!(UniswapV2Factory, "./classifier-abis/UniswapV2Factory.json");
sol!(UniswapV3Factory, "./classifier-abis/UniswapV3Factory.json");
sol!(CurveV1MetapoolFactory, "./classifier-abis/CurveMetapoolFactoryV1.json");
sol!(CurveV2MetapoolFactory, "./classifier-abis/CurveMetapoolFactoryV2.json");
sol!(CurvecrvUSDFactory, "./classifier-abis/CurveCRVUSDFactory.json");
sol!(CurveCryptoSwapFactory, "./classifier-abis/CurveCryptoSwapFactory.json");
sol!(CurveTriCryptoFactory, "./classifier-abis/CurveTriCryptoFactory.json");
sol!(PancakeSwapV3PoolDeployer, "./classifier-abis/PancakeSwapV3PoolDeployer.json");
sol!(CompoundV2Comptroller, "./classifier-abis/CompoundV2Comptroller.json");
sol!(CErc20Delegate, "./classifier-abis/CErc20Delegate.json");
sol!(BalancerV1CorePoolFactory, "./classifier-abis/balancer/BalancerV1Factory.json");
sol!(BalancerV1SmartPoolFactory, "./classifier-abis/balancer/BalancerV1CrpFactory.json");
sol!(DodoDVMFactory, "./classifier-abis/dodo/DVMFactory.json");
sol!(DodoDPPFactory, "./classifier-abis/dodo/DPPFactory.json");
sol!(DodoDSPFactory, "./classifier-abis/dodo/DSPFactory.json");

// Balancer Pool Interfaces
sol! {
    enum SwapKind {
        GIVEN_IN,
        GIVEN_OUT
    }

    struct SwapRequest {
        SwapKind kind;
        address tokenIn;
        address tokenOut;
        uint256 amount;
        // Misc data
        bytes32 poolId;
        uint256 lastChangeBlock;
        address from;
        address to;
        bytes userData;
    }

    interface IGeneralPool {
        function onSwap(
            SwapRequest memory swapRequest,
            uint256[] memory balances,
            uint256 indexIn,
            uint256 indexOut
        ) external returns (uint256 amount);
    }

    interface IMinimalSwapInfoPool {
        function onSwap(
            SwapRequest memory swapRequest,
            uint256 currentBalanceTokenIn,
            uint256 currentBalanceTokenOut
        ) external returns (uint256 amount);
    }
}

sol! {
    event Transfer(address indexed from, address indexed to, uint256 value);
    function name() public view returns (string);
    function symbol() public view returns (string);
    function decimals() public view returns (uint8);
    function totalSupply() public view returns (uint256);
}

pub static CLASSIFICATION_METRICS: OnceLock<ClassificationMetrics> = OnceLock::new();

pub trait ActionCollection: Sync + Send {
    fn dispatch<DB: LibmdbxReader + DBWriter>(
        &self,
        call_info: CallFrameInfo<'_>,
        db_tx: &DB,
        block: u64,
        tx_idx: u64,
    ) -> Option<(DexPriceMsg, Action)>;
}

pub trait IntoAction: Debug + Send + Sync {
    fn decode_call_trace<DB: LibmdbxReader + DBWriter>(
        &self,
        call_info: CallFrameInfo<'_>,
        block: u64,
        tx_idx: u64,
        db_tx: &DB,
    ) -> eyre::Result<DexPriceMsg>;
}

pub trait FactoryDiscovery {
    fn decode_create_trace<T: TracingProvider>(
        &self,
        tracer: Arc<T>,
        deployed_address: Address,
        trace_idx: u64,
        parent_calldata: Bytes,
    ) -> impl Future<Output = Vec<NormalizedNewPool>> + Send;
}

pub trait FactoryDiscoveryDispatch: Sync + Send {
    fn dispatch<T: TracingProvider>(
        &self,
        tracer: Arc<T>,
        possible_calls: Vec<(Address, Bytes)>,
        deployed_address: Address,
        trace_idx: u64,
    ) -> impl Future<Output = Vec<NormalizedNewPool>> + Send;
}