Skip to content

Commit

Permalink
chore: Refactor test cases and update alloy dependency (#3)
Browse files Browse the repository at this point in the history
The commit includes code refactoring in tests of `src/pool_lens.rs` to avoid repeating the provider creation. The provider is created using a Lazy static variable. A previously commented out test case `test_get_positions_slots` is enabled and updated. The alloy library feature 'transports' is added in dependencies, replacing 'provider-http'. Some rust warning settings are also added to `lib.rs`. The project is renamed from 'Aperture-Lens' to 'Uniswap-Lens'.
  • Loading branch information
shuhuiluo authored Jul 21, 2024
1 parent 70db597 commit f2fc8d7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 41 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ keywords = ["alloy", "ethereum", "solidity", "rust", "uniswap"]
include = ["src/**/*.rs"]

[dependencies]
alloy = { version = "0.2.0", features = ["contract", "provider-http"] }
alloy = { version = "0.2.0", features = ["contract", "transports"] }
anyhow = "1"
once_cell = "1.19"

[dev-dependencies]
alloy = { version = "0.2.0", features = ["rpc-types", "transport-http"] }
dotenv = "0.15.0"
futures = "0.3"
tokio = { version = "1.37", features = ["full"] }
once_cell = "1.19"
tokio = { version = "1", features = ["full"] }
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Aperture-Lens
# Uniswap-Lens

[![Foundry](https://github.com/Aperture-Finance/Aperture-Lens/actions/workflows/foundry.yml/badge.svg)](https://github.com/Aperture-Finance/Aperture-Lens/actions/workflows/foundry.yml)
[![Node.js](https://github.com/Aperture-Finance/Aperture-Lens/actions/workflows/nodejs.yml/badge.svg)](https://github.com/Aperture-Finance/Aperture-Lens/actions/workflows/nodejs.yml)
[![Rust](https://github.com/Aperture-Finance/Aperture-Lens/actions/workflows/rust.yml/badge.svg)](https://github.com/Aperture-Finance/Aperture-Lens/actions/workflows/rust.yml)
[![Foundry](https://github.com/shuhuiluo/uniswap-lens-rs/actions/workflows/foundry.yml/badge.svg)](https://github.com/shuhuiluo/uniswap-lens-rs/actions/workflows/foundry.yml)
[![Node.js](https://github.com/shuhuiluo/uniswap-lens-rs/actions/workflows/nodejs.yml/badge.svg)](https://github.com/shuhuiluo/uniswap-lens-rs/actions/workflows/nodejs.yml)
[![Rust](https://github.com/shuhuiluo/uniswap-lens-rs/actions/workflows/rust.yml/badge.svg)](https://github.com/shuhuiluo/uniswap-lens-rs/actions/workflows/rust.yml)

[![npm version](https://img.shields.io/npm/v/aperture-lens/latest.svg)](https://www.npmjs.com/package/aperture-lens/v/latest)
[![crates.io](https://img.shields.io/crates/v/aperture-lens.svg)](https://crates.io/crates/aperture-lens)
[![crates.io](https://img.shields.io/crates/v/uniswap-lens.svg)](https://crates.io/crates/uniswap-lens)

Contains ephemeral lens contracts that can be called without deployment and their interfaces in various Web3 libraries.
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
//! # uniswap-lens
#![warn(
missing_copy_implementations,
missing_debug_implementations,
unreachable_pub,
clippy::missing_const_for_fn,
rustdoc::all
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[allow(
missing_copy_implementations,
missing_debug_implementations,
clippy::missing_const_for_fn
)]
pub mod bindings;
pub mod caller;
pub mod pool_lens;
Expand Down
83 changes: 50 additions & 33 deletions src/pool_lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::iuniswapv3pool::IUniswapV3Pool::IUniswapV3PoolInstance;
use alloy::{primitives::address, providers::ProviderBuilder, transports::http::reqwest::Url};
use crate::bindings::iuniswapv3pool::IUniswapV3Pool::{IUniswapV3PoolInstance, Mint};
use alloy::{
primitives::address,
providers::{ProviderBuilder, ReqwestProvider},
rpc::types::Filter,
sol_types::SolEvent,
transports::http::reqwest::Url,
};
use anyhow::Result;
use dotenv::dotenv;
use futures::future::join_all;
Expand All @@ -137,10 +143,12 @@ mod tests {
.parse()
.unwrap()
});
static PROVIDER: Lazy<ReqwestProvider> =
Lazy::new(|| ProviderBuilder::new().on_http(RPC_URL.clone()));

#[tokio::test]
async fn test_get_populated_ticks_in_range() -> Result<()> {
let provider = ProviderBuilder::new().on_http(RPC_URL.clone());
let provider = PROVIDER.clone();
let pool = IUniswapV3PoolInstance::new(POOL_ADDRESS, provider.clone());
let tick_current = pool.slot0().block(*BLOCK_NUMBER).call().await?.tick;
let ticks = get_populated_ticks_in_range(
Expand Down Expand Up @@ -203,7 +211,7 @@ mod tests {

#[tokio::test]
async fn test_get_static_slots() {
let provider = ProviderBuilder::new().on_http(RPC_URL.clone());
let provider = PROVIDER.clone();
let slots = get_static_slots(POOL_ADDRESS, provider.clone(), Some(*BLOCK_NUMBER))
.await
.unwrap();
Expand All @@ -212,7 +220,7 @@ mod tests {

#[tokio::test]
async fn test_get_ticks_slots() {
let provider = ProviderBuilder::new().on_http(RPC_URL.clone());
let provider = PROVIDER.clone();
let pool = IUniswapV3PoolInstance::new(POOL_ADDRESS, provider.clone());
let tick_current = pool.slot0().block(*BLOCK_NUMBER).call().await.unwrap().tick;
let slots = get_ticks_slots(
Expand All @@ -229,38 +237,47 @@ mod tests {

#[tokio::test]
async fn test_get_tick_bitmap_slots() {
let provider = ProviderBuilder::new().on_http(RPC_URL.clone());
let provider = PROVIDER.clone();
let slots = get_tick_bitmap_slots(POOL_ADDRESS, provider.clone(), Some(*BLOCK_NUMBER))
.await
.unwrap();
verify_slots(slots, provider).await;
}

// #[tokio::test]
// async fn test_get_positions_slots() -> Result<()> {
// let client = Arc::new(MAINNET.provider());
// let filter = MintFilter::new::<&Provider<Http>, Provider<Http>>(
// Filter::new().from_block(17000000 - 10000).to_block(17000000),
// &client,
// );
// let logs = filter.query().await?;
// let positions = logs
// .iter()
// .map(
// |&MintFilter {
// owner,
// tick_lower,
// tick_upper,
// ..
// }| PositionKey {
// owner,
// tick_lower,
// tick_upper,
// },
// )
// .collect();
// let slots = get_positions_slots(POOL_ADDRESS, positions, client.clone(),
// Some(*BLOCK_NUMBER)).await?; verify_slots(slots, client).await;
// Ok(())
// }
#[tokio::test]
async fn test_get_positions_slots() -> Result<()> {
let provider = PROVIDER.clone();
// create a filter to get the mint events
let filter = Filter::new()
.from_block(17000000 - 10000)
.to_block(17000000)
.event_signature(<Mint as SolEvent>::SIGNATURE_HASH);
let logs = provider.get_logs(&filter).await?;
// decode the logs into position keys
let positions = logs
.iter()
.map(|log| <Mint as SolEvent>::decode_log_data(log.data(), true).unwrap())
.map(
|Mint {
owner,
tickLower,
tickUpper,
..
}| PositionKey {
owner,
tickLower,
tickUpper,
},
)
.collect();
let slots = get_positions_slots(
POOL_ADDRESS,
positions,
provider.clone(),
Some(*BLOCK_NUMBER),
)
.await?;
verify_slots(slots, provider).await;
Ok(())
}
}

0 comments on commit f2fc8d7

Please sign in to comment.