@alephium/web3 0.5.0-rc.14 → 0.5.0-rc.16
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/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-explorer.d.ts +67 -46
- package/dist/src/api/api-explorer.js +12 -12
- package/dist/src/api/index.d.ts +1 -1
- package/dist/src/api/index.js +2 -2
- package/dist/src/signer/types.d.ts +2 -1
- package/dist/src/utils/number.d.ts +14 -0
- package/dist/src/utils/number.fixture.d.ts +7 -0
- package/dist/src/utils/number.fixture.js +162 -0
- package/dist/src/utils/number.js +130 -0
- package/package.json +5 -4
- package/src/api/api-explorer.ts +78 -50
- package/src/api/index.ts +2 -2
- package/src/signer/types.ts +4 -1
- package/src/utils/number.fixture.ts +159 -0
- package/src/utils/number.ts +144 -0
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
export interface AcceptedTransaction {
|
|
2
|
+
/** @format 32-byte-hash */
|
|
3
|
+
hash: string;
|
|
4
|
+
/** @format block-hash */
|
|
5
|
+
blockHash: string;
|
|
6
|
+
/** @format int64 */
|
|
7
|
+
timestamp: number;
|
|
8
|
+
inputs?: Input[];
|
|
9
|
+
outputs?: Output[];
|
|
10
|
+
/** @format int32 */
|
|
11
|
+
gasAmount: number;
|
|
12
|
+
/** @format uint256 */
|
|
13
|
+
gasPrice: string;
|
|
14
|
+
coinbase: boolean;
|
|
15
|
+
type: string;
|
|
16
|
+
}
|
|
1
17
|
export interface AddressBalance {
|
|
2
18
|
/** @format uint256 */
|
|
3
19
|
balance: string;
|
|
@@ -19,6 +35,7 @@ export interface AssetOutput {
|
|
|
19
35
|
key: string;
|
|
20
36
|
/** @format uint256 */
|
|
21
37
|
attoAlphAmount: string;
|
|
38
|
+
/** @format address */
|
|
22
39
|
address: string;
|
|
23
40
|
tokens?: Token[];
|
|
24
41
|
/** @format int64 */
|
|
@@ -49,22 +66,6 @@ export interface BlockEntryLite {
|
|
|
49
66
|
/** @format bigint */
|
|
50
67
|
hashRate: string;
|
|
51
68
|
}
|
|
52
|
-
export interface ConfirmedTransaction {
|
|
53
|
-
/** @format 32-byte-hash */
|
|
54
|
-
hash: string;
|
|
55
|
-
/** @format block-hash */
|
|
56
|
-
blockHash: string;
|
|
57
|
-
/** @format int64 */
|
|
58
|
-
timestamp: number;
|
|
59
|
-
inputs?: Input[];
|
|
60
|
-
outputs?: Output[];
|
|
61
|
-
/** @format int32 */
|
|
62
|
-
gasAmount: number;
|
|
63
|
-
/** @format uint256 */
|
|
64
|
-
gasPrice: string;
|
|
65
|
-
coinbase: boolean;
|
|
66
|
-
type: string;
|
|
67
|
-
}
|
|
68
69
|
export interface ContractOutput {
|
|
69
70
|
/** @format int32 */
|
|
70
71
|
hint: number;
|
|
@@ -72,6 +73,7 @@ export interface ContractOutput {
|
|
|
72
73
|
key: string;
|
|
73
74
|
/** @format uint256 */
|
|
74
75
|
attoAlphAmount: string;
|
|
76
|
+
/** @format address */
|
|
75
77
|
address: string;
|
|
76
78
|
tokens?: Token[];
|
|
77
79
|
/** @format 32-byte-hash */
|
|
@@ -83,7 +85,9 @@ export interface Event {
|
|
|
83
85
|
blockHash: string;
|
|
84
86
|
/** @format 32-byte-hash */
|
|
85
87
|
txHash: string;
|
|
88
|
+
/** @format address */
|
|
86
89
|
contractAddress: string;
|
|
90
|
+
/** @format address */
|
|
87
91
|
inputAddress?: string;
|
|
88
92
|
/** @format int32 */
|
|
89
93
|
eventIndex: number;
|
|
@@ -105,6 +109,7 @@ export interface Input {
|
|
|
105
109
|
unlockScript?: string;
|
|
106
110
|
/** @format 32-byte-hash */
|
|
107
111
|
txHashRef?: string;
|
|
112
|
+
/** @format address */
|
|
108
113
|
address?: string;
|
|
109
114
|
/** @format uint256 */
|
|
110
115
|
attoAlphAmount?: string;
|
|
@@ -126,6 +131,22 @@ export interface LogbackValue {
|
|
|
126
131
|
name: string;
|
|
127
132
|
level: string;
|
|
128
133
|
}
|
|
134
|
+
export interface MempoolTransaction {
|
|
135
|
+
/** @format 32-byte-hash */
|
|
136
|
+
hash: string;
|
|
137
|
+
/** @format int32 */
|
|
138
|
+
chainFrom: number;
|
|
139
|
+
/** @format int32 */
|
|
140
|
+
chainTo: number;
|
|
141
|
+
inputs?: Input[];
|
|
142
|
+
outputs?: Output[];
|
|
143
|
+
/** @format int32 */
|
|
144
|
+
gasAmount: number;
|
|
145
|
+
/** @format uint256 */
|
|
146
|
+
gasPrice: string;
|
|
147
|
+
/** @format int64 */
|
|
148
|
+
lastSeen: number;
|
|
149
|
+
}
|
|
129
150
|
export interface NotFound {
|
|
130
151
|
detail: string;
|
|
131
152
|
resource: string;
|
|
@@ -137,6 +158,23 @@ export interface OutputRef {
|
|
|
137
158
|
/** @format 32-byte-hash */
|
|
138
159
|
key: string;
|
|
139
160
|
}
|
|
161
|
+
export interface PendingTransaction {
|
|
162
|
+
/** @format 32-byte-hash */
|
|
163
|
+
hash: string;
|
|
164
|
+
/** @format int32 */
|
|
165
|
+
chainFrom: number;
|
|
166
|
+
/** @format int32 */
|
|
167
|
+
chainTo: number;
|
|
168
|
+
inputs?: Input[];
|
|
169
|
+
outputs?: Output[];
|
|
170
|
+
/** @format int32 */
|
|
171
|
+
gasAmount: number;
|
|
172
|
+
/** @format uint256 */
|
|
173
|
+
gasPrice: string;
|
|
174
|
+
/** @format int64 */
|
|
175
|
+
lastSeen: number;
|
|
176
|
+
type: string;
|
|
177
|
+
}
|
|
140
178
|
export interface PerChainCount {
|
|
141
179
|
/** @format int32 */
|
|
142
180
|
chainFrom: number;
|
|
@@ -214,27 +252,10 @@ export interface Transaction {
|
|
|
214
252
|
gasPrice: string;
|
|
215
253
|
coinbase: boolean;
|
|
216
254
|
}
|
|
217
|
-
export declare type TransactionLike =
|
|
255
|
+
export declare type TransactionLike = AcceptedTransaction | PendingTransaction;
|
|
218
256
|
export interface Unauthorized {
|
|
219
257
|
detail: string;
|
|
220
258
|
}
|
|
221
|
-
export interface UnconfirmedTransaction {
|
|
222
|
-
/** @format 32-byte-hash */
|
|
223
|
-
hash: string;
|
|
224
|
-
/** @format int32 */
|
|
225
|
-
chainFrom: number;
|
|
226
|
-
/** @format int32 */
|
|
227
|
-
chainTo: number;
|
|
228
|
-
inputs?: Input[];
|
|
229
|
-
outputs?: Output[];
|
|
230
|
-
/** @format int32 */
|
|
231
|
-
gasAmount: number;
|
|
232
|
-
/** @format uint256 */
|
|
233
|
-
gasPrice: string;
|
|
234
|
-
/** @format int64 */
|
|
235
|
-
lastSeen: number;
|
|
236
|
-
type: string;
|
|
237
|
-
}
|
|
238
259
|
export declare type Val = ValAddress | ValArray | ValBool | ValByteVec | ValI256 | ValU256;
|
|
239
260
|
export interface ValAddress {
|
|
240
261
|
/** @format address */
|
|
@@ -388,7 +409,7 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
388
409
|
* @name GetTransactionsTransactionHash
|
|
389
410
|
* @request GET:/transactions/{transaction_hash}
|
|
390
411
|
*/
|
|
391
|
-
getTransactionsTransactionHash: (transactionHash: string, params?: RequestParams) => Promise<
|
|
412
|
+
getTransactionsTransactionHash: (transactionHash: string, params?: RequestParams) => Promise<AcceptedTransaction | PendingTransaction>;
|
|
392
413
|
};
|
|
393
414
|
addresses: {
|
|
394
415
|
/**
|
|
@@ -481,13 +502,13 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
481
502
|
*/
|
|
482
503
|
getAddressesAddressTotalTransactions: (address: string, params?: RequestParams) => Promise<number>;
|
|
483
504
|
/**
|
|
484
|
-
* @description List
|
|
505
|
+
* @description List mempool transactions of a given address
|
|
485
506
|
*
|
|
486
507
|
* @tags Addresses
|
|
487
|
-
* @name
|
|
488
|
-
* @request GET:/addresses/{address}/
|
|
508
|
+
* @name GetAddressesAddressMempoolTransactions
|
|
509
|
+
* @request GET:/addresses/{address}/mempool/transactions
|
|
489
510
|
*/
|
|
490
|
-
|
|
511
|
+
getAddressesAddressMempoolTransactions: (address: string, params?: RequestParams) => Promise<MempoolTransaction[]>;
|
|
491
512
|
/**
|
|
492
513
|
* @description Get address balance
|
|
493
514
|
*
|
|
@@ -648,15 +669,15 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
648
669
|
*/
|
|
649
670
|
getInfosAverageBlockTimes: (params?: RequestParams) => Promise<PerChainDuration[]>;
|
|
650
671
|
};
|
|
651
|
-
|
|
672
|
+
mempool: {
|
|
652
673
|
/**
|
|
653
|
-
* @description list
|
|
674
|
+
* @description list mempool transactions
|
|
654
675
|
*
|
|
655
|
-
* @tags
|
|
656
|
-
* @name
|
|
657
|
-
* @request GET:/
|
|
676
|
+
* @tags Mempool
|
|
677
|
+
* @name GetMempoolTransactions
|
|
678
|
+
* @request GET:/mempool/transactions
|
|
658
679
|
*/
|
|
659
|
-
|
|
680
|
+
getMempoolTransactions: (query?: {
|
|
660
681
|
/**
|
|
661
682
|
* Page number
|
|
662
683
|
* @format int32
|
|
@@ -669,7 +690,7 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
669
690
|
limit?: number;
|
|
670
691
|
/** Reverse pagination */
|
|
671
692
|
reverse?: boolean;
|
|
672
|
-
}, params?: RequestParams) => Promise<
|
|
693
|
+
}, params?: RequestParams) => Promise<MempoolTransaction[]>;
|
|
673
694
|
};
|
|
674
695
|
tokens: {
|
|
675
696
|
/**
|
|
@@ -291,14 +291,14 @@ class Api extends HttpClient {
|
|
|
291
291
|
...params
|
|
292
292
|
}).then(utils_1.convertHttpResponse),
|
|
293
293
|
/**
|
|
294
|
-
* @description List
|
|
294
|
+
* @description List mempool transactions of a given address
|
|
295
295
|
*
|
|
296
296
|
* @tags Addresses
|
|
297
|
-
* @name
|
|
298
|
-
* @request GET:/addresses/{address}/
|
|
297
|
+
* @name GetAddressesAddressMempoolTransactions
|
|
298
|
+
* @request GET:/addresses/{address}/mempool/transactions
|
|
299
299
|
*/
|
|
300
|
-
|
|
301
|
-
path: `/addresses/${address}/
|
|
300
|
+
getAddressesAddressMempoolTransactions: (address, params = {}) => this.request({
|
|
301
|
+
path: `/addresses/${address}/mempool/transactions`,
|
|
302
302
|
method: 'GET',
|
|
303
303
|
format: 'json',
|
|
304
304
|
...params
|
|
@@ -500,16 +500,16 @@ class Api extends HttpClient {
|
|
|
500
500
|
...params
|
|
501
501
|
}).then(utils_1.convertHttpResponse)
|
|
502
502
|
};
|
|
503
|
-
this.
|
|
503
|
+
this.mempool = {
|
|
504
504
|
/**
|
|
505
|
-
* @description list
|
|
505
|
+
* @description list mempool transactions
|
|
506
506
|
*
|
|
507
|
-
* @tags
|
|
508
|
-
* @name
|
|
509
|
-
* @request GET:/
|
|
507
|
+
* @tags Mempool
|
|
508
|
+
* @name GetMempoolTransactions
|
|
509
|
+
* @request GET:/mempool/transactions
|
|
510
510
|
*/
|
|
511
|
-
|
|
512
|
-
path: `/
|
|
511
|
+
getMempoolTransactions: (query, params = {}) => this.request({
|
|
512
|
+
path: `/mempool/transactions`,
|
|
513
513
|
method: 'GET',
|
|
514
514
|
query: query,
|
|
515
515
|
format: 'json',
|
package/dist/src/api/index.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export declare class ExplorerProvider {
|
|
|
29
29
|
readonly transactions: any;
|
|
30
30
|
readonly addresses: any;
|
|
31
31
|
readonly infos: any;
|
|
32
|
-
readonly
|
|
32
|
+
readonly mempool: any;
|
|
33
33
|
readonly tokens: any;
|
|
34
34
|
readonly charts: any;
|
|
35
35
|
readonly utils: any;
|
package/dist/src/api/index.js
CHANGED
|
@@ -121,7 +121,7 @@ class ExplorerProvider {
|
|
|
121
121
|
this.transactions = api_explorer_1.Api['transactions'];
|
|
122
122
|
this.addresses = api_explorer_1.Api['addresses'];
|
|
123
123
|
this.infos = api_explorer_1.Api['infos'];
|
|
124
|
-
this.
|
|
124
|
+
this.mempool = api_explorer_1.Api['mempool'];
|
|
125
125
|
this.tokens = api_explorer_1.Api['tokens'];
|
|
126
126
|
this.charts = api_explorer_1.Api['charts'];
|
|
127
127
|
this.utils = api_explorer_1.Api['utils'];
|
|
@@ -143,7 +143,7 @@ class ExplorerProvider {
|
|
|
143
143
|
this.transactions = { ...explorerApi.transactions };
|
|
144
144
|
this.addresses = { ...explorerApi.addresses };
|
|
145
145
|
this.infos = { ...explorerApi.infos };
|
|
146
|
-
this.
|
|
146
|
+
this.mempool = { ...explorerApi.mempool };
|
|
147
147
|
this.tokens = { ...explorerApi.tokens };
|
|
148
148
|
this.charts = { ...explorerApi.charts };
|
|
149
149
|
this.utils = { ...explorerApi.utils };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const isNumeric: (numToCheck: any) => boolean;
|
|
2
|
+
export interface IPrettifyNumberConfig {
|
|
3
|
+
minDecimalPlaces: number;
|
|
4
|
+
maxDecimalPlaces: number;
|
|
5
|
+
/** significant digits to show in decimals while respecting decimal places */
|
|
6
|
+
minDecimalSignificantDigits: number;
|
|
7
|
+
/** special case for zero, e.g. we may want to display $0.00 or 0.0 ALPH */
|
|
8
|
+
decimalPlacesWhenZero: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const prettifyNumberConfig: Record<string, IPrettifyNumberConfig>;
|
|
11
|
+
export declare function prettifyAttoAlphAmount(amount: bigint): string | null;
|
|
12
|
+
export declare function prettifyTokenAmount(amount: bigint, decimals: number): string | null;
|
|
13
|
+
export declare function prettifyExactAmount(amount: bigint, decimals: number): string | null;
|
|
14
|
+
export declare function prettifyNumber(amount: bigint, decimals: number, config: IPrettifyNumberConfig): string | null;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tests = void 0;
|
|
4
|
+
/*
|
|
5
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
6
|
+
This file is part of the alephium project.
|
|
7
|
+
|
|
8
|
+
The library is free software: you can redistribute it and/or modify
|
|
9
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
10
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
(at your option) any later version.
|
|
12
|
+
|
|
13
|
+
The library is distributed in the hope that it will be useful,
|
|
14
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
GNU Lesser General Public License for more details.
|
|
17
|
+
|
|
18
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
19
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
*/
|
|
21
|
+
exports.tests = [
|
|
22
|
+
{
|
|
23
|
+
raw: 123456n,
|
|
24
|
+
decimal: 5,
|
|
25
|
+
exact: '1.23456',
|
|
26
|
+
alphFormat: '1.23',
|
|
27
|
+
tokenFormat: '1.2346'
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
raw: 12345612n,
|
|
31
|
+
decimal: 2,
|
|
32
|
+
exact: '123,456.12',
|
|
33
|
+
alphFormat: '123,456.12',
|
|
34
|
+
tokenFormat: '123,456.12'
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
raw: 123456123456n,
|
|
38
|
+
decimal: 6,
|
|
39
|
+
exact: '123,456.123456',
|
|
40
|
+
alphFormat: '123,456.12',
|
|
41
|
+
tokenFormat: '123,456.1235'
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
raw: 12n,
|
|
45
|
+
decimal: 2,
|
|
46
|
+
exact: '0.12',
|
|
47
|
+
alphFormat: '0.12',
|
|
48
|
+
tokenFormat: '0.12'
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
raw: 123456n,
|
|
52
|
+
decimal: 6,
|
|
53
|
+
exact: '0.123456',
|
|
54
|
+
alphFormat: '0.12',
|
|
55
|
+
tokenFormat: '0.1235'
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
raw: 123456n,
|
|
59
|
+
decimal: 7,
|
|
60
|
+
exact: '0.0123456',
|
|
61
|
+
alphFormat: '0.012',
|
|
62
|
+
tokenFormat: '0.0123'
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
raw: 123456n,
|
|
66
|
+
decimal: 8,
|
|
67
|
+
exact: '0.00123456',
|
|
68
|
+
alphFormat: '0.0012',
|
|
69
|
+
tokenFormat: '0.0012'
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
raw: 123456n,
|
|
73
|
+
decimal: 9,
|
|
74
|
+
exact: '0.000123456',
|
|
75
|
+
alphFormat: '0.00012',
|
|
76
|
+
tokenFormat: '0.00012'
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
raw: 123456n,
|
|
80
|
+
decimal: 11,
|
|
81
|
+
exact: '0.00000123456',
|
|
82
|
+
alphFormat: '0.0000012',
|
|
83
|
+
tokenFormat: '0.0000012'
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
raw: -123456n,
|
|
87
|
+
decimal: 11,
|
|
88
|
+
exact: '-0.00000123456',
|
|
89
|
+
alphFormat: '-0.0000012',
|
|
90
|
+
tokenFormat: '-0.0000012'
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
raw: 8923088n,
|
|
94
|
+
decimal: 10,
|
|
95
|
+
exact: '0.0008923088',
|
|
96
|
+
alphFormat: '0.00089',
|
|
97
|
+
tokenFormat: '0.00089'
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
raw: 885n,
|
|
101
|
+
decimal: 6,
|
|
102
|
+
exact: '0.000885',
|
|
103
|
+
alphFormat: '0.00089',
|
|
104
|
+
tokenFormat: '0.00089'
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
raw: 100000000000n,
|
|
108
|
+
decimal: 18,
|
|
109
|
+
exact: '0.0000001',
|
|
110
|
+
alphFormat: '0.0000001',
|
|
111
|
+
tokenFormat: '0.0000001'
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
raw: 1504000000000000000n,
|
|
115
|
+
decimal: 18,
|
|
116
|
+
exact: '1.504',
|
|
117
|
+
alphFormat: '1.50',
|
|
118
|
+
tokenFormat: '1.504'
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
raw: 1505000000000000000n,
|
|
122
|
+
decimal: 18,
|
|
123
|
+
exact: '1.505',
|
|
124
|
+
alphFormat: '1.51',
|
|
125
|
+
tokenFormat: '1.505'
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
raw: 1500050000000000000n,
|
|
129
|
+
decimal: 18,
|
|
130
|
+
exact: '1.50005',
|
|
131
|
+
alphFormat: '1.50',
|
|
132
|
+
tokenFormat: '1.5001'
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
raw: 100n,
|
|
136
|
+
decimal: 0,
|
|
137
|
+
exact: '100',
|
|
138
|
+
alphFormat: '100.00',
|
|
139
|
+
tokenFormat: '100.0'
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
raw: 123456789n,
|
|
143
|
+
decimal: 0,
|
|
144
|
+
exact: '123,456,789',
|
|
145
|
+
alphFormat: '123,456,789.00',
|
|
146
|
+
tokenFormat: '123,456,789.0'
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
raw: -123456789n,
|
|
150
|
+
decimal: 0,
|
|
151
|
+
exact: '-123,456,789',
|
|
152
|
+
alphFormat: '-123,456,789.00',
|
|
153
|
+
tokenFormat: '-123,456,789.0'
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
raw: 0n,
|
|
157
|
+
decimal: 0,
|
|
158
|
+
exact: '0',
|
|
159
|
+
alphFormat: '0.00',
|
|
160
|
+
tokenFormat: '0.0'
|
|
161
|
+
}
|
|
162
|
+
];
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
4
|
+
This file is part of the alephium project.
|
|
5
|
+
|
|
6
|
+
The library is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
The library is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU Lesser General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
20
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.prettifyNumber = exports.prettifyExactAmount = exports.prettifyTokenAmount = exports.prettifyAttoAlphAmount = exports.prettifyNumberConfig = exports.isNumeric = void 0;
|
|
24
|
+
// Credits:
|
|
25
|
+
// 1. https://github.com/argentlabs/argent-x/blob/e63affa7f28b27333dca4081a3dcd375bb2da40b/packages/extension/src/shared/utils/number.ts
|
|
26
|
+
// 2. https://github.com/ethers-io/ethers.js/blob/724881f34d428406488a1c9f9dbebe54b6edecda/src.ts/utils/fixednumber.ts
|
|
27
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
28
|
+
const isNumeric = (numToCheck) => !isNaN(parseFloat(numToCheck)) && isFinite(numToCheck);
|
|
29
|
+
exports.isNumeric = isNumeric;
|
|
30
|
+
exports.prettifyNumberConfig = {
|
|
31
|
+
ALPH: {
|
|
32
|
+
minDecimalPlaces: 2,
|
|
33
|
+
maxDecimalPlaces: 10,
|
|
34
|
+
minDecimalSignificantDigits: 2,
|
|
35
|
+
decimalPlacesWhenZero: 2
|
|
36
|
+
},
|
|
37
|
+
TOKEN: {
|
|
38
|
+
minDecimalPlaces: 4,
|
|
39
|
+
maxDecimalPlaces: 16,
|
|
40
|
+
minDecimalSignificantDigits: 2,
|
|
41
|
+
decimalPlacesWhenZero: 1
|
|
42
|
+
},
|
|
43
|
+
Exact: {
|
|
44
|
+
minDecimalPlaces: 18,
|
|
45
|
+
maxDecimalPlaces: 18,
|
|
46
|
+
minDecimalSignificantDigits: 0,
|
|
47
|
+
decimalPlacesWhenZero: 0
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
function prettifyAttoAlphAmount(amount) {
|
|
51
|
+
return prettifyNumber(amount, 18, exports.prettifyNumberConfig.ALPH);
|
|
52
|
+
}
|
|
53
|
+
exports.prettifyAttoAlphAmount = prettifyAttoAlphAmount;
|
|
54
|
+
function prettifyTokenAmount(amount, decimals) {
|
|
55
|
+
return prettifyNumber(amount, decimals, exports.prettifyNumberConfig.TOKEN);
|
|
56
|
+
}
|
|
57
|
+
exports.prettifyTokenAmount = prettifyTokenAmount;
|
|
58
|
+
function prettifyExactAmount(amount, decimals) {
|
|
59
|
+
return prettifyNumber(amount, decimals, exports.prettifyNumberConfig.Exact);
|
|
60
|
+
}
|
|
61
|
+
exports.prettifyExactAmount = prettifyExactAmount;
|
|
62
|
+
function prettifyNumber(amount, decimals, config) {
|
|
63
|
+
const number = toFixedNumber(amount, decimals);
|
|
64
|
+
if (!(0, exports.isNumeric)(number)) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const numberBN = new bignumber_js_1.default(number);
|
|
68
|
+
let untrimmed;
|
|
69
|
+
if (numberBN.gte(1)) {
|
|
70
|
+
/** simplest case, formatting to minDecimalPlaces will look good */
|
|
71
|
+
untrimmed = numberBN.toFormat(config.minDecimalPlaces);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
/** now need to interrogate the appearance of decimal number < 1 */
|
|
75
|
+
/** longest case - max decimal places e.g. 0.0008923088123 -> 0.0008923088 */
|
|
76
|
+
const maxDecimalPlacesString = numberBN.toFormat(config.maxDecimalPlaces);
|
|
77
|
+
/** count the zeros, which will then allow us to know the final length with desired significant digits */
|
|
78
|
+
const decimalPart = maxDecimalPlacesString.split('.')[1];
|
|
79
|
+
const zeroMatches = decimalPart?.match(/^0+/);
|
|
80
|
+
const leadingZerosInDecimalPart = zeroMatches && zeroMatches.length ? zeroMatches[0].length : 0;
|
|
81
|
+
/** now we can re-format with leadingZerosInDecimalPart + maxDecimalSignificanDigits to give us the pretty version */
|
|
82
|
+
/** e.g. 0.0008923088123 -> 0.00089 */
|
|
83
|
+
const prettyDecimalPlaces = Math.max(leadingZerosInDecimalPart + config.minDecimalSignificantDigits, config.minDecimalPlaces);
|
|
84
|
+
untrimmed = numberBN.toFormat(prettyDecimalPlaces);
|
|
85
|
+
}
|
|
86
|
+
/** the untrimmed string may have trailing zeros, e.g. 0.0890 */
|
|
87
|
+
/** trim to a minimum specified by the config, e.g. we may want to display $0.00 or 0.0 ETH */
|
|
88
|
+
let trimmed = untrimmed.replace(/0+$/, '');
|
|
89
|
+
const minLength = 1 + untrimmed.indexOf('.') + config.decimalPlacesWhenZero;
|
|
90
|
+
if (trimmed.length < minLength) {
|
|
91
|
+
trimmed = untrimmed.substring(0, minLength);
|
|
92
|
+
}
|
|
93
|
+
if (trimmed[trimmed.length - 1] === '.') {
|
|
94
|
+
trimmed = trimmed.slice(0, -1);
|
|
95
|
+
}
|
|
96
|
+
return trimmed;
|
|
97
|
+
}
|
|
98
|
+
exports.prettifyNumber = prettifyNumber;
|
|
99
|
+
const BN_N1 = BigInt(-1);
|
|
100
|
+
const BN_0 = BigInt(0);
|
|
101
|
+
// Constant to pull zeros from for multipliers
|
|
102
|
+
const Zeros = '0000';
|
|
103
|
+
function toFixedNumber(val, decimals) {
|
|
104
|
+
let negative = '';
|
|
105
|
+
if (val < BN_0) {
|
|
106
|
+
negative = '-';
|
|
107
|
+
val *= BN_N1;
|
|
108
|
+
}
|
|
109
|
+
let str = val.toString();
|
|
110
|
+
// No decimal point for whole values
|
|
111
|
+
if (decimals === 0) {
|
|
112
|
+
return negative + str;
|
|
113
|
+
}
|
|
114
|
+
// Pad out to the whole component (including a whole digit)
|
|
115
|
+
while (str.length <= decimals) {
|
|
116
|
+
str = Zeros + str;
|
|
117
|
+
}
|
|
118
|
+
// Insert the decimal point
|
|
119
|
+
const index = str.length - decimals;
|
|
120
|
+
str = str.substring(0, index) + '.' + str.substring(index);
|
|
121
|
+
// Trim the whole component (leaving at least one 0)
|
|
122
|
+
while (str[0] === '0' && str[1] !== '.') {
|
|
123
|
+
str = str.substring(1);
|
|
124
|
+
}
|
|
125
|
+
// Trim the decimal component (leaving at least one 0)
|
|
126
|
+
while (str[str.length - 1] === '0' && str[str.length - 2] !== '.') {
|
|
127
|
+
str = str.substring(0, str.length - 1);
|
|
128
|
+
}
|
|
129
|
+
return negative + str;
|
|
130
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "0.5.0-rc.
|
|
3
|
+
"version": "0.5.0-rc.16",
|
|
4
4
|
"description": "A JS/TS library to interact with the Alephium platform",
|
|
5
5
|
"license": "GPL",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
},
|
|
28
28
|
"author": "Alephium dev <dev@alephium.org>",
|
|
29
29
|
"config": {
|
|
30
|
-
"alephium_version": "1.7.1
|
|
31
|
-
"explorer_backend_version": "1.13.0
|
|
30
|
+
"alephium_version": "1.7.1",
|
|
31
|
+
"explorer_backend_version": "1.13.0"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"build": "rm -rf dist/* && npx tsc --build . && webpack",
|
|
@@ -46,7 +46,8 @@
|
|
|
46
46
|
"buffer": "^6.0.3",
|
|
47
47
|
"cross-fetch": "^3.1.5",
|
|
48
48
|
"elliptic": "6.5.4",
|
|
49
|
-
"eventemitter3": "^4.0.7"
|
|
49
|
+
"eventemitter3": "^4.0.7",
|
|
50
|
+
"bignumber.js": "^9.0.2"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
53
|
"@babel/eslint-parser": "^7.18.9",
|