Skip to content

Commit

Permalink
Merge pull request #129 from shuhuiluo/transport
Browse files Browse the repository at this point in the history
refactor(ext): update dependencies and refactor `Transport` to `Network`
  • Loading branch information
shuhuiluo authored Feb 2, 2025
2 parents 5bb888a + 027cd3e commit 7e59a01
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 121 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-v3-sdk"
version = "3.4.0"
version = "3.5.0"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "Uniswap V3 SDK for Rust"
Expand All @@ -15,7 +15,7 @@ exclude = [".github", ".gitignore", "rustfmt.toml"]
all-features = true

[dependencies]
alloy = { version = "0.9", optional = true, features = ["contract"] }
alloy = { version = "0.11", optional = true, features = ["contract"] }
alloy-primitives = "0.8"
alloy-sol-types = "0.8"
anyhow = { version = "1.0", optional = true }
Expand All @@ -29,7 +29,7 @@ once_cell = "1.20"
regex = { version = "1.11", optional = true }
serde_json = { version = "1.0", optional = true }
thiserror = { version = "2", default-features = false }
uniswap-lens = { version = "0.10", optional = true }
uniswap-lens = { version = "0.11", optional = true }
uniswap-sdk-core = "3.4.0"

[features]
Expand All @@ -38,11 +38,11 @@ extensions = ["alloy", "anyhow", "base64", "regex", "serde_json", "uniswap-lens"
std = ["alloy?/std", "thiserror/std", "uniswap-sdk-core/std", "uniswap-lens?/std"]

[dev-dependencies]
alloy = { version = "0.9", features = ["provider-anvil-node", "signer-local"] }
alloy = { version = "0.11", features = ["provider-anvil-node", "signer-local"] }
criterion = "0.5.1"
dotenv = "0.15.0"
tokio = { version = "1.40", features = ["full"] }
uniswap_v3_math = "0.5.2"
tokio = { version = "1.43", features = ["full"] }
uniswap_v3_math = "0.5.3"

[[bench]]
name = "bit_math"
Expand Down
44 changes: 21 additions & 23 deletions examples/nonfungible_position_manager.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use alloy::{
eips::BlockId,
network::{Network, TransactionBuilder},
node_bindings::WEI_IN_ETHER,
providers::{ext::AnvilApi, Provider, ProviderBuilder},
rpc::types::TransactionRequest,
signers::{
k256::ecdsa::SigningKey,
local::{LocalSigner, PrivateKeySigner},
SignerSync,
},
transports::{http::reqwest::Url, Transport},
transports::http::reqwest::Url,
};
use alloy_primitives::{address, Address, U256};
use uniswap_lens::bindings::ierc721enumerable::IERC721Enumerable;
Expand All @@ -30,13 +30,11 @@ async fn main() {
let npm = *NONFUNGIBLE_POSITION_MANAGER_ADDRESSES.get(&1).unwrap();

// Create an Anvil fork
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.on_anvil_with_config(|anvil| {
anvil
.fork(rpc_url)
.fork_block_number(block_id.as_u64().unwrap())
});
let provider = ProviderBuilder::new().on_anvil_with_config(|anvil| {
anvil
.fork(rpc_url)
.fork_block_number(block_id.as_u64().unwrap())
});
provider.anvil_auto_impersonate_account(true).await.unwrap();
let account: LocalSigner<SigningKey> = PrivateKeySigner::random();
provider
Expand Down Expand Up @@ -120,10 +118,10 @@ async fn main() {
}

/// Mint a position
async fn mint_liquidity<T, P>(position: &mut Position, from: Address, provider: &P) -> U256
async fn mint_liquidity<N, P>(position: &mut Position, from: Address, provider: &P) -> U256
where
T: Transport + Clone,
P: Provider<T>,
N: Network,
P: Provider<N>,
{
let npm = *NONFUNGIBLE_POSITION_MANAGER_ADDRESSES.get(&1).unwrap();

Expand All @@ -139,10 +137,10 @@ where
}),
};
let params = add_call_parameters(position, options).unwrap();
let tx = TransactionRequest::default()
.from(from)
.to(npm)
.input(params.calldata.into());
let tx = N::TransactionRequest::default()
.with_from(from)
.with_to(npm)
.with_input(params.calldata);
provider
.send_transaction(tx)
.await
Expand All @@ -160,15 +158,15 @@ where
}

