From 35d525aa533d3ac10a939ffe7c031bf039693fdc Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Mon, 7 Oct 2024 08:06:15 -0400 Subject: [PATCH] refactor!: change `Currency` to `BaseCurrency` for parity (#88) Updated type constraints from `Currency` to `BaseCurrency` across various modules and methods. This change ensures consistency and aligns with the new type naming convention. Also bumped the version to 2.0.0 to reflect the breaking changes. --- Cargo.toml | 4 +-- README.md | 2 +- src/entities/pool.rs | 6 ++--- src/entities/route.rs | 8 +++--- src/entities/trade.rs | 31 +++++++++++++----------- src/extensions/price_tick_conversions.rs | 4 +-- src/nonfungible_position_manager.rs | 12 ++++----- src/quoter.rs | 6 ++--- src/swap_router.rs | 4 +-- src/utils/encode_route_to_path.rs | 4 +-- 10 files changed, 42 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1cb51c3..91912f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-v3-sdk" -version = "1.2.0" +version = "2.0.0" edition = "2021" authors = ["Shuhui Luo "] description = "Uniswap V3 SDK for Rust" @@ -30,7 +30,7 @@ rustc-hash = "2.0" serde_json = { version = "1.0", optional = true } thiserror = { version = "1.0", optional = true } uniswap-lens = { version = "0.4", optional = true } -uniswap-sdk-core = "2.4.0" +uniswap-sdk-core = "3.0.0" [features] default = [] diff --git a/README.md b/README.md index 3e1fd97..734aab2 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ It is feature-complete with unit tests matching the TypeScript SDK. Add the following to your `Cargo.toml` file: ```toml -uniswap-v3-sdk = { version = "1.2.0", features = ["extensions", "std"] } +uniswap-v3-sdk = { version = "2.0.0", features = ["extensions", "std"] } ``` ### Usage diff --git a/src/entities/pool.rs b/src/entities/pool.rs index 7e71447..c965a0c 100644 --- a/src/entities/pool.rs +++ b/src/entities/pool.rs @@ -166,7 +166,7 @@ impl Pool { /// /// returns: bool #[inline] - pub fn involves_token(&self, token: &impl Currency) -> bool { + pub fn involves_token(&self, token: &impl BaseCurrency) -> bool { self.token0.equals(token) || self.token1.equals(token) } @@ -284,7 +284,7 @@ impl Pool { #[inline] pub fn get_output_amount( &self, - input_amount: &CurrencyAmount, + input_amount: &CurrencyAmount, sqrt_price_limit_x96: Option, ) -> Result<(CurrencyAmount<&Token>, Self), Error> { assert!(self.involves_token(&input_amount.currency), "TOKEN"); @@ -339,7 +339,7 @@ impl Pool { #[inline] pub fn get_input_amount( &self, - output_amount: &CurrencyAmount, + output_amount: &CurrencyAmount, sqrt_price_limit_x96: Option, ) -> Result<(CurrencyAmount<&Token>, Self), Error> { assert!(self.involves_token(&output_amount.currency), "TOKEN"); diff --git a/src/entities/route.rs b/src/entities/route.rs index 92aad68..ef903e7 100644 --- a/src/entities/route.rs +++ b/src/entities/route.rs @@ -6,8 +6,8 @@ use uniswap_sdk_core::prelude::*; #[derive(Clone, PartialEq, Debug)] pub struct Route where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { pub pools: Vec>, @@ -20,8 +20,8 @@ where impl Route where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { /// Creates an instance of route. diff --git a/src/entities/trade.rs b/src/entities/trade.rs index 9070313..25b6488 100644 --- a/src/entities/trade.rs +++ b/src/entities/trade.rs @@ -15,8 +15,8 @@ pub fn trade_comparator( b: &Trade, ) -> Ordering where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { // must have same input and output token for comparison @@ -75,8 +75,8 @@ pub struct BestTradeOptions { #[derive(Clone, PartialEq, Debug)] pub struct Swap where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { pub route: Route, @@ -86,8 +86,8 @@ where impl Swap where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { /// Constructs a swap @@ -133,8 +133,8 @@ where #[derive(Clone, PartialEq, Debug)] pub struct Trade where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { /// The swaps of the trade, i.e. which routes and how much is swapped in each that make up the @@ -154,8 +154,8 @@ where impl Trade where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { /// Construct a trade by passing in the pre-computed property values @@ -497,8 +497,8 @@ where impl Trade where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: Clone + TickDataProvider, { /// Constructs an exact in trade with the given amount in and route @@ -539,7 +539,7 @@ where #[inline] pub fn from_route( route: Route, - amount: CurrencyAmount, + amount: CurrencyAmount, trade_type: TradeType, ) -> Result { let mut token_amount: CurrencyAmount<&Token> = amount.wrapped()?; @@ -600,7 +600,10 @@ where /// * `trade_type`: Whether the trade is an exact input or exact output swap #[inline] pub fn from_routes( - routes: Vec<(CurrencyAmount, Route)>, + routes: Vec<( + CurrencyAmount, + Route, + )>, trade_type: TradeType, ) -> Result { let mut populated_routes: Vec> = Vec::with_capacity(routes.len()); diff --git a/src/extensions/price_tick_conversions.rs b/src/extensions/price_tick_conversions.rs index 78f894c..2773dc3 100644 --- a/src/extensions/price_tick_conversions.rs +++ b/src/extensions/price_tick_conversions.rs @@ -52,8 +52,8 @@ pub fn parse_price( price: &str, ) -> Result> where - TBase: Currency, - TQuote: Currency, + TBase: BaseCurrency, + TQuote: BaseCurrency, { // Check whether `price` is a valid string of decimal number. // This regex matches any number of digits optionally followed by '.' which is then followed by diff --git a/src/nonfungible_position_manager.rs b/src/nonfungible_position_manager.rs index ec248b3..4ee74a1 100644 --- a/src/nonfungible_position_manager.rs +++ b/src/nonfungible_position_manager.rs @@ -53,7 +53,7 @@ pub struct SafeTransferOptions { } #[derive(Debug, Clone, PartialEq, Eq)] -pub struct CollectOptions { +pub struct CollectOptions { /// Indicates the ID of the position to collect for. pub token_id: U256, /// Expected value of tokensOwed0, including as-of-yet-unaccounted-for fees/liquidity value to @@ -75,7 +75,7 @@ pub struct NFTPermitOptions { /// Options for producing the calldata to exit a position. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct RemoveLiquidityOptions { +pub struct RemoveLiquidityOptions { /// The ID of the token to exit pub token_id: U256, /// The percentage of position liquidity to exit. @@ -217,7 +217,7 @@ pub fn add_call_parameters( }) } -fn encode_collect( +fn encode_collect( options: &CollectOptions, ) -> Vec { let mut calldatas: Vec = Vec::with_capacity(3); @@ -269,7 +269,7 @@ fn encode_collect( } #[inline] -pub fn collect_call_parameters( +pub fn collect_call_parameters( options: &CollectOptions, ) -> MethodParameters { let calldatas = encode_collect(options); @@ -292,8 +292,8 @@ pub fn remove_call_parameters( options: RemoveLiquidityOptions, ) -> Result where - Currency0: Currency, - Currency1: Currency, + Currency0: BaseCurrency, + Currency1: BaseCurrency, TP: TickDataProvider, { let mut calldatas: Vec = Vec::with_capacity(6); diff --git a/src/quoter.rs b/src/quoter.rs index 1c9f72c..d24db05 100644 --- a/src/quoter.rs +++ b/src/quoter.rs @@ -24,13 +24,13 @@ pub struct QuoteOptions { #[inline] pub fn quote_call_parameters( route: &Route, - amount: &CurrencyAmount, + amount: &CurrencyAmount, trade_type: TradeType, options: Option, ) -> MethodParameters where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { let options = options.unwrap_or_default(); diff --git a/src/swap_router.rs b/src/swap_router.rs index def9043..eeeb3c9 100644 --- a/src/swap_router.rs +++ b/src/swap_router.rs @@ -33,8 +33,8 @@ pub fn swap_call_parameters( options: SwapOptions, ) -> Result where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { let SwapOptions { diff --git a/src/utils/encode_route_to_path.rs b/src/utils/encode_route_to_path.rs index e582a35..8528bc9 100644 --- a/src/utils/encode_route_to_path.rs +++ b/src/utils/encode_route_to_path.rs @@ -31,8 +31,8 @@ pub fn encode_route_to_path( exact_output: bool, ) -> Bytes where - TInput: Currency, - TOutput: Currency, + TInput: BaseCurrency, + TOutput: BaseCurrency, TP: TickDataProvider, { let mut path: Vec = Vec::with_capacity(23 * route.pools.len() + 20);