fix(currency-math): correct within-step fractional bonding-curve pricing - #1172
Merged
Merged
Conversation
DiscreteBondingCurve.tokensToValue returned a negative value for a within-step
purchase with a fractional token amount — e.g. tokensToValue(0, 12.5) yielded
-0.750 instead of 0.125.
Root cause: the `startStep == endStep` short-circuit used BigDecimal `==`
(scale-sensitive equals). For a fractional within-step purchase, startStep ("0",
scale 0) and endStep ("0.0", scale 1) are value-equal but `==`-unequal, so the
code fell through to the multi-step branch and computed
middleCost = cumulative[0] - cumulative[1] = -1.0. Whole-token inputs share
scale 0, so `==` happened to work — which is why the bug stayed hidden.
Fix: compare with compareTo; also harden the sibling `tokens == ZERO` check to
signum() (same scale-sensitivity class).
Adds DiscreteBondingCurveVectorTest, asserting the curve against canonical
vectors derived from the on-chain Rust curve (both apps load identical tables),
including the fractional cases that regressed. Verified on device.
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
DiscreteBondingCurve.tokensToValuereturns a negative value for a within-step purchase with a fractional token amount:(currentSupply, tokens)(0, 12.5)0.125-0.750(0, 0.0000000001)0.000000000001-0.999999999998Root cause
The
startStep == endStepshort-circuit uses Kotlin==→BigDecimal.equals, which is scale-sensitive. For a fractional within-step purchase,startStep = "0"(scale 0) andendStep = "0.0"(scale 1) are equal in value but==-unequal, so the code falls through to the multi-step branch and computesmiddleCost = cumulative[0] - cumulative[1] = -1.0.Whole-token inputs share scale 0, so
==happened to work — which is why the bug stayed hidden.Fix
compareToinstead of==in the within-step check.tokens == BigDecimal.ZEROcheck totokens.signum() == 0(same scale-sensitivity class).Testing
Adds
DiscreteBondingCurveVectorTest(instrumented) asserting the curve against canonical vectors derived from the on-chain Rust curve (flipcash-program/api) — both apps load bit-identical pricing/cumulative tables — including the fractional cases that regressed.Verified on device (SM-S938U1): before the fix the within-step fractional cases returned negative; after, they match the reference (
0.125,1e-12). iOS already computes these correctly, so this brings Android into agreement.Reachability: the production sell path uses large (multi-step)
new_supply, so this is unlikely to be hit today — but it's a latent money-math divergence in a shared primitive.