@talismn/util 0.1.7 → 0.1.9

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @talismn/util
2
2
 
3
+ ## 0.1.9
4
+
5
+ ### Patch Changes
6
+
7
+ - f7aca48b: eslint rules
8
+ - 01bf239b: feat: crowdloan and nom pool balances
9
+ - 48f0222e: fix: removed some explicit `any`s
10
+ - 01bf239b: fix: packages publishing with incorrect interdependency versions
11
+
12
+ ## 0.1.8
13
+
14
+ ### Patch Changes
15
+
16
+ - 3068bd60: feat: stale balances and exponential rpc backoff
17
+
3
18
  ## 0.1.7
4
19
 
5
20
  ### Patch Changes
@@ -4,7 +4,7 @@
4
4
  */
5
5
  export declare const BigMath: {
6
6
  abs(x: bigint): bigint;
7
- sign(x: bigint): bigint;
7
+ sign(x: bigint): 0n | 1n | -1n;
8
8
  min(value: bigint, ...values: bigint[]): bigint;
9
9
  max(value: bigint, ...values: bigint[]): bigint;
10
10
  };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * In TypeScript, a deferred promise refers to a pattern that involves creating a promise that can be
3
+ * resolved or rejected at a later point in time, typically by code outside of the current function scope.
4
+ *
5
+ * This pattern is often used when dealing with asynchronous operations that involve multiple steps or when
6
+ * the result of an operation cannot be immediately determined.
7
+ */
8
+ export declare function Deferred<T>(): {
9
+ promise: Promise<T>;
10
+ resolve: (value: T | PromiseLike<T>) => void;
11
+ reject: (reason?: unknown) => void;
12
+ isPending: () => boolean;
13
+ isResolved: () => boolean;
14
+ isRejected: () => boolean;
15
+ };
@@ -3,11 +3,13 @@ export * from "./FunctionPropertyNames";
3
3
  export * from "./blake2Concat";
4
4
  export * from "./classNames";
5
5
  export * from "./decodeAnyAddress";
6
+ export * from "./deferred";
6
7
  export * from "./encodeAnyAddress";
7
8
  export * from "./formatDecimals";
8
9
  export * from "./getBase64ImageUrl";
9
10
  export * from "./hasOwnProperty";
10
11
  export * from "./isArrayOf";
12
+ export * from "./isEthereumAddress";
11
13
  export * from "./planckToTokens";
12
14
  export * from "./sleep";
13
15
  export * from "./throwAfter";
@@ -0,0 +1 @@
1
+ export declare const isEthereumAddress: (address: string) => boolean;
@@ -17,11 +17,11 @@ var BigNumber__default = /*#__PURE__*/_interopDefault(BigNumber);
17
17
  */
18
18
  const BigMath = {
19
19
  abs(x) {
20
- return x < BigInt("0") ? -x : x;
20
+ return x < 0n ? -x : x;
21
21
  },
22
22
  sign(x) {
23
- if (x === BigInt("0")) return BigInt("0");
24
- return x < BigInt("0") ? BigInt("-1") : BigInt("1");
23
+ if (x === 0n) return 0n;
24
+ return x < 0n ? -1n : 1n;
25
25
  },
26
26
  // TODO: Improve our babel/tsc config to let us use the `**` operator on bigint values.
27
27
  // Error thrown: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. ts(2791)
@@ -57,6 +57,41 @@ function decodeAnyAddress(encoded, ignoreChecksum, ss58Format) {
57
57
  }
58
58
  }
59
59
 
