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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#![warn(missing_docs)]

/// Defines the main simulation runtime.
pub mod arena;

/// Contains configuration types for the simulation.
pub mod config;

/// Contains the types for various price processes.
pub mod feed;

/// Defines the base strategy trait.
pub mod strategy;

/// Defines core simulation logic types, such as an [`Arbitrageur`].
pub mod engine;

use alloy::{
    network::{Ethereum, EthereumWallet},
    node_bindings::{Anvil, AnvilInstance},
    primitives::{Address, Bytes, U256},
    providers::{
        fillers::{ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller, WalletFiller},
        Identity, RootProvider,
    },
    transports::http::{Client, Http},
};

use crate::{engine::inspector::Inspector, types::PoolManager::PoolKey};

/// Provider type that includes all necessary fillers to execute transactions on an [`Anvil`] node.
pub type AnvilProvider = FillProvider<
    JoinFill<
        JoinFill<JoinFill<JoinFill<Identity, GasFiller>, NonceFiller>, ChainIdFiller>,
        WalletFiller<EthereumWallet>,
    >,
    RootProvider<Http<Client>>,
    Http<Client>,
    Ethereum,
>;

mod types {
    #![allow(clippy::too_many_arguments)]
    use alloy_sol_macro::sol;

    use crate::types::{
        Fetcher::PoolKey as FetcherPoolKey, PoolManager::PoolKey as ManagerPoolKey,
    };

    sol! {
        #[sol(rpc)]
        #[derive(Debug, Default)]
        PoolManager,
        "src/artifacts/PoolManager.json"
    }

    sol! {
        #[sol(rpc)]
        #[derive(Debug)]
        LiquidExchange,
        "src/artifacts/LiquidExchange.json"
    }

    sol! {
        #[sol(rpc)]
        #[derive(Debug)]
        ArenaToken,
        "src/artifacts/ArenaToken.json"
    }

    sol! {
        #[sol(rpc)]
        #[derive(Debug)]
        Fetcher,
        "src/artifacts/Fetcher.json"
    }

    impl From<FetcherPoolKey> for ManagerPoolKey {
        fn from(fetcher: FetcherPoolKey) -> Self {
            ManagerPoolKey {
                currency0: fetcher.currency0,
                currency1: fetcher.currency1,
                fee: fetcher.fee,
                tickSpacing: fetcher.tickSpacing,
                hooks: fetcher.hooks,
            }
        }
    }

    impl From<ManagerPoolKey> for FetcherPoolKey {
        fn from(manager: ManagerPoolKey) -> Self {
            FetcherPoolKey {
                currency0: manager.currency0,
                currency1: manager.currency1,
                fee: manager.fee,
                tickSpacing: manager.tickSpacing,
                hooks: manager.hooks,
            }
        }
    }
}

/// A signal that is passed to a [`Strategy`] to provide information about the current state of the pool.
#[derive(Debug, Clone)]
pub struct Signal {
    /// Address of the pool manager.
    pub manager: Address,

    /// Address of the fetcher.
    pub fetcher: Address,

    /// Key of the pool.
    pub pool: PoolKey,

    /// Current theoretical value of the pool.
    pub current_value: f64,

    /// Current step of the simulation.
    pub step: Option<usize>,

    /// Current tick of the pool.
    pub tick: i32,

    /// Current price of the pool.
    pub sqrt_price_x96: U256,
}

impl Signal {
    /// Public constructor function for a new [`Signal`].
    pub fn new(
        manager: Address,
        fetcher: Address,
        pool: PoolKey,
        current_value: f64,
        step: Option<usize>,
        tick: i32,
        sqrt_price_x96: U256,
    ) -> Self {
        Self {
            manager,
            fetcher,
            pool,
            current_value,
            step,
            tick,
            sqrt_price_x96,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        arena::{Arena, ArenaBuilder},
        config::Config,
        engine::{
            arbitrageur::{Arbitrageur, DefaultArbitrageur, EmptyArbitrageur},
            inspector::EmptyInspector,
        },
        feed::OrnsteinUhlenbeck,
        strategy::Strategy,
    };

    struct StrategyMock;

    impl<V> Strategy<V> for StrategyMock {
        fn init(
            &self,
            _provider: AnvilProvider,
            _signal: Signal,
            _inspector: &mut Box<dyn Inspector<V>>,
        ) {
        }
        fn process(
            &self,
            _provider: AnvilProvider,
            _signal: Signal,
            _inspector: &mut Box<dyn Inspector<V>>,
        ) {
        }
    }

    #[tokio::test]
    async fn test_arena() {
        let builder: ArenaBuilder<_> = ArenaBuilder::new();

        let mut arena: Arena<f64> = builder
            .with_strategy(Box::new(StrategyMock {}))
            .with_fee(4000)
            .with_tick_spacing(2)
            .with_feed(Box::new(OrnsteinUhlenbeck::new(0.1, 0.1, 0.1, 0.1, 0.1)))
            .with_inspector(Box::new(EmptyInspector {}))
            .with_arbitrageur(Box::new(EmptyArbitrageur {}))
            .build();

        arena.run(Config::new(2)).await;
    }
}