Skip to content

fix(token): truncate a scaled UI amount to the decimals of the mint - #1523

Open
askov wants to merge 3 commits into
solana-foundation:masterfrom
hoodieshq:fix/scaled-ui-amount-decimals
Open

askov wants to merge 3 commits into
solana-foundation:masterfrom
hoodieshq:fix/scaled-ui-amount-decimals

Conversation

@askov

@askov askov commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Truncates a scaled UI amount to the decimals of the mint on the Transfers tab, the token balance rows and the supply row. A mint with scaledUiAmountConfig showed up to 23 decimal places for an 8-decimal mint. The three places multiplied a UI amount by the multiplier and did not round the product.

Changes

  • Adds toScaledUiAmount to @entities/token-info, which scales a raw amount with the arithmetic of the token program: an f64 product, truncated toward zero.
  • Uses toScaledUiAmount on the Transfers tab, in TokenBalancesCard and on the supply row, and removes toScaledUiAmountString.
  • Replaces balance and delta in a generateTokenBalanceRows row with preAmount, postAmount and decimals, so a row holds raw amounts.
  • Adds RawTokenBalance to @entities/token-amount as the input of generateTokenBalanceRows, so a read of uiAmountString there does not compile.
  • Computes the change of a token balance row as the difference of the two scaled balances.

Rounding

The RPC truncates a scaled amount, and it multiplies in f64. The explorer does the same, so an amount matches the uiAmountString that the RPC returns for a token account.

Checked on mainnet on 2026-11-27 against the SPYx mint (XsoCS1TfEyfFhfvj8EtZ528L3CaKBDBRqRapnBbDF2W, multiplier 1.007474126658766):

Raw amount RPC uiAmountString Truncated Rounded half up
23532095161 237.0797702 237.0797702 237.07977021
126736599609 1276.83845006 1276.83845006 1276.83845007
3377966122 34.03213468 34.03213468 34.03213469

Exact decimal arithmetic differs from the f64 product in 18 of 200,000 random amounts below 1e13 base units, and in 11,000 of 200,000 below 4.4e15.

Behavior Changes

  • A scaled amount on the Transfers tab, on a token balance row and on the supply row now has at most the decimals of the mint.
  • The supply of a mint now shows every digit when the raw supply is above 2^53.
  • A token balance row in a simulation no longer scales the balance before the transaction twice for a mint with a multiplier.
  • A transfer of 1,000 tokens or more by a transfer instruction no longer renders as NaN for a mint with a multiplier.
  • A token balance below 0.000001 now renders as a decimal, for example 0.00000005, not 5e-8.
  • A token balance row now renders when the RPC response has no uiAmountString.
  • An amount of a transfer instruction on the Transfers tab now renders as 1234.5, not 1,234.50000000, which is the format of a transferChecked amount.

Type of change

  • Bug fix

Testing

Bug fixes

  • A transfer amount has at most 8 decimals. Open the Transfers tab of the SPYx mint (localhost · production) and read the Amount column. Expect at most 8 decimal places in every row. Production shows up to 23 decimal places.
    Cause: production multiplies the UI amount string by the multiplier as an exact decimal and prints every digit of the product.
  • A token balance has at most 8 decimals. Open the transaction (localhost · production) and read the SPYx rows of the Tokens card. Expect at most 8 decimal places in each balance and each change. Production shows more than 8.
    Cause: the same unrounded product, on the balance and on the change.
  • The supply has at most 8 decimals. Open the SPYx mint (localhost · production) and read the Current Supply row. Expect at most 8 decimal places. Production shows more than 8.
    Cause: production multiplies the supply by the multiplier as a Number and prints up to 20 decimal places.

Regression checks

  • A mint with no multiplier is unchanged. Open the Transfers tab of USDC (localhost · production) and the USDC mint page (localhost · production). Expect the same Current Supply format as production, and the same amounts in every transferChecked row.

Related Issues

Closes HOO-2953

Checklist

  • My code follows the project's style guidelines
  • I have added tests that prove my fix/feature works
  • All checks pass locally (pnpm test, pnpm lint, pnpm typecheck)
  • I have run build:info script to update build information

Three places multiplied a UI amount by the scaled UI amount multiplier and did
not round the product, so an 8-decimal mint showed 23 decimal places. They call
toScaledUiAmount, which scales the raw amount as the token program does.
@vercel

vercel Bot commented Nov 27, 2026

Copy link
Copy Markdown

@askov is attempting to deploy a commit to the Solana Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no new actionable defects were established.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable defects were established.

Summary

This PR limits scaled token amounts to the mint’s decimals across transfers, transaction balances, and mint supply.

  • Scaled amounts use the mint’s decimals and the token program’s rounding rule.
  • Transfer amounts use raw instruction values and the mint’s decimals.
  • Transaction token rows calculate balances and changes from raw amounts.

Reviews (4) · Last reviewed commit: "refactor(token): hold one raw amount in ..." · Reviewed by Greptile

An account read adjusts uiAmountString for the mint extensions and a
transaction does not. RawTokenBalance has no uiAmountString, so a read of it
does not compile.
A row held the amount as a formatted string and as a raw amount, and
AmountCell chose one of them. A row holds the raw amount, and AmountCell
formats it.
@askov
askov force-pushed the fix/scaled-ui-amount-decimals branch from aa0c563 to a19d38c Compare November 27, 2026 11:53

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants