pool-sync 3.0.0

A library for synchronizing and managing various types of liquidity pools across different blockchains
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use alloy::network::Network;
use alloy::primitives::Address;
use alloy::providers::Provider;
use alloy::rpc::types::{Filter, Log};
use alloy::sol_types::SolEvent;
use alloy::transports::Transport;
use anyhow::anyhow;
use anyhow::Result;
use futures::StreamExt;
use indicatif::ProgressBar;
use log::info;
use rand::Rng;
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;
use tokio::sync::{Mutex, Semaphore};
use tokio::time::{interval, Duration};

use crate::events::*;
use crate::pools::pool_builder;
use crate::pools::pool_structures::balancer_v2_structure::process_balance_data;
use crate::pools::pool_structures::v2_structure::process_sync_data;
use crate::pools::pool_structures::v3_structure::process_tick_data;
use crate::pools::PoolFetcher;
use crate::util::create_progress_bar;
use crate::{Chain, Pool, PoolInfo, PoolType};

// Retry constants
const MAX_RETRIES: u32 = 5;
const INITIAL_BACKOFF: u64 = 1000; // 1 second

// Define event configurations
#[derive(Debug)]
struct EventConfig {
    events: &'static [&'static str],
    step_size: u64,
    description: &'static str,
    requires_initial_sync: bool,
}

pub struct Rpc;
impl Rpc {
    // Fetch all pool addresses for the protocol
    pub async fn fetch_pool_addrs<P, T, N>(
        start_block: u64,
        end_block: u64,
        provider: Arc<P>,
        fetcher: Arc<dyn PoolFetcher>,
        chain: Chain,
        rate_limit: u64,
    ) -> Result<Vec<Address>>
    where
        P: Provider<T, N> + 'static,
        T: Transport + Clone + 'static,
        N: Network,
    {
        // fetch all of the logs
        let filter = Filter::new()
            .address(fetcher.factory_address(chain))
            .event(fetcher.pair_created_signature());

        let step_size: u64 = 10000;
        let num_tasks = end_block / step_size;
        let pb_info = format!(
            "{} Address Sync. Block range {}-{}",
            fetcher.pool_type(),
            start_block,
            end_block
        );
        let progress_bar = Arc::new(create_progress_bar(num_tasks, pb_info));

        // fetch all of the logs
        let logs = Rpc::fetch_event_logs(
            start_block,
            end_block,
            10000,
            provider,
            rate_limit,
            progress_bar,
            filter,
        )
        .await?;

        // extract the addresses from the logs
        let addresses: Vec<Address> = logs
            .iter()
            .map(|log| fetcher.log_to_address(&log.inner))
            .collect();
        anyhow::Ok(addresses)
    }

    pub async fn populate_pools<P, T, N>(
        pool_addrs: Vec<Address>,
        provider: Arc<P>,
        pool: PoolType,
        fetcher: Arc<dyn PoolFetcher>,
        rate_limit: u64,
        chain: Chain
    ) -> Result<Vec<Pool>>
    where
        P: Provider<T, N> + 'static,
        T: Transport + Clone + 'static,
        N: Network,
    {
        // data batch size for contract calls
        let batch_size = if pool.is_balancer() { 10 } else { 50 };

        // informational and rate limiting initialization
        let total_tasks = (pool_addrs.len() + batch_size - 1) / batch_size; // Ceiling division
        let progress_bar = create_progress_bar(total_tasks as u64, format!("{} data sync", pool));
        let semaphore = Arc::new(Semaphore::new(rate_limit as usize));
        let interval = Arc::new(tokio::sync::Mutex::new(interval(Duration::from_secs_f64(
            1.0 / rate_limit as f64,
        ))));

        // break the addresses up into chunk
        let addr_chunks: Vec<Vec<Address>> = pool_addrs
            .chunks(batch_size)
            .map(|chunk| chunk.to_vec())
            .collect();

        let mut stream = futures::stream::iter(addr_chunks.into_iter().map(|chunk| {
            let provider = provider.clone();
            let sem = semaphore.clone();
            let pb = progress_bar.clone();
            let fetcher = fetcher.clone();
            let interval = interval.clone();
            let data = fetcher.get_pool_repr();

            async move {
                let _permit = sem.acquire().await.unwrap();
                interval.lock().await.tick().await;
                let mut retry_count = 0;
                let mut backoff = 1000; // Initial backoff of 1 second
                loop {
                    // try building pools from this set of addresses
                    match pool_builder::build_pools(
                        &provider,
                        chunk.clone(),
                        pool,
                        data.clone(),
                        chain
                    )
                    .await
                    {
                        Ok(populated_pools) if !populated_pools.is_empty() => {
                            pb.inc(1);
                            drop(provider);
                            return anyhow::Ok::<Vec<Pool>>(populated_pools);
                        }
                        Err(e) => {
                            if retry_count >= MAX_RETRIES {
                                info!("Failed to populate pools data: {}", e);
                                drop(provider);
                                return Ok(Vec::new());
                            }
                            let jitter = rand::thread_rng().gen_range(0..=100);
                            let sleep_duration = Duration::from_millis(backoff + jitter);
                            tokio::time::sleep(sleep_duration).await;
                            retry_count += 1;
                            backoff *= 2; // Exponential backoff
                        }
                        _ => continue,
                    }
                }
            }
        }))
        .buffer_unordered(rate_limit as usize);

        let mut all_pools = Vec::new();

        while let Some(pool_res) = stream.next().await {
            match pool_res {
                Ok(pool) => all_pools.extend(pool),
                Err(e) => return Err(e),
            }
        }

        Ok(all_pools)
    }

