|
| 1 | +use std::{path::Path, sync::Arc}; |
| 2 | + |
| 3 | +use alloy::primitives::{Address, BlockNumber}; |
| 4 | +use alloy::providers::ReqwestProvider; |
| 5 | +use amms::amm::{ |
| 6 | + factory::Factory, |
| 7 | + uniswap_v2::{factory::UniswapV2Factory, UniswapV2Pool}, |
| 8 | + uniswap_v3::{factory::UniswapV3Factory, UniswapV3Pool}, |
| 9 | + AMM, |
| 10 | +}; |
| 11 | +use csv::Reader; |
| 12 | +use futures::future::join_all; |
| 13 | +use serde::{Deserialize, Serialize}; |
| 14 | + |
| 15 | +use crate::variant::AmmVariant; |
| 16 | + |
| 17 | +pub const DEFAULT_FEE: u32 = 300; |
| 18 | + |
| 19 | +#[derive(Clone, Debug, Deserialize, Serialize)] |
| 20 | +pub struct SpecificationEntry { |
| 21 | + pub variant: AmmVariant, |
| 22 | + pub factory: Address, |
| 23 | + pub factory_created: BlockNumber, |
| 24 | + pub pool: Address, |
| 25 | +} |
| 26 | + |
| 27 | +impl SpecificationEntry { |
| 28 | + async fn fetch( |
| 29 | + &self, |
| 30 | + provider: Arc<ReqwestProvider>, |
| 31 | + ) -> eyre::Result<(Factory, AMM)> { |
| 32 | + Ok(match self.variant { |
| 33 | + AmmVariant::UniswapV2 => ( |
| 34 | + Factory::UniswapV2Factory(UniswapV2Factory::new( |
| 35 | + self.factory, |
| 36 | + self.factory_created, |
| 37 | + DEFAULT_FEE, |
| 38 | + )), |
| 39 | + AMM::UniswapV2Pool( |
| 40 | + UniswapV2Pool::new_from_address( |
| 41 | + self.pool, |
| 42 | + DEFAULT_FEE, |
| 43 | + provider.clone(), |
| 44 | + ) |
| 45 | + .await?, |
| 46 | + ), |
| 47 | + ), |
| 48 | + AmmVariant::UniswapV3 => ( |
| 49 | + Factory::UniswapV3Factory(UniswapV3Factory::new( |
| 50 | + self.factory, |
| 51 | + self.factory_created, |
| 52 | + )), |
| 53 | + AMM::UniswapV3Pool( |
| 54 | + UniswapV3Pool::new_from_address( |
| 55 | + self.pool, |
| 56 | + DEFAULT_FEE.into(), |
| 57 | + provider.clone(), |
| 58 | + ) |
| 59 | + .await?, |
| 60 | + ), |
| 61 | + ), |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Clone, Debug, Deserialize, Serialize)] |
| 67 | +pub struct CheckpointSpecification(pub Vec<SpecificationEntry>); |
| 68 | + |
| 69 | +impl CheckpointSpecification { |
| 70 | + pub fn load<P>(path: P) -> eyre::Result<Self> |
| 71 | + where |
| 72 | + P: AsRef<Path>, |
| 73 | + { |
| 74 | + Ok(Self( |
| 75 | + Reader::from_path(path)? |
| 76 | + .deserialize() |
| 77 | + .collect::<Result<Vec<SpecificationEntry>, csv::Error>>()?, |
| 78 | + )) |
| 79 | + } |
| 80 | + |
| 81 | + pub async fn fetch( |
| 82 | + &self, |
| 83 | + provider: Arc<ReqwestProvider>, |
| 84 | + ) -> eyre::Result<Vec<(Factory, AMM)>> { |
| 85 | + join_all(self.0.iter().map(|x| x.fetch(provider.clone()))) |
| 86 | + .await |
| 87 | + .into_iter() |
| 88 | + .collect::<eyre::Result<Vec<(Factory, AMM)>>>() |
| 89 | + } |
| 90 | +} |
0 commit comments