@alephium/web3 0.5.0-rc.9 → 0.5.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.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +56 -6
- package/dist/src/api/api-alephium.js +5 -3
- package/dist/src/api/api-explorer.d.ts +222 -52
- package/dist/src/api/api-explorer.js +17 -15
- package/dist/src/api/explorer-provider.d.ts +18 -0
- package/dist/src/api/explorer-provider.js +65 -0
- package/dist/src/api/index.d.ts +2 -42
- package/dist/src/api/index.js +6 -117
- package/dist/src/api/node-provider.d.ts +21 -0
- package/dist/src/api/node-provider.js +68 -0
- package/dist/src/api/types.d.ts +9 -1
- package/dist/src/api/types.js +17 -1
- package/dist/src/contract/contract.d.ts +6 -3
- package/dist/src/contract/contract.js +31 -28
- package/dist/src/signer/signer.d.ts +4 -8
- package/dist/src/signer/signer.js +26 -8
- package/dist/src/signer/types.d.ts +5 -17
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/number.d.ts +18 -0
- package/dist/src/utils/number.fixture.d.ts +12 -0
- package/dist/src/utils/number.fixture.js +189 -0
- package/dist/src/utils/number.js +148 -0
- package/package.json +6 -5
- package/src/api/api-alephium.ts +177 -180
- package/src/api/api-explorer.ts +327 -126
- package/src/api/explorer-provider.ts +78 -0
- package/src/api/index.ts +2 -148
- package/src/api/node-provider.ts +84 -0
- package/src/api/types.ts +24 -1
- package/src/contract/contract.ts +31 -29
- package/src/signer/signer.ts +33 -26
- package/src/signer/types.ts +15 -11
- package/src/utils/index.ts +1 -0
- package/src/utils/number.fixture.ts +187 -0
- package/src/utils/number.ts +162 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
export const tests = [
|
|
19
|
+
{
|
|
20
|
+
raw: 123456n,
|
|
21
|
+
decimal: 5,
|
|
22
|
+
exact: '1.23456',
|
|
23
|
+
alphFormat: '1.23',
|
|
24
|
+
tokenFormat: '1.2346'
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
raw: 12345612n,
|
|
28
|
+
decimal: 2,
|
|
29
|
+
exact: '123,456.12',
|
|
30
|
+
alphFormat: '123,456.12',
|
|
31
|
+
tokenFormat: '123,456.12'
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
raw: 123456123456n,
|
|
35
|
+
decimal: 6,
|
|
36
|
+
exact: '123,456.123456',
|
|
37
|
+
alphFormat: '123,456.12',
|
|
38
|
+
tokenFormat: '123,456.1235'
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
raw: 12n,
|
|
42
|
+
decimal: 2,
|
|
43
|
+
exact: '0.12',
|
|
44
|
+
alphFormat: '0.12',
|
|
45
|
+
tokenFormat: '0.12'
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
raw: 123456n,
|
|
49
|
+
decimal: 6,
|
|
50
|
+
exact: '0.123456',
|
|
51
|
+
alphFormat: '0.12',
|
|
52
|
+
tokenFormat: '0.1235'
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
raw: 123456n,
|
|
56
|
+
decimal: 7,
|
|
57
|
+
exact: '0.0123456',
|
|
58
|
+
alphFormat: '0.012',
|
|
59
|
+
tokenFormat: '0.0123'
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
raw: 123456n,
|
|
63
|
+
decimal: 8,
|
|
64
|
+
exact: '0.00123456',
|
|
65
|
+
alphFormat: '0.0012',
|
|
66
|
+
tokenFormat: '0.0012'
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
raw: 123456n,
|
|
70
|
+
decimal: 9,
|
|
71
|
+
exact: '0.000123456',
|
|
72
|
+
alphFormat: '0.00012',
|
|
73
|
+
tokenFormat: '0.00012'
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
raw: 123456n,
|
|
77
|
+
decimal: 11,
|
|
78
|
+
exact: '0.00000123456',
|
|
79
|
+
alphFormat: '0.0000012',
|
|
80
|
+
tokenFormat: '0.0000012'
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
raw: -123456n,
|
|
84
|
+
decimal: 11,
|
|
85
|
+
exact: '-0.00000123456',
|
|
86
|
+
alphFormat: '-0.0000012',
|
|
87
|
+
tokenFormat: '-0.0000012'
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
raw: 8923088n,
|
|
91
|
+
decimal: 10,
|
|
92
|
+
exact: '0.0008923088',
|
|
93
|
+
alphFormat: '0.00089',
|
|
94
|
+
tokenFormat: '0.00089'
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
raw: 885n,
|
|
98
|
+
decimal: 6,
|
|
99
|
+
exact: '0.000885',
|
|
100
|
+
alphFormat: '0.00089',
|
|
101
|
+
tokenFormat: '0.00089'
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
raw: 100000000000n,
|
|
105
|
+
decimal: 18,
|
|
106
|
+
exact: '0.0000001',
|
|
107
|
+
alphFormat: '0.0000001',
|
|
108
|
+
tokenFormat: '0.0000001'
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
raw: 1504000000000000000n,
|
|
112
|
+
decimal: 18,
|
|
113
|
+
exact: '1.504',
|
|
114
|
+
alphFormat: '1.50',
|
|
115
|
+
tokenFormat: '1.504'
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
raw: 1505000000000000000n,
|
|
119
|
+
decimal: 18,
|
|
120
|
+
exact: '1.505',
|
|
121
|
+
alphFormat: '1.51',
|
|
122
|
+
tokenFormat: '1.505'
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
raw: 1500050000000000000n,
|
|
126
|
+
decimal: 18,
|
|
127
|
+
exact: '1.50005',
|
|
128
|
+
alphFormat: '1.50',
|
|
129
|
+
tokenFormat: '1.5001'
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
raw: 100n,
|
|
133
|
+
decimal: 0,
|
|
134
|
+
exact: '100',
|
|
135
|
+
alphFormat: '100.00',
|
|
136
|
+
tokenFormat: '100.0'
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
raw: 123456789n,
|
|
140
|
+
decimal: 0,
|
|
141
|
+
exact: '123,456,789',
|
|
142
|
+
alphFormat: '123,456,789.00',
|
|
143
|
+
tokenFormat: '123,456,789.0'
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
raw: -123456789n,
|
|
147
|
+
decimal: 0,
|
|
148
|
+
exact: '-123,456,789',
|
|
149
|
+
alphFormat: '-123,456,789.00',
|
|
150
|
+
tokenFormat: '-123,456,789.0'
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
raw: 0n,
|
|
154
|
+
decimal: 0,
|
|
155
|
+
exact: '0',
|
|
156
|
+
alphFormat: '0.00',
|
|
157
|
+
tokenFormat: '0.0'
|
|
158
|
+
}
|
|
159
|
+
]
|
|
160
|
+
|
|
161
|
+
export const tests1 = [
|
|
162
|
+
{
|
|
163
|
+
raw: '0',
|
|
164
|
+
decimals: 18,
|
|
165
|
+
amount: 0n
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
raw: '1.23',
|
|
169
|
+
decimals: 2,
|
|
170
|
+
amount: 123n
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
raw: '1',
|
|
174
|
+
decimals: 5,
|
|
175
|
+
amount: 100000n
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
raw: '1',
|
|
179
|
+
decimals: 18,
|
|
180
|
+
amount: 1000000000000000000n
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
raw: '1.23456789',
|
|
184
|
+
decimals: 18,
|
|
185
|
+
amount: 1234567890000000000n
|
|
186
|
+
}
|
|
187
|
+
]
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// Credits:
|
|
20
|
+
// 1. https://github.com/argentlabs/argent-x/blob/e63affa7f28b27333dca4081a3dcd375bb2da40b/packages/extension/src/shared/utils/number.ts
|
|
21
|
+
// 2. https://github.com/ethers-io/ethers.js/blob/724881f34d428406488a1c9f9dbebe54b6edecda/src.ts/utils/fixednumber.ts
|
|
22
|
+
|
|
23
|
+
import BigNumber from 'bignumber.js'
|
|
24
|
+
import { Number256 } from '..'
|
|
25
|
+
|
|
26
|
+
export const isNumeric = (numToCheck: any): boolean => !isNaN(parseFloat(numToCheck)) && isFinite(numToCheck)
|
|
27
|
+
|
|
28
|
+
export interface IPrettifyNumberConfig {
|
|
29
|
+
minDecimalPlaces: number
|
|
30
|
+
maxDecimalPlaces: number
|
|
31
|
+
/** significant digits to show in decimals while respecting decimal places */
|
|
32
|
+
minDecimalSignificantDigits: number
|
|
33
|
+
/** special case for zero, e.g. we may want to display $0.00 or 0.0 ALPH */
|
|
34
|
+
decimalPlacesWhenZero: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const prettifyNumberConfig: Record<string, IPrettifyNumberConfig> = {
|
|
38
|
+
ALPH: {
|
|
39
|
+
minDecimalPlaces: 2,
|
|
40
|
+
maxDecimalPlaces: 10,
|
|
41
|
+
minDecimalSignificantDigits: 2,
|
|
42
|
+
decimalPlacesWhenZero: 2
|
|
43
|
+
},
|
|
44
|
+
TOKEN: {
|
|
45
|
+
minDecimalPlaces: 4,
|
|
46
|
+
maxDecimalPlaces: 16,
|
|
47
|
+
minDecimalSignificantDigits: 2,
|
|
48
|
+
decimalPlacesWhenZero: 1
|
|
49
|
+
},
|
|
50
|
+
Exact: {
|
|
51
|
+
minDecimalPlaces: 18,
|
|
52
|
+
maxDecimalPlaces: 18,
|
|
53
|
+
minDecimalSignificantDigits: 0,
|
|
54
|
+
decimalPlacesWhenZero: 0
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function prettifyAttoAlphAmount(amount: Number256): string | undefined {
|
|
59
|
+
return prettifyNumber(amount, 18, prettifyNumberConfig.ALPH)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function prettifyTokenAmount(amount: Number256, decimals: number): string | undefined {
|
|
63
|
+
return prettifyNumber(amount, decimals, prettifyNumberConfig.TOKEN)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function prettifyExactAmount(amount: Number256, decimals: number): string | undefined {
|
|
67
|
+
return prettifyNumber(amount, decimals, prettifyNumberConfig.Exact)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function prettifyNumber(amount: Number256, decimals: number, config: IPrettifyNumberConfig): string | undefined {
|
|
71
|
+
const number = toFixedNumber(number256ToBigint(amount), decimals)
|
|
72
|
+
|
|
73
|
+
if (!isNumeric(number)) {
|
|
74
|
+
return undefined
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const numberBN = new BigNumber(number)
|
|
78
|
+
|
|
79
|
+
let untrimmed: string
|
|
80
|
+
if (numberBN.gte(1)) {
|
|
81
|
+
/** simplest case, formatting to minDecimalPlaces will look good */
|
|
82
|
+
untrimmed = numberBN.toFormat(config.minDecimalPlaces)
|
|
83
|
+
} else {
|
|
84
|
+
/** now need to interrogate the appearance of decimal number < 1 */
|
|
85
|
+
/** longest case - max decimal places e.g. 0.0008923088123 -> 0.0008923088 */
|
|
86
|
+
const maxDecimalPlacesString = numberBN.toFormat(config.maxDecimalPlaces)
|
|
87
|
+
/** count the zeros, which will then allow us to know the final length with desired significant digits */
|
|
88
|
+
const decimalPart = maxDecimalPlacesString.split('.')[1]
|
|
89
|
+
const zeroMatches = decimalPart?.match(/^0+/)
|
|
90
|
+
const leadingZerosInDecimalPart = zeroMatches && zeroMatches.length ? zeroMatches[0].length : 0
|
|
91
|
+
/** now we can re-format with leadingZerosInDecimalPart + maxDecimalSignificanDigits to give us the pretty version */
|
|
92
|
+
/** e.g. 0.0008923088123 -> 0.00089 */
|
|
93
|
+
const prettyDecimalPlaces = Math.max(
|
|
94
|
+
leadingZerosInDecimalPart + config.minDecimalSignificantDigits,
|
|
95
|
+
config.minDecimalPlaces
|
|
96
|
+
)
|
|
97
|
+
untrimmed = numberBN.toFormat(prettyDecimalPlaces)
|
|
98
|
+
}
|
|
99
|
+
/** the untrimmed string may have trailing zeros, e.g. 0.0890 */
|
|
100
|
+
/** trim to a minimum specified by the config, e.g. we may want to display $0.00 or 0.0 ETH */
|
|
101
|
+
let trimmed = untrimmed.replace(/0+$/, '')
|
|
102
|
+
const minLength = 1 + untrimmed.indexOf('.') + config.decimalPlacesWhenZero
|
|
103
|
+
if (trimmed.length < minLength) {
|
|
104
|
+
trimmed = untrimmed.substring(0, minLength)
|
|
105
|
+
}
|
|
106
|
+
if (trimmed[trimmed.length - 1] === '.') {
|
|
107
|
+
trimmed = trimmed.slice(0, -1)
|
|
108
|
+
}
|
|
109
|
+
return trimmed
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const BN_N1 = BigInt(-1)
|
|
113
|
+
const BN_0 = BigInt(0)
|
|
114
|
+
|
|
115
|
+
// Constant to pull zeros from for multipliers
|
|
116
|
+
const Zeros = '0000'
|
|
117
|
+
|
|
118
|
+
function toFixedNumber(val: bigint, decimals: number): string {
|
|
119
|
+
let negative = ''
|
|
120
|
+
if (val < BN_0) {
|
|
121
|
+
negative = '-'
|
|
122
|
+
val *= BN_N1
|
|
123
|
+
}
|
|
124
|
+
let str = val.toString()
|
|
125
|
+
// No decimal point for whole values
|
|
126
|
+
if (decimals === 0) {
|
|
127
|
+
return negative + str
|
|
128
|
+
}
|
|
129
|
+
// Pad out to the whole component (including a whole digit)
|
|
130
|
+
while (str.length <= decimals) {
|
|
131
|
+
str = Zeros + str
|
|
132
|
+
}
|
|
133
|
+
// Insert the decimal point
|
|
134
|
+
const index = str.length - decimals
|
|
135
|
+
str = str.substring(0, index) + '.' + str.substring(index)
|
|
136
|
+
// Trim the whole component (leaving at least one 0)
|
|
137
|
+
while (str[0] === '0' && str[1] !== '.') {
|
|
138
|
+
str = str.substring(1)
|
|
139
|
+
}
|
|
140
|
+
// Trim the decimal component (leaving at least one 0)
|
|
141
|
+
while (str[str.length - 1] === '0' && str[str.length - 2] !== '.') {
|
|
142
|
+
str = str.substring(0, str.length - 1)
|
|
143
|
+
}
|
|
144
|
+
return negative + str
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function convertAmountWithDecimals(amount: string | number, decimals: number): bigint | undefined {
|
|
148
|
+
try {
|
|
149
|
+
const result = new BigNumber(amount).multipliedBy(Math.pow(10, decimals))
|
|
150
|
+
return BigInt(result.toFormat(0, { groupSeparator: '' }))
|
|
151
|
+
} catch (e) {
|
|
152
|
+
return undefined
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function convertAlphAmount(amount: string | number): bigint | undefined {
|
|
157
|
+
return convertAmountWithDecimals(amount, 18)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function number256ToBigint(number: Number256): bigint {
|
|
161
|
+
return typeof number === 'string' ? BigInt(number) : number
|
|
162
|
+
}
|