-
Notifications
You must be signed in to change notification settings - Fork 87
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
fix(core, proto): use i128 for price instead of u128 #1991
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! Transformations of compiled protobuf types to other types. | ||
|
||
use crate::generated::astria::primitive::v1::Int128; | ||
impl From<i128> for Int128 { | ||
fn from(primitive: i128) -> Self { | ||
let [h0, h1, h2, h3, h4, h5, h6, h7, l0, l1, l2, l3, l4, l5, l6, l7] = | ||
primitive.to_be_bytes(); | ||
let lo = u64::from_be_bytes([l0, l1, l2, l3, l4, l5, l6, l7]); | ||
let hi = u64::from_be_bytes([h0, h1, h2, h3, h4, h5, h6, h7]); | ||
Self { | ||
lo, | ||
hi, | ||
} | ||
} | ||
} | ||
|
||
impl From<Int128> for i128 { | ||
fn from(pb: Int128) -> i128 { | ||
let [l0, l1, l2, l3, l4, l5, l6, l7] = pb.lo.to_be_bytes(); | ||
let [h0, h1, h2, h3, h4, h5, h6, h7] = pb.hi.to_be_bytes(); | ||
i128::from_be_bytes([ | ||
h0, h1, h2, h3, h4, h5, h6, h7, l0, l1, l2, l3, l4, l5, l6, l7, | ||
]) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a i128> for Int128 { | ||
fn from(primitive: &'a i128) -> Self { | ||
(*primitive).into() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Int128; | ||
|
||
#[track_caller] | ||
fn i128_roundtrip_check(expected: i128) { | ||
let pb: Int128 = expected.into(); | ||
let actual: i128 = pb.into(); | ||
assert_eq!(expected, actual); | ||
} | ||
#[test] | ||
fn i128_roundtrips_work() { | ||
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. This should contain the case of |
||
i128_roundtrip_check(0i128); | ||
i128_roundtrip_check(1i128); | ||
i128_roundtrip_check(i128::from(u64::MAX)); | ||
i128_roundtrip_check(i128::from(u64::MAX) + 1i128); | ||
i128_roundtrip_check(1i128 << 127); | ||
i128_roundtrip_check((1i128 << 127) + (1i128 << 63)); | ||
i128_roundtrip_check(i128::MAX); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod asset; | ||
pub mod i128; | ||
pub mod u128; | ||
|
||
pub use astria_core_address::{ | ||
|
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.
IMO compare on
Price::new(-1)
instead.