mirror of
https://github.com/status-im/sourcecred.git
synced 2025-01-09 12:15:56 +00:00
2397afc3d2
This commit adds a `grain/grain.js` module, which contains a type and logic for representing Grain balances with 18 digits of precision. We use the native BigInt type (and add the necessary babel plugin to support it). Unfortunately, Flow does not yet support BigInts (see [facebook/flow#6639]). To hack around this, we lie to Flow, claiming that BigInts are numbers, and we expect/suppress the flow errors whenever we actually instantiate one. For example: ```js // $ExpectFlowError const myBigInt = 5n; ``` We can use the BigInt operators like `+`, `-`, `>` without flow errors, since these actually exist on numbers too. However, flow will fail to detect improper combinations of regular numbers and BigInts: ```js // $ExpectFlowError const x = 5n; const y = x + 5; // Uncaught TypeError: Cannot mix BigInt and other types ``` Since any improper mixing will result in a runtime error, these issues will be easy to detect via unit tests. In addition to adding the basic Grain type, I exported a `format` function which will display Grain balances in a human readable way. It supports arbitrary decimal precision, groups large amounts with comma separators, handles negative numbers, and adds a suffix string. The format method is thoroughly documented and tested. Thanks to @Beanow for valuable feedback on its implementation. Test plan: See included unit tests. `yarn test` passes. [facebook/flow#6639]: https://github.com/facebook/flow/issues/6639
26 lines
400 B
JavaScript
26 lines
400 B
JavaScript
// @flow
|
|
|
|
const presets = [
|
|
[
|
|
"@babel/preset-env",
|
|
{
|
|
targets: {
|
|
edge: "17",
|
|
firefox: "60",
|
|
chrome: "67",
|
|
safari: "11.1",
|
|
node: true,
|
|
},
|
|
},
|
|
],
|
|
"@babel/preset-react",
|
|
"@babel/preset-flow",
|
|
];
|
|
|
|
const plugins = [
|
|
"@babel/plugin-proposal-class-properties",
|
|
"@babel/plugin-syntax-bigint",
|
|
];
|
|
|
|
module.exports = {presets, plugins};
|