Skip to content

Commit

Permalink
refactor: currency amount parameters to references
Browse files Browse the repository at this point in the history
Change function parameters in `quote` and `compute_price_impact` to use references instead of values for `CurrencyAmount`. This optimizes memory usage and performance by avoiding unnecessary cloning.
  • Loading branch information
shuhuiluo committed Sep 5, 2024
1 parent 84be7ba commit 8c334b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/entities/fractions/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
#[inline]
pub fn quote(
&self,
currency_amount: CurrencyAmount<TBase>,
currency_amount: &CurrencyAmount<TBase>,
) -> Result<CurrencyAmount<TQuote>, Error> {
if !currency_amount.currency.equals(&self.base_currency) {
return Err(Error::NotEqual);
Expand Down Expand Up @@ -184,7 +184,7 @@ mod test {
let price = Price::new(TOKEN0.clone(), TOKEN1.clone(), 1, 5);
assert_eq!(
price
.quote(CurrencyAmount::from_raw_amount(TOKEN0.clone(), 10).unwrap())
.quote(&CurrencyAmount::from_raw_amount(TOKEN0.clone(), 10).unwrap())
.unwrap(),
CurrencyAmount::from_raw_amount(TOKEN1.clone(), 50).unwrap()
);
Expand Down
18 changes: 9 additions & 9 deletions src/utils/compute_price_impact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use crate::prelude::*;
#[inline]
pub fn compute_price_impact<TBase: Currency, TQuote: Currency>(
mid_price: Price<TBase, TQuote>,
input_amount: CurrencyAmount<TBase>,
output_amount: CurrencyAmount<TQuote>,
input_amount: &CurrencyAmount<TBase>,
output_amount: &CurrencyAmount<TQuote>,
) -> Result<Percent, Error> {
let quoted_output_amount = mid_price.quote(input_amount)?;
// calculate price impact := (exactQuote - outputAmount) / exactQuote
let price_impact = quoted_output_amount
.subtract(&output_amount)?
.subtract(output_amount)?
.divide(&quoted_output_amount)?;
Ok(Percent::new(
price_impact.numerator,
Expand All @@ -43,8 +43,8 @@ mod tests {
assert!(
compute_price_impact(
Price::new(Ether::on_chain(1), token.clone(), 10, 100),
CurrencyAmount::from_raw_amount(Ether::on_chain(1), 10).unwrap(),
CurrencyAmount::from_raw_amount(token.clone(), 100).unwrap()
&CurrencyAmount::from_raw_amount(Ether::on_chain(1), 10).unwrap(),
&CurrencyAmount::from_raw_amount(token.clone(), 100).unwrap()
)
.unwrap()
== Percent::default(),
Expand All @@ -54,8 +54,8 @@ mod tests {
assert!(
compute_price_impact(
Price::new(token.clone(), token_1.clone(), 10, 100),
CurrencyAmount::from_raw_amount(token.clone(), 10).unwrap(),
CurrencyAmount::from_raw_amount(token_1.clone(), 50).unwrap()
&CurrencyAmount::from_raw_amount(token.clone(), 10).unwrap(),
&CurrencyAmount::from_raw_amount(token_1.clone(), 50).unwrap()
)
.unwrap()
== Percent::new(5000, 10000),
Expand All @@ -65,8 +65,8 @@ mod tests {
assert_eq!(
compute_price_impact(
Price::new(token.clone(), token_1.clone(), 10, 100),
CurrencyAmount::from_raw_amount(token.clone(), 10).unwrap(),
CurrencyAmount::from_raw_amount(token_1.clone(), 200).unwrap()
&CurrencyAmount::from_raw_amount(token.clone(), 10).unwrap(),
&CurrencyAmount::from_raw_amount(token_1.clone(), 200).unwrap()
)
.unwrap(),
Percent::new(-10000, 10000)
Expand Down

0 comments on commit 8c334b0

Please sign in to comment.