aether-hub 1.2.6 → 1.2.8
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/commands/account.js +280 -304
- package/commands/apy.js +480 -480
- package/commands/balance.js +276 -0
- package/commands/blockhash.js +181 -0
- package/commands/broadcast.js +323 -323
- package/commands/claim.js +292 -0
- package/commands/delegations.js +45 -95
- package/commands/emergency.js +667 -657
- package/commands/epoch.js +275 -357
- package/commands/fees.js +276 -0
- package/commands/info.js +495 -536
- package/commands/monitor.js +431 -431
- package/commands/multisig.js +726 -726
- package/commands/network.js +429 -503
- package/commands/ping.js +266 -320
- package/commands/price.js +253 -253
- package/commands/sdk-test.js +477 -0
- package/commands/sdk.js +537 -537
- package/commands/slot.js +155 -0
- package/commands/snapshot.js +509 -509
- package/commands/stake-info.js +139 -0
- package/commands/stake-positions.js +31 -46
- package/commands/stats.js +418 -418
- package/commands/status.js +326 -370
- package/commands/supply.js +37 -83
- package/commands/tps.js +238 -0
- package/commands/transfer.js +495 -0
- package/commands/tx-history.js +65 -227
- package/commands/validator-info.js +10 -4
- package/commands/validator-start.js +1 -1
- package/commands/validator-status.js +32 -73
- package/commands/validators.js +36 -75
- package/commands/wallet.js +8 -29
- package/index.js +65 -8
- package/package.json +1 -3
package/commands/broadcast.js
CHANGED
|
@@ -1,323 +1,323 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* aether-cli broadcast
|
|
4
|
-
*
|
|
5
|
-
* Broadcast a signed transaction to the Aether network.
|
|
6
|
-
* Accepts a base58-encoded transaction signature or a raw JSON payload.
|
|
7
|
-
* Useful for submitting offline-constructed transactions.
|
|
8
|
-
*
|
|
9
|
-
* Usage:
|
|
10
|
-
* aether broadcast --tx <signature> Broadcast by tx signature
|
|
11
|
-
* aether broadcast --json <payload> Broadcast raw JSON tx payload
|
|
12
|
-
* aether broadcast --file <path> Read tx from a JSON file
|
|
13
|
-
* aether broadcast --rpc <url> Use a specific RPC endpoint
|
|
14
|
-
* aether broadcast --json-output Output result as JSON
|
|
15
|
-
*
|
|
16
|
-
* Examples:
|
|
17
|
-
* aether broadcast --tx 5abcdef... # Submit pre-signed tx
|
|
18
|
-
* aether broadcast --json '{"type":"Transfer",...}'
|
|
19
|
-
* aether broadcast --file ./unsigned_tx.json
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
const fs = require('fs');
|
|
23
|
-
const path = require('path');
|
|
24
|
-
const https = require('https');
|
|
25
|
-
const http = require('http');
|
|
26
|
-
|
|
27
|
-
// ANSI colours
|
|
28
|
-
const C = {
|
|
29
|
-
reset: '\x1b[0m',
|
|
30
|
-
bright: '\x1b[1m',
|
|
31
|
-
dim: '\x1b[2m',
|
|
32
|
-
red: '\x1b[31m',
|
|
33
|
-
green: '\x1b[32m',
|
|
34
|
-
yellow: '\x1b[33m',
|
|
35
|
-
cyan: '\x1b[36m',
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const CLI_VERSION = '1.0.0';
|
|
39
|
-
|
|
40
|
-
// ---------------------------------------------------------------------------
|
|
41
|
-
// Config
|
|
42
|
-
// ---------------------------------------------------------------------------
|
|
43
|
-
|
|
44
|
-
function getDefaultRpc() {
|
|
45
|
-
return process.env.AETHER_RPC || 'http://127.0.0.1:8899';
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// ---------------------------------------------------------------------------
|
|
49
|
-
// Argument parsing
|
|
50
|
-
// ---------------------------------------------------------------------------
|
|
51
|
-
|
|
52
|
-
function parseArgs() {
|
|
53
|
-
const args = process.argv.slice(2);
|
|
54
|
-
const opts = {
|
|
55
|
-
rpc: getDefaultRpc(),
|
|
56
|
-
signature: null,
|
|
57
|
-
jsonPayload: null,
|
|
58
|
-
filePath: null,
|
|
59
|
-
asJson: false,
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
for (let i = 0; i < args.length; i++) {
|
|
63
|
-
if (args[i] === '--tx' || args[i] === '-t') {
|
|
64
|
-
opts.signature = args[++i];
|
|
65
|
-
} else if (args[i] === '--json' || args[i] === '-j') {
|
|
66
|
-
opts.jsonPayload = args[++i];
|
|
67
|
-
} else if (args[i] === '--file' || args[i] === '-f') {
|
|
68
|
-
opts.filePath = args[++i];
|
|
69
|
-
} else if (args[i] === '--rpc' || args[i] === '-r') {
|
|
70
|
-
opts.rpc = args[++i];
|
|
71
|
-
} else if (args[i] === '--json-output') {
|
|
72
|
-
opts.asJson = true;
|
|
73
|
-
} else if (args[i] === '--help' || args[i] === '-h') {
|
|
74
|
-
showHelp();
|
|
75
|
-
process.exit(0);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return opts;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function showHelp() {
|
|
83
|
-
console.log(`
|
|
84
|
-
${C.bright}${C.cyan}aether-cli broadcast${C.reset} - Broadcast a Signed Transaction
|
|
85
|
-
|
|
86
|
-
${C.bright}Usage:${C.reset}
|
|
87
|
-
aether-cli broadcast --tx <signature> Broadcast by base58 signature
|
|
88
|
-
aether-cli broadcast --json <payload> Broadcast inline JSON payload
|
|
89
|
-
aether-cli broadcast --file <path> Read tx from a JSON file
|
|
90
|
-
aether-cli broadcast --rpc <url> Override default RPC
|
|
91
|
-
aether-cli broadcast --json-output JSON output for scripting
|
|
92
|
-
|
|
93
|
-
${C.bright}Examples:${C.reset}
|
|
94
|
-
aether-cli broadcast --tx 5abcdef123456... # Submit by signature
|
|
95
|
-
aether-cli broadcast --json '{"type":"Transfer","data":{...}}'
|
|
96
|
-
aether-cli broadcast --file ./my_tx.json # Read and broadcast from file
|
|
97
|
-
`.trim());
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// ---------------------------------------------------------------------------
|
|
101
|
-
// HTTP helpers
|
|
102
|
-
// ---------------------------------------------------------------------------
|
|
103
|
-
|
|
104
|
-
function httpRequest(rpcUrl, pathStr, method = 'GET', body = null, timeoutMs = 15000) {
|
|
105
|
-
return new Promise((resolve, reject) => {
|
|
106
|
-
const url = new URL(pathStr, rpcUrl);
|
|
107
|
-
const lib = url.protocol === 'https:' ? https : http;
|
|
108
|
-
const bodyStr = body ? JSON.stringify(body) : null;
|
|
109
|
-
|
|
110
|
-
const reqOptions = {
|
|
111
|
-
hostname: url.hostname,
|
|
112
|
-
port: url.port || (url.protocol === 'https:' ? 443 : 80),
|
|
113
|
-
path: url.pathname + url.search,
|
|
114
|
-
method,
|
|
115
|
-
timeout: timeoutMs,
|
|
116
|
-
headers: {
|
|
117
|
-
'Content-Type': 'application/json',
|
|
118
|
-
...(bodyStr ? { 'Content-Length': Buffer.byteLength(bodyStr) } : {}),
|
|
119
|
-
},
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const req = lib.request(reqOptions, (res) => {
|
|
123
|
-
let data = '';
|
|
124
|
-
res.on('data', (chunk) => (data += chunk));
|
|
125
|
-
res.on('end', () => {
|
|
126
|
-
try { resolve(JSON.parse(data)); }
|
|
127
|
-
catch { resolve({ raw: data }); }
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
req.on('error', reject);
|
|
132
|
-
req.on('timeout', () => { req.destroy(); reject(new Error('Request timeout')); });
|
|
133
|
-
if (bodyStr) req.write(bodyStr);
|
|
134
|
-
req.end();
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function httpPost(rpcUrl, pathStr, body, timeoutMs = 15000) {
|
|
139
|
-
return httpRequest(rpcUrl, pathStr, 'POST', body, timeoutMs);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// ---------------------------------------------------------------------------
|
|
143
|
-
// Validate transaction payload
|
|
144
|
-
// ---------------------------------------------------------------------------
|
|
145
|
-
|
|
146
|
-
function validateTxPayload(tx) {
|
|
147
|
-
const errors = [];
|
|
148
|
-
|
|
149
|
-
if (!tx) {
|
|
150
|
-
errors.push('Transaction payload is null or empty');
|
|
151
|
-
return errors;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Must have signer
|
|
155
|
-
if (!tx.signer && !tx.from && !tx.pubkey) {
|
|
156
|
-
errors.push('Missing signer field (signer | from | pubkey)');
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Must have tx_type or type
|
|
160
|
-
if (!tx.tx_type && !tx.type) {
|
|
161
|
-
errors.push('Missing tx_type or type field');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Must have payload
|
|
165
|
-
if (!tx.payload && !tx.data) {
|
|
166
|
-
errors.push('Missing payload or data field');
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return errors;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// ---------------------------------------------------------------------------
|
|
173
|
-
// Broadcast logic
|
|
174
|
-
// ---------------------------------------------------------------------------
|
|
175
|
-
|
|
176
|
-
async function broadcast({ rpc, signature, jsonPayload, filePath, asJson }) {
|
|
177
|
-
// Build the tx object from inputs (priority: signature > file > inline JSON)
|
|
178
|
-
let tx = null;
|
|
179
|
-
|
|
180
|
-
if (signature) {
|
|
181
|
-
// Signature-only broadcast: POST /v1/tx with { signature }
|
|
182
|
-
tx = { signature };
|
|
183
|
-
} else if (filePath) {
|
|
184
|
-
// Read from file
|
|
185
|
-
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(process.cwd(), filePath);
|
|
186
|
-
if (!fs.existsSync(absPath)) {
|
|
187
|
-
throw new Error(`File not found: ${absPath}`);
|
|
188
|
-
}
|
|
189
|
-
try {
|
|
190
|
-
tx = JSON.parse(fs.readFileSync(absPath, 'utf8'));
|
|
191
|
-
} catch {
|
|
192
|
-
throw new Error(`Invalid JSON in file: ${absPath}`);
|
|
193
|
-
}
|
|
194
|
-
} else if (jsonPayload) {
|
|
195
|
-
try {
|
|
196
|
-
tx = JSON.parse(jsonPayload);
|
|
197
|
-
} catch {
|
|
198
|
-
throw new Error('Invalid JSON payload provided with --json');
|
|
199
|
-
}
|
|
200
|
-
} else {
|
|
201
|
-
throw new Error('No transaction provided. Use --tx, --json, or --file');
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Validate the transaction has required fields
|
|
205
|
-
if (!signature) {
|
|
206
|
-
const validationErrors = validateTxPayload(tx);
|
|
207
|
-
if (validationErrors.length > 0) {
|
|
208
|
-
throw new Error('Invalid transaction payload:\n ' + validationErrors.join('\n '));
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (!asJson) {
|
|
213
|
-
console.log(`\n${C.bright}${C.cyan}── Broadcast Transaction ─────────────────────────────────────${C.reset}`);
|
|
214
|
-
console.log(` ${C.dim}RPC: ${rpc}${C.reset}`);
|
|
215
|
-
if (signature) {
|
|
216
|
-
console.log(` ${C.dim}Signature:${C.reset} ${C.cyan}${signature}${C.reset}`);
|
|
217
|
-
} else {
|
|
218
|
-
const txType = tx.tx_type || tx.type || 'Unknown';
|
|
219
|
-
const signer = tx.signer || tx.from || tx.pubkey || 'unknown';
|
|
220
|
-
console.log(` ${C.dim}Type:${C.reset} ${C.cyan}${txType}${C.reset}`);
|
|
221
|
-
console.log(` ${C.dim}Signer:${C.reset} ${C.cyan}${signer}${C.reset}`);
|
|
222
|
-
}
|
|
223
|
-
console.log();
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// Submit the transaction
|
|
227
|
-
let result;
|
|
228
|
-
let latencyMs;
|
|
229
|
-
|
|
230
|
-
try {
|
|
231
|
-
const start = Date.now();
|
|
232
|
-
result = await httpPost(rpc, '/v1/tx', tx);
|
|
233
|
-
latencyMs = Date.now() - start;
|
|
234
|
-
} catch (err) {
|
|
235
|
-
if (asJson) {
|
|
236
|
-
console.log(JSON.stringify({
|
|
237
|
-
success: false,
|
|
238
|
-
error: err.message,
|
|
239
|
-
rpc,
|
|
240
|
-
cli_version: CLI_VERSION,
|
|
241
|
-
timestamp: new Date().toISOString(),
|
|
242
|
-
}, null, 2));
|
|
243
|
-
} else {
|
|
244
|
-
console.log(` ${C.red}✗ Network error:${C.reset} ${err.message}`);
|
|
245
|
-
console.log(` ${C.dim} Check that your RPC is accessible: ${rpc}${C.reset}`);
|
|
246
|
-
}
|
|
247
|
-
process.exit(1);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const success = result && !result.error && result.accepted !== false;
|
|
251
|
-
|
|
252
|
-
if (asJson) {
|
|
253
|
-
console.log(JSON.stringify({
|
|
254
|
-
success,
|
|
255
|
-
accepted: result?.accepted ?? null,
|
|
256
|
-
signature: result?.signature ?? result?.tx_signature ?? signature ?? null,
|
|
257
|
-
slot: result?.slot ?? null,
|
|
258
|
-
error: result?.error ?? null,
|
|
259
|
-
rpc,
|
|
260
|
-
latency_ms: latencyMs,
|
|
261
|
-
cli_version: CLI_VERSION,
|
|
262
|
-
timestamp: new Date().toISOString(),
|
|
263
|
-
}, null, 2));
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
if (success) {
|
|
268
|
-
const sig = result?.signature ?? result?.tx_signature ?? signature ?? 'unknown';
|
|
269
|
-
console.log(`${C.green}✓ Transaction accepted!${C.reset}`);
|
|
270
|
-
console.log(` ${C.green}★${C.reset} ${C.bright}Signature:${C.reset} ${sig}`);
|
|
271
|
-
if (result?.slot) {
|
|
272
|
-
console.log(` ${C.dim} Slot:${C.reset} ${result.slot}`);
|
|
273
|
-
}
|
|
274
|
-
console.log(` ${C.dim} Latency:${C.reset} ${latencyMs}ms`);
|
|
275
|
-
console.log(` ${C.dim} RPC:${C.reset} ${rpc}`);
|
|
276
|
-
console.log();
|
|
277
|
-
} else {
|
|
278
|
-
const errMsg = result?.error || 'Transaction rejected by network';
|
|
279
|
-
console.log(`${C.red}✗ Transaction rejected${C.reset}`);
|
|
280
|
-
if (result?.error) {
|
|
281
|
-
console.log(` ${C.red} Error:${C.reset} ${result.error}`);
|
|
282
|
-
}
|
|
283
|
-
if (result?.logs) {
|
|
284
|
-
console.log(` ${C.dim} Logs:${C.reset}`);
|
|
285
|
-
for (const log of result.logs) {
|
|
286
|
-
console.log(` ${C.dim}${log}${C.reset}`);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
console.log(` ${C.dim} Latency:${C.reset} ${latencyMs}ms`);
|
|
290
|
-
console.log(` ${C.dim} RPC:${C.reset} ${rpc}`);
|
|
291
|
-
console.log();
|
|
292
|
-
process.exit(1);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// ---------------------------------------------------------------------------
|
|
297
|
-
// Main
|
|
298
|
-
// ---------------------------------------------------------------------------
|
|
299
|
-
|
|
300
|
-
async function main() {
|
|
301
|
-
const opts = parseArgs();
|
|
302
|
-
|
|
303
|
-
try {
|
|
304
|
-
await broadcast(opts);
|
|
305
|
-
} catch (err) {
|
|
306
|
-
if (opts.asJson) {
|
|
307
|
-
console.log(JSON.stringify({
|
|
308
|
-
success: false,
|
|
309
|
-
error: err.message,
|
|
310
|
-
rpc: opts.rpc,
|
|
311
|
-
cli_version: CLI_VERSION,
|
|
312
|
-
timestamp: new Date().toISOString(),
|
|
313
|
-
}, null, 2));
|
|
314
|
-
} else {
|
|
315
|
-
console.log(`\n ${C.red}✗ ${err.message}${C.reset}\n`);
|
|
316
|
-
}
|
|
317
|
-
process.exit(1);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
main();
|
|
322
|
-
|
|
323
|
-
module.exports = { broadcastCommand: main };
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* aether-cli broadcast
|
|
4
|
+
*
|
|
5
|
+
* Broadcast a signed transaction to the Aether network.
|
|
6
|
+
* Accepts a base58-encoded transaction signature or a raw JSON payload.
|
|
7
|
+
* Useful for submitting offline-constructed transactions.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* aether broadcast --tx <signature> Broadcast by tx signature
|
|
11
|
+
* aether broadcast --json <payload> Broadcast raw JSON tx payload
|
|
12
|
+
* aether broadcast --file <path> Read tx from a JSON file
|
|
13
|
+
* aether broadcast --rpc <url> Use a specific RPC endpoint
|
|
14
|
+
* aether broadcast --json-output Output result as JSON
|
|
15
|
+
*
|
|
16
|
+
* Examples:
|
|
17
|
+
* aether broadcast --tx 5abcdef... # Submit pre-signed tx
|
|
18
|
+
* aether broadcast --json '{"type":"Transfer",...}'
|
|
19
|
+
* aether broadcast --file ./unsigned_tx.json
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const fs = require('fs');
|
|
23
|
+
const path = require('path');
|
|
24
|
+
const https = require('https');
|
|
25
|
+
const http = require('http');
|
|
26
|
+
|
|
27
|
+
// ANSI colours
|
|
28
|
+
const C = {
|
|
29
|
+
reset: '\x1b[0m',
|
|
30
|
+
bright: '\x1b[1m',
|
|
31
|
+
dim: '\x1b[2m',
|
|
32
|
+
red: '\x1b[31m',
|
|
33
|
+
green: '\x1b[32m',
|
|
34
|
+
yellow: '\x1b[33m',
|
|
35
|
+
cyan: '\x1b[36m',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const CLI_VERSION = '1.0.0';
|
|
39
|
+
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// Config
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
function getDefaultRpc() {
|
|
45
|
+
return process.env.AETHER_RPC || 'http://127.0.0.1:8899';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
// Argument parsing
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
function parseArgs() {
|
|
53
|
+
const args = process.argv.slice(2);
|
|
54
|
+
const opts = {
|
|
55
|
+
rpc: getDefaultRpc(),
|
|
56
|
+
signature: null,
|
|
57
|
+
jsonPayload: null,
|
|
58
|
+
filePath: null,
|
|
59
|
+
asJson: false,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
for (let i = 0; i < args.length; i++) {
|
|
63
|
+
if (args[i] === '--tx' || args[i] === '-t') {
|
|
64
|
+
opts.signature = args[++i];
|
|
65
|
+
} else if (args[i] === '--json' || args[i] === '-j') {
|
|
66
|
+
opts.jsonPayload = args[++i];
|
|
67
|
+
} else if (args[i] === '--file' || args[i] === '-f') {
|
|
68
|
+
opts.filePath = args[++i];
|
|
69
|
+
} else if (args[i] === '--rpc' || args[i] === '-r') {
|
|
70
|
+
opts.rpc = args[++i];
|
|
71
|
+
} else if (args[i] === '--json-output') {
|
|
72
|
+
opts.asJson = true;
|
|
73
|
+
} else if (args[i] === '--help' || args[i] === '-h') {
|
|
74
|
+
showHelp();
|
|
75
|
+
process.exit(0);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return opts;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function showHelp() {
|
|
83
|
+
console.log(`
|
|
84
|
+
${C.bright}${C.cyan}aether-cli broadcast${C.reset} - Broadcast a Signed Transaction
|
|
85
|
+
|
|
86
|
+
${C.bright}Usage:${C.reset}
|
|
87
|
+
aether-cli broadcast --tx <signature> Broadcast by base58 signature
|
|
88
|
+
aether-cli broadcast --json <payload> Broadcast inline JSON payload
|
|
89
|
+
aether-cli broadcast --file <path> Read tx from a JSON file
|
|
90
|
+
aether-cli broadcast --rpc <url> Override default RPC
|
|
91
|
+
aether-cli broadcast --json-output JSON output for scripting
|
|
92
|
+
|
|
93
|
+
${C.bright}Examples:${C.reset}
|
|
94
|
+
aether-cli broadcast --tx 5abcdef123456... # Submit by signature
|
|
95
|
+
aether-cli broadcast --json '{"type":"Transfer","data":{...}}'
|
|
96
|
+
aether-cli broadcast --file ./my_tx.json # Read and broadcast from file
|
|
97
|
+
`.trim());
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
// HTTP helpers
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
function httpRequest(rpcUrl, pathStr, method = 'GET', body = null, timeoutMs = 15000) {
|
|
105
|
+
return new Promise((resolve, reject) => {
|
|
106
|
+
const url = new URL(pathStr, rpcUrl);
|
|
107
|
+
const lib = url.protocol === 'https:' ? https : http;
|
|
108
|
+
const bodyStr = body ? JSON.stringify(body) : null;
|
|
109
|
+
|
|
110
|
+
const reqOptions = {
|
|
111
|
+
hostname: url.hostname,
|
|
112
|
+
port: url.port || (url.protocol === 'https:' ? 443 : 80),
|
|
113
|
+
path: url.pathname + url.search,
|
|
114
|
+
method,
|
|
115
|
+
timeout: timeoutMs,
|
|
116
|
+
headers: {
|
|
117
|
+
'Content-Type': 'application/json',
|
|
118
|
+
...(bodyStr ? { 'Content-Length': Buffer.byteLength(bodyStr) } : {}),
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const req = lib.request(reqOptions, (res) => {
|
|
123
|
+
let data = '';
|
|
124
|
+
res.on('data', (chunk) => (data += chunk));
|
|
125
|
+
res.on('end', () => {
|
|
126
|
+
try { resolve(JSON.parse(data)); }
|
|
127
|
+
catch { resolve({ raw: data }); }
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
req.on('error', reject);
|
|
132
|
+
req.on('timeout', () => { req.destroy(); reject(new Error('Request timeout')); });
|
|
133
|
+
if (bodyStr) req.write(bodyStr);
|
|
134
|
+
req.end();
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function httpPost(rpcUrl, pathStr, body, timeoutMs = 15000) {
|
|
139
|
+
return httpRequest(rpcUrl, pathStr, 'POST', body, timeoutMs);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
// Validate transaction payload
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
function validateTxPayload(tx) {
|
|
147
|
+
const errors = [];
|
|
148
|
+
|
|
149
|
+
if (!tx) {
|
|
150
|
+
errors.push('Transaction payload is null or empty');
|
|
151
|
+
return errors;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Must have signer
|
|
155
|
+
if (!tx.signer && !tx.from && !tx.pubkey) {
|
|
156
|
+
errors.push('Missing signer field (signer | from | pubkey)');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Must have tx_type or type
|
|
160
|
+
if (!tx.tx_type && !tx.type) {
|
|
161
|
+
errors.push('Missing tx_type or type field');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Must have payload
|
|
165
|
+
if (!tx.payload && !tx.data) {
|
|
166
|
+
errors.push('Missing payload or data field');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return errors;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
// Broadcast logic
|
|
174
|
+
// ---------------------------------------------------------------------------
|
|
175
|
+
|
|
176
|
+
async function broadcast({ rpc, signature, jsonPayload, filePath, asJson }) {
|
|
177
|
+
// Build the tx object from inputs (priority: signature > file > inline JSON)
|
|
178
|
+
let tx = null;
|
|
179
|
+
|
|
180
|
+
if (signature) {
|
|
181
|
+
// Signature-only broadcast: POST /v1/tx with { signature }
|
|
182
|
+
tx = { signature };
|
|
183
|
+
} else if (filePath) {
|
|
184
|
+
// Read from file
|
|
185
|
+
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(process.cwd(), filePath);
|
|
186
|
+
if (!fs.existsSync(absPath)) {
|
|
187
|
+
throw new Error(`File not found: ${absPath}`);
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
tx = JSON.parse(fs.readFileSync(absPath, 'utf8'));
|
|
191
|
+
} catch {
|
|
192
|
+
throw new Error(`Invalid JSON in file: ${absPath}`);
|
|
193
|
+
}
|
|
194
|
+
} else if (jsonPayload) {
|
|
195
|
+
try {
|
|
196
|
+
tx = JSON.parse(jsonPayload);
|
|
197
|
+
} catch {
|
|
198
|
+
throw new Error('Invalid JSON payload provided with --json');
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
throw new Error('No transaction provided. Use --tx, --json, or --file');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Validate the transaction has required fields
|
|
205
|
+
if (!signature) {
|
|
206
|
+
const validationErrors = validateTxPayload(tx);
|
|
207
|
+
if (validationErrors.length > 0) {
|
|
208
|
+
throw new Error('Invalid transaction payload:\n ' + validationErrors.join('\n '));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (!asJson) {
|
|
213
|
+
console.log(`\n${C.bright}${C.cyan}── Broadcast Transaction ─────────────────────────────────────${C.reset}`);
|
|
214
|
+
console.log(` ${C.dim}RPC: ${rpc}${C.reset}`);
|
|
215
|
+
if (signature) {
|
|
216
|
+
console.log(` ${C.dim}Signature:${C.reset} ${C.cyan}${signature}${C.reset}`);
|
|
217
|
+
} else {
|
|
218
|
+
const txType = tx.tx_type || tx.type || 'Unknown';
|
|
219
|
+
const signer = tx.signer || tx.from || tx.pubkey || 'unknown';
|
|
220
|
+
console.log(` ${C.dim}Type:${C.reset} ${C.cyan}${txType}${C.reset}`);
|
|
221
|
+
console.log(` ${C.dim}Signer:${C.reset} ${C.cyan}${signer}${C.reset}`);
|
|
222
|
+
}
|
|
223
|
+
console.log();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Submit the transaction
|
|
227
|
+
let result;
|
|
228
|
+
let latencyMs;
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
const start = Date.now();
|
|
232
|
+
result = await httpPost(rpc, '/v1/tx', tx);
|
|
233
|
+
latencyMs = Date.now() - start;
|
|
234
|
+
} catch (err) {
|
|
235
|
+
if (asJson) {
|
|
236
|
+
console.log(JSON.stringify({
|
|
237
|
+
success: false,
|
|
238
|
+
error: err.message,
|
|
239
|
+
rpc,
|
|
240
|
+
cli_version: CLI_VERSION,
|
|
241
|
+
timestamp: new Date().toISOString(),
|
|
242
|
+
}, null, 2));
|
|
243
|
+
} else {
|
|
244
|
+
console.log(` ${C.red}✗ Network error:${C.reset} ${err.message}`);
|
|
245
|
+
console.log(` ${C.dim} Check that your RPC is accessible: ${rpc}${C.reset}`);
|
|
246
|
+
}
|
|
247
|
+
process.exit(1);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const success = result && !result.error && result.accepted !== false;
|
|
251
|
+
|
|
252
|
+
if (asJson) {
|
|
253
|
+
console.log(JSON.stringify({
|
|
254
|
+
success,
|
|
255
|
+
accepted: result?.accepted ?? null,
|
|
256
|
+
signature: result?.signature ?? result?.tx_signature ?? signature ?? null,
|
|
257
|
+
slot: result?.slot ?? null,
|
|
258
|
+
error: result?.error ?? null,
|
|
259
|
+
rpc,
|
|
260
|
+
latency_ms: latencyMs,
|
|
261
|
+
cli_version: CLI_VERSION,
|
|
262
|
+
timestamp: new Date().toISOString(),
|
|
263
|
+
}, null, 2));
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (success) {
|
|
268
|
+
const sig = result?.signature ?? result?.tx_signature ?? signature ?? 'unknown';
|
|
269
|
+
console.log(`${C.green}✓ Transaction accepted!${C.reset}`);
|
|
270
|
+
console.log(` ${C.green}★${C.reset} ${C.bright}Signature:${C.reset} ${sig}`);
|
|
271
|
+
if (result?.slot) {
|
|
272
|
+
console.log(` ${C.dim} Slot:${C.reset} ${result.slot}`);
|
|
273
|
+
}
|
|
274
|
+
console.log(` ${C.dim} Latency:${C.reset} ${latencyMs}ms`);
|
|
275
|
+
console.log(` ${C.dim} RPC:${C.reset} ${rpc}`);
|
|
276
|
+
console.log();
|
|
277
|
+
} else {
|
|
278
|
+
const errMsg = result?.error || 'Transaction rejected by network';
|
|
279
|
+
console.log(`${C.red}✗ Transaction rejected${C.reset}`);
|
|
280
|
+
if (result?.error) {
|
|
281
|
+
console.log(` ${C.red} Error:${C.reset} ${result.error}`);
|
|
282
|
+
}
|
|
283
|
+
if (result?.logs) {
|
|
284
|
+
console.log(` ${C.dim} Logs:${C.reset}`);
|
|
285
|
+
for (const log of result.logs) {
|
|
286
|
+
console.log(` ${C.dim}${log}${C.reset}`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
console.log(` ${C.dim} Latency:${C.reset} ${latencyMs}ms`);
|
|
290
|
+
console.log(` ${C.dim} RPC:${C.reset} ${rpc}`);
|
|
291
|
+
console.log();
|
|
292
|
+
process.exit(1);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// ---------------------------------------------------------------------------
|
|
297
|
+
// Main
|
|
298
|
+
// ---------------------------------------------------------------------------
|
|
299
|
+
|
|
300
|
+
async function main() {
|
|
301
|
+
const opts = parseArgs();
|
|
302
|
+
|
|
303
|
+
try {
|
|
304
|
+
await broadcast(opts);
|
|
305
|
+
} catch (err) {
|
|
306
|
+
if (opts.asJson) {
|
|
307
|
+
console.log(JSON.stringify({
|
|
308
|
+
success: false,
|
|
309
|
+
error: err.message,
|
|
310
|
+
rpc: opts.rpc,
|
|
311
|
+
cli_version: CLI_VERSION,
|
|
312
|
+
timestamp: new Date().toISOString(),
|
|
313
|
+
}, null, 2));
|
|
314
|
+
} else {
|
|
315
|
+
console.log(`\n ${C.red}✗ ${err.message}${C.reset}\n`);
|
|
316
|
+
}
|
|
317
|
+
process.exit(1);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
main();
|
|
322
|
+
|
|
323
|
+
module.exports = { broadcastCommand: main };
|