@chainflip/utils 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/assertion.js +2 -2
  2. package/dist/async.cjs +1 -1
  3. package/dist/async.d.cts +1 -1
  4. package/dist/async.d.ts +1 -1
  5. package/dist/async.js +1 -1
  6. package/dist/base58.js +4 -4
  7. package/dist/bytes.js +3 -3
  8. package/dist/chainflip.cjs +147 -0
  9. package/dist/chainflip.d.cts +99 -0
  10. package/dist/chainflip.d.ts +99 -0
  11. package/dist/chainflip.js +22 -0
  12. package/dist/{chunk-D7RIA7SA.js → chunk-2KA626OL.js} +1 -1
  13. package/dist/{chunk-UPA7KLTI.js → chunk-CGMSQQDO.js} +1 -1
  14. package/dist/{chunk-CZNX6EUV.js → chunk-HBIFE4XN.js} +7 -1
  15. package/dist/chunk-WGS2RGVR.js +1348 -0
  16. package/dist/chunk-XFHVSV76.js +115 -0
  17. package/dist/{chunk-MYP3UYWE.js → chunk-ZHIKNZLU.js} +1 -1
  18. package/dist/consts.cjs +146 -0
  19. package/dist/consts.d.cts +148 -0
  20. package/dist/consts.d.ts +148 -0
  21. package/dist/consts.js +113 -0
  22. package/dist/date.cjs +497 -0
  23. package/dist/date.d.cts +16 -0
  24. package/dist/date.d.ts +16 -0
  25. package/dist/date.js +455 -0
  26. package/dist/guard.cjs +9 -0
  27. package/dist/guard.d.cts +11 -8
  28. package/dist/guard.d.ts +11 -8
  29. package/dist/guard.js +7 -1
  30. package/dist/number.cjs +1386 -0
  31. package/dist/number.d.cts +6 -1
  32. package/dist/number.d.ts +6 -1
  33. package/dist/number.js +33 -0
  34. package/dist/ss58.cjs +23 -23
  35. package/dist/ss58.js +10 -10
  36. package/dist/string.cjs +21 -0
  37. package/dist/string.d.cts +7 -2
  38. package/dist/string.d.ts +7 -2
  39. package/dist/string.js +18 -0
  40. package/dist/tickMath.cjs +1502 -0
  41. package/dist/tickMath.d.cts +15 -0
  42. package/dist/tickMath.d.ts +15 -0
  43. package/dist/tickMath.js +35 -0
  44. package/package.json +9 -2
package/dist/number.d.cts CHANGED
@@ -1,5 +1,10 @@
1
+ import BigNumber from 'bignumber.js';
1
2
  import { HexString } from './types.cjs';
2
3
 
3
4
  declare const hexEncodeNumber: (num: number | bigint) => HexString;
5
+ declare function formatUsdValue(value: BigNumber.Value, precise?: boolean): string;
6
+ declare function formatUsdValue(value: null, precise?: boolean): null;
7
+ declare function formatUsdValue(value: undefined, precise?: boolean): undefined;
8
+ declare function formatUsdValue(value: BigNumber.Value | null | undefined, precise?: boolean): string | null | undefined;
4
9
 
5
- export { hexEncodeNumber };
10
+ export { formatUsdValue, hexEncodeNumber };
package/dist/number.d.ts CHANGED
@@ -1,5 +1,10 @@
1
+ import BigNumber from 'bignumber.js';
1
2
  import { HexString } from './types.js';
2
3
 
3
4
  declare const hexEncodeNumber: (num: number | bigint) => HexString;
5
+ declare function formatUsdValue(value: BigNumber.Value, precise?: boolean): string;
6
+ declare function formatUsdValue(value: null, precise?: boolean): null;
7
+ declare function formatUsdValue(value: undefined, precise?: boolean): undefined;
8
+ declare function formatUsdValue(value: BigNumber.Value | null | undefined, precise?: boolean): string | null | undefined;
4
9
 