60
+ /**
61
+ * In TypeScript, a deferred promise refers to a pattern that involves creating a promise that can be
62
+ * resolved or rejected at a later point in time, typically by code outside of the current function scope.
63
+ *
64
+ * This pattern is often used when dealing with asynchronous operations that involve multiple steps or when
65
+ * the result of an operation cannot be immediately determined.
66
+ */
67
+ function Deferred() {
68
+ let resolve;
69
+ let reject;
70
+ let isPending = true;
71
+ let isResolved = false;
72
+ let isRejected = false;
73
+ const promise = new Promise((innerResolve, innerReject) => {
74
+ resolve = value => {
75
+ isPending = false;
76
+ isResolved = true;
77
+ innerResolve(value);
78
+ };
79
+ reject = reason => {
80
+ isPending = false;
81
+ isRejected = true;
82
+ innerReject(reason);
83
+ };
84
+ });
85
+ return {
86
+ promise,
87
+ resolve,
88
+ reject,
89
+ isPending: () => isPending,
90
+ isResolved: () => isResolved,
91
+ isRejected: () => isRejected
92
+ };
93
+ }
94
+
60
95
  function encodeAnyAddress(key, ss58Format) {
61
96
  try {
62
97
  return keyring.encodeAddress(key, ss58Format);
@@ -134,11 +169,13 @@ function isArrayOf(array, func) {
134
169
  return false;
135
170
  }
136
171
 
172
+ const isEthereumAddress = address => address.startsWith("0x") && address.length === 42;
173
+
137
174
  function planckToTokens(planck, tokenDecimals) {
138
175
  if (typeof planck !== "string" || typeof tokenDecimals !== "number") return;
139
- const base = new BigNumber__default["default"](10);
140
- const exponent = new BigNumber__default["default"](tokenDecimals).negated();
141
- const multiplier = base.pow(exponent);
176
+ const base = 10;
177
+ const exponent = -1 * tokenDecimals;
178
+ const multiplier = base ** exponent;
142
179
  return new BigNumber__default["default"](planck).multipliedBy(multiplier).toString(10);
143
180
  }
144
181
 
@@ -148,9 +185,9 @@ const throwAfter = (ms, reason) => new Promise((_, reject) => setTimeout(() => r
148
185
 
149
186
  function tokensToPlanck(tokens, tokenDecimals) {
150
187
  if (typeof tokens !== "string" || typeof tokenDecimals !== "number") return;
151
- const base = new BigNumber__default["default"](10);
152
- const exponent = new BigNumber__default["default"](tokenDecimals);
153
- const multiplier = base.pow(exponent);
188
+ const base = 10;
189
+ const exponent = tokenDecimals;
190
+ const multiplier = base ** exponent;
154
191
  return new BigNumber__default["default"](tokens).multipliedBy(multiplier).toString(10);
155
192
  }
156
193
 
@@ -160,6 +197,7 @@ function twox64Concat(input) {
160
197
  }
161
198
 
162
199
  exports.BigMath = BigMath;
200
+ exports.Deferred = Deferred;
163
201
  exports.MAX_DECIMALS_FORMAT = MAX_DECIMALS_FORMAT;
164
202
  exports.blake2Concat = blake2Concat;
165
203
  exports.classNames = classNames;
@@ -169,6 +207,7 @@ exports.formatDecimals = formatDecimals;
169
207
  exports.getBase64ImageUrl = getBase64ImageUrl;
170
208
  exports.hasOwnProperty = hasOwnProperty;
171
209
  exports.isArrayOf = isArrayOf;
210
+ exports.isEthereumAddress = isEthereumAddress;
172
211
  exports.planckToTokens = planckToTokens;
173
212
  exports.sleep = sleep;
174
213
  exports.throwAfter = throwAfter;
@@ -17,11 +17,11 @@ var BigNumber__default = /*#__PURE__*/_interopDefault(BigNumber);
17
17
  */
18
18
  const BigMath = {
19
19
  abs(x) {
20
- return x < BigInt("0") ? -x : x;
20
+ return x < 0n ? -x : x;
21
21
  },
22
22
  sign(x) {
23
- if (x === BigInt("0")) return BigInt("0");
24
- return x < BigInt("0") ? BigInt("-1") : BigInt("1");
23
+ if (x === 0n) return 0n;
24
+ return x < 0n ? -1n : 1n;
25
25
  },
26
26
  // TODO: Improve our babel/tsc config to let us use the `**` operator on bigint values.
27
27
  // Error thrown: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. ts(2791)
@@ -57,6 +57,41 @@ function decodeAnyAddress(encoded, ignoreChecksum, ss58Format) {
57
57
  }
58
58
  }
59
59
 
60
+ /**
61
+ * In TypeScript, a deferred promise refers to a pattern that involves creating a promise that can be
62
+ * resolved or rejected at a later point in time, typically by code outside of the current function scope.
63
+ *
64
+ * This pattern is often used when dealing with asynchronous operations that involve multiple steps or when
65
+ * the result of an operation cannot be immediately determined.
66
+ */
67
+ function Deferred() {
68
+ let resolve;
69
+ let reject;
70
+ let isPending = true;
71
+ let isResolved = false;
72
+ let isRejected = false;
73
+ const promise = new Promise((innerResolve, innerReject) => {
74
+ resolve = value => {
75
+ isPending = false;
76
+ isResolved = true;
77
+ innerResolve(value);
78
+ };
79
+ reject = reason => {
80
+ isPending = false;
81
+ isRejected = true;
82
+ innerReject(reason);
83
+ };
84
+ });
85
+ return {
86
+ promise,
87
+ resolve,
88
+ reject,
89
+ isPending: () => isPending,
90
+ isResolved: () => isResolved,
91
+ isRejected: () => isRejected
92
+ };
93
+ }
94
+
60
95
  function encodeAnyAddress(key, ss58Format) {
61
96
  try {
62
97
  return keyring.encodeAddress(key, ss58Format);
@@ -134,11 +169,13 @@ function isArrayOf(array, func) {
134
169
  return false;
135
170
  }
136
171
 
172
+ const isEthereumAddress = address => address.startsWith("0x") && address.length === 42;
173
+
137
174
  function planckToTokens(planck, tokenDecimals) {
138
175
  if (typeof planck !== "string" || typeof tokenDecimals !== "number") return;
139
- const base = new BigNumber__default["default"](10);
140
- const exponent = new BigNumber__default["default"](tokenDecimals).negated();
141
- const multiplier = base.pow(exponent);
176
+ const base = 10;
177
+ const exponent = -1 * tokenDecimals;
178
+ const multiplier = base ** exponent;
142
179
  return new BigNumber__default["default"](planck).multipliedBy(multiplier).toString(10);
143
180
  }
144
181
 
@@ -148,9 +185,9 @@ const throwAfter = (ms, reason) => new Promise((_, reject) => setTimeout(() => r
148
185
 
149
186
  function tokensToPlanck(tokens, tokenDecimals) {
150
187
  if (typeof tokens !== "string" || typeof tokenDecimals !== "number") return;
151
- const base = new BigNumber__default["default"](10);
152
- const exponent = new BigNumber__default["default"](tokenDecimals);
153
- const multiplier = base.pow(exponent);
188
+ const base = 10;
189
+ const exponent = tokenDecimals;
190
+ const multiplier = base ** exponent;
154
191
  return new BigNumber__default["default"](tokens).multipliedBy(multiplier).toString(10);
155
192
  }
156
193
 
@@ -160,6 +197,7 @@ function twox64Concat(input) {
160
197
  }
161
198
 
162
199
  exports.BigMath = BigMath;
200
+ exports.Deferred = Deferred;
163
201
  exports.MAX_DECIMALS_FORMAT = MAX_DECIMALS_FORMAT;
164
202
  exports.blake2Concat = blake2Concat;
165
203
  exports.classNames = classNames;
@@ -169,6 +207,7 @@ exports.formatDecimals = formatDecimals;
169
207
  exports.getBase64ImageUrl = getBase64ImageUrl;
170
208
  exports.hasOwnProperty = hasOwnProperty;
171
209
  exports.isArrayOf = isArrayOf;
210
+ exports.isEthereumAddress = isEthereumAddress;
172
211
  exports.planckToTokens = planckToTokens;
173
212
  exports.sleep = sleep;
174
213
  exports.throwAfter = throwAfter;
@@ -1,5 +1,5 @@
1
1
  import { u8aToHex, u8aConcat, u8aToU8a, hexToU8a } from '@polkadot/util';
2
- import { blake2AsU8a, isEthereumAddress, ethereumEncode, xxhashAsU8a } from '@polkadot/util-crypto';
2
+ import { blake2AsU8a, isEthereumAddress as isEthereumAddress$1, ethereumEncode, xxhashAsU8a } from '@polkadot/util-crypto';
3
3
  import { decodeAddress, encodeAddress } from '@polkadot/keyring';
4
4
  import BigNumber from 'bignumber.js';
5
5
 
@@ -9,11 +9,11 @@ import BigNumber from 'bignumber.js';
9
9
  */
10
10
  const BigMath = {
11
11
  abs(x) {
12
- return x < BigInt("0") ? -x : x;
12
+ return x < 0n ? -x : x;
13
13
  },
14
14
  sign(x) {
15
- if (x === BigInt("0")) return BigInt("0");
16
- return x < BigInt("0") ? BigInt("-1") : BigInt("1");
15
+ if (x === 0n) return 0n;
16
+ return x < 0n ? -1n : 1n;
17
17
  },
18
18
  // TODO: Improve our babel/tsc config to let us use the `**` operator on bigint values.
19
19
  // Error thrown: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. ts(2791)
@@ -44,17 +44,52 @@ function decodeAnyAddress(encoded, ignoreChecksum, ss58Format) {
44
44
  return decodeAddress(encoded, ignoreChecksum, ss58Format);
45
45
  } catch (error) {
46
46
  if (typeof encoded !== "string") throw error;
47
- if (!isEthereumAddress(encoded)) throw error;
47
+ if (!isEthereumAddress$1(encoded)) throw error;
48
48
  return hexToU8a(encoded.slice("0x".length));
49
49
  }
50
50
  }
51
51
 
52
+ /**
53
+ * In TypeScript, a deferred promise refers to a pattern that involves creating a promise that can be
54
+ * resolved or rejected at a later point in time, typically by code outside of the current function scope.
55
+ *
56
+ * This pattern is often used when dealing with asynchronous operations that involve multiple steps or when
57
+ * the result of an operation cannot be immediately determined.
58
+ */
59
+ function Deferred() {
60
+ let resolve;
61
+ let reject;
62
+ let isPending = true;
63
+ let isResolved = false;
64
+ let isRejected = false;
65
+ const promise = new Promise((innerResolve, innerReject) => {
66
+ resolve = value => {
67
+ isPending = false;
68
+ isResolved = true;
69
+ innerResolve(value);
70
+ };
71
+ reject = reason => {
72
+ isPending = false;
73
+ isRejected = true;
74
+ innerReject(reason);
75
+ };
76
+ });
77
+ return {
78
+ promise,
79
+ resolve,
80
+ reject,
81
+ isPending: () => isPending,
82
+ isResolved: () => isResolved,
83
+ isRejected: () => isRejected
84
+ };
85
+ }
86
+
52
87
  function encodeAnyAddress(key, ss58Format) {
53
88
  try {
54
89
  return encodeAddress(key, ss58Format);
55
90
  } catch (error) {
56
91
  if (typeof key !== "string") throw error;
57
- if (!isEthereumAddress(key)) throw error;
92
+ if (!isEthereumAddress$1(key)) throw error;
58
93
  return ethereumEncode(key);
59
94
  }
60
95
  }
@@ -126,11 +161,13 @@ function isArrayOf(array, func) {
126
161
  return false;
127
162
  }
128
163
 
164
+ const isEthereumAddress = address => address.startsWith("0x") && address.length === 42;
165
+
129
166
  function planckToTokens(planck, tokenDecimals) {
130
167
  if (typeof planck !== "string" || typeof tokenDecimals !== "number") return;
131
- const base = new BigNumber(10);
132
- const exponent = new BigNumber(tokenDecimals).negated();
133
- const multiplier = base.pow(exponent);
168
+ const base = 10;
169
+ const exponent = -1 * tokenDecimals;
170
+ const multiplier = base ** exponent;
134
171
  return new BigNumber(planck).multipliedBy(multiplier).toString(10);
135
172
  }
136
173
 
@@ -140,9 +177,9 @@ const throwAfter = (ms, reason) => new Promise((_, reject) => setTimeout(() => r
140
177
 
141
178
  function tokensToPlanck(tokens, tokenDecimals) {
142
179
  if (typeof tokens !== "string" || typeof tokenDecimals !== "number") return;
143
- const base = new BigNumber(10);
144
- const exponent = new BigNumber(tokenDecimals);
145
- const multiplier = base.pow(exponent);
180
+ const base = 10;
181
+ const exponent = tokenDecimals;
182
+ const multiplier = base ** exponent;
146
183
  return new BigNumber(tokens).multipliedBy(multiplier).toString(10);
147
184
  }
148
185
 
@@ -151,4 +188,4 @@ function twox64Concat(input) {
151
188
  return u8aToHex(u8aConcat(xxhashAsU8a(input, bitLength), u8aToU8a(input)));
152
189
  }
153
190
 
154
- export { BigMath, MAX_DECIMALS_FORMAT, blake2Concat, classNames, decodeAnyAddress, encodeAnyAddress, formatDecimals, getBase64ImageUrl, hasOwnProperty, isArrayOf, planckToTokens, sleep, throwAfter, tokensToPlanck, twox64Concat };
191
+ export { BigMath, Deferred, MAX_DECIMALS_FORMAT, blake2Concat, classNames, decodeAnyAddress, encodeAnyAddress, formatDecimals, getBase64ImageUrl, hasOwnProperty, isArrayOf, isEthereumAddress, planckToTokens, sleep, throwAfter, tokensToPlanck, twox64Concat };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/util",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "UNLICENSED",
@@ -18,22 +18,22 @@
18
18
  "/dist"
19
19
  ],
20
20
  "engines": {
21
- "node": ">=14"
21
+ "node": ">=18"
22
22
  },
23
23
  "scripts": {
24
24
  "test": "jest",
25
- "lint": "eslint . --max-warnings 0",
25
+ "lint": "eslint src --max-warnings 0",
26
26
  "clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
27
27
  },
28
28
  "dependencies": {
29
- "bignumber.js": "^9.1.0"
29
+ "bignumber.js": "^9.1.1"
30
30
  },
31
31
  "devDependencies": {
32
- "@polkadot/keyring": "^10.1.7",
33
- "@polkadot/util": "^10.1.7",
34
- "@polkadot/util-crypto": "^10.1.7",
35
- "@talismn/eslint-config": "^0.0.1",
36
- "@talismn/tsconfig": "^0.0.2",
32
+ "@polkadot/keyring": "^12.2.1",
33
+ "@polkadot/util": "^12.2.1",
34
+ "@polkadot/util-crypto": "^12.2.1",
35
+ "@talismn/eslint-config": "0.0.2",
36
+ "@talismn/tsconfig": "0.0.2",
37
37
  "@types/jest": "^27.5.1",
38
38
  "eslint": "^8.4.0",
39
39
  "jest": "^28.1.0",
@@ -41,9 +41,9 @@
41
41
  "typescript": "^4.6.4"
42
42
  },
43
43
  "peerDependencies": {
44
- "@polkadot/keyring": "10.x",
45
- "@polkadot/util": "10.x",
46
- "@polkadot/util-crypto": "^10.x"
44
+ "@polkadot/keyring": "12.x",
45
+ "@polkadot/util": "12.x",
46
+ "@polkadot/util-crypto": "12.x"
47
47
  },
48
48
  "eslintConfig": {
49
49
  "root": true,