-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbignumber.ts
48 lines (37 loc) · 1.46 KB
/
bignumber.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { BigNumber } from 'bignumber.js';
import JSONBigBase from 'json-bigint';
// ensure BigNumber doesn't switch to exponential notation
const bigNumberPrecisionConfig = {
EXPONENTIAL_AT: 1e9,
};
BigNumber.config(bigNumberPrecisionConfig);
// this is a workaround for json-bigint not having a way to set the exponential limit
// it's hacky but it's the only way to access the internal BigNumber instance
const forceBigNumberExponentialForJsonBig = (jsonBigInstance: { parse: (val: string) => any }) => {
const temp = jsonBigInstance.parse('{"big":1234567890123456789012345678901234567890}');
temp.big.constructor.config(bigNumberPrecisionConfig);
};
const JSONBig = JSONBigBase({
alwaysParseAsBig: true,
});
const JSONBigOnDemand = JSONBigBase();
forceBigNumberExponentialForJsonBig(JSONBig);
forceBigNumberExponentialForJsonBig(JSONBigOnDemand);
// general utils
const bigMinMax =
(isMax: boolean) =>
(first: BigNumber | bigint | number, ...args: (BigNumber | bigint | number)[]): BigNumber => {
let best = BigNumber.isBigNumber(first) ? first : new BigNumber(first.toString());
for (const arg of args) {
const big = BigNumber.isBigNumber(arg) ? arg : new BigNumber(arg.toString());
if (best === undefined || (isMax && big.gt(best)) || (!isMax && big.lt(best))) {
best = big;
}
}
return best;
};
const bigUtils = {
min: bigMinMax(false),
max: bigMinMax(true),
};
export { BigNumber, JSONBig, JSONBigOnDemand, bigUtils };