-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Migrate to TypeScript, add tests #6
Conversation
stacks-network/c32check#1 has been merged and deployed to npm. Just pushed a changed to this PR with the version bump -- all tests still passing |
All of these are indicative of the underlying problem with using floating point numbers for currency. This is bad [1], [2], [3], [4], [5]. Compounding on the problem is that these topics are referring to fiat currency with two decimals places (usually representable with 64 bits). Floating points with cryptocurrency is especially bad since we are dealing with 256 bit numbers. TLDR: For JSON representation and lib interop, typically For numerical operations in the lib, I find the const microstacks = 18470754444446n;
const stacks = `${microstacks / 1000000n}.${microstacks % 1000000n}`;
console.log(stacks);
// > "18470754.444446" |
Thanks for the comments Matt. Understand the floating point imprecision issues. For a utility library like this, I'm wondering what the most helpful API to expose is. Are you saying these helper functions shouldn't accept Tbh I'm not a big fan of number strings, unless they're really needed, as the lib then needs to handle parsing errors/edge cases. Invalid strings, locale-specific formatting with commas vs full stops to separate 1000 units etc. Given the use case, we should be okay without: // Number.MAX_SAFE_INTEGER > total supply of stacks in micro stacks
9007199254740991 > 2048913388 * 1000000 // true Most the downfalls of using Using If you are using BigNumber: const sum = new BigNumber(someNumber).toNumber();
const result = new BigNumber(microToStacks(sum)); Are there any other reasons to use strings, or ways in which imprecision could be introduced? I worry that this choice would result in superfluous * With some edge cases. Numbers outside the bounds of |
b0e2f4d
to
ce41202
Compare
fdb4c64
to
ca4151b
Compare
I want to second @zone117x 's opinion. The use of
No, it isn't. Even using
There are infinitely many Stacks -- inflation never stops, but maxes out at 300 STX/block after 15 years. The Rust implementation uses an unsigned 128-bit integer to represent microSTX, which means it can handle up to |
Supporting the native For return values representing STX, I think a decimal string is common practice and reasonable. Here's an example in paypal's API: https://developer.paypal.com/docs/api/orders/v2/ Code dealing with localization should generally treat IMO the API should look something like: // Culture-invariant microSTX to STX operation.
// Input should contain no thousands or decimal separators.
// Returns a string with `.` decimal separator, padded to stx 6 decimals.
// ex: '1' to '0.000001', or '1000000' to '1.000000'.
function microToStacks(microStx: BigInt | string): string;
// Culture-invariant STX to microSTX operation.
// Input should contain '.' decimal separator, and not contain thousand separators.
// Returns an integer string with no decimal or thousands separators.
function stacksToMicro(stx: string, outputEncoding = 'bigint'): BigInt;
function stacksToMicro(stx: string): string;
// Locale-aware formatting for display of stacks data.
// ex: '1000.3' input to '1.000,300000' output.
function formatStacks(stx: string): string;
// Locale-aware parsing from user entry.
// ex: '1.000,3' input to '1000.300000' output.
function parseStacks(stx: string): string; Locale-aware formatting and parsing functions/options could use I think BigNumber.js would also be a reasonable replacement for |
Alright, let's not use I was looking at this very much from a browser-environment perspective, where |
2e43fc9
to
489fe8d
Compare
489fe8d
to
e5777ff
Compare
e5777ff
to
53f1545
Compare
Closes #5.
May as well wait on Matt's PR in stacks-network/c32check#1 to utilise its typings.
Haven't checked building/packaging this yet. Unfamiliar with
microbundle
, looks like it works with TypeScript though.Thoughts/Questions
units.ts
files to only accept numberstacksToMicro(Number.MAX_SAFE_INTEGER)
? Currently9.007199254740991e+21
stacksToMicro(Infinity | NaN)
tx
interface