Skip to content

Commit

Permalink
refactor!: change Currency to BaseCurrency for parity (#88)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shuhuiluo authored Oct 7, 2024
1 parent 15c7e88 commit 35d525a
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-v3-sdk"
version = "1.2.0"
version = "2.0.0"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "Uniswap V3 SDK for Rust"
Expand Down Expand Up @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/entities/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl<TP: TickDataProvider> Pool<TP> {
///
/// 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)
}

Expand Down Expand Up @@ -284,7 +284,7 @@ impl<TP: Clone + TickDataProvider> Pool<TP> {
#[inline]
pub fn get_output_amount(
&self,
input_amount: &CurrencyAmount<impl Currency>,
input_amount: &CurrencyAmount<impl BaseCurrency>,
sqrt_price_limit_x96: Option<U160>,
) -> Result<(CurrencyAmount<&Token>, Self), Error> {
assert!(self.involves_token(&input_amount.currency), "TOKEN");
Expand Down Expand Up @@ -339,7 +339,7 @@ impl<TP: Clone + TickDataProvider> Pool<TP> {
#[inline]
pub fn get_input_amount(
&self,
output_amount: &CurrencyAmount<impl Currency>,
output_amount: &CurrencyAmount<impl BaseCurrency>,
sqrt_price_limit_x96: Option<U160>,
) -> Result<(CurrencyAmount<&Token>, Self), Error> {
assert!(self.involves_token(&output_amount.currency), "TOKEN");
Expand Down
8 changes: 4 additions & 4 deletions src/entities/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use uniswap_sdk_core::prelude::*;
#[derive(Clone, PartialEq, Debug)]
pub struct Route<TInput, TOutput, TP>
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: TickDataProvider,
{
pub pools: Vec<Pool<TP>>,
Expand All @@ -20,8 +20,8 @@ where

impl<TInput, TOutput, TP> Route<TInput, TOutput, TP>
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: TickDataProvider,
{
/// Creates an instance of route.
Expand Down
31 changes: 17 additions & 14 deletions src/entities/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub fn trade_comparator<TInput, TOutput, TP>(
b: &Trade<TInput, TOutput, TP>,
) -> Ordering
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: TickDataProvider,
{
// must have same input and output token for comparison
Expand Down Expand Up @@ -75,8 +75,8 @@ pub struct BestTradeOptions {
#[derive(Clone, PartialEq, Debug)]
pub struct Swap<TInput, TOutput, TP>
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: TickDataProvider,
{
pub route: Route<TInput, TOutput, TP>,
Expand All @@ -86,8 +86,8 @@ where

impl<TInput, TOutput, TP> Swap<TInput, TOutput, TP>
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: TickDataProvider,
{
/// Constructs a swap
Expand Down Expand Up @@ -133,8 +133,8 @@ where
#[derive(Clone, PartialEq, Debug)]
pub struct Trade<TInput, TOutput, TP>
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
Expand All @@ -154,8 +154,8 @@ where

impl<TInput, TOutput, TP> Trade<TInput, TOutput, TP>
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: TickDataProvider,
{
/// Construct a trade by passing in the pre-computed property values
Expand Down Expand Up @@ -497,8 +497,8 @@ where

impl<TInput, TOutput, TP> Trade<TInput, TOutput, TP>
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: Clone + TickDataProvider,
{
/// Constructs an exact in trade with the given amount in and route
Expand Down Expand Up @@ -539,7 +539,7 @@ where
#[inline]
pub fn from_route(
route: Route<TInput, TOutput, TP>,
amount: CurrencyAmount<impl Currency>,
amount: CurrencyAmount<impl BaseCurrency>,
trade_type: TradeType,
) -> Result<Self, Error> {
let mut token_amount: CurrencyAmount<&Token> = amount.wrapped()?;
Expand Down Expand Up @@ -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<impl Currency>, Route<TInput, TOutput, TP>)>,
routes: Vec<(
CurrencyAmount<impl BaseCurrency>,
Route<TInput, TOutput, TP>,
)>,
trade_type: TradeType,
) -> Result<Self, Error> {
let mut populated_routes: Vec<Swap<TInput, TOutput, TP>> = Vec::with_capacity(routes.len());
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/price_tick_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub fn parse_price<TBase, TQuote>(
price: &str,
) -> Result<Price<TBase, TQuote>>
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
Expand Down
12 changes: 6 additions & 6 deletions src/nonfungible_position_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct SafeTransferOptions {
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CollectOptions<Currency0: Currency, Currency1: Currency> {
pub struct CollectOptions<Currency0: BaseCurrency, Currency1: BaseCurrency> {
/// 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
Expand All @@ -75,7 +75,7 @@ pub struct NFTPermitOptions {

/// Options for producing the calldata to exit a position.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoveLiquidityOptions<Currency0: Currency, Currency1: Currency> {
pub struct RemoveLiquidityOptions<Currency0: BaseCurrency, Currency1: BaseCurrency> {
/// The ID of the token to exit
pub token_id: U256,
/// The percentage of position liquidity to exit.
Expand Down Expand Up @@ -217,7 +217,7 @@ pub fn add_call_parameters<TP: TickDataProvider>(
})
}

fn encode_collect<Currency0: Currency, Currency1: Currency>(
fn encode_collect<Currency0: BaseCurrency, Currency1: BaseCurrency>(
options: &CollectOptions<Currency0, Currency1>,
) -> Vec<Bytes> {
let mut calldatas: Vec<Bytes> = Vec::with_capacity(3);
Expand Down Expand Up @@ -269,7 +269,7 @@ fn encode_collect<Currency0: Currency, Currency1: Currency>(
}

#[inline]
pub fn collect_call_parameters<Currency0: Currency, Currency1: Currency>(
pub fn collect_call_parameters<Currency0: BaseCurrency, Currency1: BaseCurrency>(
options: &CollectOptions<Currency0, Currency1>,
) -> MethodParameters {
let calldatas = encode_collect(options);
Expand All @@ -292,8 +292,8 @@ pub fn remove_call_parameters<Currency0, Currency1, TP>(
options: RemoveLiquidityOptions<Currency0, Currency1>,
) -> Result<MethodParameters, Error>
where
Currency0: Currency,
Currency1: Currency,
Currency0: BaseCurrency,
Currency1: BaseCurrency,
TP: TickDataProvider,
{
let mut calldatas: Vec<Bytes> = Vec::with_capacity(6);
Expand Down
6 changes: 3 additions & 3 deletions src/quoter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pub struct QuoteOptions {
#[inline]
pub fn quote_call_parameters<TInput, TOutput, TP>(
route: &Route<TInput, TOutput, TP>,
amount: &CurrencyAmount<impl Currency>,
amount: &CurrencyAmount<impl BaseCurrency>,
trade_type: TradeType,
options: Option<QuoteOptions>,
) -> MethodParameters
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: TickDataProvider,
{
let options = options.unwrap_or_default();
Expand Down
4 changes: 2 additions & 2 deletions src/swap_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub fn swap_call_parameters<TInput, TOutput, TP>(
options: SwapOptions,
) -> Result<MethodParameters, Error>
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: TickDataProvider,
{
let SwapOptions {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/encode_route_to_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub fn encode_route_to_path<TInput, TOutput, TP>(
exact_output: bool,
) -> Bytes
where
TInput: Currency,
TOutput: Currency,
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: TickDataProvider,
{
let mut path: Vec<u8> = Vec::with_capacity(23 * route.pools.len() + 20);
Expand Down

0 comments on commit 35d525a

Please sign in to comment.