UNPKG

1.93 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.utils = void 0;
4const utils_1 = require("@0x/utils");
5const _ = require("lodash");
6exports.utils = {
7 convertHexToNumber(value) {
8 const valueBigNumber = new utils_1.BigNumber(value);
9 const valueNumber = valueBigNumber.toNumber();
10 return valueNumber;
11 },
12 convertHexToNumberOrNull(hex) {
13 if (hex === null) {
14 return null;
15 }
16 const decimal = exports.utils.convertHexToNumber(hex);
17 return decimal;
18 },
19 convertAmountToBigNumber(value) {
20 const num = value || 0;
21 const isBigNumber = utils_1.BigNumber.isBigNumber(num);
22 if (isBigNumber) {
23 return num;
24 }
25 if (_.isString(num) && (num.indexOf('0x') === 0 || num.indexOf('-0x') === 0)) {
26 return new utils_1.BigNumber(num.replace('0x', ''), 16);
27 }
28 const baseTen = 10;
29 return new utils_1.BigNumber(num.toString(baseTen), baseTen);
30 },
31 encodeAmountAsHexString(value) {
32 const valueBigNumber = exports.utils.convertAmountToBigNumber(value);
33 const hexBase = 16;
34 const valueHex = valueBigNumber.toString(hexBase);
35 return valueBigNumber.isLessThan(0) ? `-0x${valueHex.substr(1)}` : `0x${valueHex}`;
36 },
37 numberToHex(value) {
38 if (!isFinite(value) && !exports.utils.isHexStrict(value)) {
39 throw new Error(`Given input ${value} is not a number.`);
40 }
41 const valueBigNumber = new utils_1.BigNumber(value);
42 const hexBase = 16;
43 const result = valueBigNumber.toString(hexBase);
44 return valueBigNumber.lt(0) ? `-0x${result.substr(1)}` : `0x${result}`;
45 },
46 isHexStrict(hex) {
47 return ((_.isString(hex) || _.isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(_.isNumber(hex) ? hex.toString() : hex));
48 },
49};
50//# sourceMappingURL=utils.js.map
\No newline at end of file