@clonegod/ttd-sol-common 1.0.0 → 1.0.1

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,7 @@
1
+ import { StandardPoolInfoType } from '@clonegod/ttd-common';
2
+ export declare const enum COMMITMENT_LEVEL {
3
+ PROCESSED = "processed",
4
+ CONFIRMED = "confirmed",
5
+ _FINALIZED = "finalized"
6
+ }
7
+ export declare const subscibe_pool_change_by_geyser: (geyser_ws_url: string, pool_list: StandardPoolInfoType[], callback: (data_buff: any) => {}) => Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,88 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.subscibe_pool_change_by_geyser = void 0;
16
+ const dist_1 = require("@clonegod/ttd-common/dist");
17
+ const ws_1 = __importDefault(require("ws"));
18
+ const subscibe_pool_change_by_geyser = (geyser_ws_url, pool_list, callback) => __awaiter(void 0, void 0, void 0, function* () {
19
+ if ((0, dist_1.isEmpty)(geyser_ws_url)) {
20
+ (0, dist_1.log_warn)(`geyser_ws_url is empty, cann't subscribe`);
21
+ return;
22
+ }
23
+ if ((0, dist_1.isEmpty)(pool_list)) {
24
+ (0, dist_1.log_warn)(`pool_list is empty, nothing to subscribe`);
25
+ return;
26
+ }
27
+ yield (0, dist_1.sleep)(1000);
28
+ var ws_client = new ws_1.default(geyser_ws_url);
29
+ function subscribe_account_change(ws_client, pool) {
30
+ (0, dist_1.log_info)(`subscribe pool: ${pool.pool_name}, ${pool.pool_address}`);
31
+ const request = {
32
+ jsonrpc: '2.0',
33
+ id: 420,
34
+ method: 'accountSubscribe',
35
+ params: [
36
+ pool.pool_address,
37
+ {
38
+ encoding: 'jsonParsed',
39
+ commitment: "processed",
40
+ },
41
+ ],
42
+ };
43
+ ws_client.send(JSON.stringify(request));
44
+ }
45
+ ws_client.on('open', function open() {
46
+ (0, dist_1.log_info)(`WebSocket is open`);
47
+ pool_list.forEach((pool) => {
48
+ subscribe_account_change(ws_client, pool);
49
+ });
50
+ });
51
+ ws_client.on('message', function incoming(data) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ let messageStr = '';
54
+ try {
55
+ messageStr = data.toString('utf8');
56
+ if (messageStr == '{}') {
57
+ (0, dist_1.log_info)(`receive empty message, something wrong! exit ...`);
58
+ ws_client.close();
59
+ }
60
+ const messageObj = JSON.parse(messageStr);
61
+ if (messageObj['id']) {
62
+ (0, dist_1.log_info)(`accountSubscribe success!`, messageStr);
63
+ return;
64
+ }
65
+ else {
66
+ const res_data = messageObj['params']['result']['value']['data'];
67
+ let data_buff = Buffer.from(res_data[0], res_data[1]);
68
+ callback(data_buff);
69
+ }
70
+ }
71
+ catch (err) {
72
+ (0, dist_1.log_error)(`Failed to parse JSON: ${messageStr}`, err);
73
+ ws_client.close();
74
+ }
75
+ });
76
+ });
77
+ ws_client.on('error', function error(err) {
78
+ (0, dist_1.log_error)(`WebSocket error:`, err);
79
+ ws_client.close();
80
+ });
81
+ ws_client.on('close', function close() {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ (0, dist_1.log_warn)(`Socket is closed. Reconnect will be attempted in few seconds`);
84
+ (0, exports.subscibe_pool_change_by_geyser)(geyser_ws_url, pool_list, callback);
85
+ });
86
+ });
87
+ });
88
+ exports.subscibe_pool_change_by_geyser = subscibe_pool_change_by_geyser;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sol-common",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
File without changes