This article explains the underlying computational logic for determining Solana SPL token quantities and provides a table illustrating the relationship between token precision and maximum supply.
In digital calculations, handling decimals often leads to precision loss because computers use binary systems to store numbers, which cannot always represent decimals accurately. For example, if a product costs 0.99 USD and a customer pays 1 USD, using floating-point data types for calculations may yield imprecise results due to binary representation limitations.
Thus, financial systems typically use integers to avoid precision loss in floating-point operations.
How to Replace Floating-Point Operations with Integers?
Suppose a product costs 0.99 USD, and a customer pays 1 USD:
- If token precision is set to 2, the underlying storage and calculation logic is:
100 - 99 = 1
. - If token precision is set to 3, the logic becomes:
1000 - 990 = 10
.
For display purposes, the result is adjusted for precision:
- At precision 2, the frontend shows
0.01
(moving the decimal point 2 places left). - At precision 3, the frontend shows
0.01
(moving the decimal point 3 places left).
Higher precision allows for more decimal places in display.
SPL Token Precision and Supply
Solana's SPL-Token program uses u64
to store and calculate token quantities. The maximum integer u64
can represent is 18446744073709551615
.
SPL tokens typically have a precision between 0 and 9.
- Precision 0: The maximum supply is
18446744073709551615
, but the smallest representable value is1
. This precision is often used for NFTs. - Other Precisions: Follow the formula:
Maximum Supply = u64::MAX / (10 ^ Precision)
Below is a table of precision levels and their corresponding maximum supplies:
Precision | Maximum Supply |
---|---|
9 | 18446744073.709551615 |
8 | 184467440737.09551615 |
7 | 1844674407370.9551615 |
6 | 18446744073709.551615 |
5 | 184467440737095.51615 |
4 | 1844674407370955.1615 |
3 | 18446744073709551.615 |
2 | 184467440737095516.15 |
1 | 1844674407370955161.5 |
0 | 18446744073709551615 |
Experimental Validation
To validate the data above, we conducted an experiment on Solana's Devnet:
Create a Token with Precision 6:
spl-token create-token --decimals 6
Output:
Creating token 5wYhtgLtowkvYeEXoYpCeGvdUHjqsQPvKqM1Qy6gpFSv under program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA Address: 5wYhtgLtowkvYeEXoYpCeGvdUHjqsQPvKqM1Qy6gpFSv Decimals: 6
Mint Tokens Up to Maximum Supply:
spl-token mint 5wYhtgLtowkvYeEXoYpCeGvdUHjqsQPvKqM1Qy6gpFSv 18446744073709.551615
Output:
Minting 18446744073709.55 tokens
Check Balance:
spl-token balance 5wYhtgLtowkvYeEXoYpCeGvdUHjqsQPvKqM1Qy6gpFSv
Output:
18446744073709.551615
Attempt to Mint Beyond Maximum Supply:
spl-token mint 5wYhtgLtowkvYeEXoYpCeGvdUHjqsQPvKqM1Qy6gpFSv 0.000001
Output:
Error: Operation overflowed
The experiment confirms that 18446744073709.551615
is indeed the maximum supply for precision 6.
FAQ
Q1: What is token precision in Solana SPL tokens?
A1: Token precision refers to the number of decimal places a token can support. For example, a precision of 6 means the token can represent values up to 6 decimal places.
Q2: Why does precision affect maximum supply?
A2: Higher precision divides the u64
storage space into smaller units, reducing the maximum integer value representable without overflow.
Q3: Can I change a token's precision after creation?
A3: No, precision is immutable once the token is created.
Q4: What happens if I exceed the maximum supply?
A4: The transaction will fail with an "Operation overflowed" error.