5
- export { hexEncodeNumber };
10
+ export { formatUsdValue, hexEncodeNumber };
package/dist/number.js CHANGED
@@ -1,5 +1,38 @@
1
+ import {
2
+ bignumber_default
3
+ } from "./chunk-WGS2RGVR.js";
4
+
1
5
  // src/number.ts
6
+ import assert from "assert";
2
7
  var hexEncodeNumber = (num) => `0x${num.toString(16)}`;
8
+ function formatUsdValue(value, precise = true) {
9
+ if (value == null) return value;
10
+ let usdAmount = new bignumber_default(value);
11
+ assert(usdAmount.gte(0), "negative amounts not supported");
12
+ if (!usdAmount.eq(0) && usdAmount.lt(0.01)) return `<$0.01`;
13
+ let suffix = "";
14
+ let decimals = 2;
15
+ if (!precise) {
16
+ if (usdAmount.gte(1e12)) {
17
+ usdAmount = usdAmount.shiftedBy(-12);
18
+ suffix = "T";
19
+ } else if (usdAmount.gte(1e9)) {
20
+ usdAmount = usdAmount.shiftedBy(-9);
21
+ suffix = "B";
22
+ } else if (usdAmount.gte(1e6)) {
23
+ usdAmount = usdAmount.shiftedBy(-6);
24
+ suffix = "M";
25
+ }
26
+ if (suffix === "" && usdAmount.gte(1e4)) {
27
+ decimals = 0;
28
+ } else if (suffix !== "") {
29
+ decimals = void 0;
30
+ }
31
+ }
32
+ usdAmount = usdAmount.decimalPlaces(2);
33
+ return `$${usdAmount.toFormat(decimals)}${suffix}`;
34
+ }
3
35
  export {
36
+ formatUsdValue,
4
37
  hexEncodeNumber
5
38
  };
package/dist/ss58.cjs CHANGED
@@ -26,24 +26,7 @@ __export(ss58_exports, {
26
26
  });
27
27
  module.exports = __toCommonJS(ss58_exports);
28
28
 
29
- // src/guard.ts
30
- var createIsGuard = (type) => (value) => typeof value === type;
31
- var isString = createIsGuard("string");
32
- var isNumber = createIsGuard("number");
33
- var isBigInt = createIsGuard("bigint");
34
- var isBoolean = createIsGuard("boolean");
35
- var isSymbol = createIsGuard("symbol");
36
- var isObject = createIsGuard("object");
37
- var isUndefined = createIsGuard("undefined");
38
-
39
- // src/assertion.ts
40
- function assert(condition, message = "assertion failed", Constructor = Error) {
41
- if (!condition) {
42
- throw new Constructor(message);
43
- }
44
- }
45
-
46
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/_assert.js
29
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/_assert.js
47
30
  function number(n) {
48
31
  if (!Number.isSafeInteger(n) || n < 0)
49
32
  throw new Error(`positive integer expected, not ${n}`);
@@ -71,7 +54,7 @@ function output(out, instance) {
71
54
  }
72
55
  }
73
56
 
74
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/utils.js
57
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/utils.js
75
58
  var u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
76
59
  var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
77
60
  var byteSwap = (word) => word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
@@ -108,7 +91,7 @@ function wrapConstructorWithOpts(hashCons) {
108
91
  return hashC;
109
92
  }
110
93
 
