@autonomys/auto-utils 1.5.16 → 1.5.17
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/api.js +19 -35
- package/dist/constants/network.js +35 -7
- package/dist/constants/token.js +5 -1
- package/dist/number.d.ts +95 -16
- package/dist/number.d.ts.map +1 -1
- package/dist/number.js +164 -17
- package/dist/utils/sign.js +3 -12
- package/dist/utils/signAndSendTx.js +6 -15
- package/dist/wallet.js +14 -20
- package/package.json +2 -2
package/dist/api.js
CHANGED
|
@@ -1,24 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
-
var t = {};
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
-
t[p[i]] = s[p[i]];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
3
|
exports.disconnect = exports.activateDomain = exports.activate = exports.createConnection = void 0;
|
|
24
4
|
const api_1 = require("@polkadot/api");
|
|
@@ -63,15 +43,19 @@ const network_2 = require("./types/network");
|
|
|
63
43
|
*
|
|
64
44
|
* @throws {Error} When connection fails or API initialization fails.
|
|
65
45
|
*/
|
|
66
|
-
const createConnection = (endpoint, options) =>
|
|
67
|
-
var _a;
|
|
46
|
+
const createConnection = async (endpoint, options) => {
|
|
68
47
|
// Create the provider
|
|
69
48
|
const provider = new api_1.WsProvider(endpoint);
|
|
70
49
|
// Create the API instance
|
|
71
|
-
const api =
|
|
72
|
-
|
|
50
|
+
const api = await api_1.ApiPromise.create({
|
|
51
|
+
...options,
|
|
52
|
+
types: { ...network_2.CHAIN_TYPES, ...options?.types },
|
|
53
|
+
noInitWarn: options?.noInitWarn ?? true,
|
|
54
|
+
provider,
|
|
55
|
+
});
|
|
56
|
+
await api.isReady;
|
|
73
57
|
return api;
|
|
74
|
-
}
|
|
58
|
+
};
|
|
75
59
|
exports.createConnection = createConnection;
|
|
76
60
|
/**
|
|
77
61
|
* Activates a connection to the Autonomys Network consensus layer.
|
|
@@ -110,14 +94,14 @@ exports.createConnection = createConnection;
|
|
|
110
94
|
*
|
|
111
95
|
* @throws {Error} When the specified network is not found or connection fails.
|
|
112
96
|
*/
|
|
113
|
-
const activate = (params) =>
|
|
97
|
+
const activate = async (params) => {
|
|
114
98
|
// Get the first rpc urls for the network
|
|
115
99
|
const endpoint = (0, network_1.getNetworkRpcUrls)(params);
|
|
116
100
|
// Remove the networkId from the input
|
|
117
101
|
if (params)
|
|
118
102
|
delete params.networkId;
|
|
119
|
-
return
|
|
120
|
-
}
|
|
103
|
+
return await (0, exports.createConnection)(endpoint, params);
|
|
104
|
+
};
|
|
121
105
|
exports.activate = activate;
|
|
122
106
|
/**
|
|
123
107
|
* Activates a connection to a specific domain within the Autonomys Network.
|
|
@@ -164,14 +148,14 @@ exports.activate = activate;
|
|
|
164
148
|
*
|
|
165
149
|
* @throws {Error} When the specified network or domain is not found, or connection fails.
|
|
166
150
|
*/
|
|
167
|
-
const activateDomain = (params) =>
|
|
151
|
+
const activateDomain = async (params) => {
|
|
168
152
|
// Get the first rpc urls for the network
|
|
169
153
|
const endpoint = (0, network_1.getNetworkDomainRpcUrls)(params);
|
|
170
154
|
// Remove the domainId from the input
|
|
171
155
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
172
|
-
const { domainId
|
|
173
|
-
return
|
|
174
|
-
}
|
|
156
|
+
const { domainId, ...rest } = params;
|
|
157
|
+
return await (0, exports.createConnection)(endpoint, rest);
|
|
158
|
+
};
|
|
175
159
|
exports.activateDomain = activateDomain;
|
|
176
160
|
/**
|
|
177
161
|
* Disconnects an active API connection and cleans up resources.
|
|
@@ -200,8 +184,8 @@ exports.activateDomain = activateDomain;
|
|
|
200
184
|
* // Or use the API instance method directly
|
|
201
185
|
* // await api.disconnect()
|
|
202
186
|
*/
|
|
203
|
-
const disconnect = (api) =>
|
|
187
|
+
const disconnect = async (api) => {
|
|
204
188
|
// Disconnect the API instance and the provider
|
|
205
|
-
|
|
206
|
-
}
|
|
189
|
+
await api.disconnect();
|
|
190
|
+
};
|
|
207
191
|
exports.disconnect = disconnect;
|
|
@@ -38,7 +38,11 @@ exports.networks = [
|
|
|
38
38
|
},
|
|
39
39
|
],
|
|
40
40
|
domains: [
|
|
41
|
-
|
|
41
|
+
{
|
|
42
|
+
domainId: '0',
|
|
43
|
+
...domain_1.domains[domain_1.DomainRuntime.AUTO_EVM],
|
|
44
|
+
rpcUrls: ['wss://auto-evm.mainnet.autonomys.xyz/ws'],
|
|
45
|
+
},
|
|
42
46
|
],
|
|
43
47
|
token: token_1.DEFAULT_TOKEN,
|
|
44
48
|
},
|
|
@@ -53,7 +57,11 @@ exports.networks = [
|
|
|
53
57
|
},
|
|
54
58
|
],
|
|
55
59
|
domains: [
|
|
56
|
-
|
|
60
|
+
{
|
|
61
|
+
domainId: '0',
|
|
62
|
+
...domain_1.domains[domain_1.DomainRuntime.AUTO_EVM],
|
|
63
|
+
rpcUrls: ['wss://auto-evm.chronos.autonomys.xyz/ws'],
|
|
64
|
+
},
|
|
57
65
|
],
|
|
58
66
|
token: token_1.TESTNET_TOKEN,
|
|
59
67
|
isTestnet: true,
|
|
@@ -64,7 +72,11 @@ exports.networks = [
|
|
|
64
72
|
rpcUrls: ['wss://rpc-0.taurus.autonomys.xyz/ws'],
|
|
65
73
|
explorer: [],
|
|
66
74
|
domains: [
|
|
67
|
-
|
|
75
|
+
{
|
|
76
|
+
domainId: '0',
|
|
77
|
+
...domain_1.domains[domain_1.DomainRuntime.AUTO_EVM],
|
|
78
|
+
rpcUrls: ['wss://auto-evm.taurus.autonomys.xyz/ws'],
|
|
79
|
+
},
|
|
68
80
|
],
|
|
69
81
|
token: token_1.TESTNET_TOKEN,
|
|
70
82
|
isTestnet: true,
|
|
@@ -76,8 +88,16 @@ exports.networks = [
|
|
|
76
88
|
rpcUrls: ['ws://rpc.devnet.autonomys.xyz/ws'],
|
|
77
89
|
explorer: [],
|
|
78
90
|
domains: [
|
|
79
|
-
|
|
80
|
-
|
|
91
|
+
{
|
|
92
|
+
domainId: '0',
|
|
93
|
+
...domain_1.domains[domain_1.DomainRuntime.AUTO_EVM],
|
|
94
|
+
rpcUrls: ['wss://auto-evm.devnet.autonomys.xyz/ws'],
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
domainId: '1',
|
|
98
|
+
...domain_1.domains[domain_1.DomainRuntime.AUTO_ID],
|
|
99
|
+
rpcUrls: ['wss://autoid.devnet.autonomys.xyz/ws'],
|
|
100
|
+
},
|
|
81
101
|
],
|
|
82
102
|
token: token_1.TESTNET_TOKEN,
|
|
83
103
|
isTestnet: true,
|
|
@@ -89,8 +109,16 @@ exports.networks = [
|
|
|
89
109
|
rpcUrls: ['ws://127.0.0.1:9944/ws'],
|
|
90
110
|
explorer: [],
|
|
91
111
|
domains: [
|
|
92
|
-
|
|
93
|
-
|
|
112
|
+
{
|
|
113
|
+
domainId: '0',
|
|
114
|
+
...domain_1.domains[domain_1.DomainRuntime.AUTO_EVM],
|
|
115
|
+
rpcUrls: ['ws://127.0.0.1:9945/ws'],
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
domainId: '1',
|
|
119
|
+
...domain_1.domains[domain_1.DomainRuntime.AUTO_ID],
|
|
120
|
+
rpcUrls: ['ws://127.0.0.1:9946/ws'],
|
|
121
|
+
},
|
|
94
122
|
],
|
|
95
123
|
token: token_1.TESTNET_TOKEN,
|
|
96
124
|
isTestnet: true,
|
package/dist/constants/token.js
CHANGED
|
@@ -9,4 +9,8 @@ exports.DEFAULT_TOKEN = {
|
|
|
9
9
|
symbol: exports.DEFAULT_TOKEN_SYMBOL,
|
|
10
10
|
name: exports.DEFAULT_TOKEN_NAME,
|
|
11
11
|
};
|
|
12
|
-
exports.TESTNET_TOKEN =
|
|
12
|
+
exports.TESTNET_TOKEN = {
|
|
13
|
+
...exports.DEFAULT_TOKEN,
|
|
14
|
+
symbol: 't' + exports.DEFAULT_TOKEN_SYMBOL,
|
|
15
|
+
name: 'Testnet ' + exports.DEFAULT_TOKEN_NAME,
|
|
16
|
+
};
|
package/dist/number.d.ts
CHANGED
|
@@ -3,59 +3,64 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This function converts token amounts stored in their smallest unit (with decimal places)
|
|
5
5
|
* back to their standard decimal representation. It handles both string and BigInt inputs
|
|
6
|
-
* and is
|
|
6
|
+
* and is useful for displaying token balances in user interfaces.
|
|
7
|
+
*
|
|
8
|
+
* This function uses floating-point arithmetic and may lose precision
|
|
9
|
+
* for large values or high-precision decimals. For precise calculations, consider using
|
|
10
|
+
* `formatUnits` or `shannonsToAi3` instead.
|
|
7
11
|
*
|
|
8
12
|
* @param amount - The token amount in smallest units (string or BigInt).
|
|
9
13
|
* @param decimals - Number of decimal places the token uses. Defaults to 18 for AI3 tokens.
|
|
10
|
-
* @returns The parsed amount as a number
|
|
14
|
+
* @returns The parsed amount as a number (for string input) or bigint (for BigInt input).
|
|
11
15
|
*
|
|
12
16
|
* @example
|
|
13
17
|
* import { parseTokenAmount } from '@autonomys/auto-utils'
|
|
14
18
|
*
|
|
15
|
-
* // Parse AI3 token amount (18 decimals)
|
|
19
|
+
* // Parse AI3 token amount (18 decimals) - string input returns number
|
|
16
20
|
* const balance = '1000000000000000000' // 1 AI3 in smallest units
|
|
17
21
|
* const readable = parseTokenAmount(balance)
|
|
18
|
-
* console.log(readable) // Output: 1
|
|
22
|
+
* console.log(readable) // Output: 1 (number)
|
|
19
23
|
*
|
|
20
|
-
* // Parse with
|
|
24
|
+
* // Parse with BigInt input - returns bigint
|
|
21
25
|
* const customBalance = BigInt('1000000') // 1 token with 6 decimals
|
|
22
26
|
* const customReadable = parseTokenAmount(customBalance, 6)
|
|
23
|
-
* console.log(customReadable) // Output:
|
|
27
|
+
* console.log(customReadable) // Output: 1n (bigint)
|
|
24
28
|
*
|
|
25
29
|
* // Parse fractional amounts
|
|
26
30
|
* const fractional = '500000000000000000' // 0.5 AI3
|
|
27
31
|
* const fractionalReadable = parseTokenAmount(fractional)
|
|
28
|
-
* console.log(fractionalReadable) // Output: 0.5
|
|
32
|
+
* console.log(fractionalReadable) // Output: 0.5 (number)
|
|
29
33
|
*/
|
|
30
34
|
export declare const parseTokenAmount: (amount: string | bigint, decimals?: number) => number | bigint;
|
|
31
35
|
/**
|
|
32
36
|
* Formats a human-readable token amount to its smallest unit representation.
|
|
33
37
|
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
38
|
+
*
|
|
39
|
+
* This function uses floating-point arithmetic for number inputs
|
|
40
|
+
* and may lose precision for large values or high-precision decimals. For precise calculations,
|
|
41
|
+
* consider using `parseUnits` or `ai3ToShannons` instead.
|
|
37
42
|
*
|
|
38
43
|
* @param amount - The token amount in human-readable format (number or BigInt).
|
|
39
44
|
* @param decimals - Number of decimal places the token uses. Defaults to 18 for AI3 tokens.
|
|
40
|
-
* @returns The formatted amount in smallest units (
|
|
45
|
+
* @returns The formatted amount in smallest units (number for number input, BigInt for BigInt input).
|
|
41
46
|
*
|
|
42
47
|
* @example
|
|
43
48
|
* import { formatTokenAmount } from '@autonomys/auto-utils'
|
|
44
49
|
*
|
|
45
|
-
* // Format AI3 token amount (18 decimals)
|
|
50
|
+
* // Format AI3 token amount (18 decimals) - number input returns number
|
|
46
51
|
* const userAmount = 1.5 // 1.5 AI3
|
|
47
52
|
* const formatted = formatTokenAmount(userAmount)
|
|
48
|
-
* console.log(formatted) // Output: 1500000000000000000
|
|
53
|
+
* console.log(formatted) // Output: 1500000000000000000 (number)
|
|
49
54
|
*
|
|
50
|
-
* // Format with BigInt input
|
|
55
|
+
* // Format with BigInt input - returns BigInt
|
|
51
56
|
* const bigAmount = BigInt(10)
|
|
52
57
|
* const bigFormatted = formatTokenAmount(bigAmount)
|
|
53
|
-
* console.log(bigFormatted) // Output: 10000000000000000000n
|
|
58
|
+
* console.log(bigFormatted) // Output: 10000000000000000000n (BigInt)
|
|
54
59
|
*
|
|
55
60
|
* // Format with custom decimals
|
|
56
61
|
* const customAmount = 100
|
|
57
62
|
* const customFormatted = formatTokenAmount(customAmount, 6)
|
|
58
|
-
* console.log(customFormatted) // Output: 100000000
|
|
63
|
+
* console.log(customFormatted) // Output: 100000000 (number)
|
|
59
64
|
*/
|
|
60
65
|
export declare const formatTokenAmount: (amount: number | bigint, decimals?: number) => number | bigint;
|
|
61
66
|
/**
|
|
@@ -92,4 +97,78 @@ export declare const formatTokenAmount: (amount: number | bigint, decimals?: num
|
|
|
92
97
|
* console.log(formatSpacePledged(hugeSpace)) // Output: "1.00 EiB"
|
|
93
98
|
*/
|
|
94
99
|
export declare const formatSpacePledged: (value: bigint, decimals?: number) => string;
|
|
100
|
+
/**
|
|
101
|
+
* Parse a human-readable decimal string into smallest units as a bigint.
|
|
102
|
+
*
|
|
103
|
+
* - Accepts optional leading sign and a single decimal point
|
|
104
|
+
* - Disallows scientific notation
|
|
105
|
+
* - If fractional digits exceed `decimals`, defaults to throwing; caller can opt into truncation or rounding
|
|
106
|
+
* - If `options.rounding` is `'round'`, rounds half up based on the first discarded digit (i.e., if the first omitted digit is 5 or greater, rounds up)
|
|
107
|
+
* - If `options.rounding` is `'ceil'`, always rounds up if there are any excess fractional digits (ceiling function)
|
|
108
|
+
* - This is not banker's rounding (half to even) or round away from zero
|
|
109
|
+
*/
|
|
110
|
+
export declare const parseUnits: (value: string, decimals?: number, options?: {
|
|
111
|
+
rounding?: "error" | "truncate" | "round" | "ceil";
|
|
112
|
+
}) => bigint;
|
|
113
|
+
/**
|
|
114
|
+
* Format smallest units bigint into a human-readable decimal string.
|
|
115
|
+
*
|
|
116
|
+
* - No floating point used
|
|
117
|
+
* - Trims trailing zeros by default and removes decimal point if fractional becomes zero
|
|
118
|
+
*/
|
|
119
|
+
export declare const formatUnits: (value: bigint | string, decimals?: number, options?: {
|
|
120
|
+
trimTrailingZeros?: boolean;
|
|
121
|
+
}) => string;
|
|
122
|
+
/**
|
|
123
|
+
* Convert AI3 token amount to Shannon units (smallest units).
|
|
124
|
+
*
|
|
125
|
+
* Converts a human-readable AI3 amount (like "1.5") to its exact representation
|
|
126
|
+
* in Shannon units as a BigInt. This ensures perfect precision for financial calculations.
|
|
127
|
+
*
|
|
128
|
+
* @param ai3Amount - AI3 amount as a decimal string (e.g., "1.5", "0.000000000000000001")
|
|
129
|
+
* @param options - Rounding behavior for excess decimal places
|
|
130
|
+
* @returns Shannon amount as BigInt (e.g., 1500000000000000000n for "1.5" AI3)
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* import { ai3ToShannons } from '@autonomys/auto-utils'
|
|
134
|
+
*
|
|
135
|
+
* // Convert 1.5 AI3 to Shannon
|
|
136
|
+
* const shannons = ai3ToShannons("1.5")
|
|
137
|
+
* console.log(shannons) // 1500000000000000000n
|
|
138
|
+
*
|
|
139
|
+
* // Convert tiny amount
|
|
140
|
+
* const tinyShannons = ai3ToShannons("0.000000000000000001")
|
|
141
|
+
* console.log(tinyShannons) // 1n
|
|
142
|
+
*
|
|
143
|
+
* // Always round up with excess decimals
|
|
144
|
+
* const ceilShannons = ai3ToShannons("1.0000000000000000001", { rounding: 'ceil' })
|
|
145
|
+
* console.log(shannonsToAi3(ceilShannons)) // "1.000000000000000001"
|
|
146
|
+
*/
|
|
147
|
+
export declare const ai3ToShannons: (ai3Amount: string, options?: {
|
|
148
|
+
rounding?: "error" | "truncate" | "round" | "ceil";
|
|
149
|
+
}) => bigint;
|
|
150
|
+
/**
|
|
151
|
+
* Convert Shannon units to AI3 token amount.
|
|
152
|
+
*
|
|
153
|
+
* Converts Shannon units (smallest units) back to a human-readable AI3 amount.
|
|
154
|
+
* Uses exact BigInt arithmetic to avoid any precision loss.
|
|
155
|
+
*
|
|
156
|
+
* @param shannons - Shannon amount as BigInt or string
|
|
157
|
+
* @param options - Formatting options (trailing zero trimming)
|
|
158
|
+
* @returns AI3 amount as decimal string (e.g., "1.5" for 1500000000000000000n Shannon)
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* import { shannonsToAi3 } from '@autonomys/auto-utils'
|
|
162
|
+
*
|
|
163
|
+
* // Convert Shannon back to AI3
|
|
164
|
+
* const ai3Amount = shannonsToAi3(1500000000000000000n)
|
|
165
|
+
* console.log(ai3Amount) // "1.5"
|
|
166
|
+
*
|
|
167
|
+
* // Convert single Shannon
|
|
168
|
+
* const tinyAi3 = shannonsToAi3(1n)
|
|
169
|
+
* console.log(tinyAi3) // "0.000000000000000001"
|
|
170
|
+
*/
|
|
171
|
+
export declare const shannonsToAi3: (shannons: bigint | string, options?: {
|
|
172
|
+
trimTrailingZeros?: boolean;
|
|
173
|
+
}) => string;
|
|
95
174
|
//# sourceMappingURL=number.d.ts.map
|
package/dist/number.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AAGA
|
|
1
|
+
{"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,eAAO,MAAM,gBAAgB,GAC3B,QAAQ,MAAM,GAAG,MAAM,EACvB,WAAU,MAA+B,oBAI1C,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,eAAO,MAAM,iBAAiB,GAC5B,QAAQ,MAAM,GAAG,MAAM,EACvB,WAAU,MAA+B,oBAI1C,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,EAAE,iBAAY,WAU7D,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,UAAU,GACrB,OAAO,MAAM,EACb,WAAU,MAA+B,EACzC,UAAS;IAAE,QAAQ,CAAC,EAAE,OAAO,GAAG,UAAU,GAAG,OAAO,GAAG,MAAM,CAAA;CAAO,KACnE,MA2DF,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GACtB,OAAO,MAAM,GAAG,MAAM,EACtB,WAAU,MAA+B,EACzC,UAAS;IAAE,iBAAiB,CAAC,EAAE,OAAO,CAAA;CAAO,KAC5C,MAkBF,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,aAAa,GACxB,WAAW,MAAM,EACjB,UAAS;IAAE,QAAQ,CAAC,EAAE,OAAO,GAAG,UAAU,GAAG,OAAO,GAAG,MAAM,CAAA;CAAO,KACnE,MAAgE,CAAA;AAEnE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,aAAa,GACxB,UAAU,MAAM,GAAG,MAAM,EACzB,UAAS;IAAE,iBAAiB,CAAC,EAAE,OAAO,CAAA;CAAO,KAC5C,MAAgE,CAAA"}
|
package/dist/number.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatSpacePledged = exports.formatTokenAmount = exports.parseTokenAmount = void 0;
|
|
3
|
+
exports.shannonsToAi3 = exports.ai3ToShannons = exports.formatUnits = exports.parseUnits = exports.formatSpacePledged = exports.formatTokenAmount = exports.parseTokenAmount = void 0;
|
|
4
4
|
const number_1 = require("./constants/number");
|
|
5
5
|
const token_1 = require("./constants/token");
|
|
6
6
|
/**
|
|
@@ -8,29 +8,33 @@ const token_1 = require("./constants/token");
|
|
|
8
8
|
*
|
|
9
9
|
* This function converts token amounts stored in their smallest unit (with decimal places)
|
|
10
10
|
* back to their standard decimal representation. It handles both string and BigInt inputs
|
|
11
|
-
* and is
|
|
11
|
+
* and is useful for displaying token balances in user interfaces.
|
|
12
|
+
*
|
|
13
|
+
* This function uses floating-point arithmetic and may lose precision
|
|
14
|
+
* for large values or high-precision decimals. For precise calculations, consider using
|
|
15
|
+
* `formatUnits` or `shannonsToAi3` instead.
|
|
12
16
|
*
|
|
13
17
|
* @param amount - The token amount in smallest units (string or BigInt).
|
|
14
18
|
* @param decimals - Number of decimal places the token uses. Defaults to 18 for AI3 tokens.
|
|
15
|
-
* @returns The parsed amount as a number
|
|
19
|
+
* @returns The parsed amount as a number (for string input) or bigint (for BigInt input).
|
|
16
20
|
*
|
|
17
21
|
* @example
|
|
18
22
|
* import { parseTokenAmount } from '@autonomys/auto-utils'
|
|
19
23
|
*
|
|
20
|
-
* // Parse AI3 token amount (18 decimals)
|
|
24
|
+
* // Parse AI3 token amount (18 decimals) - string input returns number
|
|
21
25
|
* const balance = '1000000000000000000' // 1 AI3 in smallest units
|
|
22
26
|
* const readable = parseTokenAmount(balance)
|
|
23
|
-
* console.log(readable) // Output: 1
|
|
27
|
+
* console.log(readable) // Output: 1 (number)
|
|
24
28
|
*
|
|
25
|
-
* // Parse with
|
|
29
|
+
* // Parse with BigInt input - returns bigint
|
|
26
30
|
* const customBalance = BigInt('1000000') // 1 token with 6 decimals
|
|
27
31
|
* const customReadable = parseTokenAmount(customBalance, 6)
|
|
28
|
-
* console.log(customReadable) // Output:
|
|
32
|
+
* console.log(customReadable) // Output: 1n (bigint)
|
|
29
33
|
*
|
|
30
34
|
* // Parse fractional amounts
|
|
31
35
|
* const fractional = '500000000000000000' // 0.5 AI3
|
|
32
36
|
* const fractionalReadable = parseTokenAmount(fractional)
|
|
33
|
-
* console.log(fractionalReadable) // Output: 0.5
|
|
37
|
+
* console.log(fractionalReadable) // Output: 0.5 (number)
|
|
34
38
|
*/
|
|
35
39
|
const parseTokenAmount = (amount, decimals = token_1.DEFAULT_TOKEN_DECIMALS) => {
|
|
36
40
|
if (typeof amount === 'bigint')
|
|
@@ -41,31 +45,32 @@ exports.parseTokenAmount = parseTokenAmount;
|
|
|
41
45
|
/**
|
|
42
46
|
* Formats a human-readable token amount to its smallest unit representation.
|
|
43
47
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
48
|
+
*
|
|
49
|
+
* This function uses floating-point arithmetic for number inputs
|
|
50
|
+
* and may lose precision for large values or high-precision decimals. For precise calculations,
|
|
51
|
+
* consider using `parseUnits` or `ai3ToShannons` instead.
|
|
47
52
|
*
|
|
48
53
|
* @param amount - The token amount in human-readable format (number or BigInt).
|
|
49
54
|
* @param decimals - Number of decimal places the token uses. Defaults to 18 for AI3 tokens.
|
|
50
|
-
* @returns The formatted amount in smallest units (
|
|
55
|
+
* @returns The formatted amount in smallest units (number for number input, BigInt for BigInt input).
|
|
51
56
|
*
|
|
52
57
|
* @example
|
|
53
58
|
* import { formatTokenAmount } from '@autonomys/auto-utils'
|
|
54
59
|
*
|
|
55
|
-
* // Format AI3 token amount (18 decimals)
|
|
60
|
+
* // Format AI3 token amount (18 decimals) - number input returns number
|
|
56
61
|
* const userAmount = 1.5 // 1.5 AI3
|
|
57
62
|
* const formatted = formatTokenAmount(userAmount)
|
|
58
|
-
* console.log(formatted) // Output: 1500000000000000000
|
|
63
|
+
* console.log(formatted) // Output: 1500000000000000000 (number)
|
|
59
64
|
*
|
|
60
|
-
* // Format with BigInt input
|
|
65
|
+
* // Format with BigInt input - returns BigInt
|
|
61
66
|
* const bigAmount = BigInt(10)
|
|
62
67
|
* const bigFormatted = formatTokenAmount(bigAmount)
|
|
63
|
-
* console.log(bigFormatted) // Output: 10000000000000000000n
|
|
68
|
+
* console.log(bigFormatted) // Output: 10000000000000000000n (BigInt)
|
|
64
69
|
*
|
|
65
70
|
* // Format with custom decimals
|
|
66
71
|
* const customAmount = 100
|
|
67
72
|
* const customFormatted = formatTokenAmount(customAmount, 6)
|
|
68
|
-
* console.log(customFormatted) // Output: 100000000
|
|
73
|
+
* console.log(customFormatted) // Output: 100000000 (number)
|
|
69
74
|
*/
|
|
70
75
|
const formatTokenAmount = (amount, decimals = token_1.DEFAULT_TOKEN_DECIMALS) => {
|
|
71
76
|
if (typeof amount === 'bigint')
|
|
@@ -116,3 +121,145 @@ const formatSpacePledged = (value, decimals = 2) => {
|
|
|
116
121
|
return (Number(value) / Math.pow(k, i)).toFixed(dm) + ' ' + sizes[i];
|
|
117
122
|
};
|
|
118
123
|
exports.formatSpacePledged = formatSpacePledged;
|
|
124
|
+
/**
|
|
125
|
+
* Parse a human-readable decimal string into smallest units as a bigint.
|
|
126
|
+
*
|
|
127
|
+
* - Accepts optional leading sign and a single decimal point
|
|
128
|
+
* - Disallows scientific notation
|
|
129
|
+
* - If fractional digits exceed `decimals`, defaults to throwing; caller can opt into truncation or rounding
|
|
130
|
+
* - If `options.rounding` is `'round'`, rounds half up based on the first discarded digit (i.e., if the first omitted digit is 5 or greater, rounds up)
|
|
131
|
+
* - If `options.rounding` is `'ceil'`, always rounds up if there are any excess fractional digits (ceiling function)
|
|
132
|
+
* - This is not banker's rounding (half to even) or round away from zero
|
|
133
|
+
*/
|
|
134
|
+
const parseUnits = (value, decimals = token_1.DEFAULT_TOKEN_DECIMALS, options = {}) => {
|
|
135
|
+
const { rounding = 'error' } = options;
|
|
136
|
+
if (typeof value !== 'string')
|
|
137
|
+
throw new Error('parseUnits: value must be a string');
|
|
138
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
139
|
+
throw new Error('parseUnits: decimals must be a non-negative integer');
|
|
140
|
+
}
|
|
141
|
+
const trimmed = value.trim();
|
|
142
|
+
if (trimmed.length === 0)
|
|
143
|
+
throw new Error('parseUnits: empty string');
|
|
144
|
+
const signIsNegative = trimmed.startsWith('-');
|
|
145
|
+
const unsigned = trimmed.replace(/^[+-]/, '');
|
|
146
|
+
if (!/^\d+(?:\.\d+)?$/.test(unsigned))
|
|
147
|
+
throw new Error(`parseUnits: invalid numeric string "${value}"`);
|
|
148
|
+
const [wholePartRaw, fractionalRaw = ''] = unsigned.split('.');
|
|
149
|
+
const wholePart = wholePartRaw || '0';
|
|
150
|
+
const fractional = fractionalRaw.slice(0, decimals);
|
|
151
|
+
const extra = fractionalRaw.slice(decimals);
|
|
152
|
+
const base = BigInt(10) ** BigInt(decimals);
|
|
153
|
+
const whole = BigInt(wholePart) * base;
|
|
154
|
+
let frac = BigInt((fractional || '0').padEnd(decimals, '0'));
|
|
155
|
+
if (extra.length > 0) {
|
|
156
|
+
switch (rounding) {
|
|
157
|
+
case 'error':
|
|
158
|
+
throw new Error(`parseUnits: too many decimal places for ${decimals} decimals`);
|
|
159
|
+
case 'round': {
|
|
160
|
+
const roundUp = extra[0] >= '5';
|
|
161
|
+
if (roundUp) {
|
|
162
|
+
frac = frac + BigInt(1);
|
|
163
|
+
// handle carry into whole if frac rolls over
|
|
164
|
+
if (frac === base) {
|
|
165
|
+
frac = BigInt(0);
|
|
166
|
+
return (signIsNegative ? BigInt(-1) : BigInt(1)) * (whole + base);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
case 'ceil':
|
|
172
|
+
// Always round up if there are any excess digits (ceiling function)
|
|
173
|
+
frac = frac + BigInt(1);
|
|
174
|
+
// handle carry into whole if frac rolls over
|
|
175
|
+
if (frac === base) {
|
|
176
|
+
frac = BigInt(0);
|
|
177
|
+
return (signIsNegative ? BigInt(-1) : BigInt(1)) * (whole + base);
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
case 'truncate':
|
|
181
|
+
default:
|
|
182
|
+
// truncate (no-op; we already sliced)
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const result = whole + frac;
|
|
187
|
+
return signIsNegative ? -result : result;
|
|
188
|
+
};
|
|
189
|
+
exports.parseUnits = parseUnits;
|
|
190
|
+
/**
|
|
191
|
+
* Format smallest units bigint into a human-readable decimal string.
|
|
192
|
+
*
|
|
193
|
+
* - No floating point used
|
|
194
|
+
* - Trims trailing zeros by default and removes decimal point if fractional becomes zero
|
|
195
|
+
*/
|
|
196
|
+
const formatUnits = (value, decimals = token_1.DEFAULT_TOKEN_DECIMALS, options = {}) => {
|
|
197
|
+
const { trimTrailingZeros = true } = options;
|
|
198
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
199
|
+
throw new Error('formatUnits: decimals must be a non-negative integer');
|
|
200
|
+
}
|
|
201
|
+
const v = typeof value === 'bigint' ? value : BigInt(value);
|
|
202
|
+
const negative = v < BigInt(0);
|
|
203
|
+
const abs = negative ? -v : v;
|
|
204
|
+
const base = BigInt(10) ** BigInt(decimals);
|
|
205
|
+
const whole = abs / base;
|
|
206
|
+
const fractional = abs % base;
|
|
207
|
+
if (fractional === BigInt(0))
|
|
208
|
+
return `${negative ? '-' : ''}${whole.toString()}`;
|
|
209
|
+
let fracStr = fractional.toString().padStart(decimals, '0');
|
|
210
|
+
if (trimTrailingZeros)
|
|
211
|
+
fracStr = fracStr.replace(/0+$/, '');
|
|
212
|
+
const sign = negative ? '-' : '';
|
|
213
|
+
return fracStr.length ? `${sign}${whole.toString()}.${fracStr}` : `${sign}${whole.toString()}`;
|
|
214
|
+
};
|
|
215
|
+
exports.formatUnits = formatUnits;
|
|
216
|
+
/**
|
|
217
|
+
* Convert AI3 token amount to Shannon units (smallest units).
|
|
218
|
+
*
|
|
219
|
+
* Converts a human-readable AI3 amount (like "1.5") to its exact representation
|
|
220
|
+
* in Shannon units as a BigInt. This ensures perfect precision for financial calculations.
|
|
221
|
+
*
|
|
222
|
+
* @param ai3Amount - AI3 amount as a decimal string (e.g., "1.5", "0.000000000000000001")
|
|
223
|
+
* @param options - Rounding behavior for excess decimal places
|
|
224
|
+
* @returns Shannon amount as BigInt (e.g., 1500000000000000000n for "1.5" AI3)
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* import { ai3ToShannons } from '@autonomys/auto-utils'
|
|
228
|
+
*
|
|
229
|
+
* // Convert 1.5 AI3 to Shannon
|
|
230
|
+
* const shannons = ai3ToShannons("1.5")
|
|
231
|
+
* console.log(shannons) // 1500000000000000000n
|
|
232
|
+
*
|
|
233
|
+
* // Convert tiny amount
|
|
234
|
+
* const tinyShannons = ai3ToShannons("0.000000000000000001")
|
|
235
|
+
* console.log(tinyShannons) // 1n
|
|
236
|
+
*
|
|
237
|
+
* // Always round up with excess decimals
|
|
238
|
+
* const ceilShannons = ai3ToShannons("1.0000000000000000001", { rounding: 'ceil' })
|
|
239
|
+
* console.log(shannonsToAi3(ceilShannons)) // "1.000000000000000001"
|
|
240
|
+
*/
|
|
241
|
+
const ai3ToShannons = (ai3Amount, options = {}) => (0, exports.parseUnits)(ai3Amount, token_1.DEFAULT_TOKEN_DECIMALS, options);
|
|
242
|
+
exports.ai3ToShannons = ai3ToShannons;
|
|
243
|
+
/**
|
|
244
|
+
* Convert Shannon units to AI3 token amount.
|
|
245
|
+
*
|
|
246
|
+
* Converts Shannon units (smallest units) back to a human-readable AI3 amount.
|
|
247
|
+
* Uses exact BigInt arithmetic to avoid any precision loss.
|
|
248
|
+
*
|
|
249
|
+
* @param shannons - Shannon amount as BigInt or string
|
|
250
|
+
* @param options - Formatting options (trailing zero trimming)
|
|
251
|
+
* @returns AI3 amount as decimal string (e.g., "1.5" for 1500000000000000000n Shannon)
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* import { shannonsToAi3 } from '@autonomys/auto-utils'
|
|
255
|
+
*
|
|
256
|
+
* // Convert Shannon back to AI3
|
|
257
|
+
* const ai3Amount = shannonsToAi3(1500000000000000000n)
|
|
258
|
+
* console.log(ai3Amount) // "1.5"
|
|
259
|
+
*
|
|
260
|
+
* // Convert single Shannon
|
|
261
|
+
* const tinyAi3 = shannonsToAi3(1n)
|
|
262
|
+
* console.log(tinyAi3) // "0.000000000000000001"
|
|
263
|
+
*/
|
|
264
|
+
const shannonsToAi3 = (shannons, options = {}) => (0, exports.formatUnits)(shannons, token_1.DEFAULT_TOKEN_DECIMALS, options);
|
|
265
|
+
exports.shannonsToAi3 = shannonsToAi3;
|
package/dist/utils/sign.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.signatureVerify = exports.signingKey = exports.signMessage = void 0;
|
|
13
4
|
const util_crypto_1 = require("@polkadot/util-crypto");
|
|
@@ -49,15 +40,15 @@ const format_1 = require("./format");
|
|
|
49
40
|
*
|
|
50
41
|
* @throws {Error} When the signer doesn't support raw signing or signing fails.
|
|
51
42
|
*/
|
|
52
|
-
const signMessage = (signer, address, data) =>
|
|
43
|
+
const signMessage = async (signer, address, data) => {
|
|
53
44
|
if (!signer.signRaw)
|
|
54
45
|
throw new Error('signRaw not available on the signer');
|
|
55
|
-
return
|
|
46
|
+
return await signer.signRaw({
|
|
56
47
|
address,
|
|
57
48
|
type: 'bytes',
|
|
58
49
|
data,
|
|
59
50
|
});
|
|
60
|
-
}
|
|
51
|
+
};
|
|
61
52
|
exports.signMessage = signMessage;
|
|
62
53
|
/**
|
|
63
54
|
* Converts a public key to a hexadecimal string representation.
|
|
@@ -1,14 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// file: src/utils/signAndSendTx.ts
|
|
3
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
4
|
exports.signAndSendTx = void 0;
|
|
14
5
|
const detectTxSuccess_1 = require("./detectTxSuccess");
|
|
@@ -85,14 +76,14 @@ const validateEvents_1 = require("./validateEvents");
|
|
|
85
76
|
* @throws {Error} When the transaction is retracted, dropped, or invalid.
|
|
86
77
|
* @throws {Error} When custom error mapping indicates a specific error condition.
|
|
87
78
|
*/
|
|
88
|
-
const signAndSendTx =
|
|
79
|
+
const signAndSendTx = async (sender, tx, options = {}, eventsExpected = events_1.expectSuccessfulTxEvent, log = false, mapErrorCodeToEnum) => {
|
|
89
80
|
let success = false;
|
|
90
81
|
let txHashHex = undefined;
|
|
91
82
|
let blockHash = undefined;
|
|
92
83
|
const eventsValidated = { expected: [], found: [] };
|
|
93
84
|
let identifier = null;
|
|
94
|
-
const receipt =
|
|
95
|
-
tx.signAndSend(sender, options, (result) =>
|
|
85
|
+
const receipt = await new Promise((resolve, reject) => {
|
|
86
|
+
tx.signAndSend(sender, options, async (result) => {
|
|
96
87
|
const { events = [], status, dispatchError } = result;
|
|
97
88
|
if (status.isInBlock || status.isFinalized) {
|
|
98
89
|
txHashHex = result.txHash.toHex();
|
|
@@ -112,7 +103,7 @@ const signAndSendTx = (sender_1, tx_1, ...args_1) => __awaiter(void 0, [sender_1
|
|
|
112
103
|
events.forEach(({ event: { section, method, data } }) => {
|
|
113
104
|
if (section === 'system' && method === 'ExtrinsicFailed') {
|
|
114
105
|
const dispatchErrorJson = JSON.parse(dispatchError.toString());
|
|
115
|
-
const errorEnum = mapErrorCodeToEnum
|
|
106
|
+
const errorEnum = mapErrorCodeToEnum?.(dispatchErrorJson.module.error);
|
|
116
107
|
reject(new Error(`Extrinsic failed: ${errorEnum} in block #${blockHash} with error: ${dispatchErrorJson}`));
|
|
117
108
|
}
|
|
118
109
|
if (section === 'autoId' && method === 'NewAutoIdRegistered') {
|
|
@@ -134,8 +125,8 @@ const signAndSendTx = (sender_1, tx_1, ...args_1) => __awaiter(void 0, [sender_1
|
|
|
134
125
|
console.error('Transaction failed');
|
|
135
126
|
reject(new Error('Transaction failed'));
|
|
136
127
|
}
|
|
137
|
-
})
|
|
128
|
+
});
|
|
138
129
|
});
|
|
139
130
|
return { success, txHash: txHashHex, blockHash, events: eventsValidated, receipt, identifier };
|
|
140
|
-
}
|
|
131
|
+
};
|
|
141
132
|
exports.signAndSendTx = signAndSendTx;
|
package/dist/wallet.js
CHANGED
|
@@ -33,15 +33,6 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
return result;
|
|
34
34
|
};
|
|
35
35
|
})();
|
|
36
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
37
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
38
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
39
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
40
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
41
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
42
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
37
|
exports.getMockWallet = exports.mockWallets = exports.activateWallet = exports.generateWallet = exports.setupWallet = void 0;
|
|
47
38
|
const api_1 = require("@polkadot/api");
|
|
@@ -203,13 +194,13 @@ exports.generateWallet = generateWallet;
|
|
|
203
194
|
*
|
|
204
195
|
* @throws {Error} When no wallet credentials are provided or connection fails.
|
|
205
196
|
*/
|
|
206
|
-
const activateWallet = (params) =>
|
|
197
|
+
const activateWallet = async (params) => {
|
|
207
198
|
if (!params.api) {
|
|
208
199
|
// Create the API instance if not provided
|
|
209
200
|
params.api =
|
|
210
201
|
params.domainId === undefined
|
|
211
|
-
?
|
|
212
|
-
:
|
|
202
|
+
? await (0, api_2.activate)(params)
|
|
203
|
+
: await (0, api_2.activateDomain)(params);
|
|
213
204
|
}
|
|
214
205
|
const accounts = [];
|
|
215
206
|
if (params.mnemonic || params.uri) {
|
|
@@ -219,11 +210,11 @@ const activateWallet = (params) => __awaiter(void 0, void 0, void 0, function* (
|
|
|
219
210
|
accounts.push(keyringPair);
|
|
220
211
|
}
|
|
221
212
|
else if (typeof window !== 'undefined') {
|
|
222
|
-
const { web3Enable, web3Accounts } =
|
|
213
|
+
const { web3Enable, web3Accounts } = await Promise.resolve().then(() => __importStar(require('@polkadot/extension-dapp')));
|
|
223
214
|
// Enable Polkadot.js extension in the browser
|
|
224
|
-
|
|
215
|
+
await web3Enable(params.appName || 'Auto');
|
|
225
216
|
// Get the list of accounts from the extension
|
|
226
|
-
const allAccounts =
|
|
217
|
+
const allAccounts = await web3Accounts();
|
|
227
218
|
accounts.push(...allAccounts);
|
|
228
219
|
if (allAccounts.length === 0)
|
|
229
220
|
console.warn('No accounts found in the Polkadot.js extension');
|
|
@@ -237,7 +228,7 @@ const activateWallet = (params) => __awaiter(void 0, void 0, void 0, function* (
|
|
|
237
228
|
? accounts[0].address
|
|
238
229
|
: (0, address_1.address)(accounts[0].address),
|
|
239
230
|
};
|
|
240
|
-
}
|
|
231
|
+
};
|
|
241
232
|
exports.activateWallet = activateWallet;
|
|
242
233
|
/**
|
|
243
234
|
* Creates a collection of mock wallets for testing and development purposes.
|
|
@@ -285,18 +276,21 @@ exports.activateWallet = activateWallet;
|
|
|
285
276
|
* await wallet.api.disconnect()
|
|
286
277
|
* }
|
|
287
278
|
*/
|
|
288
|
-
const mockWallets =
|
|
279
|
+
const mockWallets = async (network = { networkId: network_1.defaultNetwork.id }, api, type) => {
|
|
289
280
|
if (!type)
|
|
290
281
|
type = 'sr25519';
|
|
291
282
|
const wallets = [];
|
|
292
283
|
for (const uri of wallet_1.mockURIs) {
|
|
293
|
-
const wallet =
|
|
284
|
+
const wallet = await (0, exports.activateWallet)({
|
|
285
|
+
...network,
|
|
286
|
+
uri,
|
|
294
287
|
api,
|
|
295
|
-
type
|
|
288
|
+
type,
|
|
289
|
+
});
|
|
296
290
|
wallets.push(wallet);
|
|
297
291
|
}
|
|
298
292
|
return wallets;
|
|
299
|
-
}
|
|
293
|
+
};
|
|
300
294
|
exports.mockWallets = mockWallets;
|
|
301
295
|
/**
|
|
302
296
|
* Retrieves a specific mock wallet by name from a collection of mock wallets.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autonomys/auto-utils",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.17",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"ts-jest": "^29.3.1",
|
|
40
40
|
"typescript": "^5.8.3"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "890e12b9cc437f3f4409b74663a77b854bf048aa"
|
|
43
43
|
}
|