@clonegod/ttd-bsc-common 1.0.62 → 1.0.63
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.
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class BscStreamWSClient {
|
|
2
|
+
private host;
|
|
3
|
+
private port;
|
|
4
|
+
private wsUrl;
|
|
5
|
+
private ws;
|
|
6
|
+
constructor(host: string, port: number);
|
|
7
|
+
subscribeBlockNumber(callback: (res: any) => void): void;
|
|
8
|
+
subscribeWalletReceipts(wallets: string[], callback: (res: any) => void): void;
|
|
9
|
+
subscribeWalletBalance(callback: (res: any) => void): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.BscStreamWSClient = void 0;
|
|
7
|
+
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
8
|
+
const ws_1 = __importDefault(require("ws"));
|
|
9
|
+
class BscStreamWSClient {
|
|
10
|
+
constructor(host, port) {
|
|
11
|
+
this.ws = null;
|
|
12
|
+
this.host = host;
|
|
13
|
+
this.port = port;
|
|
14
|
+
this.wsUrl = `ws://${this.host}:${this.port}`;
|
|
15
|
+
}
|
|
16
|
+
subscribeBlockNumber(callback) {
|
|
17
|
+
this.ws = new ws_1.default(this.wsUrl);
|
|
18
|
+
this.ws.on('open', () => {
|
|
19
|
+
(0, dist_1.log_info)(`[BLOCK_NUMBER] WebSocket connected to ${this.wsUrl}`);
|
|
20
|
+
this.ws.send(JSON.stringify({ type: 'subscribe', data: { type: 'BLOCK_NUMBER' } }));
|
|
21
|
+
});
|
|
22
|
+
this.ws.on('message', (data) => {
|
|
23
|
+
try {
|
|
24
|
+
const msg = JSON.parse(data.toString());
|
|
25
|
+
callback(msg);
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
(0, dist_1.log_info)(`[BLOCK_NUMBER] received raw message:`, data.toString());
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
this.ws.on('error', (error) => {
|
|
32
|
+
(0, dist_1.log_error)(`[BLOCK_NUMBER] WebSocket error:`, error);
|
|
33
|
+
this.ws.close();
|
|
34
|
+
});
|
|
35
|
+
this.ws.on('close', () => {
|
|
36
|
+
(0, dist_1.log_info)(`[BLOCK_NUMBER] WebSocket closed, reconnecting in 1s...`);
|
|
37
|
+
setTimeout(() => {
|
|
38
|
+
this.subscribeBlockNumber(callback);
|
|
39
|
+
}, 1000);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
subscribeWalletReceipts(wallets = [], callback) {
|
|
43
|
+
this.ws = new ws_1.default(this.wsUrl);
|
|
44
|
+
this.ws.on('open', () => {
|
|
45
|
+
(0, dist_1.log_info)(`[WALLET_RECEIPT] WebSocket connected to ${this.wsUrl}, wallets=${wallets.length}`, wallets);
|
|
46
|
+
this.ws.send(JSON.stringify({ type: 'subscribe', data: { type: 'WALLET_RECEIPT', wallets } }));
|
|
47
|
+
});
|
|
48
|
+
this.ws.on('message', (data) => {
|
|
49
|
+
try {
|
|
50
|
+
const msg = JSON.parse(data.toString());
|
|
51
|
+
callback(msg);
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
(0, dist_1.log_info)(`[WALLET_RECEIPT] received raw message:`, data.toString());
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
this.ws.on('error', (error) => {
|
|
58
|
+
(0, dist_1.log_error)(`[WALLET_RECEIPT] WebSocket error:`, error);
|
|
59
|
+
this.ws.close();
|
|
60
|
+
});
|
|
61
|
+
this.ws.on('close', () => {
|
|
62
|
+
(0, dist_1.log_info)(`[WALLET_RECEIPT] WebSocket closed, try reconnect in 1s...`);
|
|
63
|
+
setTimeout(() => {
|
|
64
|
+
this.subscribeWalletReceipts(wallets, callback);
|
|
65
|
+
}, 1000);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
subscribeWalletBalance(callback) {
|
|
69
|
+
this.ws = new ws_1.default(this.wsUrl);
|
|
70
|
+
this.ws.on('open', () => {
|
|
71
|
+
(0, dist_1.log_info)(`[WALLET_BALANCE] WebSocket connected to ${this.wsUrl}`);
|
|
72
|
+
this.ws.send(JSON.stringify({ type: 'subscribe', data: { type: 'WALLET_BALANCE' } }));
|
|
73
|
+
});
|
|
74
|
+
this.ws.on('message', (data) => {
|
|
75
|
+
try {
|
|
76
|
+
const msg = JSON.parse(data.toString());
|
|
77
|
+
callback(msg);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
(0, dist_1.log_info)(`[WALLET_BALANCE] received raw message:`, data.toString());
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
this.ws.on('error', (error) => {
|
|
84
|
+
(0, dist_1.log_error)(`[WALLET_BALANCE] WebSocket error:`, error);
|
|
85
|
+
this.ws.close();
|
|
86
|
+
});
|
|
87
|
+
this.ws.on('close', () => {
|
|
88
|
+
(0, dist_1.log_info)(`[WALLET_BALANCE] WebSocket closed, try reconnect in 1s...`);
|
|
89
|
+
setTimeout(() => {
|
|
90
|
+
this.subscribeWalletBalance(callback);
|
|
91
|
+
}, 1000);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.BscStreamWSClient = BscStreamWSClient;
|
package/dist/ws/index.d.ts
CHANGED
package/dist/ws/index.js
CHANGED
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./subscribe_v2_events"), exports);
|
|
18
18
|
__exportStar(require("./subscribe_v3_events"), exports);
|
|
19
|
+
__exportStar(require("./bsc_stream_ws_client"), exports);
|