/// Burn a position with a permit
async fn burn_liquidity<T, P>(
async fn burn_liquidity<N, P>(
token_id: U256,
position: &Position,
owner: &LocalSigner<SigningKey>,
sender: Address,
provider: &P,
) where
T: Transport + Clone,
P: Provider<T>,
N: Network,
P: Provider<N>,
{
let npm = *NONFUNGIBLE_POSITION_MANAGER_ADDRESSES.get(&1).unwrap();

Expand Down Expand Up @@ -213,10 +211,10 @@ async fn burn_liquidity<T, P>(
},
};
let params = remove_call_parameters(position, options).unwrap();
let tx = TransactionRequest::default()
.from(sender)
.to(npm)
.input(params.calldata.into());
let tx = N::TransactionRequest::default()
.with_from(sender)
.with_to(npm)
.with_input(params.calldata);
provider
.send_transaction(tx)
.await
Expand Down
12 changes: 5 additions & 7 deletions examples/self_permit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ async fn main() {
let block_id = BlockId::from(17000000);

// Create an Anvil fork
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.on_anvil_with_config(|anvil| {
anvil
.fork(rpc_url)
.fork_block_number(block_id.as_u64().unwrap())
});
let provider = ProviderBuilder::new().on_anvil_with_config(|anvil| {
anvil
.fork(rpc_url)
.fork_block_number(block_id.as_u64().unwrap())
});
provider.anvil_auto_impersonate_account(true).await.unwrap();

let usdc = token!(1, "A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", 6);
Expand Down
12 changes: 5 additions & 7 deletions examples/swap_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ async fn main() {
println!("Quoter amount out: {}", amount_out);

// Create an Anvil fork
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.on_anvil_with_config(|anvil| {
anvil
.fork(rpc_url)
.fork_block_number(block_id.as_u64().unwrap())
});
let provider = ProviderBuilder::new().on_anvil_with_config(|anvil| {
anvil
.fork(rpc_url)
.fork_block_number(block_id.as_u64().unwrap())
});
let account = provider.get_accounts().await.unwrap()[0];

// Build the swap transaction
Expand Down
8 changes: 4 additions & 4 deletions src/entities/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ where

/// Returns the input currency of the swap
#[inline]
pub fn input_currency(&self) -> &TInput {
&self.input_amount.currency
pub const fn input_currency(&self) -> &TInput {
&self.input_amount.meta.currency
}

/// Returns the output currency of the swap
#[inline]
pub fn output_currency(&self) -> &TOutput {
&self.output_amount.currency
pub const fn output_currency(&self) -> &TOutput {
&self.output_amount.meta.currency
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/extensions/ephemeral_tick_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! A data provider that fetches ticks using an [ephemeral contract](https://github.com/Aperture-Finance/Aperture-Lens/blob/904101e4daed59e02fd4b758b98b0749e70b583b/contracts/EphemeralGetPopulatedTicksInRange.sol) in a single `eth_call`.
use crate::prelude::*;
use alloy::{eips::BlockId, providers::Provider, transports::Transport};
use alloy::{eips::BlockId, network::Network, providers::Provider};
use alloy_primitives::{aliases::I24, Address};
use derive_more::Deref;
use uniswap_lens::pool_lens;
Expand All @@ -21,16 +21,16 @@ pub struct EphemeralTickDataProvider<I = I24> {

impl<I: TickIndex> EphemeralTickDataProvider<I> {
#[inline]
pub async fn new<T, P>(
pub async fn new<N, P>(
pool: Address,
provider: P,
tick_lower: Option<I>,
tick_upper: Option<I>,
block_id: Option<BlockId>,
) -> Result<Self, Error>
where
T: Transport + Clone,
P: Provider<T>,
N: Network,
P: Provider<N>,
{
let tick_lower = tick_lower.map_or(MIN_TICK, I::to_i24);
let tick_upper = tick_upper.map_or(MAX_TICK, I::to_i24);
Expand Down
8 changes: 4 additions & 4 deletions src/extensions/ephemeral_tick_map_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! A data provider that fetches ticks using an [ephemeral contract](https://github.com/Aperture-Finance/Aperture-Lens/blob/904101e4daed59e02fd4b758b98b0749e70b583b/contracts/EphemeralGetPopulatedTicksInRange.sol) in a single `eth_call`.
use crate::prelude::*;
use alloy::{eips::BlockId, providers::Provider, transports::Transport};
use alloy::{eips::BlockId, network::Network, providers::Provider};
use alloy_primitives::{aliases::I24, Address};
use derive_more::Deref;

Expand All @@ -20,16 +20,16 @@ pub struct EphemeralTickMapDataProvider<I = I24> {

impl<I: TickIndex> EphemeralTickMapDataProvider<I> {
#[inline]
pub async fn new<T, P>(
pub async fn new<N, P>(
pool: Address,
provider: P,
tick_lower: Option<I>,
tick_upper: Option<I>,
block_id: Option<BlockId>,
) -> Result<Self, Error>
where
T: Transport + Clone,
P: Provider<T>,
N: Network,
P: Provider<N>,
{
let provider =
EphemeralTickDataProvider::new(pool, provider, tick_lower, tick_upper, block_id)
Expand Down
36 changes: 18 additions & 18 deletions src/extensions/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use crate::prelude::*;
use alloy::{
eips::{BlockId, BlockNumberOrTag},
network::Network,
providers::Provider,
transports::Transport,
};
use alloy_primitives::{Address, ChainId, B256};
use uniswap_lens::{
Expand All @@ -19,16 +19,16 @@ use uniswap_lens::{
use uniswap_sdk_core::{prelude::Token, token};

#[inline]
pub fn get_pool_contract<T, P>(
pub fn get_pool_contract<N, P>(
factory: Address,
token_a: Address,
token_b: Address,
fee: FeeAmount,
provider: P,
) -> IUniswapV3PoolInstance<T, P>
) -> IUniswapV3PoolInstance<(), P, N>
where
T: Transport + Clone,
P: Provider<T>,
N: Network,
P: Provider<N>,
{
IUniswapV3PoolInstance::new(
compute_pool_address(factory, token_a, token_b, fee, None, None),
Expand All @@ -49,7 +49,7 @@ impl Pool {
/// * `provider`: The alloy provider
/// * `block_id`: Optional block number to query.
#[inline]
pub async fn from_pool_key<T, P>(
pub async fn from_pool_key<N, P>(
chain_id: ChainId,
factory: Address,
token_a: Address,
Expand All @@ -59,13 +59,13 @@ impl Pool {
block_id: Option<BlockId>,
) -> Result<Self, Error>
where
T: Transport + Clone,
P: Provider<T> + Clone,
N: Network,
P: Provider<N>,
{
let block_id = block_id.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest));
let pool_contract = get_pool_contract(factory, token_a, token_b, fee, provider.clone());
let token_a_contract = IERC20Metadata::new(token_a, provider.clone());
let token_b_contract = IERC20Metadata::new(token_b, provider);
let pool_contract = get_pool_contract(factory, token_a, token_b, fee, provider.root());
let token_a_contract = IERC20Metadata::new(token_a, provider.root());
let token_b_contract = IERC20Metadata::new(token_b, provider.root());
// TODO: use multicall
let slot_0 = pool_contract.slot0().block(block_id).call().await?;
let liquidity = pool_contract.liquidity().block(block_id).call().await?._0;
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<I: TickIndex> Pool<EphemeralTickMapDataProvider<I>> {
/// }
/// ```
#[inline]
pub async fn from_pool_key_with_tick_data_provider<T, P>(
pub async fn from_pool_key_with_tick_data_provider<N, P>(
chain_id: ChainId,
factory: Address,
token_a: Address,
Expand All @@ -156,16 +156,16 @@ impl<I: TickIndex> Pool<EphemeralTickMapDataProvider<I>> {
block_id: Option<BlockId>,
) -> Result<Self, Error>
where
T: Transport + Clone,
P: Provider<T> + Clone,
N: Network,
P: Provider<N>,
{
let pool = Pool::from_pool_key(
chain_id,
factory,
token_a,
token_b,
fee,
provider.clone(),
provider.root(),
block_id,
)
.await?;
Expand Down Expand Up @@ -265,7 +265,7 @@ pub fn reconstruct_liquidity_array<I: TickIndex>(
///
/// An array of ticks and corresponding cumulative liquidity.
#[inline]
pub async fn get_liquidity_array_for_pool<TP, T, P>(
pub async fn get_liquidity_array_for_pool<TP, N, P>(
pool: Pool<TP>,
tick_lower: TP::Index,
tick_upper: TP::Index,
Expand All @@ -276,8 +276,8 @@ pub async fn get_liquidity_array_for_pool<TP, T, P>(
) -> Result<Vec<(TP::Index, u128)>, Error>
where
TP: TickDataProvider,
T: Transport + Clone,
P: Provider<T>,
N: Network,
P: Provider<N>,
{
let (tick_current_aligned, tick_lower, tick_upper) = normalize_ticks(
pool.tick_current,
Expand Down
Loading

0 comments on commit 7e59a01

Please sign in to comment.