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

Fix formatRate, support negative exponents in parseFixed #1726

Merged
merged 4 commits into from
Jan 13, 2025
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
5 changes: 5 additions & 0 deletions .changeset/tricky-goats-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/fixed-point-wasm": patch
---

Added support for negative exponents to `parseFixed`, e.g., `parseFixed(1e-18)`.
1 change: 1 addition & 0 deletions apps/hyperdrive-trading/src/base/formatRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function formatRate({
let formatted = fixed(rate).format({
percent: true,
decimals: 2,
trailingZeros: true,
});

if (formatted === "-0.00%") {
Expand Down
7 changes: 6 additions & 1 deletion apps/hyperdrive-trading/src/testing/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import * as fixedPoint from "@delvtech/fixed-point-wasm";
import { beforeEach } from "vitest";

beforeEach(() => {});
beforeEach(() => {
// This is safe to call multiple times. Internally it will return early if
// the WASM module has already been initialized.
fixedPoint.initSync(fixedPoint.wasmBuffer);
});
9 changes: 4 additions & 5 deletions crates/fixed-point-wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,27 @@ pub fn fixed(value: Numberish, decimals: Option<u8>) -> Result<WasmFixedPoint, E
pub fn parse_fixed(value: Numberish, decimals: Option<u8>) -> Result<WasmFixedPoint, Error> {
// If the value is already a fixed-point number, it's already scaled.
if value.is_fixed_point() == Some(true) {
return fixed(value, decimals);
return WasmFixedPoint::new(value, decimals);
}

let decimals = decimals.unwrap_or(INNER_DECIMALS);
let mut s = value.to_string().as_string().unwrap_or_default();

if s.contains("e") {
s = value.as_string().unwrap_or_default();
let mut parts = s.split("e");
let mantissa_str = parts.next().unwrap_or_default();
let exponent_str = parts.next().unwrap_or_default();
let exponent = u8::from_str_radix(exponent_str, 10).to_result()?;
let exponent = i8::from_str_radix(exponent_str, 10).to_result()?;
let mut mantissa_parts = mantissa_str.split(".");
let int_str = mantissa_parts.next().unwrap_or_default();
let fraction_str = mantissa_parts.next().unwrap_or_default();
let mut inner = I256::from_dec_str(&format!("{int_str}{fraction_str}"))
.to_result()?
.fixed();
let fraction_digits = fraction_str.len() as u8;
let fraction_digits = fraction_str.len() as i8;

if fraction_digits > exponent {
inner /= (10u32.pow((fraction_digits - exponent) as u32)).into();
inner /= (10u128.pow((fraction_digits - exponent) as u32)).into();
return Ok(WasmFixedPoint { inner, decimals });
}

Expand Down
2 changes: 1 addition & 1 deletion packages/fixed-point-wasm/fixed_point_wasm.js

Large diffs are not rendered by default.

Loading