@clonegod/ttd-bsc-common 1.0.62 → 1.0.64

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.
@@ -44,8 +44,16 @@ class BaseTxParser {
44
44
  }
45
45
  calculateGasFee(txReceipt) {
46
46
  try {
47
- const gasUsed = txReceipt.gasUsed || ethers_1.ethers.BigNumber.from(0);
48
- const effectiveGasPrice = txReceipt.effectiveGasPrice || ethers_1.ethers.BigNumber.from(0);
47
+ const gasUsed = txReceipt.gasUsed
48
+ ? (typeof txReceipt.gasUsed === 'string' || typeof txReceipt.gasUsed === 'number'
49
+ ? ethers_1.ethers.BigNumber.from(txReceipt.gasUsed)
50
+ : txReceipt.gasUsed)
51
+ : ethers_1.ethers.BigNumber.from(0);
52
+ const effectiveGasPrice = txReceipt.effectiveGasPrice
53
+ ? (typeof txReceipt.effectiveGasPrice === 'string' || typeof txReceipt.effectiveGasPrice === 'number'
54
+ ? ethers_1.ethers.BigNumber.from(txReceipt.effectiveGasPrice)
55
+ : txReceipt.effectiveGasPrice)
56
+ : ethers_1.ethers.BigNumber.from(0);
49
57
  const gasFeeBN = gasUsed.mul(effectiveGasPrice);
50
58
  const base_fee = Number(ethers_1.ethers.utils.formatEther(gasFeeBN));
51
59
  const priority_fee = 0;
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-bsc-common",
3
- "version": "1.0.62",
3
+ "version": "1.0.64",
4
4
  "description": "BSC common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",