From 366403a7d0774d2bffeff704326dcfb24b582199 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Mon, 30 Dec 2024 02:46:23 -0500 Subject: [PATCH] perf: refactor multicall encoding and cleanup redundant clones (#123) Simplifies encoding logic in `encode_multicall` by reducing unnecessary clones and updates type usage for consistency. Improves readability in other modules by removing redundant `.clone()` calls and ensures cleaner ownership handling. Also aligns imports to address missing dependencies. --- Cargo.toml | 2 +- src/entities/trade.rs | 2 +- src/extensions/pool.rs | 4 ++-- src/extensions/state_overrides.rs | 1 + src/multicall.rs | 11 +++++++---- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7af7aa3..ac8cfa3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-v3-sdk" -version = "3.1.1" +version = "3.1.2" edition = "2021" authors = ["Shuhui Luo "] description = "Uniswap V3 SDK for Rust" diff --git a/src/entities/trade.rs b/src/entities/trade.rs index d71b95a..c476182 100644 --- a/src/entities/trade.rs +++ b/src/entities/trade.rs @@ -618,7 +618,7 @@ where let mut populated_routes: Vec> = Vec::with_capacity(routes.len()); for (amount, route) in routes { let trade = Self::from_route(route, amount, trade_type)?; - populated_routes.push(trade.swaps[0].clone()); + populated_routes.push(trade.swaps.into_iter().next().unwrap()); } Self::new(populated_routes, trade_type) } diff --git a/src/extensions/pool.rs b/src/extensions/pool.rs index 8ad168a..c197239 100644 --- a/src/extensions/pool.rs +++ b/src/extensions/pool.rs @@ -65,7 +65,7 @@ impl Pool { 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.clone()); + let token_b_contract = IERC20Metadata::new(token_b, provider); // 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; @@ -171,7 +171,7 @@ impl Pool> { .await?; let tick_data_provider = EphemeralTickMapDataProvider::new( pool.address(None, None), - provider.clone(), + provider, None, None, block_id, diff --git a/src/extensions/state_overrides.rs b/src/extensions/state_overrides.rs index adb91a2..199a2df 100644 --- a/src/extensions/state_overrides.rs +++ b/src/extensions/state_overrides.rs @@ -1,4 +1,5 @@ use crate::prelude::Error; +use alloc::vec::Vec; use alloy::{ eips::eip2930::{AccessList, AccessListItem}, providers::Provider, diff --git a/src/multicall.rs b/src/multicall.rs index ce01f30..13b75c2 100644 --- a/src/multicall.rs +++ b/src/multicall.rs @@ -5,11 +5,14 @@ use alloy_sol_types::{Error, SolCall}; #[inline] #[must_use] pub fn encode_multicall>(data: Vec) -> Bytes { - let data: Vec = data.into_iter().map(Into::into).collect(); if data.len() == 1 { - data[0].clone() + data.into_iter().next().unwrap().into() } else { - IMulticall::multicallCall { data }.abi_encode().into() + IMulticall::multicallCall { + data: data.into_iter().map(Into::into).collect(), + } + .abi_encode() + .into() } } @@ -91,7 +94,7 @@ mod tests { hex!("ac9650d800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000020aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000020bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") ); - let decoded_calldata: Vec> = Multicall::decode_multicall(encoded).unwrap(); + let decoded_calldata = >>::decode_multicall(encoded).unwrap(); assert_eq!(decoded_calldata, calldata_list); } }