@chainflip/utils 0.3.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 (46) 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 +32 -25
  35. package/dist/ss58.d.cts +2 -1
  36. package/dist/ss58.d.ts +2 -1
  37. package/dist/ss58.js +14 -11
  38. package/dist/string.cjs +21 -0
  39. package/dist/string.d.cts +7 -2
  40. package/dist/string.d.ts +7 -2
  41. package/dist/string.js +18 -0
  42. package/dist/tickMath.cjs +1502 -0
  43. package/dist/tickMath.d.cts +15 -0
  44. package/dist/tickMath.d.ts +15 -0
  45. package/dist/tickMath.js +35 -0
  46. 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
@@ -21,28 +21,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var ss58_exports = {};
22
22
  __export(ss58_exports, {
23
23
  decode: () => decode2,
24
- encode: () => encode2
24
+ encode: () => encode2,
25
+ toPublicKey: () => toPublicKey
25
26
  });
26
27
  module.exports = __toCommonJS(ss58_exports);
27
28
 
28
- // src/guard.ts
29
- var createIsGuard = (type) => (value) => typeof value === type;
30
- var isString = createIsGuard("string");
31
- var isNumber = createIsGuard("number");
32
- var isBigInt = createIsGuard("bigint");
33
- var isBoolean = createIsGuard("boolean");
34
- var isSymbol = createIsGuard("symbol");
35
- var isObject = createIsGuard("object");
36
- var isUndefined = createIsGuard("undefined");
37
-
38
- // src/assertion.ts
39
- function assert(condition, message = "assertion failed", Constructor = Error) {
40
- if (!condition) {
41
- throw new Constructor(message);
42
- }
43
- }
44
-
45
- // ../../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
46
30
  function number(n) {
47
31
  if (!Number.isSafeInteger(n) || n < 0)
48
32
  throw new Error(`positive integer expected, not ${n}`);
@@ -70,7 +54,7 @@ function output(out, instance) {
70
54
  }
71
55
  }
72
56
 
73
- // ../../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
74
58
  var u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
75
59
  var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
76
60
  var byteSwap = (word) => word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
@@ -107,7 +91,7 @@ function wrapConstructorWithOpts(hashCons) {
107
91
  return hashC;
108
92
  }
109
93
 
