Imported from calculated-finance/calc-js (
repos/rujira-ui/.claude/skills/decimal-math/SKILL.md). Install upstream withnpx skills add calculated-finance/calc-js --skill decimal-math. Copyright stays with the author.
Guardrails for decimal math in a DeFi codebase. CLAUDE.md calls this out as the single most dangerous class of bug: rounding errors can lose user funds.
Rules
- Use the
Bigintscalar everywhere amounts are transported.AssetString/ custom scalars inpackages/main/data/schema.graphqlare the source of truth; do not coerce to JSnumberexcept for display. - Never use floating-point multiply/divide on amounts. Use the helpers in
packages/rujira.js/src/bigint.ts(mul,div,toFixed,fromHuman,toHuman) or the decimal helpers inpackages/rujira.ui/src/helpers/. - Carry decimals explicitly. An amount without its
decimalscontext is meaningless. Pass both or use a typed wrapper. - Round deterministically. For display use
toFixed(n)with an explicit rounding mode; neverMath.roundon a converted value. - Gas and fees are amounts too. Simulation results (
Simulation { amount, decimals, gas, symbol }) must not be massaged with plain arithmetic. - Don't parse user input with
parseFloat. UsefromHuman(str, decimals)so trailing-zero and locale variants are handled.
Checks to run when this skill fires
- Search the changed file for
parseFloat,Number(,* 1e,/ 1e,Math.pow(10,toFixed(with a magic argument. - Flag any
Number(...)/+...conversion of something typed asbigintorstringamount. - Confirm display-side conversions live at the UI boundary only, not in business logic.
When to escalate
- The user is adding a new asset type, new chain decimals, or a new fee schedule — run the
domain-contextskill first to confirm protocol parameters. - The amount crosses a Relay boundary — confirm the scalar in
schema.graphqlisBigint, notInt/Float.
References
packages/rujira.js/src/bigint.ts— canonical arithmetic helperspackages/rujira.js/src/prices.ts— price conversionpackages/rujira.js/src/asset.ts— asset metadata incl. decimalsCONVENTIONS.md— immutability and numerical patterns