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 From<FPDecimal> for Uint256 for FPDecimal #217

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/injective-math/src/fp_decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ impl From<Uint128> for FPDecimal {
}
}

impl From<FPDecimal> for Uint256 {
fn from(value: FPDecimal) -> Self {
value.to_u256()
}
Comment on lines +75 to +78
Copy link

Choose a reason for hiding this comment

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

Add documentation for the From<FPDecimal> for Uint256 implementation.

It's important to document how the conversion handles the sign of FPDecimal and any potential edge cases. This will help future developers understand the conversion process better and ensure correct usage.

}

impl From<Uint256> for FPDecimal {
fn from(x: Uint256) -> FPDecimal {
FPDecimal::from_str(&x.to_string()).unwrap()
Expand Down Expand Up @@ -456,6 +462,18 @@ mod tests {
assert_eq!(uint256, Uint256::from(12345u64));
}

#[test]
fn test_into_u256() {
let fp_decimal: Uint256 = FPDecimal {
num: U256::from(12345u64),
sign: 1, // Assuming it's always positive
}
.into();
let u256 = Uint256::from(12345u64);

assert_eq!(u256, fp_decimal)
}
Comment on lines +465 to +475
Copy link

Choose a reason for hiding this comment

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

Enhance the test coverage for test_into_u256.

The current test only covers a basic positive number conversion. Consider adding tests for edge cases such as large numbers, negative numbers, and zero to ensure the robustness of the conversion process.


#[test]
fn into_uint256_floor() {
let fp_decimal = FPDecimal {
Expand Down
Loading