@clonegod/ttd-sol-common 1.0.172 → 1.0.173

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.
@@ -1 +1,23 @@
1
+ import { SolanaGrpcService } from './subscribe_grpc';
1
2
  export * from './subscribe_grpc';
3
+ export type GrpcServiceConfig = {
4
+ endpoint: string;
5
+ token: string;
6
+ pingInterval?: number;
7
+ maxRetries?: number;
8
+ region?: string;
9
+ };
10
+ export type GrpcCallbacks = {
11
+ onConnectionStateChanged?: (status: 'connected' | 'disconnected' | 'reconnecting') => void;
12
+ onError?: (error: Error) => void;
13
+ };
14
+ export declare function getSolanaGrpcService(config: GrpcServiceConfig, callbacks?: GrpcCallbacks): SolanaGrpcService;
15
+ export declare class GrpcServiceManager {
16
+ private static instance;
17
+ private service;
18
+ private connectionPromise;
19
+ private constructor();
20
+ static getInstance(): GrpcServiceManager;
21
+ getService(config: GrpcServiceConfig): Promise<SolanaGrpcService>;
22
+ disconnect(): Promise<void>;
23
+ }
@@ -13,5 +13,92 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
13
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
16
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.GrpcServiceManager = void 0;
27
+ exports.getSolanaGrpcService = getSolanaGrpcService;
28
+ const dist_1 = require("@clonegod/ttd-core/dist");
29
+ const subscribe_grpc_1 = require("./subscribe_grpc");
17
30
  __exportStar(require("./subscribe_grpc"), exports);
31
+ function getSolanaGrpcService(config, callbacks) {
32
+ var _a, _b;
33
+ if (!config.endpoint) {
34
+ throw new Error('GRPC_ENDPOINT is required');
35
+ }
36
+ const defaultCallbacks = {
37
+ onConnectionStateChanged: (status) => {
38
+ (0, dist_1.log_info)('grpc connection status:', status);
39
+ switch (status) {
40
+ case 'connected':
41
+ (0, dist_1.log_info)('grpc connected success');
42
+ break;
43
+ case 'disconnected':
44
+ (0, dist_1.log_warn)('grpc disconnected');
45
+ break;
46
+ case 'reconnecting':
47
+ (0, dist_1.log_info)('grpc reconnecting...');
48
+ break;
49
+ }
50
+ },
51
+ onError: (error) => {
52
+ (0, dist_1.log_error)('grpc error:', error);
53
+ }
54
+ };
55
+ return new subscribe_grpc_1.SolanaGrpcService({
56
+ grpc_endpoint: config.endpoint,
57
+ grpc_token: config.token,
58
+ pingInterval: (_a = config.pingInterval) !== null && _a !== void 0 ? _a : 30000,
59
+ maxRetries: (_b = config.maxRetries) !== null && _b !== void 0 ? _b : 5,
60
+ }, Object.assign(Object.assign({}, defaultCallbacks), callbacks));
61
+ }
62
+ class GrpcServiceManager {
63
+ constructor() {
64
+ this.service = null;
65
+ this.connectionPromise = null;
66
+ }
67
+ static getInstance() {
68
+ if (!GrpcServiceManager.instance) {
69
+ GrpcServiceManager.instance = new GrpcServiceManager();
70
+ }
71
+ return GrpcServiceManager.instance;
72
+ }
73
+ getService(config) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ if (this.connectionPromise) {
76
+ return this.connectionPromise;
77
+ }
78
+ if (this.service) {
79
+ return this.service;
80
+ }
81
+ this.connectionPromise = (() => __awaiter(this, void 0, void 0, function* () {
82
+ try {
83
+ this.service = getSolanaGrpcService(config);
84
+ yield this.service.connect();
85
+ return this.service;
86
+ }
87
+ finally {
88
+ this.connectionPromise = null;
89
+ }
90
+ }))();
91
+ return this.connectionPromise;
92
+ });
93
+ }
94
+ disconnect() {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ if (this.service) {
97
+ yield this.service.disconnect();
98
+ this.service = null;
99
+ }
100
+ this.connectionPromise = null;
101
+ });
102
+ }
103
+ }
104
+ exports.GrpcServiceManager = GrpcServiceManager;
@@ -17,6 +17,7 @@ const subscribe_grpc_1 = require("./subscribe_grpc");
17
17
  const web3_js_1 = require("@solana/web3.js");
18
18
  const yellowstone_grpc_1 = require("@triton-one/yellowstone-grpc");
19
19
  const dotenv_1 = __importDefault(require("dotenv"));
20
+ const _1 = require(".");
20
21
  dotenv_1.default.config();
21
22
  function createGrpcService() {
22
23
  return new subscribe_grpc_1.SolanaGrpcService({
@@ -78,7 +79,12 @@ function testAccountsSubscription() {
78
79
  }
79
80
  function testBlockMetaSubscription() {
80
81
  return __awaiter(this, void 0, void 0, function* () {
81
- const grpcService = createGrpcService();
82
+ const grpcService = yield _1.GrpcServiceManager.getInstance().getService({
83
+ endpoint: process.env.GRPC_ENDPOINT || '',
84
+ token: process.env.GRPC_TOKEN || '',
85
+ pingInterval: 30000,
86
+ maxRetries: 5,
87
+ });
82
88
  try {
83
89
  yield grpcService.connect();
84
90
  console.log('开始测试区块元数据订阅...');
package/dist/index.d.ts CHANGED
@@ -5,3 +5,4 @@ export * from './quote';
5
5
  export * from './send_tx';
6
6
  export * from './trade';
7
7
  export * from './token';
8
+ export * from './grpc';
package/dist/index.js CHANGED
@@ -21,3 +21,4 @@ __exportStar(require("./quote"), exports);
21
21
  __exportStar(require("./send_tx"), exports);
22
22
  __exportStar(require("./trade"), exports);
23
23
  __exportStar(require("./token"), exports);
24
+ __exportStar(require("./grpc"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sol-common",
3
- "version": "1.0.172",
3
+ "version": "1.0.173",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",