Skip to content

Commit

Permalink
Refactor code to replace 'meta.currency' with 'currency' (#39)
Browse files Browse the repository at this point in the history
This commit refines the uniswap-v3-sdk's source code by replacing occurrences of '.meta.currency' with '.currency'. The change covers several .rs files, from 'swap_router.rs' to 'nonfungible_position_manager.rs', effectively streamlining the code for improved clarity and readability. The 'uniswap-sdk-core' version is also updated from 0.15.0 to 0.16.0.
  • Loading branch information
shuhuiluo authored Feb 26, 2024
1 parent 48450f7 commit a838941
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 185 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 = "0.24.1"
version = "0.24.2"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "Uniswap V3 SDK for Rust"
Expand Down Expand Up @@ -30,7 +30,7 @@ regex = { version = "1.10", optional = true }
ruint = "1.11"
serde_json = { version = "1.0", optional = true }
thiserror = "1.0"
uniswap-sdk-core = "0.15.0"
uniswap-sdk-core = "0.16.0"
uniswap_v3_math = "0.4.1"

[features]
Expand Down
16 changes: 8 additions & 8 deletions src/entities/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ where
input_amount: CurrencyAmount<Token>,
sqrt_price_limit_x96: Option<U256>,
) -> Result<(CurrencyAmount<Token>, Self)> {
assert!(self.involves_token(&input_amount.meta.currency), "TOKEN");
assert!(self.involves_token(&input_amount.currency), "TOKEN");

let zero_for_one = input_amount.meta.currency.equals(&self.token0);
let zero_for_one = input_amount.currency.equals(&self.token0);

let (output_amount, sqrt_ratio_x96, liquidity, _) = self._swap(
zero_for_one,
Expand Down Expand Up @@ -263,9 +263,9 @@ where
output_amount: CurrencyAmount<Token>,
sqrt_price_limit_x96: Option<U256>,
) -> Result<(CurrencyAmount<Token>, Self)> {
assert!(self.involves_token(&output_amount.meta.currency), "TOKEN");
assert!(self.involves_token(&output_amount.currency), "TOKEN");

let zero_for_one = output_amount.meta.currency.equals(&self.token1);
let zero_for_one = output_amount.currency.equals(&self.token1);

let (input_amount, sqrt_ratio_x96, liquidity, _) = self._swap(
zero_for_one,
Expand Down Expand Up @@ -696,7 +696,7 @@ mod tests {
fn get_output_amount_usdc_to_dai() -> Result<()> {
let (output_amount, _) =
POOL.get_output_amount(CurrencyAmount::from_raw_amount(USDC.clone(), 100)?, None)?;
assert!(output_amount.meta.currency.equals(&DAI.clone()));
assert!(output_amount.currency.equals(&DAI.clone()));
assert_eq!(output_amount.quotient(), 98.into());
Ok(())
}
Expand All @@ -705,7 +705,7 @@ mod tests {
fn get_output_amount_dai_to_usdc() -> Result<()> {
let (output_amount, _) =
POOL.get_output_amount(CurrencyAmount::from_raw_amount(DAI.clone(), 100)?, None)?;
assert!(output_amount.meta.currency.equals(&USDC.clone()));
assert!(output_amount.currency.equals(&USDC.clone()));
assert_eq!(output_amount.quotient(), 98.into());
Ok(())
}
Expand All @@ -714,7 +714,7 @@ mod tests {
fn get_input_amount_usdc_to_dai() -> Result<()> {
let (input_amount, _) =
POOL.get_input_amount(CurrencyAmount::from_raw_amount(DAI.clone(), 98)?, None)?;
assert!(input_amount.meta.currency.equals(&USDC.clone()));
assert!(input_amount.currency.equals(&USDC.clone()));
assert_eq!(input_amount.quotient(), 100.into());
Ok(())
}
Expand All @@ -723,7 +723,7 @@ mod tests {
fn get_input_amount_dai_to_usdc() -> Result<()> {
let (input_amount, _) =
POOL.get_input_amount(CurrencyAmount::from_raw_amount(USDC.clone(), 98)?, None)?;
assert!(input_amount.meta.currency.equals(&DAI.clone()));
assert!(input_amount.currency.equals(&DAI.clone()));
assert_eq!(input_amount.quotient(), 100.into());
Ok(())
}
Expand Down
32 changes: 16 additions & 16 deletions src/entities/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ mod tests {
let mut route = Route::new(vec![POOL_0_1.clone()], TOKEN0.clone(), TOKEN1.clone());
let price = route.mid_price().unwrap();
assert_eq!(price.to_fixed(4, Rounding::RoundHalfUp), "0.2000");
assert_eq!(price.meta.base_currency, TOKEN0.clone());
assert_eq!(price.meta.quote_currency, TOKEN1.clone());
assert_eq!(price.base_currency, TOKEN0.clone());
assert_eq!(price.quote_currency, TOKEN1.clone());
}

#[test]
Expand All @@ -229,8 +229,8 @@ mod tests {
let mut route = Route::new(vec![POOL_0_1.clone()], TOKEN1.clone(), TOKEN0.clone());
let price = route.mid_price().unwrap();
assert_eq!(price.to_fixed(4, Rounding::RoundHalfUp), "5.0000");
assert_eq!(price.meta.base_currency, TOKEN1.clone());
assert_eq!(price.meta.quote_currency, TOKEN0.clone());
assert_eq!(price.base_currency, TOKEN1.clone());
assert_eq!(price.quote_currency, TOKEN0.clone());
}

#[test]
Expand All @@ -242,8 +242,8 @@ mod tests {
);
let price = route.mid_price().unwrap();
assert_eq!(price.to_fixed(4, Rounding::RoundHalfUp), "0.1000");
assert_eq!(price.meta.base_currency, TOKEN0.clone());
assert_eq!(price.meta.quote_currency, TOKEN2.clone());
assert_eq!(price.base_currency, TOKEN0.clone());
assert_eq!(price.quote_currency, TOKEN2.clone());
}

#[test]
Expand All @@ -255,26 +255,26 @@ mod tests {
);
let price = route.mid_price().unwrap();
assert_eq!(price.to_fixed(4, Rounding::RoundHalfUp), "10.0000");
assert_eq!(price.meta.base_currency, TOKEN2.clone());
assert_eq!(price.meta.quote_currency, TOKEN0.clone());
assert_eq!(price.base_currency, TOKEN2.clone());
assert_eq!(price.quote_currency, TOKEN0.clone());
}

#[test]
fn correct_for_ether_0() {
let mut route = Route::new(vec![POOL_0_WETH.clone()], ETHER.clone(), TOKEN0.clone());
let price = route.mid_price().unwrap();
assert_eq!(price.to_fixed(4, Rounding::RoundHalfUp), "0.3333");
assert_eq!(price.meta.base_currency, ETHER.clone());
assert_eq!(price.meta.quote_currency, TOKEN0.clone());
assert_eq!(price.base_currency, ETHER.clone());
assert_eq!(price.quote_currency, TOKEN0.clone());
}

#[test]
fn correct_for_1_weth() {
let mut route = Route::new(vec![POOL_1_WETH.clone()], TOKEN1.clone(), WETH.clone());
let price = route.mid_price().unwrap();
assert_eq!(price.to_fixed(4, Rounding::RoundHalfUp), "0.1429");
assert_eq!(price.meta.base_currency, TOKEN1.clone());
assert_eq!(price.meta.quote_currency, WETH.clone());
assert_eq!(price.base_currency, TOKEN1.clone());
assert_eq!(price.quote_currency, WETH.clone());
}

#[test]
Expand All @@ -289,8 +289,8 @@ mod tests {
price.to_significant(4, Rounding::RoundHalfUp).unwrap(),
"0.009524"
);
assert_eq!(price.meta.base_currency, ETHER.clone());
assert_eq!(price.meta.quote_currency, WETH.clone());
assert_eq!(price.base_currency, ETHER.clone());
assert_eq!(price.quote_currency, WETH.clone());
}

#[test]
Expand All @@ -305,8 +305,8 @@ mod tests {
price.to_significant(4, Rounding::RoundHalfUp).unwrap(),
"0.009524"
);
assert_eq!(price.meta.base_currency, WETH.clone());
assert_eq!(price.meta.quote_currency, ETHER.clone());
assert_eq!(price.base_currency, WETH.clone());
assert_eq!(price.quote_currency, ETHER.clone());
}
}
}
Loading

0 comments on commit a838941

Please sign in to comment.