From 43921d0c7920fa1d727d911ba866866002fac8e2 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Fri, 8 Nov 2024 01:34:12 -0500 Subject: [PATCH] chore: update dependencies and refactor code (#104) * chore: update dependencies and refactor code Updated several package dependencies to their latest versions to improve compatibility and performance. Refactored code to use `PrimitiveSignature` instead of `Signature` and simplified pool entity creation by eliminating redundant method calls. * Refactor `Signature` to `PrimitiveSignature` Updated various structs and functions to use `PrimitiveSignature` instead of `Signature` for better consistency and clarity. Modified the creation logic and type casting for the `v` field to handle boolean values. --- Cargo.toml | 10 +++++----- src/entities/pool.rs | 24 ++++++++++-------------- src/nonfungible_position_manager.rs | 10 +++++----- src/self_permit.rs | 26 +++++++++++++------------- 4 files changed, 33 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8cc28fc..af2b21f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-v3-sdk" -version = "2.5.0" +version = "2.6.0" edition = "2021" authors = ["Shuhui Luo "] description = "Uniswap V3 SDK for Rust" @@ -14,7 +14,7 @@ exclude = [".github", ".gitignore", "rustfmt.toml"] all-features = true [dependencies] -alloy = { version = "0.5", optional = true, features = ["contract"] } +alloy = { version = "0.6", optional = true, features = ["contract"] } alloy-primitives = "0.8" alloy-sol-types = "0.8" anyhow = { version = "1.0", optional = true } @@ -29,7 +29,7 @@ regex = { version = "1.11", optional = true } rustc-hash = "2.0" serde_json = { version = "1.0", optional = true } thiserror = { version = "2", default-features = false } -uniswap-lens = { version = "0.6", optional = true } +uniswap-lens = { version = "0.7", optional = true } uniswap-sdk-core = "3.1.0" [features] @@ -38,8 +38,8 @@ extensions = ["alloy", "anyhow", "base64", "regex", "serde_json", "uniswap-lens" std = ["alloy?/std", "thiserror/std", "uniswap-sdk-core/std", "uniswap-lens?/std"] [dev-dependencies] -alloy-signer = "0.5" -alloy-signer-local = "0.5" +alloy-signer = "0.6" +alloy-signer-local = "0.6" criterion = "0.5.1" dotenv = "0.15.0" tokio = { version = "1.40", features = ["full"] } diff --git a/src/entities/pool.rs b/src/entities/pool.rs index 515315f..f10ebb6 100644 --- a/src/entities/pool.rs +++ b/src/entities/pool.rs @@ -298,14 +298,12 @@ impl Pool { }; Ok(( CurrencyAmount::from_raw_amount(output_token, -output_amount.to_big_int())?, - Self::new_with_tick_data_provider( - self.token0.clone(), - self.token1.clone(), - self.fee, - sqrt_price_x96, + Self { + sqrt_ratio_x96: sqrt_price_x96, + tick_current: TP::Index::from_i24(sqrt_price_x96.get_tick_at_sqrt_ratio()?), liquidity, - self.tick_data_provider.clone(), - )?, + ..self.clone() + }, )) } @@ -355,14 +353,12 @@ impl Pool { }; Ok(( CurrencyAmount::from_raw_amount(input_token, input_amount.to_big_int())?, - Self::new_with_tick_data_provider( - self.token0.clone(), - self.token1.clone(), - self.fee, - sqrt_price_x96, + Self { + sqrt_ratio_x96: sqrt_price_x96, + tick_current: TP::Index::from_i24(sqrt_price_x96.get_tick_at_sqrt_ratio()?), liquidity, - self.tick_data_provider.clone(), - )?, + ..self.clone() + }, )) } } diff --git a/src/nonfungible_position_manager.rs b/src/nonfungible_position_manager.rs index f0279e5..0146255 100644 --- a/src/nonfungible_position_manager.rs +++ b/src/nonfungible_position_manager.rs @@ -1,5 +1,5 @@ use crate::prelude::{Error, *}; -use alloy_primitives::{Bytes, Signature, U256}; +use alloy_primitives::{Bytes, PrimitiveSignature, U256}; use alloy_sol_types::{eip712_domain, Eip712Domain, SolCall}; use uniswap_sdk_core::prelude::*; @@ -74,7 +74,7 @@ pub struct NFTPermitData { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct NFTPermitOptions { - pub signature: Signature, + pub signature: PrimitiveSignature, pub deadline: U256, pub spender: Address, } @@ -335,7 +335,7 @@ where spender: permit.spender, tokenId: token_id, deadline: permit.deadline, - v: permit.signature.v().y_parity_byte(), + v: permit.signature.v() as u8, r: permit.signature.r().into(), s: permit.signature.s().into(), } @@ -435,7 +435,7 @@ pub fn safe_transfer_from_parameters(options: SafeTransferOptions) -> MethodPara /// ## Examples /// /// ``` -/// use alloy_primitives::{address, b256, uint, Signature, B256}; +/// use alloy_primitives::{address, b256, uint, PrimitiveSignature, B256}; /// use alloy_signer::SignerSync; /// use alloy_signer_local::PrivateKeySigner; /// use alloy_sol_types::SolStruct; @@ -458,7 +458,7 @@ pub fn safe_transfer_from_parameters(options: SafeTransferOptions) -> MethodPara /// let hash: B256 = data.values.eip712_signing_hash(&data.domain); /// /// let signer = PrivateKeySigner::random(); -/// let signature: Signature = signer.sign_hash_sync(&hash).unwrap(); +/// let signature: PrimitiveSignature = signer.sign_hash_sync(&hash).unwrap(); /// assert_eq!( /// signature.recover_address_from_prehash(&hash).unwrap(), /// signer.address() diff --git a/src/self_permit.rs b/src/self_permit.rs index b79abb9..7734761 100644 --- a/src/self_permit.rs +++ b/src/self_permit.rs @@ -1,5 +1,5 @@ use super::abi::ISelfPermit; -use alloy_primitives::{Bytes, Signature, U256}; +use alloy_primitives::{Bytes, PrimitiveSignature, U256}; use alloy_sol_types::{eip712_domain, Eip712Domain, SolCall, SolStruct}; use uniswap_sdk_core::prelude::*; @@ -26,7 +26,7 @@ pub struct ERC20PermitData { /// ## Examples /// /// ``` -/// use alloy_primitives::{address, b256, uint, Signature, B256}; +/// use alloy_primitives::{address, b256, uint, PrimitiveSignature, B256}; /// use alloy_signer::SignerSync; /// use alloy_signer_local::PrivateKeySigner; /// use alloy_sol_types::SolStruct; @@ -67,7 +67,7 @@ pub struct ERC20PermitData { /// // Derive the EIP-712 signing hash. /// let hash: B256 = data.values.eip712_signing_hash(&data.domain); /// -/// let signature: Signature = signer.sign_hash_sync(&hash).unwrap(); +/// let signature: PrimitiveSignature = signer.sign_hash_sync(&hash).unwrap(); /// assert_eq!( /// signature.recover_address_from_prehash(&hash).unwrap(), /// signer.address() @@ -96,14 +96,14 @@ pub fn get_erc20_permit_data( #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct StandardPermitArguments { - pub signature: Signature, + pub signature: PrimitiveSignature, pub amount: U256, pub deadline: U256, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct AllowedPermitArguments { - pub signature: Signature, + pub signature: PrimitiveSignature, pub nonce: U256, pub expiry: U256, } @@ -117,9 +117,9 @@ pub enum PermitOptions { impl StandardPermitArguments { #[inline] #[must_use] - pub fn new(r: U256, s: U256, v: u64, amount: U256, deadline: U256) -> Self { + pub fn new(r: U256, s: U256, v: bool, amount: U256, deadline: U256) -> Self { Self { - signature: Signature::from_rs_and_parity(r, s, v).unwrap(), + signature: PrimitiveSignature::new(r, s, v), amount, deadline, } @@ -129,9 +129,9 @@ impl StandardPermitArguments { impl AllowedPermitArguments { #[inline] #[must_use] - pub fn new(r: U256, s: U256, v: u64, nonce: U256, expiry: U256) -> Self { + pub fn new(r: U256, s: U256, v: bool, nonce: U256, expiry: U256) -> Self { Self { - signature: Signature::from_rs_and_parity(r, s, v).unwrap(), + signature: PrimitiveSignature::new(r, s, v), nonce, expiry, } @@ -146,7 +146,7 @@ pub fn encode_permit(token: &Token, options: PermitOptions) -> Bytes { token: token.address(), value: args.amount, deadline: args.deadline, - v: args.signature.v().y_parity_byte(), + v: args.signature.v() as u8, r: args.signature.r().into(), s: args.signature.s().into(), } @@ -155,7 +155,7 @@ pub fn encode_permit(token: &Token, options: PermitOptions) -> Bytes { token: token.address(), nonce: args.nonce, expiry: args.expiry, - v: args.signature.v().y_parity_byte(), + v: args.signature.v() as u8, r: args.signature.r().into(), s: args.signature.s().into(), } @@ -179,7 +179,7 @@ mod tests { let standard_permit_options = StandardPermitArguments::new( uint!(1_U256), uint!(2_U256), - 0, + false, uint!(123_U256), uint!(123_U256), ); @@ -192,7 +192,7 @@ mod tests { let allowed_permit_options = AllowedPermitArguments::new( uint!(1_U256), uint!(2_U256), - 0, + false, uint!(123_U256), uint!(123_U256), );