    pub async fn populate_liquidity<P, T, N>(
        start_block: u64,
        end_block: u64,
        pools: &mut [Pool],
        provider: Arc<P>,
        pool_type: PoolType,
        rate_limit: u64,
        is_initial_sync: bool,
    ) -> anyhow::Result<()>
    where
        P: Provider<T, N> + Sync + 'static,
        T: Transport + Sync + Clone,
        N: Network,
    {
        if pools.is_empty() {
            return anyhow::Ok(());
        }

        let address_to_index: HashMap<Address, usize> = pools
            .iter()
            .enumerate()
            .map(|(i, pool)| (pool.address(), i))
            .collect();

        let batch_size = 1_000_000;
        let mut current_block = start_block;

        // get the configuration for this sync and config we should sync
        let config = Rpc::get_event_config(pool_type, is_initial_sync);
        if is_initial_sync && config.requires_initial_sync {
            return anyhow::Ok(());
        }

        // construct the progress bar
        let num_tasks = (end_block - start_block) / config.step_size;
        let pb_info = format!(
            "{} {}. Block range {}-{}",
            pool_type, config.description, current_block, end_block
        );
        let progress_bar = Arc::new(create_progress_bar(num_tasks, pb_info));

        // sync in batches
        while current_block <= end_block {
            let batch_end = (current_block + batch_size).min(end_block);

            let logs = Rpc::fetch_logs_for_config(
                &config,
                current_block,
                batch_end,
                provider.clone(),
                progress_bar.clone(),
                rate_limit,
            )
            .await?;

            // create pb for block processing
            let processing_pb_info = format!(
                "Processing logs batch for blocks {}-{}",
                start_block, end_block
            );
            let processing_progress_bar =
                create_progress_bar(logs.len().try_into().unwrap(), processing_pb_info);

            // Process logs immediately after fetching
            let mut ordered_logs: BTreeMap<u64, Vec<Log>> = BTreeMap::new();
            for log in logs {
                if let Some(block_number) = log.block_number {
                    ordered_logs.entry(block_number).or_default().push(log);
                }
            }

            // Process logs in order
            for (_, log_group) in ordered_logs {
                for log in log_group {
                    let address = log.address();
                    if let Some(&index) = address_to_index.get(&address) {
                        if let Some(pool) = pools.get_mut(index) {
                            if pool_type.is_v3() {
                                process_tick_data(
                                    pool.get_v3_mut().unwrap(),
                                    log,
                                    pool_type,
                                    is_initial_sync,
                                );
                            } else if pool_type.is_balancer() {
                                process_balance_data(pool.get_balancer_mut().unwrap(), log);
                            } else {
                                process_sync_data(pool.get_v2_mut().unwrap(), log, pool_type);
                            }
                        }
                    }
                    processing_progress_bar.inc(1);
                }
            }

            processing_progress_bar.finish_and_clear();
            current_block = batch_end + 1;
        }
        anyhow::Ok(())
    }

