From 29cb052bc2088642815c3a421a652c4a8166e2a6 Mon Sep 17 00:00:00 2001 From: 0xgleb Date: Sat, 19 Jul 2025 18:00:52 +0400 Subject: [PATCH] fix bindings --- crates/float/src/js_api.rs | 16 +++++++++++----- crates/float/src/lib.rs | 18 ++++++++++++++++-- test_js/float.test.ts | 6 +++--- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/crates/float/src/js_api.rs b/crates/float/src/js_api.rs index 9d8446cd..efdc0a4c 100644 --- a/crates/float/src/js_api.rs +++ b/crates/float/src/js_api.rs @@ -138,8 +138,9 @@ impl Float { /// assert(float.format() === "123.45"); /// ``` #[wasm_export(js_name = "fromFixedDecimal", preserve_js_class)] - pub fn from_fixed_decimal_js(value: String, decimals: u8) -> Result { - let val = U256::from_str(&value)?; + pub fn from_fixed_decimal_js(value: BigInt, decimals: u8) -> Result { + let value_str: String = value.to_string(10)?.into(); + let val = U256::from_str(&value_str)?; Self::from_fixed_decimal(val, decimals) } @@ -164,10 +165,15 @@ impl Float { /// } /// assert(result.value === "12345"); /// ``` - #[wasm_export(js_name = "toFixedDecimal")] - pub fn to_fixed_decimal_js(&self, decimals: u8) -> Result { + #[wasm_export( + js_name = "toFixedDecimal", + preserve_js_class, + unchecked_return_type = "bigint" + )] + pub fn to_fixed_decimal_js(&self, decimals: u8) -> Result { let fixed = self.to_fixed_decimal(decimals)?; - Ok(fixed.to_string()) + BigInt::from_str(&fixed.to_string()) + .map_err(|e| FloatError::JsSysError(e.to_string().into())) } /// Packs a coefficient and exponent into a `Float` in a lossless manner. diff --git a/crates/float/src/lib.rs b/crates/float/src/lib.rs index 801dfd0b..fccbac87 100644 --- a/crates/float/src/lib.rs +++ b/crates/float/src/lib.rs @@ -492,7 +492,14 @@ impl Float { /// anyhow::Ok(()) /// ``` pub fn lte(self, b: Self) -> Result { - Ok(self.lt(b)? || self.eq(b)?) + let Float(a) = self; + let Float(b) = b; + let calldata = DecimalFloat::lteCall { a, b }.abi_encode(); + + execute_call(Bytes::from(calldata), |output| { + let decoded = DecimalFloat::lteCall::abi_decode_returns(output.as_ref())?; + Ok(decoded) + }) } /// Returns `true` if `self` is greater than or equal to `b`. @@ -519,7 +526,14 @@ impl Float { /// anyhow::Ok(()) /// ``` pub fn gte(self, b: Self) -> Result { - Ok(self.gt(b)? || self.eq(b)?) + let Float(a) = self; + let Float(b) = b; + let calldata = DecimalFloat::gteCall { a, b }.abi_encode(); + + execute_call(Bytes::from(calldata), |output| { + let decoded = DecimalFloat::gteCall::abi_decode_returns(output.as_ref())?; + Ok(decoded) + }) } } diff --git a/test_js/float.test.ts b/test_js/float.test.ts index 90f34a85..f1d6cbb1 100644 --- a/test_js/float.test.ts +++ b/test_js/float.test.ts @@ -24,17 +24,17 @@ describe('Test Float Bindings', () => { }); it('should test format18 and fromFixedDecimal', () => { - const float = Float.fromFixedDecimal('12345', 2)?.value!; + const float = Float.fromFixedDecimal(12345n, 2)?.value!; expect(float.format18()?.value!).toBe('123.45'); }); it('should test toFixedDecimal', () => { const float = Float.parse('123.45')?.value!; - expect(float.toFixedDecimal(2)?.value!).toBe('12345'); + expect(float.toFixedDecimal(2)?.value!).toBe(12345n); }); it('should test toFixedDecimal roundtrip', () => { - const originalValue = '9876543210'; + const originalValue = 9876543210n; const decimals = 8; const float = Float.fromFixedDecimal(originalValue, decimals)?.value!; const result = float.toFixedDecimal(decimals)?.value!;