Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_recoverable_error + bump version to 0.9.0 #28

Closed
wants to merge 5 commits into from

Conversation

malik672
Copy link
Owner

@malik672 malik672 commented Jan 8, 2024

switched from unrecoverable error to recoverable errors, to improve error handling
added to error.rs file for custom errors
bump version
fixed issue #26

@malik672 malik672 requested a review from shuhuiluo January 8, 2024 19:09
.DS_Store Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to .gitignore.

Comment on lines 52 to 56
) -> Result<Self, Error> {
if chain_id == 0 {
return Err(Error::ChainIdError { field: "CHAIN_ID" });
}
Ok(Self {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the chain_id == 0 edge case is worth handling. Returning a Result also complicates the syntax.

src/error.rs Outdated Show resolved Hide resolved
src/error.rs Outdated Show resolved Hide resolved
Comment on lines +29 to +30
#[error("incorrect: {0}")]
Incorrect(String),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other(String)?

Comment on lines 117 to 118
pub fn adjusted_for_decimals(&self) -> Result<Fraction, Error> {
self.as_fraction()? * self.meta.scalar.clone()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn adjusted_for_decimals(&self) -> Result<Fraction, Error> {
self.as_fraction()? * self.meta.scalar.clone()
pub fn adjusted_for_decimals(&self) -> Fraction {
self.as_fraction() * self.meta.scalar.clone()

@@ -92,6 +98,7 @@ macro_rules! token {
None,
None,
)
.expect("failed to create token")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.expect("failed to create token")

src/entities/token.rs Outdated Show resolved Hide resolved
@@ -114,6 +122,7 @@ macro_rules! token {
None,
None,
)
.expect("failed to create token")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.expect("failed to create token")

.divide(&quoted_output_amount),
Err(e) => Err(e),
};
let price_impact_clone = price_impact?.clone();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't need to be cloned.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i know, but i placed it incase price_impact was ever used again in the same closure

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't.

@@ -165,7 +182,7 @@ impl<M: Clone> PartialOrd<Self> for FractionLike<M> {
}

impl<M: Clone> Add for FractionLike<M> {
type Output = Self;
type Output = Result<Self, Error>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FractionTrait::new only fails when denominator is zero, which is not possible here.

@@ -186,7 +203,7 @@ impl<M: Clone> Add for FractionLike<M> {
}

impl<M: Clone> Sub for FractionLike<M> {
type Output = Self;
type Output = Result<Self, Error>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FractionTrait::new only fails when denominator is zero, which is not possible here.

@@ -207,7 +224,7 @@ impl<M: Clone> Sub for FractionLike<M> {
}

impl<M: Clone> Mul for FractionLike<M> {
type Output = Self;
type Output = Result<Self, Error>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FractionTrait::new only fails when denominator is zero, which is not possible here.

@@ -57,21 +64,23 @@ where
}

// Returns the remainder after floor division as a new fraction
fn remainder(&self) -> Self {
fn remainder(&self) -> Result<Self, Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FractionTrait::new only fails when denominator is zero, which is not possible here.

@shuhuiluo shuhuiluo linked an issue Jan 10, 2024 that may be closed by this pull request
@malik672 malik672 force-pushed the add_recoverable_error branch from 5724e17 to f6724ac Compare January 10, 2024 08:14
@malik672 malik672 requested a review from shuhuiluo January 10, 2024 08:53
.DS_Store Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove from git indexing.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

src/.DS_Store Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove from git indexing.

pub fn from_raw_amount(
currency: T,
raw_amount: impl Into<BigInt>,
) -> Result<CurrencyAmount<T>, Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) -> Result<CurrencyAmount<T>, Error> {
) -> Result<Self, Error> {

nit

@@ -40,23 +49,23 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {
currency: T,
numerator: impl Into<BigInt>,
denominator: impl Into<BigInt>,
) -> CurrencyAmount<T> {
) -> Result<CurrencyAmount<T>, Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) -> Result<CurrencyAmount<T>, Error> {
) -> Result<Self, Error> {

nit

@@ -108,23 +115,29 @@ where
}

// Helper method for converting any superclass back to a simple Fraction
fn as_fraction(&self) -> Fraction {
fn as_fraction(&self) -> Result<Fraction, Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give an example when this will error?

Comment on lines 61 to 62
multiplied.clone().numerator().clone(),
multiplied.clone().denominator().clone(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the purpose of these clones. It affects nothing but performance.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fraction::new takes ownership of the data the only way to do this i know is to clone

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numerator and denominator take &self. They don't need a fresh instance of Fraction. Only the BigInt numerator and denominator need to be cloned. The code was working fine.

Comment on lines 294 to 295
println!("{:?}", amount.clone().to_exact());
// assert_eq!(amount.clone().to_exact(), "123456");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it commented out?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lmao, forgot I ever did something like this

return Err(Error::NotEqual("the comparison are not equal".to_owned()));
}

let fraction = (self.as_fraction()? * other.as_fraction()?)?.clone();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let fraction = (self.as_fraction()? * other.as_fraction()?)?.clone();
let fraction = self.as_fraction() * other.as_fraction();

{
return Err(Error::NotEqual("the comparison are not equal".to_owned()));
}
let fraction = (self.as_fraction()? * currency_amount.as_fraction()?)?.clone();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let fraction = (self.as_fraction()? * currency_amount.as_fraction()?)?.clone();
let fraction = self.as_fraction() * currency_amount.as_fraction();

Copy link
Owner Author

@malik672 malik672 Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i actually wanted to do this, but somehow, they are all linked way to the top, the easiest and possible way to solve this that i saw was to remove this

impl<T: CurrencyTrait> CurrencyAmount<T> {
    // Constructor method for creating a new currency amount
    fn new(
        currency: T,
        numerator: impl Into<BigInt>,
        denominator: impl Into<BigInt>,
    ) -> Result<Self, Error> {
        let numerator = numerator.into();
        let denominator = denominator.into();
        // Ensure the amount does not exceed MAX_UINT256
        if !numerator.div_floor(&denominator).le(&MAX_UINT256) {
            return Err(Error::MaxUint);
        }
        let exponent = currency.decimals();
        FractionTrait::new(
            numerator,
            denominator,
            CurrencyMeta {
                currency,
                decimal_scale: BigUint::from(10u64).pow(exponent as u32),
            },
        )
    }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we're on the same page. What I suggested was making as_fraction and mul return Self instead of Result. Rationale:

  1. FractionLike::new only errors if the denominator is 0.
  2. Any existing FractionLike must have nonzero denominator.
  3. Converting any FractionLike to a base Fraction is impossible to error, which means we can safely unwrap FractionTrait::new in as_fraction and return Self.
  4. When adding, subtracting or multiplying two FractionLike, the denominator also cannot be zero. Therefore we should return Self instead of Result for these ops for the sake of readability, dev experience and performance.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fraction::new should not return an errror instead, in my opinion it should panic

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make the code a lot cleaner. For example in Solidity, dividing by 0 is a panic.

@malik672 malik672 changed the title add_recoverable_error + bump version to 0.8.0 add_recoverable_error + bump version to 0.9.0 Jan 10, 2024
@malik672 malik672 requested a review from shuhuiluo January 10, 2024 18:49
@malik672 malik672 closed this Jan 10, 2024
@shuhuiluo
Copy link
Collaborator

target was pushed?

@malik672
Copy link
Owner Author

@shuhuiluo yeah mistake, just pruned the branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve error handling
2 participants