@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.
Files changed (38) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.map +1 -1
  3. package/dist/src/api/api-alephium.d.ts +56 -6
  4. package/dist/src/api/api-alephium.js +5 -3
  5. package/dist/src/api/api-explorer.d.ts +222 -52
  6. package/dist/src/api/api-explorer.js +17 -15
  7. package/dist/src/api/explorer-provider.d.ts +18 -0
  8. package/dist/src/api/explorer-provider.js +65 -0
  9. package/dist/src/api/index.d.ts +2 -42
  10. package/dist/src/api/index.js +6 -117
  11. package/dist/src/api/node-provider.d.ts +21 -0
  12. package/dist/src/api/node-provider.js +68 -0
  13. package/dist/src/api/types.d.ts +9 -1
  14. package/dist/src/api/types.js +17 -1
  15. package/dist/src/contract/contract.d.ts +6 -3
  16. package/dist/src/contract/contract.js +31 -28
  17. package/dist/src/signer/signer.d.ts +4 -8
  18. package/dist/src/signer/signer.js +26 -8
  19. package/dist/src/signer/types.d.ts +5 -17
  20. package/dist/src/utils/index.d.ts +1 -0
  21. package/dist/src/utils/index.js +1 -0
  22. package/dist/src/utils/number.d.ts +18 -0
  23. package/dist/src/utils/number.fixture.d.ts +12 -0
  24. package/dist/src/utils/number.fixture.js +189 -0
  25. package/dist/src/utils/number.js +148 -0
  26. package/package.json +6 -5
  27. package/src/api/api-alephium.ts +177 -180
  28. package/src/api/api-explorer.ts +327 -126
  29. package/src/api/explorer-provider.ts +78 -0
  30. package/src/api/index.ts +2 -148
  31. package/src/api/node-provider.ts +84 -0
  32. package/src/api/types.ts +24 -1
  33. package/src/contract/contract.ts +31 -29
  34. package/src/signer/signer.ts +33 -26
  35. package/src/signer/types.ts +15 -11
  36. package/src/utils/index.ts +1 -0
  37. package/src/utils/number.fixture.ts +187 -0
  38. package/src/utils/number.ts +162 -0
@@ -0,0 +1,189 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tests1 = 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
+ ];
163
+ exports.tests1 = [
164
+ {
165
+ raw: '0',
166
+ decimals: 18,
167
+ amount: 0n
168
+ },
169
+ {
170
+ raw: '1.23',
171
+ decimals: 2,
172
+ amount: 123n
173
+ },
174
+ {
175
+ raw: '1',
176
+ decimals: 5,
177
+ amount: 100000n
178
+ },
179
+ {
180
+ raw: '1',
181
+ decimals: 18,
182
+ amount: 1000000000000000000n
183
+ },
184
+ {
185
+ raw: '1.23456789',
186
+ decimals: 18,
187
+ amount: 1234567890000000000n
188
+ }
189
+ ];
@@ -0,0 +1,148 @@
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.number256ToBigint = exports.convertAlphAmount = exports.convertAmountWithDecimals = 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(number256ToBigint(amount), decimals);
64
+ if (!(0, exports.isNumeric)(number)) {
65
+ return undefined;
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
+ }
131
+ function convertAmountWithDecimals(amount, decimals) {
132
+ try {
133
+ const result = new bignumber_js_1.default(amount).multipliedBy(Math.pow(10, decimals));
134
+ return BigInt(result.toFormat(0, { groupSeparator: '' }));
135
+ }
136
+ catch (e) {
137
+ return undefined;
138
+ }
139
+ }
140
+ exports.convertAmountWithDecimals = convertAmountWithDecimals;
141
+ function convertAlphAmount(amount) {
142
+ return convertAmountWithDecimals(amount, 18);
143
+ }
144
+ exports.convertAlphAmount = convertAlphAmount;
145
+ function number256ToBigint(number) {
146
+ return typeof number === 'string' ? BigInt(number) : number;
147
+ }
148
+ exports.number256ToBigint = number256ToBigint;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.5.0-rc.9",
3
+ "version": "0.5.1",
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.0",
31
- "explorer_backend_version": "1.12.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",
@@ -78,7 +79,7 @@
78
79
  "rewire": "^6.0.0",
79
80
  "shelljs": "^0.8.5",
80
81
  "stream-browserify": "^3.0.0",
81
- "swagger-typescript-api": "^9.2.0",
82
+ "swagger-typescript-api": "12.0.3",
82
83
  "terser-webpack-plugin": "^5.3.1",
83
84
  "ts-jest": "^28.0.2",
84
85
  "ts-node": "^10.7.0",