-
Notifications
You must be signed in to change notification settings - Fork 10
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
Issue #249 divisibility arbitrary percision #251
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e1e2388
WIP Precision
elpiel 3aa1e8e
Merge branch 'dev' into issue-249-divisibility-arbitrary-percision
elpiel f6f94db
WIP Precision
elpiel a2cc8c2
Merge branch 'issue-249-divisibility-arbitrary-percision' of github.c…
elpiel 6e8b1e4
primitives - big_num - Multiplier - takes the `pow` value, instead of…
elpiel 611a12c
Merge branch 'dev' into issue-249-divisibility-arbitrary-percision
elpiel f2251fb
primitives - Multiplier & MultiplierNum + clean-up
elpiel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,62 @@ use std::str::FromStr; | |
use num::rational::Ratio; | ||
use num::{BigUint, CheckedSub, Integer}; | ||
use num_derive::{Num, NumOps, One, Zero}; | ||
use num_traits::Pow; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
/// DAI has precision of 18 decimals | ||
/// For CPM we have 3 decimals precision, but that's for 1000 (3 decimals more) | ||
/// This in terms means we need 18 - (3 + 3) = 12 decimals precision | ||
pub const GLOBAL_MULTIPLIER: Multiplier = Multiplier(12); | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
/// Multiplier - Pow of 10 (10**n) | ||
pub struct Multiplier(u64); | ||
|
||
impl Mul<PrecisionU64> for Multiplier { | ||
type Output = BigUint; | ||
|
||
fn mul(self, rhs: PrecisionU64) -> Self::Output { | ||
let real_multiplier = BigUint::from(10u8).pow(BigUint::from(GLOBAL_MULTIPLIER.0)); | ||
|
||
real_multiplier * rhs.0 | ||
} | ||
} | ||
|
||
impl Into<BigNum> for Multiplier { | ||
fn into(self) -> BigNum { | ||
BigNum(self.into()) | ||
} | ||
} | ||
|
||
impl Into<BigUint> for Multiplier { | ||
fn into(self) -> BigUint { | ||
BigUint::from(10u8).pow(BigUint::from(self.0)) | ||
} | ||
} | ||
|
||
/// | ||
// @TODO: (De)serialize | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct PrecisionU64(u64); | ||
|
||
impl Into<BigNum> for PrecisionU64 { | ||
fn into(self) -> BigNum { | ||
BigNum(GLOBAL_MULTIPLIER * self) | ||
} | ||
} | ||
|
||
impl From<BigNum> for PrecisionU64 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TryFrom would be better as 0 may be magic :( I think error should be handled properly |
||
fn from(bignum: BigNum) -> Self { | ||
let precision = bignum | ||
.div_floor(&GLOBAL_MULTIPLIER.into()) | ||
.to_u64() | ||
.unwrap_or(0); | ||
|
||
Self(precision) | ||
} | ||
} | ||
|
||
#[derive( | ||
Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, NumOps, One, Zero, Num, | ||
)] | ||
|
@@ -283,4 +337,24 @@ mod test { | |
let expected: BigNum = 11.into(); | ||
assert_eq!(expected, &big_num * &ratio); | ||
} | ||
|
||
#[test] | ||
fn precision_u64_to_bignum() { | ||
let precision = PrecisionU64(5); | ||
let bignum = precision.into(); | ||
|
||
assert_eq!(BigNum::from(5_000_000_000_000), bignum) | ||
} | ||
|
||
#[test] | ||
fn bignum_to_precision_u64() { | ||
// less than the multiplier 12 | ||
let zero_bignum = BigNum::from(900_000_000_000); | ||
// it should floor to 0 | ||
assert_eq!(PrecisionU64(0), PrecisionU64::from(zero_bignum)); | ||
|
||
let bignum = BigNum::from(5_000_000_000_000); | ||
|
||
assert_eq!(PrecisionU64(5), PrecisionU64::from(bignum)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this not 10**12 rather than 12? it would save you from having to do a pow operation
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.
Good idea! I will impl it.