Skip to content

Commit

Permalink
Merge pull request #27 from shuhuiluo/arith
Browse files Browse the repository at this point in the history
Impl arithmetic traits on fractions
  • Loading branch information
malik672 authored Jan 7, 2024
2 parents 8253a83 + 31e71a9 commit 6a2fcea
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 191 deletions.
54 changes: 27 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-sdk-core"
version = "0.7.0"
version = "0.8.0"
edition = "2021"
authors = ["malik <[email protected]>", "Shuhui Luo <twitter.com/aureliano_law>"]
description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange"
Expand Down
14 changes: 6 additions & 8 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {

// Multiplication of currency amount by another fractional amount
pub fn multiply<M>(&self, other: &impl FractionTrait<M>) -> Self {
let multiplied = self.as_fraction().multiply(&other.as_fraction());
let multiplied = self.as_fraction() * other.as_fraction();
Self::from_fractional_amount(
self.meta.currency.clone(),
multiplied.numerator().clone(),
Expand All @@ -56,7 +56,7 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {

// Division of currency amount by another fractional amount
pub fn divide<M>(&self, other: &impl FractionTrait<M>) -> Self {
let divided = self.as_fraction().divide(&other.as_fraction());
let divided = self.as_fraction() / other.as_fraction();
Self::from_fractional_amount(
self.meta.currency.clone(),
divided.numerator().clone(),
Expand All @@ -75,7 +75,7 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {
// Addition of another currency amount to the current amount
pub fn add(&self, other: &Self) -> Self {
assert!(self.meta.currency.equals(&other.meta.currency), "CURRENCY");
let added = self.as_fraction().add(&other.as_fraction());
let added = self.as_fraction() + other.as_fraction();
Self::from_fractional_amount(
self.meta.currency.clone(),
added.numerator().clone(),
Expand All @@ -86,7 +86,7 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {
// Subtraction of another currency amount from the current amount
pub fn subtract(&self, other: &Self) -> Self {
assert!(self.meta.currency.equals(&other.meta.currency), "CURRENCY");
let subtracted = self.as_fraction().subtract(&other.as_fraction());
let subtracted = self.as_fraction() - other.as_fraction();
Self::from_fractional_amount(
self.meta.currency.clone(),
subtracted.numerator().clone(),
Expand All @@ -96,16 +96,14 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {

// Convert the currency amount to a string with a specified number of significant digits
pub fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> String {
self.as_fraction()
.divide(&Fraction::new(self.meta.decimal_scale.clone(), 1))
(self.as_fraction() / Fraction::new(self.meta.decimal_scale.clone(), 1))
.to_significant(significant_digits, rounding)
}

// Convert the currency amount to a string with a fixed number of decimal places
pub fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
assert!(decimal_places <= self.meta.currency.decimals(), "DECIMALS");
self.as_fraction()
.divide(&Fraction::new(self.meta.decimal_scale.clone(), 1))
(self.as_fraction() / Fraction::new(self.meta.decimal_scale.clone(), 1))
.to_fixed(decimal_places, rounding)
}
}
Expand Down
Loading

0 comments on commit 6a2fcea

Please sign in to comment.