    pub async fn fetch_event_logs<T, N, P>(
        start_block: u64,
        end_block: u64,
        step_size: u64,
        provider: Arc<P>,
        rate_limit: u64,
        progress_bar: Arc<ProgressBar>,
        filter: Filter,
    ) -> anyhow::Result<Vec<Log>>
    where
        T: Transport + Clone,
        N: Network,
        P: Provider<T, N> + 'static,
    {
        // generate the block range for the sync and setup progress bar
        let block_range = Rpc::get_block_range(step_size, start_block, end_block);

        // semaphore and interval for rate limiting
        let semaphore = Arc::new(Semaphore::new(rate_limit as usize));
        let interval = Arc::new(Mutex::new(interval(Duration::from_secs_f64(
            1.0 / rate_limit as f64,
        ))));

        // Create a stream of futures
        let mut stream =
            futures::stream::iter(block_range.into_iter().map(|(from_block, to_block)| {
                let provider = provider.clone();
                let sem = semaphore.clone();
                let pb = progress_bar.clone();
                let interval = interval.clone();
                let filter = filter.clone();

                async move {
                    let _permit = sem.acquire().await.unwrap();
                    interval.lock().await.tick().await;

                    let filter = filter.from_block(from_block).to_block(to_block);
                    let logs = Rpc::get_logs_with_retry(provider, &filter).await;
                    if logs.is_ok() {
                        pb.inc(1);
                    }
                    logs
                }
            }))
            .buffer_unordered(rate_limit as usize); // Process up to rate_limit tasks concurrently

        let mut all_logs = Vec::new();

        // Process results as they complete
        while let Some(result) = stream.next().await {
            match result {
                Ok(logs) => all_logs.extend(logs),
                Err(e) => return Err(e),
            }
        }

        Ok(all_logs)
    }

    // Given a config and a range, fetch all the logs for it
    // This is a top level call which will delegate to individual fetching
    // functions to get the logs and to ensure retries on failure
    async fn fetch_logs_for_config<P, T, N>(
        config: &EventConfig,
        start_block: u64,
        end_block: u64,
        provider: Arc<P>,
        progress_bar: Arc<ProgressBar>,
        rate_limit: u64,
    ) -> Result<Vec<Log>>
    where
        P: Provider<T, N> + 'static,
        T: Transport + Clone + 'static,
        N: Network,
    {
        let filter = Filter::new().events(config.events.iter().copied());
        Rpc::fetch_event_logs(
            start_block,
            end_block,
            config.step_size,
            provider,
            rate_limit,
            progress_bar,
            filter,
        )
        .await
    }

    // Fetch logs with retry functionality
    async fn get_logs_with_retry<P, T, N>(
        provider: Arc<P>,
        filter: &Filter,
    ) -> anyhow::Result<Vec<Log>>
    where
        P: Provider<T, N> + 'static,
        T: Transport + Clone + 'static,
        N: Network,
    {
        let mut retry_count = 0;
        let mut backoff = INITIAL_BACKOFF;

        loop {
            match provider.get_logs(filter).await {
                Ok(logs) => {
                    return anyhow::Ok(logs);
                }
                Err(e) => {
                    if retry_count >= MAX_RETRIES {
                        return Err(anyhow!(e));
                    }
                    let jitter = rand::thread_rng().gen_range(0..=100);
                    let sleep_duration = Duration::from_millis(backoff + jitter);
                    tokio::time::sleep(sleep_duration).await;
                    retry_count += 1;
                    backoff *= 2;
                }
            }
        }
    }

    fn get_event_config(pool_type: PoolType, is_initial_sync: bool) -> EventConfig {
        match pool_type {
            pt if pt.is_v3() => {
                if is_initial_sync {
                    EventConfig {
                        events: &[DataEvents::Mint::SIGNATURE, DataEvents::Burn::SIGNATURE],
                        step_size: 1500,
                        description: "Tick sync",
                        requires_initial_sync: false, // Always fetch these
                    }
                } else {
                    EventConfig {
                        events: &[
                            DataEvents::Mint::SIGNATURE,
                            DataEvents::Burn::SIGNATURE,
                            DataEvents::Swap::SIGNATURE,
                        ],
                        step_size: 50,
                        description: "Full sync",
                        requires_initial_sync: true, // Always fetch these
                    }
                }
            }
            pt if pt.is_balancer() => EventConfig {
                events: &[BalancerV2Event::Swap::SIGNATURE],
                step_size: 5000,
                description: "Swap Sync",
                requires_initial_sync: true,
            },
            _ => EventConfig {
                events: &[AerodromeSync::Sync::SIGNATURE, DataEvents::Sync::SIGNATURE],
                step_size: 250,
                description: "Reserve Sync",
                requires_initial_sync: true,
            },
        }
    }

    // Generate a range of blocks of step size distance
    pub fn get_block_range(step_size: u64, start_block: u64, end_block: u64) -> Vec<(u64, u64)> {
        if start_block == end_block {
            return vec![(start_block, end_block)];
        }

        let block_difference = end_block.saturating_sub(start_block);
        let (_, step_size) = if block_difference < step_size {
            (1, block_difference)
        } else {
            (
                ((block_difference as f64) / (step_size as f64)).ceil() as u64,
                step_size,
            )
        };
        let block_ranges: Vec<(u64, u64)> = (start_block..=end_block)
            .step_by(step_size as usize)
            .map(|from_block| {
                let to_block = (from_block + step_size - 1).min(end_block);
                (from_block, to_block)
            })
            .collect();
        block_ranges
    }
}