fix: use exact decimal math and fix the displayed swap rate (Closes #159) - #164
Open
waterWang wants to merge 1 commit into
Open
fix: use exact decimal math and fix the displayed swap rate (Closes #159)#164waterWang wants to merge 1 commit into
waterWang wants to merge 1 commit into
Conversation
- Add lib/math.ts: toBaseUnits, fromBaseUnits, computeRate, applySlippage - Fix lib/format.ts: use BigInt instead of Number arithmetic - Fix SwapPreview.tsx: rate = output / input (not raw output amount) - Fix swap/page.tsx: use applySlippage for minAmountOut - Add jest.setup.ts for React 19 act-compat - Add __tests__/lib/math.test.ts: 22 cases + round-trip property tests Closes Nodus-protocol#159
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the swap preview rate bug: it previously rendered the entire quoted output amount as
1 tokenIn = ..., instead of computing output ÷ input. Also replaces all floating-pointNumberarithmetic in the financial path with exactBigIntdecimal math.Closes #159
Type of Change
What Changed
New:
lib/math.tstoBaseUnits(display, decimals)— parse human input into integer base units withBigIntonly: noNumbercoercion, no scientific-notation / truncation surprises, excess decimals truncated safely.fromBaseUnits(base, decimals, maxDecimals)— exact base-unit → display formatting.computeRate(amountOut, amountIn, outDecimals, inDecimals)— rate = output ÷ input with the correct decimal scale in both directions (e.g.10 USDC → 1 XLM = 10 USDC/XLMand reversed). Uses integer division with a scaled dividend, never floating point.applySlippage(amountOut, slippageBps)— integer basis-point minimum-output math, explicitly rounding down (favorable to safety).MathError— structured error with a machine-readabletag(invalid/overflow/division_by_zero).roundTripDisplay— property-test helper verifying display → base → display round trips.lib/format.tsstroopsToXlm/rawToUsdcnow useBigIntdivision throughformatDecimalsinstead ofNumber/— exact for every input, including values beyondNumber.MAX_SAFE_INTEGER.xlmToStroops/usdcToRawdelegate totoBaseUnits.components/SwapPreview.tsxcomputeRate(amountOut, amountIn, ...)instead of the raw output amount — the actual per-1-unit exchange rate.app/swap/page.tsxminAmountOutnow usesapplySlippage()(documented rounding direction) instead of inline BigInt.jest.setup.ts+jest.config.jsIS_REACT_ACT_ENVIRONMENTandNODE_ENV=testso React 19 + jsdom component tests stop crashing withReact.act is not a function.How Was It Tested?
__tests__/lib/math.test.ts: 22 cases covering base-unit conversion, exact large/small amounts, rate both directions, boundary ratios, slippage rounding direction, invalid input, and round-trip property checks.__tests__/lib/format.test.ts: unchanged expectations all pass (BigInt refactor is behavior-preserving).NODE_ENV=development npx jest).npx tsc --noEmit— clean.npm run lint(eslint) — no errors.Checklist
CONTRIBUTING.mdmain(no merge commits)Closes #159)