You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The new web3.js leans into JavaScript BigInts and opaque types for amounts like Lamports. While BigInts can prevent truncation errors (see solana-labs/solana-web3.js#1116) they are in some ways more difficult to do math on. While the Lamports type gives developers a strong and typesafe signal that a given input or output is in lamports rather than SOL, it offers no way to convert between the two or to prepare one for UI display in the other.
The goal of this issue is to develop an API that allows developers to do both without introducing rounding or precision errors.
Spitballing an API
Values as basis points
Starting with values expressed as an opaque type having basis points and a decimal:
functionformat(value: Value<bigint>, ...formatArgs: ConstructorParameters<typeofIntl.NumberFormat>): string{// Don't love this architecture though, since it means recreating the `NumberFormat` object on every pass.constformatter=newIntl.NumberFormat(...formatArgs);constdecimalString=valueToDecimalString(value);returnformatter.format(decimalString);}
The text was updated successfully, but these errors were encountered:
The new web3.js leans into JavaScript
BigInts
and opaque types for amounts likeLamports
. WhileBigInts
can prevent truncation errors (see solana-labs/solana-web3.js#1116) they are in some ways more difficult to do math on. While theLamports
type gives developers a strong and typesafe signal that a given input or output is in lamports rather than SOL, it offers no way to convert between the two or to prepare one for UI display in the other.The goal of this issue is to develop an API that allows developers to do both without introducing rounding or precision errors.
Spitballing an API
Values as basis points
Starting with values expressed as an opaque type having basis points and a decimal:
And coercion functions:
Important
Decimals must be 0n or greater.
Math
TODO
Formatting
Tip
Apparently we can achieve UI display using
Intl.NumberFormat
v3 by leaning on scientific notation. Check it out.See also
Convert values to decimal strings for formatting.
Let callers supply a formatter.
Maybe, just maybe, we can make a passthrough:
The text was updated successfully, but these errors were encountered: