Fix raw balance precision loss in sumSingleBalance#219
Open
azeemshaik025 wants to merge 1 commit into
Open
Conversation
sumSingleBalance was coercing aggregated BigInt totals back through JavaScript Number before storing them as strings. For raw uint256 balances above Number.MAX_SAFE_INTEGER this either rounds to the nearest float or emits scientific notation, both of which corrupt the stored balance and break downstream normalizeBalances when it calls BigInt() on the result. Store BigInt totals as exact decimal strings instead. The number- valued path (USD / coingecko amounts) is intentionally untouched. Add public-seam regression tests through ChainApi, Balances, and normalizeBalances in src/generalUtil.test.ts. Update the existing "Indexer - getLogs with processor" snapshot in src/util/indexer.test.ts, which was asserting scientific-notation and Number-rounded outputs as expected. Replaced with structural assertions (decimal-integer string, positive BigInt) since exact on-chain integers can't be recovered from the corrupted strings without a credentialed indexer run.
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.
What
sumSingleBalancecurrently coerces aggregated BigInt totals back through JavaScriptNumberbefore storing them as strings. For raw uint256 token balances aboveNumber.MAX_SAFE_INTEGER(~9e15) this either rounds to the nearest float or emits scientific notation, both of which corrupt the stored balance.const prevBalance = convertToBigInt(balances[token]); const value = (prevBalance + convertToBigInt(balance)) - isValidNumber(Number(value)) - balances[token] = Number(value).toString() + isValidNumber(value); + balances[token] = value.toString();The number-valued path (USD / coingecko amounts) is intentionally untouched.
Why it matters
The path
ChainApi.add/ChainApi.addGasToken→Balances.add→sumSingleBalanceis the standard route adapters use, and many of them pass raw uint256 BigInts or integer strings:projects/shmonad/index.js,projects/obol/index.js,projects/neopin-liquid-staking/index.jscallapi.addGasToken(<BigInt>)projects/lombard-vault/index.jscallsapi.add(token, <BigInt>)projects/blueberry/v2.js,projects/dtrinity,projects/kvants,projects/sharkycallapi.add(token, '<raw uint256 string>')With the old code these get silently corrupted:
Once a value is stored as a scientific-notation string, downstream
normalizeBalancesthrows:The pre-existing
Indexer - getLogs with processortest insrc/util/indexer.test.tswas asserting these scientific-notation outputs as the expected values (e.g.'2.0821579300721433e+27','1e+21','8.9999999e+21'). That snapshot is direct evidence the corruption was reaching real adapter output via event log processing.Tests
Added 6 tests in
src/generalUtil.test.tscovering public seams:sumSingleBalance preserves unsafe integer strings exactlyBalances.add preserves unsafe integer strings through chain-token balancesChainApi preserves exact raw integer balances returned by adaptersnormalizeBalances accepts gas-token balances produced by ChainApisumSingleBalance preserves exact BigInt aggregation beyond Number precisionsumSingleBalance keeps number-valued balances on the number path(regression guard so the USD / decimal path is untouched)RED/GREEN verified: reverting just the implementation change produces 5 failures including the
normalizeBalancesBigIntSyntaxError. Restoring the fix gives 19/19 pass.Snapshot update
The 12 expected balance values in
Indexer - getLogs with processorwere encoding the bug. Replaced them with structural assertions (decimal-integer string, positive BigInt) plus a comment, because the exact on-chain integers can't be recovered from the previously rounded scientific-notation strings without a credentialed indexer run. Can rebake exact values if a maintainer with an indexer endpoint runs the test, or that can happen as a follow-up.Verification
npm run test-only -- src/generalUtil.test.ts --runTestsByPath→ 19/19 passtsc --noEmit→ cleanindexer.test.tsrequiresLLAMA_INDEXER_V2_ENDPOINT/LLAMA_INDEXER_V2_API_KEY, so the structural assertions were not executed locally. Type-checks clean and the assertions are direct expressions of the property the fix guarantees.