Repository navigation
fix(trade): use the exact base-unit balance for percent and auto-adju… - #851
furkan3152 wants to merge 1 commit into
Conversation
…sted amounts resolvePercentAmount and validateBalance read the sell-token balance as a JavaScript number and turned it back into the quoted amount. Most 18-decimal balances, and large 9-decimal SPL balances, do not fit in a float, so 100% could quote more than the wallet holds or leave dust, an amount equal to the balance was reported as exceeding it, and a small balance became "5e-7". Keep the balance in base units (BigInt) for these checks, apply the percentage exactly and format the result as a plain decimal string.
nansen-pr-reviewer summaryThe change moves balance comparisons and percentage calculations to base-unit BigInt arithmetic, preserving exact token amounts and avoiding scientific notation. Raw balance fetching, native gas reserves, formatting, and the new edge cases are covered by mocked unit tests, and the user-facing fix includes a changeset. No correctness or security issues were identified. Deterministic check: success — No issues found Risk: 2/5 (Low) ✅ Auto-approved — Deterministic gates passed: severity thresholds met, recommendation "approve", risk level 2/5 within threshold 3. Token usage: 156,087 input, 1,957 output, 95,019 cache read | Usage Guide Cooldown: for the next 10 minutes (counting from when this review finished), new pushes to this PR will not trigger another review — the next push after the window expires will. Need a fresh review sooner? Comment |
There was a problem hiding this comment.
✅ Auto-approved by Nansen review
Deterministic gates passed: severity thresholds met, recommendation "approve", risk level 2/5 within threshold 3.
- The deterministic severity check passed: No issues found
- Risk level 2/5 (Low)
- The model recommended: approve
Approval was decided by deterministic gates, not by the model. The model's recommendation can block auto-approval but never cause it.
Summary
resolvePercentAmountandvalidateBalanceread the sell-token balance as a JavaScript number (Number(raw) / 10 ** decimals) and turn that number back into the amount the quote is requested for. Most 18-decimal balances, and 9-decimal SPL balances above about 9M tokens, do not fit in a float, so the quoted amount is off:--amount 100 --amount-unit percent--amount 100 --amount-unit percent--amount 100 --amount-unit percent--amount 0.987654321987654321 --amount-unit token--amount 100 --amount-unit percent--amount 100 --amount-unit percentInvalid amount: "5e-7"The Base rows were measured by running
trade quotewith the RPC and quote endpoints mocked and reading theamountsent to/quote; the SPL row comes from the unit test. A quote for more than the balance fails when the swap is executed.The two functions now keep the balance in base units (BigInt):
convertToBaseUnits().Error and warning messages keep their wording and now show the exact balance.
fetchNativeBalanceandfetchTokenBalancestill return numbers (validateGasBalanceuses the native one) and now wrap the base-unit reads.formatBaseUnitsis local because importingformatAmountfromtransfer.jswould create an import cycle.New tests in
trade-validation.test.jscover the rows above. On main they fail with the values in the table, for exampleexpected '1.234567891234568' to be '1.234567891234567891'andexpected '5e-7' to be '0.0000005'.Checklist
npm test)src/schema.jsonupdated if new commands or flags were added (none added)README.mdupdated if new top-level commands or categories were added (none added).changeset/exact-balance-amounts.md)npm run lint: no errors.npm test: