From e81d06248b238f8db241fb9c26c57b92dc62443b Mon Sep 17 00:00:00 2001 From: Jean Marchand Date: Fri, 29 Mar 2024 12:09:06 +0100 Subject: [PATCH 1/2] Add `From for Uint256` for FPDecimal --- packages/injective-math/src/fp_decimal/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/injective-math/src/fp_decimal/mod.rs b/packages/injective-math/src/fp_decimal/mod.rs index 40c2bbcf..5fb8d65d 100644 --- a/packages/injective-math/src/fp_decimal/mod.rs +++ b/packages/injective-math/src/fp_decimal/mod.rs @@ -72,6 +72,12 @@ impl From for FPDecimal { } } +impl From for Uint256 { + fn from(value: FPDecimal) -> Self { + value.to_u256() + } +} + impl From for FPDecimal { fn from(x: Uint256) -> FPDecimal { FPDecimal::from_str(&x.to_string()).unwrap() From 716d0fd968b25518e7e0fcece1d55eb224b0cc35 Mon Sep 17 00:00:00 2001 From: Jean Marchand Date: Fri, 31 May 2024 16:33:39 +0700 Subject: [PATCH 2/2] Add a test --- packages/injective-math/src/fp_decimal/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/injective-math/src/fp_decimal/mod.rs b/packages/injective-math/src/fp_decimal/mod.rs index 5fb8d65d..86ca54e4 100644 --- a/packages/injective-math/src/fp_decimal/mod.rs +++ b/packages/injective-math/src/fp_decimal/mod.rs @@ -462,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) + } + #[test] fn into_uint256_floor() { let fp_decimal = FPDecimal {