fix(ripple): represent XRPL issued-currency amounts exactly (no float scaling)#19
Merged
Merged
Conversation
… scaling) XRPL issued currencies (IOUs) carry an arbitrary-precision decimal value with no fixed on-chain integer unit. The extractor scaled them with round(parseFloat(value) * 1e6), which both hardcoded XRP's 6-decimal drops scaling onto IOUs and lost precision via float math. Example: BITX value 0.00026764546195073 was reported as amount '268'; 7979.002624671 was rounded; large whole values like 1000000000000000 became nonsense 1e21. Parse the value losslessly into (mantissa bigint, decimals) using string/BigInt math (handles plain + scientific notation), mirroring how EVM token amounts work (raw integer + decimals). Adds an optional NetworkTransfer.decimals field, populated only for IOUs; native XRP (integer drops) is unchanged. Also decode 40-hex XRPL currency codes to their ASCII symbol (e.g. 'BITx') instead of emitting the raw hex blob. Verified against live ledger 105011630 (BITx/USDV/MAG now exact) plus inline tests for the fractional, whole, and scientific-notation cases.
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.
Problem
Reported by Mesh (Mircea) while validating XRPL. The Ripple token-transfer extractor scaled issued-currency (IOU) amounts with:
Two compounding errors:
* 1e6— that's XRP's drops scaling. IOUs aren't denominated in drops; theirvalueis an arbitrary-precision decimal (up to 15 significant digits, wide exponent) with no fixed integer unit and no universal decimals.parseFloat+Math.round— lossy on large / scientific-notation values.Observed on live ledger
105011630:amount0.000267645461950732680.000267645461950737979.0026246717979002625(rounded)7979.0026246710.00001100.00001Native XRP (integer drops) was already correct.
Fix
valuelosslessly into(mantissa: bigint, decimals: number)using string/BigInt math — neverparseFloat. Handles plain and scientific-notation values; trims trailing fractional zeros to a minimal scale.NetworkTransfer.decimalsfield (additive, non-breaking), populated only for IOUs.human = amount / 10 ** decimals. This mirrors how EVM token amounts already work (raw integer + decimals applied out-of-band). Native XRP / EVM / all other paths are unchanged — nodecimalsemitted, so existing consumers and tests are unaffected.BITx) instead of emitting the raw hex blob; non-printable codes (LP tokens, etc.) keep the hex.Verification
268regression), large-whole, and scientific-notation cases, plus hex-currency decode.105011630— BITx / USDV / MAG now reconstruct to their exact on-ledger values.Note on token identity
tokenis set to the (decoded) currency code, preserving prior semantics. XRPL IOUs are uniquely identified bycurrency+issuer, so same-symbol tokens from different issuers still collide ontokenalone — out of scope here, worth a follow-up if we need issuer-level disambiguation.🤖 Generated with Claude Code