Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 11 additions & 5 deletions crates/float/src/js_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float, FloatError> {
let val = U256::from_str(&value)?;
pub fn from_fixed_decimal_js(value: BigInt, decimals: u8) -> Result<Float, FloatError> {
let value_str: String = value.to_string(10)?.into();
let val = U256::from_str(&value_str)?;
Self::from_fixed_decimal(val, decimals)
}

Expand All @@ -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<String, FloatError> {
#[wasm_export(
js_name = "toFixedDecimal",
preserve_js_class,
unchecked_return_type = "bigint"
)]
pub fn to_fixed_decimal_js(&self, decimals: u8) -> Result<BigInt, FloatError> {
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.
Expand Down
18 changes: 16 additions & 2 deletions crates/float/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,14 @@ impl Float {
/// anyhow::Ok(())
/// ```
pub fn lte(self, b: Self) -> Result<bool, FloatError> {
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`.
Expand All @@ -519,7 +526,14 @@ impl Float {
/// anyhow::Ok(())
/// ```
pub fn gte(self, b: Self) -> Result<bool, FloatError> {
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)
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions test_js/float.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down
Loading