111
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/_blake.js
94
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/_blake.js
112
95
  var SIGMA = /* @__PURE__ */ new Uint8Array([
113
96
  0,
114
97
  1,
@@ -397,7 +380,7 @@ var BLAKE = class extends Hash {
397
380
  }
398
381
  };
399
382
 
400
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/_u64.js
383
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/_u64.js
401
384
  var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
402
385
  var _32n = /* @__PURE__ */ BigInt(32);
403
386
  function fromBig(n, le = false) {
@@ -463,7 +446,7 @@ var u64 = {
463
446
  };
464
447
  var u64_default = u64;
465
448
 
466
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/blake2b.js
449
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/blake2b.js
467
450
  var B2B_IV = /* @__PURE__ */ new Uint32Array([
468
451
  4089235720,
469
452
  1779033703,
@@ -642,6 +625,23 @@ var BLAKE2b = class extends BLAKE {
642
625
  };
643
626
  var blake2b = /* @__PURE__ */ wrapConstructorWithOpts((opts) => new BLAKE2b(opts));
644
627
 
628
+ // src/guard.ts
629
+ var createIsGuard = (type) => (value) => typeof value === type;
630
+ var isString = createIsGuard("string");
631
+ var isNumber = createIsGuard("number");
632
+ var isBigInt = createIsGuard("bigint");
633
+ var isBoolean = createIsGuard("boolean");
634
+ var isSymbol = createIsGuard("symbol");
635
+ var isObject = createIsGuard("object");
636
+ var isUndefined = createIsGuard("undefined");
637
+
638
+ // src/assertion.ts
639
+ function assert(condition, message = "assertion failed", Constructor = Error) {
640
+ if (!condition) {
641
+ throw new Constructor(message);
642
+ }
643
+ }
644
+
645
645
  // src/bytes.ts
646
646
  var bytesToHex = (input) => {
647
647
  const bytes2 = new Uint8Array(input);
@@ -710,7 +710,7 @@ var decode2 = (input) => {
710
710
  ss58Format = (decodedBytes[0] & 63) << 2 | decodedBytes[1] >> 6 | (decodedBytes[1] & 63) << 8;
711
711
  } else {
712
712
  ss58FormatLen = 1;
713
- ss58Format = decodedBytes[0];
713
+ [ss58Format] = decodedBytes;
714
714
  }
715
715
  assert(!RESERVED_PREFIXES.includes(ss58Format), `Reserved prefix: ${ss58Format}`);
716
716
  const checksumBytes = decodedBytes.slice(-CHECKSUM_BYTE_LENGTH);
package/dist/ss58.js CHANGED
@@ -1,17 +1,17 @@
1
1
  import {
2
2
  decode,
3
3
  encode
4
- } from "./chunk-UPA7KLTI.js";
4
+ } from "./chunk-CGMSQQDO.js";
5
5
  import {
6
6
  bytesToHex,
7
7
  hexToBytes
8
- } from "./chunk-D7RIA7SA.js";
8
+ } from "./chunk-2KA626OL.js";
9
9
  import {
10
10
  assert
11
- } from "./chunk-MYP3UYWE.js";
12
- import "./chunk-CZNX6EUV.js";
11
+ } from "./chunk-ZHIKNZLU.js";
12
+ import "./chunk-HBIFE4XN.js";
13
13
 
14
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/_assert.js
14
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/_assert.js
15
15
  function number(n) {
16
16
  if (!Number.isSafeInteger(n) || n < 0)
17
17
  throw new Error(`positive integer expected, not ${n}`);
@@ -39,7 +39,7 @@ function output(out, instance) {
39
39
  }
40
40
  }
41
41
 
42
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/utils.js
42
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/utils.js
43
43
  var u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
44
44
  var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
45
45
  var byteSwap = (word) => word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
@@ -76,7 +76,7 @@ function wrapConstructorWithOpts(hashCons) {
76
76
  return hashC;
77
77
  }
78
78
 
79
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/_blake.js
79
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/_blake.js
80
80
  var SIGMA = /* @__PURE__ */ new Uint8Array([
81
81
  0,
82
82
  1,
@@ -365,7 +365,7 @@ var BLAKE = class extends Hash {
365
365
  }
366
366
  };
367
367
 
368
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/_u64.js
368
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/_u64.js
369
369
  var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
370
370
  var _32n = /* @__PURE__ */ BigInt(32);
371
371
  function fromBig(n, le = false) {
@@ -431,7 +431,7 @@ var u64 = {
431
431
  };
432
432
  var u64_default = u64;
433
433
 
434
- // ../../node_modules/.pnpm/@noble+hashes@1.4.0/node_modules/@noble/hashes/esm/blake2b.js
434
+ // ../../node_modules/.pnpm/@noble+hashes@1.5.0/node_modules/@noble/hashes/esm/blake2b.js
435
435
  var B2B_IV = /* @__PURE__ */ new Uint32Array([
436
436
  4089235720,
437
437
  1779033703,
@@ -630,7 +630,7 @@ var decode2 = (input) => {
630
630
  ss58Format = (decodedBytes[0] & 63) << 2 | decodedBytes[1] >> 6 | (decodedBytes[1] & 63) << 8;
631
631
  } else {
632
632
  ss58FormatLen = 1;
633
- ss58Format = decodedBytes[0];
633
+ [ss58Format] = decodedBytes;
634
634
  }
635
635
  assert(!RESERVED_PREFIXES.includes(ss58Format), `Reserved prefix: ${ss58Format}`);
636
636
  const checksumBytes = decodedBytes.slice(-CHECKSUM_BYTE_LENGTH);
package/dist/string.cjs CHANGED
@@ -20,26 +20,47 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/string.ts
21
21
  var string_exports = {};
22
22
  __export(string_exports, {
23
+ abbreviate: () => abbreviate,
23
24
  capitalize: () => capitalize,
24
25
  isHex: () => isHex,
26
+ isInteger: () => isInteger,
25
27
  split: () => split,
26
28
  toLowerCase: () => toLowerCase,
27
29
  toUpperCase: () => toUpperCase,
30
+ truncateString: () => truncateString,
28
31
  uncapitalize: () => uncapitalize
29
32
  });
30
33
  module.exports = __toCommonJS(string_exports);
31
34
  var toUpperCase = (str) => str.toUpperCase();
32
35
  var toLowerCase = (str) => str.toLowerCase();
36
+ var isInteger = (string) => /^\d+$/.test(string);
33
37
  var split = (str, delimiter) => str.split(delimiter);
34
38
  var capitalize = (str) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
35
39
  var uncapitalize = (str) => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;
36
40
  var isHex = (str) => /^0x[\da-f]+$/i.test(str);
41
+ var abbreviate = (text, showLength = 4, space = false) => {
42
+ if (typeof text !== "string") return "";
43
+ const leftPart = text.slice(0, showLength);
44
+ const rightPart = text.slice(text.length - showLength);
45
+ return [leftPart, rightPart].join(space ? ". . ." : "\u2026");
46
+ };
47
+ var truncateString = (string, numCharacters = 20, ellipsis = true) => {
48
+ if (string.length < numCharacters) return string;
49
+ let slicedString = string.slice(0, numCharacters);
50
+ if (ellipsis) {
51
+ slicedString += "...";
52
+ }
53
+ return slicedString;
54
+ };
37
55
  // Annotate the CommonJS export names for ESM import in node:
38
56
  0 && (module.exports = {
57
+ abbreviate,
39
58
  capitalize,
40
59
  isHex,
60
+ isInteger,
41
61
  split,
42
62
  toLowerCase,
43
63
  toUpperCase,
64
+ truncateString,
44
65
  uncapitalize
45
66
  });
package/dist/string.d.cts CHANGED
@@ -1,9 +1,14 @@
1
+ import { HexString } from './types.cjs';
2
+
1
3
  declare const toUpperCase: <const T extends string>(str: T) => Uppercase<T>;
2
4
  declare const toLowerCase: <const T extends string>(str: T) => Lowercase<T>;
5
+ declare const isInteger: (string: string) => boolean;
3
6
  type Split<T extends string, D extends string> = T extends `${infer L}${D}${infer R}` ? [L, ...Split<R, D>] : [T];
4
7
  declare const split: <const T extends string, D extends string>(str: T, delimiter: D) => Split<T, D>;
5
8
  declare const capitalize: <const T extends string>(str: T) => Capitalize<T>;
6
9
  declare const uncapitalize: <const T extends string>(str: T) => Uncapitalize<T>;
7
- declare const isHex: (str: string) => str is `0x${string}`;
10
+ declare const isHex: (str: string) => str is HexString;
11
+ declare const abbreviate: (text: string | undefined | null, showLength?: number, space?: boolean) => string;
12
+ declare const truncateString: (string: string, numCharacters?: number, ellipsis?: boolean) => string;
8
13
 
9
- export { capitalize, isHex, split, toLowerCase, toUpperCase, uncapitalize };
14
+ export { abbreviate, capitalize, isHex, isInteger, split, toLowerCase, toUpperCase, truncateString, uncapitalize };
package/dist/string.d.ts CHANGED
@@ -1,9 +1,14 @@
1
+ import { HexString } from './types.js';
2
+
1
3
  declare const toUpperCase: <const T extends string>(str: T) => Uppercase<T>;
2
4
  declare const toLowerCase: <const T extends string>(str: T) => Lowercase<T>;
5
+ declare const isInteger: (string: string) => boolean;
3
6
  type Split<T extends string, D extends string> = T extends `${infer L}${D}${infer R}` ? [L, ...Split<R, D>] : [T];
4
7
  declare const split: <const T extends string, D extends string>(str: T, delimiter: D) => Split<T, D>;
5
8
  declare const capitalize: <const T extends string>(str: T) => Capitalize<T>;
6
9
  declare const uncapitalize: <const T extends string>(str: T) => Uncapitalize<T>;
7
- declare const isHex: (str: string) => str is `0x${string}`;
10
+ declare const isHex: (str: string) => str is HexString;
11
+ declare const abbreviate: (text: string | undefined | null, showLength?: number, space?: boolean) => string;
12
+ declare const truncateString: (string: string, numCharacters?: number, ellipsis?: boolean) => string;
8
13
 
9
- export { capitalize, isHex, split, toLowerCase, toUpperCase, uncapitalize };
14
+ export { abbreviate, capitalize, isHex, isInteger, split, toLowerCase, toUpperCase, truncateString, uncapitalize };
package/dist/string.js CHANGED
@@ -1,15 +1,33 @@
1
1
  // src/string.ts
2
2
  var toUpperCase = (str) => str.toUpperCase();
3
3
  var toLowerCase = (str) => str.toLowerCase();
4
+ var isInteger = (string) => /^\d+$/.test(string);
4
5
  var split = (str, delimiter) => str.split(delimiter);
5
6
  var capitalize = (str) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
6
7
  var uncapitalize = (str) => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;
7
8
  var isHex = (str) => /^0x[\da-f]+$/i.test(str);
9
+ var abbreviate = (text, showLength = 4, space = false) => {
10
+ if (typeof text !== "string") return "";
11
+ const leftPart = text.slice(0, showLength);
12
+ const rightPart = text.slice(text.length - showLength);
13
+ return [leftPart, rightPart].join(space ? ". . ." : "\u2026");
14
+ };
15
+ var truncateString = (string, numCharacters = 20, ellipsis = true) => {
16
+ if (string.length < numCharacters) return string;
17
+ let slicedString = string.slice(0, numCharacters);
18
+ if (ellipsis) {
19
+ slicedString += "...";
20
+ }
21
+ return slicedString;
22
+ };
8
23
  export {
24
+ abbreviate,
9
25
  capitalize,
10
26
  isHex,
27
+ isInteger,
11
28
  split,
12
29
  toLowerCase,
13
30
  toUpperCase,
31
+ truncateString,
14
32
  uncapitalize
15
33
  };