-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocket-demo.ts
More file actions
97 lines (81 loc) · 2.94 KB
/
Copy pathwebsocket-demo.ts
File metadata and controls
97 lines (81 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Node.js WebSocket Listening Example
* Demonstrates how to listen for pending transactions in real-time
*/
import 'dotenv/config';
import { MultiSigClient, TransactionDetail } from '../../src';
async function main() {
// 1. Initialize client
const client = new MultiSigClient({
baseURL: process.env.BASE_URL!,
secretId: process.env.SECRET_ID!,
secretKey: process.env.SECRET_KEY!,
channel: process.env.CHANNEL!
});
const testAddress = process.env.TEST_ADDRESS!;
console.log('=== TRON Multi-Signature Transaction Real-Time Monitoring ===');
console.log('Monitoring address:', testAddress);
console.log('Establishing connection...\n');
try {
// 2. Establish WebSocket connection
await client.connectWebSocket(testAddress);
// 3. Listen for pending transactions
client.onPendingTransaction((transactions: TransactionDetail | TransactionDetail[]) => {
if (!Array.isArray(transactions)) {
transactions = [transactions];
}
console.log(`\nReceived ${transactions.length} transaction updates:`);
console.log('='.repeat(60));
transactions.forEach((tx, index) => {
console.log(`\n[Transaction ${index + 1}]`);
console.log(' Hash:', tx.hash);
console.log(' Type:', tx.contract_type);
console.log(' Originator address:', tx.originator_address);
console.log(' Threshold/Current weight:', `${tx.current_weight}/${tx.threshold}`);
console.log(' Signed:', tx.is_sign === 1 ? 'Yes' : 'No');
console.log(' State:', getStateText(tx.state));
// Display signature progress
console.log(' Signature progress:');
tx.signature_progress.forEach(signer => {
const status = signer.is_sign === 1 ? '✓ Signed' : '○ Pending';
const time = signer.sign_time > 0 ? new Date(signer.sign_time * 1000).toLocaleString() : '-';
console.log(` ${status} ${signer.address} (weight: ${signer.weight}, time: ${time})`);
});
// Display contract data
if (tx.contract_data && Object.keys(tx.contract_data).length > 0) {
console.log(' Contract data:', JSON.stringify(tx.contract_data, null, 4));
}
});
console.log('\n' + '='.repeat(60));
});
// Keep connection alive
console.log('WebSocket connection successful! Waiting for transaction pushes...');
console.log('Press Ctrl+C to exit\n');
// Graceful exit
process.on('SIGINT', () => {
console.log('\n\nClosing connection...');
client.disconnect();
process.exit(0);
});
} catch (error: any) {
console.error('Connection failed:', error.message);
process.exit(1);
}
}
/**
* Get state text
*/
function getStateText(state: number): string {
switch (state) {
case 0:
return 'Processing';
case 1:
return 'Success';
case 2:
return 'Failed';
default:
return `Unknown(${state})`;
}
}
// Run example
main().catch(console.error);