loom_types_entities/
swappath.rs

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::Arc;

use alloy_primitives::Address;
use eyre::Result;

use crate::{PoolWrapper, Token};

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct SwapPath {
    pub tokens: Vec<Arc<Token>>,
    pub pools: Vec<PoolWrapper>,
}

impl SwapPath {
    pub fn new<T: Into<Arc<Token>>, P: Into<PoolWrapper>>(tokens: Vec<T>, pools: Vec<P>) -> Self {
        SwapPath { tokens: tokens.into_iter().map(|i| i.into()).collect(), pools: pools.into_iter().map(|i| i.into()).collect() }
    }

    pub fn is_emply(&self) -> bool {
        self.tokens.is_empty() && self.pools.is_empty()
    }

    pub fn tokens_count(&self) -> usize {
        self.tokens.len()
    }

    pub fn pool_count(&self) -> usize {
        self.pools.len()
    }

    pub fn new_swap(token_from: Arc<Token>, token_to: Arc<Token>, pool: PoolWrapper) -> Self {
        SwapPath { tokens: vec![token_from, token_to], pools: vec![pool] }
    }

    pub fn push_swap_hope(&mut self, token_from: Arc<Token>, token_to: Arc<Token>, pool: PoolWrapper) -> Result<&mut Self> {
        if self.is_emply() {
            self.tokens = vec![token_from, token_to];
            self.pools = vec![pool];
        } else {
            if token_from.as_ref() != self.tokens.last().map_or(&Default::default(), |t| t.as_ref()) {
                return Err(eyre::eyre!("NEW_SWAP_NOT_CONNECTED"));
            }
            self.tokens.push(token_to);
            self.pools.push(pool);
        }
        Ok(self)
    }

    pub fn insert_swap_hope(&mut self, token_from: Arc<Token>, token_to: Arc<Token>, pool: PoolWrapper) -> Result<&mut Self> {
        if self.is_emply() {
            self.tokens = vec![token_from, token_to];
            self.pools = vec![pool];
        } else {
            if token_to.as_ref() != self.tokens.first().map_or(&Default::default(), |t| t.as_ref()) {
                return Err(eyre::eyre!("NEW_SWAP_NOT_CONNECTED"));
            }
            self.tokens.insert(0, token_from);
            self.pools.insert(0, pool);
        }

        Ok(self)
    }

    pub fn contains_pool(&self, pool: &PoolWrapper) -> bool {
        for p in self.pools.iter() {
            if p.get_address() == pool.get_address() {
                return true;
            }
        }
        false
    }
}

impl Hash for SwapPath {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.tokens.hash(state);
        self.pools.hash(state);
    }
}

#[derive(Clone, Debug, Default)]
pub struct SwapPaths {
    paths: HashSet<Arc<SwapPath>>,
    pool_paths: HashMap<Address, Arc<Vec<SwapPath>>>,
}

impl SwapPaths {
    pub fn new() -> SwapPaths {
        SwapPaths { paths: HashSet::new(), pool_paths: HashMap::new() }
    }
    pub fn from(paths: Vec<SwapPath>) -> Self {
        let mut ret = Self::default();
        for p in paths {
            ret.add(p);
        }
        ret
    }

    pub fn add_mut(&mut self, path: SwapPath) -> bool {
        let rc_path = Arc::new(path.clone());

        if self.paths.insert(rc_path.clone()) {
            for pool in rc_path.pools.iter() {
                let mut e = self.pool_paths.get(&pool.get_address()).cloned().unwrap_or(Arc::new(Vec::new()));
                let e_mut = Arc::make_mut(&mut e);

                e_mut.push(path.clone());
                self.pool_paths.insert(pool.get_address(), e);
            }
            true
        } else {
            false
        }
    }

    pub fn add<T: Into<SwapPath> + Clone>(&mut self, path: T) {
        let rc_path: Arc<SwapPath> = Arc::new(path.clone().into());

        if self.paths.insert(rc_path.clone()) {
            for pool in rc_path.pools.iter() {
                let mut e = self.pool_paths.get(&pool.get_address()).cloned().map_or_else(Vec::new, |v| v.deref().clone());
                //let e_mut = Arc::make_mut(&mut e);

                e.push(path.clone().into());
                self.pool_paths.insert(pool.get_address(), Arc::new(e));
            }
        }
    }

    pub fn get_pool_paths_hashset(&self, pool_address: &Address) -> Option<&Arc<Vec<SwapPath>>> {
        self.pool_paths.get(pool_address)
    }

