Skip to content

Commit

Permalink
Remove ttl-cache dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinquaXD committed Jan 4, 2025
1 parent 50eb0a0 commit 09b77ee
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 25 deletions.
16 changes: 0 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ contracts = { path = "../contracts" }
dashmap = { workspace = true }
database = { path = "../database" }
derive_more = { workspace = true }
ttl_cache = "0.5"
derivative = { workspace = true }
ethcontract = { workspace = true }
ethrpc = { path = "../ethrpc" }
Expand Down
14 changes: 6 additions & 8 deletions crates/shared/src/sources/uniswap_v2/pool_fetching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {
super::pair_provider::PairProvider,
crate::{baseline_solver::BaselineSolvable, ethrpc::Web3, recent_block_cache::Block},
anyhow::Result,
cached::{Cached, TimedCache},
contracts::{errors::EthcontractErrorType, IUniswapLikePair, ERC20},
ethcontract::{errors::MethodError, BlockId, H160, U256},
futures::{
Expand All @@ -11,7 +12,6 @@ use {
model::TokenPair,
num::rational::Ratio,
std::{collections::HashSet, sync::RwLock, time::Duration},
ttl_cache::TtlCache,
};

const POOL_SWAP_GAS_COST: usize = 60_000;
Expand Down Expand Up @@ -182,17 +182,15 @@ impl BaselineSolvable for Pool {
pub struct PoolFetcher<Reader> {
pub pool_reader: Reader,
pub web3: Web3,
pub cache_time: Duration,
pub non_existent_pools: RwLock<TtlCache<TokenPair, ()>>,
pub non_existent_pools: RwLock<TimedCache<TokenPair, ()>>,
}

impl<Reader> PoolFetcher<Reader> {
pub fn new(reader: Reader, web3: Web3, cache_time: Duration) -> Self {
Self {
pool_reader: reader,
web3,
cache_time,
non_existent_pools: RwLock::new(TtlCache::new(usize::MAX)),
non_existent_pools: RwLock::new(TimedCache::with_lifespan(cache_time.as_secs())),
}
}
}
Expand All @@ -205,8 +203,8 @@ where
async fn fetch(&self, token_pairs: HashSet<TokenPair>, at_block: Block) -> Result<Vec<Pool>> {
let mut token_pairs: Vec<_> = token_pairs.into_iter().collect();
{
let non_existent_pools = self.non_existent_pools.read().unwrap();
token_pairs.retain(|pair| !non_existent_pools.contains_key(pair));
let mut non_existent_pools = self.non_existent_pools.write().unwrap();
token_pairs.retain(|pair| non_existent_pools.cache_get(pair).is_none());
}
let block = BlockId::Number(at_block.into());
let futures = token_pairs
Expand All @@ -228,7 +226,7 @@ where
tracing::debug!(token_pairs = ?new_missing_pairs, "stop indexing liquidity");
let mut non_existent_pools = self.non_existent_pools.write().unwrap();
for pair in new_missing_pairs {
non_existent_pools.insert(pair, (), self.cache_time);
non_existent_pools.cache_set(pair, ());
}
}
Ok(pools)
Expand Down

0 comments on commit 09b77ee

Please sign in to comment.