Open steveluscher opened 8 months ago
Bullish on a string formatting API that looks something like:
function formatLamportsAsSol(formatter: Intl.NumberFormat, lamports: Lamports): string {
return formatter.format(`${lamports}E-9`);
}
function formatSolAsLamports(formatter: Intl.NumberFormat, solDecimalString: string): string {
return formatter.format(`${solDecimalString}E9`);
}
Nicely does:
console.log(formatSolAsLamports(formatter, '2.123456789'));
> "2123456789"
^ these are my half-baked thoughts.
The new web3.js leans into JavaScript
BigInts
and opaque types for amounts likeLamports
. WhileBigInts
can prevent truncation errors (see #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:
Math
TODO
Formatting
Convert values to decimal strings for formatting.
Let callers supply a formatter.
Maybe, just maybe, we can make a passthrough: