@injectivelabs/utils 1.16.25-alpha.1 → 1.16.25
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/dist/cjs/classes/BigNumber.d.ts +14 -0
- package/dist/cjs/classes/BigNumber.js +33 -0
- package/dist/cjs/classes/BigNumberInBase.d.ts +16 -0
- package/dist/cjs/classes/BigNumberInBase.js +39 -0
- package/dist/cjs/classes/BigNumberInWei.d.ts +16 -0
- package/dist/cjs/classes/BigNumberInWei.js +39 -0
- package/dist/cjs/classes/HttpClient.d.ts +11 -0
- package/dist/cjs/classes/HttpClient.js +39 -0
- package/dist/cjs/classes/HttpRestClient.d.ts +14 -0
- package/dist/cjs/classes/HttpRestClient.js +110 -0
- package/dist/cjs/classes/LocalStorage.d.ts +9 -0
- package/dist/cjs/classes/LocalStorage.js +25 -0
- package/dist/cjs/classes/Status.d.ts +22 -0
- package/dist/cjs/classes/Status.js +61 -0
- package/dist/cjs/classes/StreamManager.d.ts +9 -0
- package/dist/cjs/classes/StreamManager.js +41 -0
- package/dist/cjs/classes/index.d.ts +10 -0
- package/dist/cjs/classes/index.js +60 -0
- package/dist/cjs/constants.d.ts +37 -0
- package/dist/cjs/constants.js +54 -0
- package/dist/cjs/enum.d.ts +8 -0
- package/dist/cjs/enum.js +10 -0
- package/dist/cjs/formatters.d.ts +5 -0
- package/dist/cjs/formatters.js +31 -0
- package/dist/cjs/helpers.d.ts +73 -0
- package/dist/cjs/helpers.js +145 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/numbers.d.ts +4 -0
- package/dist/cjs/numbers.js +37 -0
- package/dist/cjs/package.json +2 -2
- package/dist/cjs/test-utils/index.d.ts +2 -0
- package/dist/cjs/test-utils/index.js +18 -0
- package/dist/cjs/test-utils/mocks/index.d.ts +153 -0
- package/dist/cjs/test-utils/mocks/index.js +106 -0
- package/dist/cjs/test-utils/msgs.d.ts +44 -0
- package/dist/cjs/test-utils/msgs.js +54 -0
- package/dist/cjs/time.d.ts +9 -0
- package/dist/cjs/time.js +41 -0
- package/dist/cjs/types.d.ts +3 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/classes/BigNumber.d.ts +14 -0
- package/dist/esm/classes/BigNumber.js +27 -0
- package/dist/esm/classes/BigNumberInBase.d.ts +16 -0
- package/dist/esm/classes/BigNumberInBase.js +33 -0
- package/dist/esm/classes/BigNumberInWei.d.ts +16 -0
- package/dist/esm/classes/BigNumberInWei.js +33 -0
- package/dist/esm/classes/HttpClient.d.ts +11 -0
- package/dist/esm/classes/HttpClient.js +33 -0
- package/dist/esm/classes/HttpRestClient.d.ts +14 -0
- package/dist/esm/classes/HttpRestClient.js +104 -0
- package/dist/esm/classes/LocalStorage.d.ts +9 -0
- package/dist/esm/classes/LocalStorage.js +22 -0
- package/dist/esm/classes/Status.d.ts +22 -0
- package/dist/esm/classes/Status.js +58 -0
- package/dist/esm/classes/StreamManager.d.ts +9 -0
- package/dist/esm/classes/StreamManager.js +38 -0
- package/dist/esm/classes/index.d.ts +10 -0
- package/dist/esm/classes/index.js +10 -0
- package/dist/esm/constants.d.ts +37 -0
- package/dist/esm/constants.js +46 -0
- package/dist/esm/enum.d.ts +8 -0
- package/dist/esm/enum.js +7 -0
- package/dist/esm/formatters.d.ts +5 -0
- package/dist/esm/formatters.js +24 -0
- package/dist/esm/helpers.d.ts +73 -0
- package/dist/esm/helpers.js +100 -0
- package/dist/esm/index.d.ts +6 -279
- package/dist/esm/index.js +6 -509
- package/dist/esm/numbers.d.ts +4 -0
- package/dist/esm/numbers.js +28 -0
- package/dist/esm/package.json +2 -2
- package/dist/esm/test-utils/index.d.ts +2 -218
- package/dist/esm/test-utils/index.js +2 -157
- package/dist/esm/test-utils/mocks/index.d.ts +153 -0
- package/dist/esm/test-utils/mocks/index.js +103 -0
- package/dist/esm/test-utils/msgs.d.ts +44 -0
- package/dist/esm/test-utils/msgs.js +47 -0
- package/dist/esm/time.d.ts +9 -0
- package/dist/esm/time.js +27 -0
- package/dist/esm/types.d.ts +3 -0
- package/dist/esm/types.js +1 -0
- package/package.json +16 -16
- package/dist/cjs/constants-DEbTiKzu.cjs +0 -192
- package/dist/cjs/index.cjs +0 -586
- package/dist/cjs/index.d.cts +0 -279
- package/dist/cjs/test-utils/index.cjs +0 -161
- package/dist/cjs/test-utils/index.d.cts +0 -218
- package/dist/esm/constants-BxcSsKjm.js +0 -66
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { StatusType } from '../enum.js';
|
|
2
|
+
export default class Status {
|
|
3
|
+
status;
|
|
4
|
+
constructor(status = StatusType.Idle) {
|
|
5
|
+
this.status = status;
|
|
6
|
+
}
|
|
7
|
+
get() {
|
|
8
|
+
return this.status;
|
|
9
|
+
}
|
|
10
|
+
set(status) {
|
|
11
|
+
this.status = status;
|
|
12
|
+
}
|
|
13
|
+
is(status) {
|
|
14
|
+
return this.status === status;
|
|
15
|
+
}
|
|
16
|
+
isLoading() {
|
|
17
|
+
return this.is(StatusType.Loading);
|
|
18
|
+
}
|
|
19
|
+
isNotLoading() {
|
|
20
|
+
return !this.is(StatusType.Loading);
|
|
21
|
+
}
|
|
22
|
+
isCompleted() {
|
|
23
|
+
return this.is(StatusType.Completed);
|
|
24
|
+
}
|
|
25
|
+
isConfirmed() {
|
|
26
|
+
return this.is(StatusType.Confirmed);
|
|
27
|
+
}
|
|
28
|
+
isIdle() {
|
|
29
|
+
return this.is(StatusType.Idle);
|
|
30
|
+
}
|
|
31
|
+
isError() {
|
|
32
|
+
return this.is(StatusType.Error);
|
|
33
|
+
}
|
|
34
|
+
setLoading() {
|
|
35
|
+
this.set(StatusType.Loading);
|
|
36
|
+
}
|
|
37
|
+
setCompleted() {
|
|
38
|
+
this.set(StatusType.Completed);
|
|
39
|
+
}
|
|
40
|
+
setConfirmed() {
|
|
41
|
+
this.set(StatusType.Confirmed);
|
|
42
|
+
}
|
|
43
|
+
setError() {
|
|
44
|
+
this.set(StatusType.Error);
|
|
45
|
+
}
|
|
46
|
+
setIdle() {
|
|
47
|
+
this.set(StatusType.Idle);
|
|
48
|
+
}
|
|
49
|
+
toggle() {
|
|
50
|
+
this.set(this.status === StatusType.Idle ? StatusType.Loading : StatusType.Idle);
|
|
51
|
+
}
|
|
52
|
+
toString() {
|
|
53
|
+
return this.get();
|
|
54
|
+
}
|
|
55
|
+
valueOf() {
|
|
56
|
+
return this.get();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export default class StreamManager {
|
|
2
|
+
streams = new Map();
|
|
3
|
+
set(stream, streamKey) {
|
|
4
|
+
if (this.streams.has(streamKey)) {
|
|
5
|
+
throw new Error(`Stream ${streamKey} already exists`);
|
|
6
|
+
}
|
|
7
|
+
this.streams.set(streamKey, stream);
|
|
8
|
+
}
|
|
9
|
+
get(streamKey) {
|
|
10
|
+
if (!this.streams.has(streamKey)) {
|
|
11
|
+
throw new Error(`Stream ${streamKey} is not found`);
|
|
12
|
+
}
|
|
13
|
+
return this.streams.get(streamKey);
|
|
14
|
+
}
|
|
15
|
+
exists(streamKey) {
|
|
16
|
+
return this.streams.has(streamKey);
|
|
17
|
+
}
|
|
18
|
+
cancelAll() {
|
|
19
|
+
this.streams.forEach((stream) => {
|
|
20
|
+
stream.cancel();
|
|
21
|
+
});
|
|
22
|
+
this.streams = new Map();
|
|
23
|
+
}
|
|
24
|
+
cancel(streamKey) {
|
|
25
|
+
if (!this.streams.has(streamKey)) {
|
|
26
|
+
throw new Error(`Stream ${streamKey} is not found`);
|
|
27
|
+
}
|
|
28
|
+
this.streams.get(streamKey).cancel();
|
|
29
|
+
this.streams.delete(streamKey);
|
|
30
|
+
}
|
|
31
|
+
cancelIfExists(streamKey) {
|
|
32
|
+
if (!this.streams.has(streamKey)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
this.streams.get(streamKey).cancel();
|
|
36
|
+
this.streams.delete(streamKey);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import Status from './Status.js';
|
|
2
|
+
import { StatusType } from '../enum.js';
|
|
3
|
+
import HttpClient from './HttpClient.js';
|
|
4
|
+
import LocalStorage from './LocalStorage.js';
|
|
5
|
+
import StreamManager from './StreamManager.js';
|
|
6
|
+
import BigNumberInWei from './BigNumberInWei.js';
|
|
7
|
+
import HttpRestClient from './HttpRestClient.js';
|
|
8
|
+
import BigNumberInBase from './BigNumberInBase.js';
|
|
9
|
+
import { toBigNumber, toChainFormat, toHumanReadable, default as BigNumber } from './BigNumber.js';
|
|
10
|
+
export { Status, BigNumber, HttpClient, StatusType, toBigNumber, LocalStorage, StreamManager, toChainFormat, BigNumberInWei, HttpRestClient, toHumanReadable, BigNumberInBase, };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import Status from './Status.js';
|
|
2
|
+
import { StatusType } from '../enum.js';
|
|
3
|
+
import HttpClient from './HttpClient.js';
|
|
4
|
+
import LocalStorage from './LocalStorage.js';
|
|
5
|
+
import StreamManager from './StreamManager.js';
|
|
6
|
+
import BigNumberInWei from './BigNumberInWei.js';
|
|
7
|
+
import HttpRestClient from './HttpRestClient.js';
|
|
8
|
+
import BigNumberInBase from './BigNumberInBase.js';
|
|
9
|
+
import { toBigNumber, toChainFormat, toHumanReadable, default as BigNumber, } from './BigNumber.js';
|
|
10
|
+
export { Status, BigNumber, HttpClient, StatusType, toBigNumber, LocalStorage, StreamManager, toChainFormat, BigNumberInWei, HttpRestClient, toHumanReadable, BigNumberInBase, };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
2
|
+
export declare const INJ_DENOM = "inj";
|
|
3
|
+
export declare const INJECTIVE_DENOM = "inj";
|
|
4
|
+
export declare const DEFAULT_FEE_DENOM = "inj";
|
|
5
|
+
export declare const DEFAULT_GAS_LIMIT = 400000;
|
|
6
|
+
export declare const DEFAULT_IBC_GAS_LIMIT = 300000;
|
|
7
|
+
export declare const DEFAULT_GAS_PRICE = 160000000;
|
|
8
|
+
export declare const DEFAULT_EXCHANGE_LIMIT = 200000;
|
|
9
|
+
export declare const DEFAULT_BRIDGE_FEE_DENOM = "inj";
|
|
10
|
+
export declare const DEFAULT_BRIDGE_FEE_PRICE = "160000000";
|
|
11
|
+
export declare const DEFAULT_BRIDGE_FEE_AMOUNT = "200000000000000";
|
|
12
|
+
export declare const DEFAULT_BLOCK_TIMEOUT_HEIGHT = 120;
|
|
13
|
+
export declare const DEFAULT_BLOCK_TIME_IN_SECONDS = 0.7;
|
|
14
|
+
/**
|
|
15
|
+
* Default timeout for transaction block inclusion polling.
|
|
16
|
+
*
|
|
17
|
+
* Calculation: 120 blocks × 0.7 seconds/block = 84 seconds = 84,000ms
|
|
18
|
+
*/
|
|
19
|
+
export declare const DEFAULT_TX_BLOCK_INCLUSION_TIMEOUT_IN_MS: number;
|
|
20
|
+
export declare const DEFAULT_TIMESTAMP_TIMEOUT_MS: number;
|
|
21
|
+
export declare const getDefaultStdFee: () => {
|
|
22
|
+
amount: {
|
|
23
|
+
amount: string;
|
|
24
|
+
denom: string;
|
|
25
|
+
}[];
|
|
26
|
+
gas: string;
|
|
27
|
+
payer: string;
|
|
28
|
+
granter: string;
|
|
29
|
+
feePayer: string;
|
|
30
|
+
};
|
|
31
|
+
export declare const DEFAULT_STD_FEE_BY_DENOM: (denom?: string) => {
|
|
32
|
+
amount: {
|
|
33
|
+
denom: string;
|
|
34
|
+
amount: string;
|
|
35
|
+
}[];
|
|
36
|
+
gas: string;
|
|
37
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import BigNumber from './classes/BigNumber.js';
|
|
2
|
+
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
3
|
+
export const INJ_DENOM = 'inj';
|
|
4
|
+
export const INJECTIVE_DENOM = 'inj';
|
|
5
|
+
export const DEFAULT_FEE_DENOM = 'inj';
|
|
6
|
+
export const DEFAULT_GAS_LIMIT = 400000;
|
|
7
|
+
export const DEFAULT_IBC_GAS_LIMIT = 300000;
|
|
8
|
+
export const DEFAULT_GAS_PRICE = 160000000;
|
|
9
|
+
export const DEFAULT_EXCHANGE_LIMIT = 200000;
|
|
10
|
+
export const DEFAULT_BRIDGE_FEE_DENOM = 'inj';
|
|
11
|
+
export const DEFAULT_BRIDGE_FEE_PRICE = '160000000';
|
|
12
|
+
export const DEFAULT_BRIDGE_FEE_AMOUNT = '200000000000000';
|
|
13
|
+
export const DEFAULT_BLOCK_TIMEOUT_HEIGHT = 120;
|
|
14
|
+
export const DEFAULT_BLOCK_TIME_IN_SECONDS = 0.7;
|
|
15
|
+
/**
|
|
16
|
+
* Default timeout for transaction block inclusion polling.
|
|
17
|
+
*
|
|
18
|
+
* Calculation: 120 blocks × 0.7 seconds/block = 84 seconds = 84,000ms
|
|
19
|
+
*/
|
|
20
|
+
export const DEFAULT_TX_BLOCK_INCLUSION_TIMEOUT_IN_MS = Math.floor(DEFAULT_BLOCK_TIMEOUT_HEIGHT * DEFAULT_BLOCK_TIME_IN_SECONDS * 1000);
|
|
21
|
+
export const DEFAULT_TIMESTAMP_TIMEOUT_MS = 60 * 1000 * 3;
|
|
22
|
+
export const getDefaultStdFee = () => ({
|
|
23
|
+
amount: [
|
|
24
|
+
{
|
|
25
|
+
amount: new BigNumber(DEFAULT_GAS_LIMIT)
|
|
26
|
+
.times(DEFAULT_GAS_PRICE)
|
|
27
|
+
.toString(),
|
|
28
|
+
denom: 'inj',
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
gas: DEFAULT_GAS_LIMIT.toString(),
|
|
32
|
+
payer: '',
|
|
33
|
+
granter: '',
|
|
34
|
+
feePayer: '',
|
|
35
|
+
});
|
|
36
|
+
export const DEFAULT_STD_FEE_BY_DENOM = (denom = 'inj') => ({
|
|
37
|
+
amount: [
|
|
38
|
+
{
|
|
39
|
+
denom,
|
|
40
|
+
amount: new BigNumber(DEFAULT_GAS_LIMIT)
|
|
41
|
+
.times(DEFAULT_GAS_PRICE)
|
|
42
|
+
.toString(),
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
gas: DEFAULT_GAS_LIMIT.toString(),
|
|
46
|
+
});
|
package/dist/esm/enum.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AccountAddress } from '@injectivelabs/ts-types';
|
|
2
|
+
export declare const formatWalletAddress: (address: AccountAddress, substrLength?: number) => string;
|
|
3
|
+
export declare const toPascalCase: (str: string) => string;
|
|
4
|
+
export declare const snakeToPascal: (str: string) => string;
|
|
5
|
+
export declare const capitalize: (str: string) => string;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const formatWalletAddress = (address, substrLength = 6) => {
|
|
2
|
+
if (address.length <= 10) {
|
|
3
|
+
return address;
|
|
4
|
+
}
|
|
5
|
+
return `${address.slice(0, substrLength)}...${address.slice(address.length - substrLength, address.length)}`;
|
|
6
|
+
};
|
|
7
|
+
export const toPascalCase = (str) => {
|
|
8
|
+
return `${str}`
|
|
9
|
+
.toLowerCase()
|
|
10
|
+
.replace(new RegExp(/[-_]+/, 'g'), ' ')
|
|
11
|
+
.replace(new RegExp(/[^\w\s]/, 'g'), '')
|
|
12
|
+
.replace(new RegExp(/\s+(.)(\w*)/, 'g'), (_$1, $2, $3) => `${$2.toUpperCase() + $3}`)
|
|
13
|
+
.replace(new RegExp(/\w/), (s) => s.toUpperCase());
|
|
14
|
+
};
|
|
15
|
+
export const snakeToPascal = (str) => {
|
|
16
|
+
return str
|
|
17
|
+
.split('/')
|
|
18
|
+
.map((snake) => snake
|
|
19
|
+
.split('_')
|
|
20
|
+
.map((substr) => substr.charAt(0).toUpperCase() + substr.slice(1))
|
|
21
|
+
.join(''))
|
|
22
|
+
.join('/');
|
|
23
|
+
};
|
|
24
|
+
export const capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { Awaited } from './types.js';
|
|
2
|
+
export declare const sleep: (timeout: number) => Promise<void>;
|
|
3
|
+
/**
|
|
4
|
+
* When we want to execute the promises in batch
|
|
5
|
+
*/
|
|
6
|
+
export declare const awaitAll: <T, S>(array: Array<T>, callback: (item: T) => Promise<S>) => Promise<Awaited<S>[]>;
|
|
7
|
+
/**
|
|
8
|
+
* When we want to execute the promises one by one
|
|
9
|
+
* and not all in batch as with await Promise.all()
|
|
10
|
+
*/
|
|
11
|
+
export declare const awaitForAll: <T, S>(array: Array<T>, callback: (item: T) => Promise<S>) => Promise<S[]>;
|
|
12
|
+
export declare const splitArrayToChunksThrow: <T>({ array, filter, chunkSize, }: {
|
|
13
|
+
array: Array<T>;
|
|
14
|
+
chunkSize: number;
|
|
15
|
+
filter?: (item: T) => boolean;
|
|
16
|
+
}) => T[][];
|
|
17
|
+
export declare const splitArrayToChunks: <T>({ array, filter, chunkSize, }: {
|
|
18
|
+
array: Array<T>;
|
|
19
|
+
chunkSize: number;
|
|
20
|
+
filter?: (item: T) => boolean;
|
|
21
|
+
}) => T[][];
|
|
22
|
+
export declare const getStdFeeForToken: (token?: {
|
|
23
|
+
denom: string;
|
|
24
|
+
decimals: number;
|
|
25
|
+
}, gasPrice?: string, gasLimit?: string) => {
|
|
26
|
+
amount: {
|
|
27
|
+
denom: string;
|
|
28
|
+
amount: string;
|
|
29
|
+
}[];
|
|
30
|
+
gas: string;
|
|
31
|
+
};
|
|
32
|
+
export declare const getStdFeeFromObject: (args?: {
|
|
33
|
+
gas?: string | number;
|
|
34
|
+
payer?: string;
|
|
35
|
+
granter?: string;
|
|
36
|
+
gasPrice?: string | number;
|
|
37
|
+
feePayer?: string;
|
|
38
|
+
}) => {
|
|
39
|
+
amount: {
|
|
40
|
+
denom: string;
|
|
41
|
+
amount: string;
|
|
42
|
+
}[];
|
|
43
|
+
gas: string;
|
|
44
|
+
payer: string | undefined; /** for Web3Gateway fee delegation */
|
|
45
|
+
granter: string | undefined;
|
|
46
|
+
feePayer: string | undefined;
|
|
47
|
+
};
|
|
48
|
+
export declare const getStdFeeFromString: (gasPrice: string) => {
|
|
49
|
+
amount: {
|
|
50
|
+
denom: string;
|
|
51
|
+
amount: string;
|
|
52
|
+
}[];
|
|
53
|
+
gas: string;
|
|
54
|
+
payer: string | undefined; /** for Web3Gateway fee delegation */
|
|
55
|
+
granter: string | undefined;
|
|
56
|
+
feePayer: string | undefined;
|
|
57
|
+
};
|
|
58
|
+
export declare const getStdFee: (args?: string | {
|
|
59
|
+
payer?: string;
|
|
60
|
+
granter?: string;
|
|
61
|
+
feePayer?: string;
|
|
62
|
+
gas?: string | number;
|
|
63
|
+
gasPrice?: string | number;
|
|
64
|
+
}) => {
|
|
65
|
+
amount: {
|
|
66
|
+
denom: string;
|
|
67
|
+
amount: string;
|
|
68
|
+
}[];
|
|
69
|
+
gas: string;
|
|
70
|
+
payer: string | undefined; /** for Web3Gateway fee delegation */
|
|
71
|
+
granter: string | undefined;
|
|
72
|
+
feePayer: string | undefined;
|
|
73
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { getDefaultStdFee, DEFAULT_GAS_LIMIT, DEFAULT_GAS_PRICE, } from './constants.js';
|
|
2
|
+
import { toBigNumber, toChainFormat, toHumanReadable, default as BigNumber, } from './classes/BigNumber.js';
|
|
3
|
+
export const sleep = (timeout) => new Promise((resolve) => setTimeout(resolve, timeout));
|
|
4
|
+
/**
|
|
5
|
+
* When we want to execute the promises in batch
|
|
6
|
+
*/
|
|
7
|
+
export const awaitAll = async (array, callback) => await Promise.all(array.map(async (item) => await callback(item)));
|
|
8
|
+
/**
|
|
9
|
+
* When we want to execute the promises one by one
|
|
10
|
+
* and not all in batch as with await Promise.all()
|
|
11
|
+
*/
|
|
12
|
+
export const awaitForAll = async (array, callback) => {
|
|
13
|
+
const result = [];
|
|
14
|
+
for (let i = 0; i < array.length; i += 1) {
|
|
15
|
+
try {
|
|
16
|
+
result.push(await callback(array[i]));
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
//
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
24
|
+
export const splitArrayToChunksThrow = ({ array, filter, chunkSize, }) => {
|
|
25
|
+
const chunks = [];
|
|
26
|
+
const chunkSizeInNumber = Number(chunkSize);
|
|
27
|
+
if (isNaN(chunkSizeInNumber)) {
|
|
28
|
+
throw new Error('Invalid chunk size, must be a valid number');
|
|
29
|
+
}
|
|
30
|
+
for (let i = 0; i < array.length; i += chunkSizeInNumber) {
|
|
31
|
+
const chunk = array.slice(i, i + chunkSizeInNumber);
|
|
32
|
+
if (filter) {
|
|
33
|
+
chunks.push(chunk.filter(filter));
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
chunks.push(chunk);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return chunks;
|
|
40
|
+
};
|
|
41
|
+
export const splitArrayToChunks = ({ array, filter, chunkSize, }) => {
|
|
42
|
+
try {
|
|
43
|
+
return splitArrayToChunksThrow({ array, chunkSize, filter });
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return [array];
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
export const getStdFeeForToken = (token = { denom: 'inj', decimals: 18 }, gasPrice, gasLimit) => {
|
|
50
|
+
const gasPriceInBase = gasPrice || toHumanReadable(DEFAULT_GAS_PRICE, 18);
|
|
51
|
+
const gasPriceScaled = toChainFormat(gasPriceInBase, token.decimals).toFixed(0);
|
|
52
|
+
const gasNormalized = new BigNumber(gasLimit || DEFAULT_GAS_LIMIT).toFixed(0);
|
|
53
|
+
return {
|
|
54
|
+
amount: [
|
|
55
|
+
{
|
|
56
|
+
denom: token.denom,
|
|
57
|
+
amount: new BigNumber(gasPriceScaled).times(gasNormalized).toFixed(),
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
gas: (gasLimit || DEFAULT_GAS_LIMIT).toString(),
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
export const getStdFeeFromObject = (args) => {
|
|
64
|
+
if (!args) {
|
|
65
|
+
return getDefaultStdFee();
|
|
66
|
+
}
|
|
67
|
+
const { payer, granter, feePayer, gasPrice = DEFAULT_GAS_PRICE, gas = DEFAULT_GAS_LIMIT.toString(), } = args;
|
|
68
|
+
const gasNormalized = toBigNumber(gas).toFixed(0);
|
|
69
|
+
const gasPriceNormalized = toBigNumber(gasPrice).toFixed(0);
|
|
70
|
+
return {
|
|
71
|
+
amount: [
|
|
72
|
+
{
|
|
73
|
+
denom: 'inj',
|
|
74
|
+
amount: toBigNumber(gasNormalized).times(gasPriceNormalized).toFixed(),
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
gas: toBigNumber(gasNormalized).toFixed(),
|
|
78
|
+
payer /** for Web3Gateway fee delegation */,
|
|
79
|
+
granter,
|
|
80
|
+
feePayer,
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
export const getStdFeeFromString = (gasPrice) => {
|
|
84
|
+
const matchResult = gasPrice.match(/^([0-9.]+)([a-zA-Z][a-zA-Z0-9/:._-]*)$/);
|
|
85
|
+
if (!matchResult) {
|
|
86
|
+
throw new Error('Invalid gas price string');
|
|
87
|
+
}
|
|
88
|
+
const [_, amount] = matchResult;
|
|
89
|
+
const gas = toChainFormat(amount, 18).dividedBy(DEFAULT_GAS_PRICE).toFixed(0);
|
|
90
|
+
return getStdFeeFromObject({ gas, gasPrice: DEFAULT_GAS_PRICE });
|
|
91
|
+
};
|
|
92
|
+
export const getStdFee = (args) => {
|
|
93
|
+
if (!args) {
|
|
94
|
+
return getDefaultStdFee();
|
|
95
|
+
}
|
|
96
|
+
if (typeof args === 'string') {
|
|
97
|
+
return getStdFeeFromString(args);
|
|
98
|
+
}
|
|
99
|
+
return getStdFeeFromObject({ ...args });
|
|
100
|
+
};
|