-
Notifications
You must be signed in to change notification settings - Fork 8
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
Conversation
.DS_Store
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add to .gitignore
.
src/entities/token.rs
Outdated
) -> Result<Self, Error> { | ||
if chain_id == 0 { | ||
return Err(Error::ChainIdError { field: "CHAIN_ID" }); | ||
} | ||
Ok(Self { |
There was a problem hiding this comment.
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.
#[error("incorrect: {0}")] | ||
Incorrect(String), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other(String)
?
src/entities/fractions/price.rs
Outdated
pub fn adjusted_for_decimals(&self) -> Result<Fraction, Error> { | ||
self.as_fraction()? * self.meta.scalar.clone() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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() |
src/entities/token.rs
Outdated
@@ -92,6 +98,7 @@ macro_rules! token { | |||
None, | |||
None, | |||
) | |||
.expect("failed to create token") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.expect("failed to create token") |
src/entities/token.rs
Outdated
@@ -114,6 +122,7 @@ macro_rules! token { | |||
None, | |||
None, | |||
) | |||
.expect("failed to create token") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.expect("failed to create token") |
.divide("ed_output_amount), | ||
Err(e) => Err(e), | ||
}; | ||
let price_impact_clone = price_impact?.clone(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't.
src/entities/fractions/fraction.rs
Outdated
@@ -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>; |
There was a problem hiding this comment.
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.
src/entities/fractions/fraction.rs
Outdated
@@ -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>; |
There was a problem hiding this comment.
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.
src/entities/fractions/fraction.rs
Outdated
@@ -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>; |
There was a problem hiding this comment.
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.
src/entities/fractions/fraction.rs
Outdated
@@ -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> { |
There was a problem hiding this comment.
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.
5724e17
to
f6724ac
Compare
.DS_Store
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove from git indexing.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
) -> 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> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
) -> Result<CurrencyAmount<T>, Error> { | |
) -> Result<Self, Error> { |
nit
src/entities/fractions/fraction.rs
Outdated
@@ -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> { |
There was a problem hiding this comment.
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?
multiplied.clone().numerator().clone(), | ||
multiplied.clone().denominator().clone(), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
println!("{:?}", amount.clone().to_exact()); | ||
// assert_eq!(amount.clone().to_exact(), "123456"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
src/entities/fractions/price.rs
Outdated
return Err(Error::NotEqual("the comparison are not equal".to_owned())); | ||
} | ||
|
||
let fraction = (self.as_fraction()? * other.as_fraction()?)?.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let fraction = (self.as_fraction()? * other.as_fraction()?)?.clone(); | |
let fraction = self.as_fraction() * other.as_fraction(); |
src/entities/fractions/price.rs
Outdated
{ | ||
return Err(Error::NotEqual("the comparison are not equal".to_owned())); | ||
} | ||
let fraction = (self.as_fraction()? * currency_amount.as_fraction()?)?.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let fraction = (self.as_fraction()? * currency_amount.as_fraction()?)?.clone(); | |
let fraction = self.as_fraction() * currency_amount.as_fraction(); |
There was a problem hiding this comment.
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),
},
)
}
There was a problem hiding this comment.
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:
FractionLike::new
only errors if the denominator is 0.- Any existing
FractionLike
must have nonzero denominator. - Converting any
FractionLike
to a baseFraction
is impossible to error, which means we can safely unwrapFractionTrait::new
inas_fraction
and returnSelf
. - When adding, subtracting or multiplying two
FractionLike
, the denominator also cannot be zero. Therefore we should returnSelf
instead ofResult
for these ops for the sake of readability, dev experience and performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
@shuhuiluo yeah mistake, just pruned the branch |
switched from unrecoverable error to recoverable errors, to improve error handling
added to error.rs file for custom errors
bump version
fixed issue #26