    pub fn get_pool_paths_vec(&self, pool_address: &Address) -> Option<Vec<SwapPath>> {
        self.get_pool_paths_hashset(pool_address).map(|set| set.iter().cloned().collect())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::pool::DefaultAbiSwapEncoder;
    use crate::required_state::RequiredState;
    use crate::{AbiSwapEncoder, Pool};
    use alloy_primitives::U256;
    use eyre::{eyre, ErrReport};
    use revm::primitives::Env;
    use revm::DatabaseRef;
    use tokio::task::JoinHandle;
    use tracing::error;

    #[derive(Clone)]
    pub struct EmptyPool {
        address: Address,
    }

    impl EmptyPool {
        pub fn new(address: Address) -> Self {
            EmptyPool { address }
        }
    }

    impl Pool for EmptyPool {
        fn get_address(&self) -> Address {
            self.address
        }

        fn calculate_out_amount(
            &self,
            _state: &dyn DatabaseRef<Error = ErrReport>,
            _env: Env,
            _token_address_from: &Address,
            _token_address_to: &Address,
            _in_amount: U256,
        ) -> Result<(U256, u64), ErrReport> {
            Err(eyre!("NOT_IMPLEMENTED"))
        }

        fn calculate_in_amount(
            &self,
            _state: &dyn DatabaseRef<Error = ErrReport>,
            _env: Env,
            _token_address_from: &Address,
            _token_address_to: &Address,
            _out_amount: U256,
        ) -> eyre::Result<(U256, u64), ErrReport> {
            Err(eyre!("NOT_IMPLEMENTED"))
        }

        fn can_flash_swap(&self) -> bool {
            false
        }

        fn get_encoder(&self) -> &dyn AbiSwapEncoder {
            &DefaultAbiSwapEncoder {}
        }

        fn get_state_required(&self) -> Result<RequiredState> {
            Ok(RequiredState::new())
        }
    }

    #[test]
    fn test_add_path() {
        let basic_token = Token::new(Address::repeat_byte(0x11));

        let paths_vec: Vec<SwapPath> = (0..10)
            .map(|i| {
                SwapPath::new(
                    vec![basic_token.clone(), Token::new(Address::repeat_byte(i)), basic_token.clone()],
                    vec![
                        PoolWrapper::new(Arc::new(EmptyPool::new(Address::repeat_byte(i + 1)))),
                        PoolWrapper::new(Arc::new(EmptyPool::new(Address::repeat_byte(i + 2)))),
                    ],
                )
            })
            .collect();
        let paths = SwapPaths::from(paths_vec);

        println!("{paths:?}")
    }

    #[tokio::test]
    async fn async_test() {
        let basic_token = Token::new(Address::repeat_byte(0x11));

        const PATHS_COUNT: usize = 10;

        let pool_address_vec: Vec<(PoolWrapper, PoolWrapper)> = (0..PATHS_COUNT)
            .map(|i| {
                (
                    PoolWrapper::new(Arc::new(EmptyPool::new(Address::repeat_byte(i as u8)))),
                    PoolWrapper::new(Arc::new(EmptyPool::new(Address::repeat_byte((i + 1) as u8)))),
                )
            })
            .collect();

        let paths_vec: Vec<SwapPath> = pool_address_vec
            .iter()
            .map(|p| {
                SwapPath::new(
                    vec![basic_token.clone(), Token::new(Address::repeat_byte(1)), basic_token.clone()],
                    vec![p.0.clone(), p.1.clone()],
                )
            })
            .collect();

        let mut paths = SwapPaths::from(paths_vec.clone());
        for path in paths_vec.clone() {
            println!("{}", paths.add_mut(path));
        }

        let paths_shared = Arc::new(tokio::sync::RwLock::new(paths));

        let mut tasks: Vec<JoinHandle<_>> = Vec::new();

        for (i, pools) in pool_address_vec.into_iter().enumerate() {
            let pool_address = pools.0.get_address();
            let paths_shared_clone = paths_shared.clone();
            tasks.push(tokio::task::spawn(async move {
                let pool = PoolWrapper::new(Arc::new(EmptyPool::new(pool_address)));
                let path_guard = paths_shared_clone.read().await;
                let pool_paths = path_guard.get_pool_paths_hashset(&pool.get_address());
                println!("{i} {pool_address}: {pool_paths:?}");
            }));
        }

        for t in tasks {
            if let Err(e) = t.await {
                error!("{}", e)
            }
        }
    }
}