110
- // ../../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
111
95
  var SIGMA = /* @__PURE__ */ new Uint8Array([
112
96
  0,
113
97
  1,
@@ -396,7 +380,7 @@ var BLAKE = class extends Hash {
396
380
  }
397
381
  };
398
382
 
399
- // ../../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
400
384
  var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
401
385
  var _32n = /* @__PURE__ */ BigInt(32);
402
386
  function fromBig(n, le = false) {
@@ -462,7 +446,7 @@ var u64 = {
462
446
  };
463
447
  var u64_default = u64;
464
448
 
465
- // ../../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
466
450
  var B2B_IV = /* @__PURE__ */ new Uint32Array([
467
451
  4089235720,
468
452
  1779033703,
@@ -641,7 +625,28 @@ var BLAKE2b = class extends BLAKE {
641
625
  };
642
626
  var blake2b = /* @__PURE__ */ wrapConstructorWithOpts((opts) => new BLAKE2b(opts));
643
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
+
644
645
  // src/bytes.ts
646
+ var bytesToHex = (input) => {
647
+ const bytes2 = new Uint8Array(input);
648
+ return `0x${Array.from(bytes2).map((byte) => byte.toString(16).padStart(2, "0")).join("")}`;
649
+ };
645
650
  var hexToBytes = (input) => {
646
651
  assert(/^0x[\da-f]*$/i.test(input) && input.length % 2 === 0, "Invalid hex string");
647
652
  const hex = input.slice(2);
@@ -705,7 +710,7 @@ var decode2 = (input) => {
705
710
  ss58Format = (decodedBytes[0] & 63) << 2 | decodedBytes[1] >> 6 | (decodedBytes[1] & 63) << 8;
706
711
  } else {
707
712
  ss58FormatLen = 1;
708
- ss58Format = decodedBytes[0];
713
+ [ss58Format] = decodedBytes;
709
714
  }
710
715
  assert(!RESERVED_PREFIXES.includes(ss58Format), `Reserved prefix: ${ss58Format}`);
711
716
  const checksumBytes = decodedBytes.slice(-CHECKSUM_BYTE_LENGTH);
@@ -745,10 +750,12 @@ var encode2 = ({
745
750
  const checksum = computeChecksum(new Uint8Array([...prefixBytes, ...data]));
746
751
  return encode([...prefixBytes, ...data, ...checksum]);
747
752
  };
753
+ var toPublicKey = (address) => bytesToHex(decode2(address).data);
748
754
  // Annotate the CommonJS export names for ESM import in node:
749
755
  0 && (module.exports = {
750
756
  decode,
751
- encode
757
+ encode,
758
+ toPublicKey
752
759
  });
753
760
  /*! Bundled license information:
754
761
 
package/dist/ss58.d.cts CHANGED
@@ -8,5 +8,6 @@ declare const encode: ({ data: input, ss58Format, }: {
8
8
  data: Uint8Array | HexString;
9
9
  ss58Format: number;
10
10
  }) => string;
11
+ declare const toPublicKey: (address: string) => HexString;
11
12
 
12
- export { decode, encode };
13
+ export { decode, encode, toPublicKey };
package/dist/ss58.d.ts CHANGED
@@ -8,5 +8,6 @@ declare const encode: ({ data: input, ss58Format, }: {
8
8
  data: Uint8Array | HexString;
9
9
  ss58Format: number;
10
10
  }) => string;
11
+ declare const toPublicKey: (address: string) => HexString;
11
12
 
12
- export { decode, encode };
13
+ export { decode, encode, toPublicKey };
package/dist/ss58.js CHANGED
@@ -1,16 +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
+ bytesToHex,
6
7
  hexToBytes
7
- } from "./chunk-D7RIA7SA.js";
8
+ } from "./chunk-2KA626OL.js";
8
9
  import {
9
10
  assert
10
- } from "./chunk-MYP3UYWE.js";
11
- import "./chunk-CZNX6EUV.js";
11
+ } from "./chunk-ZHIKNZLU.js";
12
+ import "./chunk-HBIFE4XN.js";
12
13
 
13
- // ../../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
14
15
  function number(n) {
15
16
  if (!Number.isSafeInteger(n) || n < 0)
16
17
  throw new Error(`positive integer expected, not ${n}`);
@@ -38,7 +39,7 @@ function output(out, instance) {
38
39
  }
39
40
  }
40
41
 
41
- // ../../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
42
43
  var u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
43
44
  var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
44
45
  var byteSwap = (word) => word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
@@ -75,7 +76,7 @@ function wrapConstructorWithOpts(hashCons) {
75
76
  return hashC;
76
77
  }
77
78
 
78
- // ../../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
79
80
  var SIGMA = /* @__PURE__ */ new Uint8Array([
80
81
  0,
81
82
  1,
@@ -364,7 +365,7 @@ var BLAKE = class extends Hash {
364
365
  }
365
366
  };
366
367
 
367
- // ../../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
368
369
  var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
369
370
  var _32n = /* @__PURE__ */ BigInt(32);
370
371
  function fromBig(n, le = false) {
@@ -430,7 +431,7 @@ var u64 = {
430
431
  };
431
432
  var u64_default = u64;
432
433
 
433
- // ../../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
434
435
  var B2B_IV = /* @__PURE__ */ new Uint32Array([
435
436
  4089235720,
436
437
  1779033703,
@@ -629,7 +630,7 @@ var decode2 = (input) => {
629
630
  ss58Format = (decodedBytes[0] & 63) << 2 | decodedBytes[1] >> 6 | (decodedBytes[1] & 63) << 8;
630
631
  } else {
631
632
  ss58FormatLen = 1;
632
- ss58Format = decodedBytes[0];
633
+ [ss58Format] = decodedBytes;
633
634
  }
634
635
  assert(!RESERVED_PREFIXES.includes(ss58Format), `Reserved prefix: ${ss58Format}`);
635
636
  const checksumBytes = decodedBytes.slice(-CHECKSUM_BYTE_LENGTH);
@@ -669,9 +670,11 @@ var encode2 = ({
669
670
  const checksum = computeChecksum(new Uint8Array([...prefixBytes, ...data]));
670
671
  return encode([...prefixBytes, ...data, ...checksum]);
671
672
  };
673
+ var toPublicKey = (address) => bytesToHex(decode2(address).data);
672
674
  export {
673
675
  decode2 as decode,
674
- encode2 as encode
676
+ encode2 as encode,
677
+ toPublicKey
675
678
  };
676
679
  /*! Bundled license information:
677
680
 
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
  };