Skip to content

Commit

Permalink
feat: add no_std feature and update dependencies (#8)
Browse files Browse the repository at this point in the history
* Add `no_std` support and update dependencies

Introduces `no_std` compatibility to the library and includes necessary updates to dependencies in `Cargo.toml`. This change enables the crate to be used in environments without the standard library, expanding its applicability.

* Remove "transports" feature from alloy dependency

The "transports" feature in the alloy dependency is unnecessary and has been removed. This change helps streamline the dependencies and potentially reduces the binary size. All current functionality remains intact without this feature.

* Simplify `BLOCK_NUMBER` usage

Refactor code to remove unnecessary dereferencing of BLOCK_NUMBER across multiple modules. This enhances readability and adheres to Rust best practices by directly passing the constant reference.

* Add note on `no_std` support in README

This update explains that the library, by default, does not depend on the standard library. It also mentions that the `std` feature can be enabled using the `std` feature flag.
  • Loading branch information
shuhuiluo authored Sep 2, 2024
1 parent b728ef0 commit a3beba9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-lens"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "A library for querying Uniswap V3 using ephemeral lens contracts."
Expand All @@ -11,9 +11,13 @@ keywords = ["alloy", "ethereum", "solidity", "uniswap"]
include = ["src/**/*.rs"]

[dependencies]
alloy = { version = "0.3.0", features = ["contract", "rpc-types", "transports"] }
alloy = { version = "0.3.0", features = ["contract", "rpc-types"] }
anyhow = "1"

[features]
default = []
std = ["alloy/std"]

[dev-dependencies]
alloy = { version = "0.3.0", features = ["transport-http"] }
dotenv = "0.15.0"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ A library for querying Uniswap V3 using ephemeral lens contracts.
- Lens contracts in Solidity using the revert-in-constructor pattern
- Rust SDK for querying lens contracts using [alloy-rs](https://github.com/alloy-rs)
- TypeScript SDK for querying lens contracts using viem

## Note on `no_std`

By default, this library does not depend on the standard library (`std`). However, the `std` feature can be enabled via
the `std` feature flag.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! # uniswap-lens
//!
//! A library for querying Uniswap V3 using ephemeral lens contracts.
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#![warn(
missing_copy_implementations,
missing_debug_implementations,
Expand All @@ -12,6 +13,8 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

extern crate alloc;

#[allow(
missing_copy_implementations,
missing_debug_implementations,
Expand Down
28 changes: 15 additions & 13 deletions src/pool_lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
},
call_ephemeral_contract,
};
use alloc::vec::Vec;
use alloy::{
contract::Error,
eips::BlockId,
Expand Down Expand Up @@ -199,14 +200,14 @@ mod tests {
async fn test_get_populated_ticks_in_range() -> Result<()> {
let provider = PROVIDER.clone();
let pool = IUniswapV3PoolInstance::new(POOL_ADDRESS, provider.clone());
let tick_current = pool.slot0().block(*BLOCK_NUMBER).call().await?.tick;
let tick_spacing = pool.tickSpacing().block(*BLOCK_NUMBER).call().await?._0;
let tick_current = pool.slot0().block(BLOCK_NUMBER).call().await?.tick;
let tick_spacing = pool.tickSpacing().block(BLOCK_NUMBER).call().await?._0;
let ticks = get_populated_ticks_in_range(
POOL_ADDRESS,
tick_current,
tick_current + (tick_spacing << 8),
provider,
Some(*BLOCK_NUMBER),
Some(BLOCK_NUMBER),
)
.await?;
assert!(!ticks.is_empty());
Expand All @@ -219,7 +220,7 @@ mod tests {
// );
// #[allow(clippy::type_complexity)]
// let alt_ticks: Vec<(u128, i128, U256, U256, i64, U256, u32, bool)> = multicall
// .block(match *BLOCK_NUMBER {
// .block(match BLOCK_NUMBER {
// BlockId::Number(n) => n,
// _ => panic!("block id must be a number"),
// })
Expand Down Expand Up @@ -251,7 +252,7 @@ mod tests {
let futures = slots[0..4].iter().map(|slot| async move {
let data = provider
.get_storage_at(POOL_ADDRESS, slot.slot)
.block_id(*BLOCK_NUMBER)
.block_id(BLOCK_NUMBER)
.await
.unwrap();
assert!(slot.data.eq(&data));
Expand All @@ -262,7 +263,7 @@ mod tests {
#[tokio::test]
async fn test_get_static_slots() {
let provider = PROVIDER.clone();
let slots = get_static_slots(POOL_ADDRESS, provider.clone(), Some(*BLOCK_NUMBER))
let slots = get_static_slots(POOL_ADDRESS, provider.clone(), Some(BLOCK_NUMBER))
.await
.unwrap();
verify_slots(slots, provider).await;
Expand All @@ -272,13 +273,13 @@ mod tests {
async fn test_get_ticks_slots() {
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 tick_current = pool.slot0().block(BLOCK_NUMBER).call().await.unwrap().tick;
let slots = get_ticks_slots(
POOL_ADDRESS,
tick_current,
tick_current,
provider.clone(),
Some(*BLOCK_NUMBER),
Some(BLOCK_NUMBER),
)
.await
.unwrap();
Expand All @@ -288,7 +289,7 @@ mod tests {
#[tokio::test]
async fn test_get_tick_bitmap_slots() {
let provider = PROVIDER.clone();
let slots = get_tick_bitmap_slots(POOL_ADDRESS, provider.clone(), Some(*BLOCK_NUMBER))
let slots = get_tick_bitmap_slots(POOL_ADDRESS, provider.clone(), Some(BLOCK_NUMBER))
.await
.unwrap();
verify_slots(slots, provider).await;
Expand All @@ -299,12 +300,12 @@ mod tests {
let provider = PROVIDER.clone();
// create a filter to get the mint events
let filter = Filter::new()
.from_block(17000000 - 10000)
.to_block(17000000)
.from_block(BLOCK_NUMBER.as_u64().unwrap() - 10000)
.to_block(BLOCK_NUMBER.as_u64().unwrap())
.event_signature(<Mint as SolEvent>::SIGNATURE_HASH);
let logs = provider.get_logs(&filter).await?;
// decode the logs into position keys
let positions = logs
let positions: Vec<_> = logs
.iter()
.map(|log| <Mint as SolEvent>::decode_log_data(log.data(), true).unwrap())
.map(
Expand All @@ -320,11 +321,12 @@ mod tests {
},
)
.collect();
assert!(!positions.is_empty());
let slots = get_positions_slots(
POOL_ADDRESS,
positions,
provider.clone(),
Some(*BLOCK_NUMBER),
Some(BLOCK_NUMBER),
)
.await?;
verify_slots(slots, provider).await;
Expand Down
15 changes: 8 additions & 7 deletions src/position_lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
},
call_ephemeral_contract,
};
use alloc::vec::Vec;
use alloy::{
contract::Error,
eips::BlockId,
Expand Down Expand Up @@ -191,14 +192,14 @@ mod tests {
NPM_ADDRESS,
uint!(4_U256),
provider.clone(),
Some(*BLOCK_NUMBER),
Some(BLOCK_NUMBER),
)
.await?;
let pool = IUniswapV3PoolInstance::new(
compute_pool_address(FACTORY_ADDRESS, token0, token1, fee, POOL_INIT_CODE_HASH),
provider,
);
let slot0 = pool.slot0().block(*BLOCK_NUMBER).call().await?;
let slot0 = pool.slot0().block(BLOCK_NUMBER).call().await?;
assert_eq!(tokenId, uint!(4_U256));
assert_eq!(sqrtPriceX96, slot0.sqrtPriceX96);
assert_eq!(tick, slot0.tick);
Expand Down Expand Up @@ -233,7 +234,7 @@ mod tests {
// u128,
// u128,
// )> = multicall
// .block(match *BLOCK_NUMBER {
// .block(match BLOCK_NUMBER {
// BlockId::Number(n) => n,
// _ => panic!("block id must be a number"),
// })
Expand Down Expand Up @@ -277,7 +278,7 @@ mod tests {
.map(|i| U256::from_limbs([i, 0, 0, 0]))
.collect(),
provider.clone(),
Some(*BLOCK_NUMBER),
Some(BLOCK_NUMBER),
)
.await?;
let _npm = IUniswapV3NonfungiblePositionManager::new(NPM_ADDRESS, provider);
Expand All @@ -289,15 +290,15 @@ mod tests {
async fn test_get_all_positions_by_owner() -> Result<()> {
let provider = PROVIDER.clone();
let npm = IUniswapV3NonfungiblePositionManager::new(NPM_ADDRESS, provider.clone());
let total_supply: U256 = npm.totalSupply().block(*BLOCK_NUMBER).call().await?._0;
let total_supply: U256 = npm.totalSupply().block(BLOCK_NUMBER).call().await?._0;
let owner = npm
.ownerOf(total_supply - uint!(1_U256))
.block(*BLOCK_NUMBER)
.block(BLOCK_NUMBER)
.call()
.await?
.owner;
let _positions =
get_all_positions_by_owner(NPM_ADDRESS, owner, provider, Some(*BLOCK_NUMBER)).await?;
get_all_positions_by_owner(NPM_ADDRESS, owner, provider, Some(BLOCK_NUMBER)).await?;
Ok(())
// verify_position_details(positions, npm).await
}
Expand Down
5 changes: 3 additions & 2 deletions src/storage_lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use crate::bindings::ephemeralstoragelens::{
EphemeralStorageLens, EphemeralStorageLens::EphemeralStorageLensInstance,
};
use alloc::vec::Vec;
use alloy::{
eips::BlockId,
primitives::{Address, FixedBytes},
Expand Down Expand Up @@ -73,15 +74,15 @@ mod tests {
.map(|i| FixedBytes::from(U256::from_limbs([i, 0, 0, 0])))
.collect(),
provider.clone(),
Some(*BLOCK_NUMBER),
Some(BLOCK_NUMBER),
)
.await?;
let slots_ref = slots.as_slice();
let provider = provider.root();
let futures = (0..10).map(|i| async move {
let slot = provider
.get_storage_at(POOL_ADDRESS, U256::from_limbs([i, 0, 0, 0]))
.block_id(*BLOCK_NUMBER)
.block_id(BLOCK_NUMBER)
.await
.unwrap();
assert_eq!(slot, U256::from_be_bytes(slots_ref[i as usize].0));
Expand Down
4 changes: 2 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use alloy::{
eips::BlockId,
eips::{BlockId, BlockNumberOrTag},
providers::{ProviderBuilder, ReqwestProvider},
transports::http::reqwest::Url,
};
use dotenv::dotenv;
use once_cell::sync::Lazy;

pub(crate) static BLOCK_NUMBER: Lazy<BlockId> = Lazy::new(|| BlockId::from(17000000));
pub(crate) const BLOCK_NUMBER: BlockId = BlockId::Number(BlockNumberOrTag::Number(17000000));
pub(crate) static RPC_URL: Lazy<Url> = Lazy::new(|| {
dotenv().ok();
std::env::var("MAINNET_RPC_URL").unwrap().parse().unwrap()
Expand Down

0 comments on commit a3beba9

Please sign in to comment.