@clonegod/ttd-sui-common 1.0.19 → 1.0.21

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,21 @@
1
+ export declare class GasPriceCache {
2
+ private static instance;
3
+ private cachedGasPrice;
4
+ private lastUpdateTime;
5
+ private updateInterval;
6
+ private ledgerService;
7
+ private refreshTimer?;
8
+ private constructor();
9
+ static getInstance(ledgerService: any): GasPriceCache;
10
+ getGasPrice(): Promise<number>;
11
+ refreshGasPrice(): Promise<void>;
12
+ private startAutoRefresh;
13
+ stopAutoRefresh(): void;
14
+ getCacheInfo(): {
15
+ cachedGasPrice: number;
16
+ lastUpdateTime: number;
17
+ isExpired: boolean;
18
+ timeUntilNextRefresh: number;
19
+ };
20
+ destroy(): void;
21
+ }
@@ -0,0 +1,83 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.GasPriceCache = void 0;
13
+ const dist_1 = require("@clonegod/ttd-core/dist");
14
+ class GasPriceCache {
15
+ constructor(ledgerService) {
16
+ this.cachedGasPrice = 0;
17
+ this.lastUpdateTime = 0;
18
+ this.updateInterval = 5 * 60 * 1000;
19
+ this.ledgerService = ledgerService;
20
+ this.startAutoRefresh();
21
+ }
22
+ static getInstance(ledgerService) {
23
+ if (!GasPriceCache.instance) {
24
+ GasPriceCache.instance = new GasPriceCache(ledgerService);
25
+ }
26
+ return GasPriceCache.instance;
27
+ }
28
+ getGasPrice() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ if (Date.now() - this.lastUpdateTime > this.updateInterval || this.cachedGasPrice === 0) {
31
+ yield this.refreshGasPrice();
32
+ }
33
+ return this.cachedGasPrice;
34
+ });
35
+ }
36
+ refreshGasPrice() {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ try {
39
+ const epochRes = yield this.ledgerService.getEpoch();
40
+ this.cachedGasPrice = Number(epochRes['epoch']['reference_gas_price']);
41
+ this.lastUpdateTime = Date.now();
42
+ (0, dist_1.log_info)(`Gas price refreshed: ${this.cachedGasPrice} MIST (${this.cachedGasPrice / 1e9} SUI)`);
43
+ }
44
+ catch (error) {
45
+ (0, dist_1.log_error)('refresh gas price failed', error);
46
+ this.cachedGasPrice = Number(process.env.DEFAULT_GAS_PRICE || '600');
47
+ }
48
+ });
49
+ }
50
+ startAutoRefresh() {
51
+ this.refreshGasPrice().catch(error => {
52
+ (0, dist_1.log_error)('initial gas price refresh failed', error);
53
+ });
54
+ this.refreshTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
55
+ yield this.refreshGasPrice();
56
+ }), this.updateInterval);
57
+ (0, dist_1.log_info)('Gas price auto refresh started, refresh interval: 5 minutes');
58
+ }
59
+ stopAutoRefresh() {
60
+ if (this.refreshTimer) {
61
+ clearInterval(this.refreshTimer);
62
+ this.refreshTimer = undefined;
63
+ (0, dist_1.log_info)('Gas price auto refresh stopped');
64
+ }
65
+ }
66
+ getCacheInfo() {
67
+ const now = Date.now();
68
+ const timeSinceLastUpdate = now - this.lastUpdateTime;
69
+ const isExpired = timeSinceLastUpdate > this.updateInterval;
70
+ const timeUntilNextRefresh = Math.max(0, this.updateInterval - timeSinceLastUpdate);
71
+ return {
72
+ cachedGasPrice: this.cachedGasPrice,
73
+ lastUpdateTime: this.lastUpdateTime,
74
+ isExpired,
75
+ timeUntilNextRefresh
76
+ };
77
+ }
78
+ destroy() {
79
+ this.stopAutoRefresh();
80
+ GasPriceCache.instance = null;
81
+ }
82
+ }
83
+ exports.GasPriceCache = GasPriceCache;
@@ -4,3 +4,4 @@ export { LiveDataService } from './live-data-service';
4
4
  export { TransactionService } from './transaction-service';
5
5
  export { SubscriptionService } from './subscription-service';
6
6
  export { SuiGrpcClient } from './sui-grpc-client';
7
+ export { GasPriceCache } from './gas-price-cache';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SuiGrpcClient = exports.SubscriptionService = exports.TransactionService = exports.LiveDataService = exports.LedgerService = exports.GrpcConnection = void 0;
3
+ exports.GasPriceCache = exports.SuiGrpcClient = exports.SubscriptionService = exports.TransactionService = exports.LiveDataService = exports.LedgerService = exports.GrpcConnection = void 0;
4
4
  var grpc_connection_1 = require("./grpc-connection");
5
5
  Object.defineProperty(exports, "GrpcConnection", { enumerable: true, get: function () { return grpc_connection_1.GrpcConnection; } });
6
6
  var ledger_service_1 = require("./ledger-service");
@@ -13,3 +13,5 @@ var subscription_service_1 = require("./subscription-service");
13
13
  Object.defineProperty(exports, "SubscriptionService", { enumerable: true, get: function () { return subscription_service_1.SubscriptionService; } });
14
14
  var sui_grpc_client_1 = require("./sui-grpc-client");
15
15
  Object.defineProperty(exports, "SuiGrpcClient", { enumerable: true, get: function () { return sui_grpc_client_1.SuiGrpcClient; } });
16
+ var gas_price_cache_1 = require("./gas-price-cache");
17
+ Object.defineProperty(exports, "GasPriceCache", { enumerable: true, get: function () { return gas_price_cache_1.GasPriceCache; } });
@@ -2,11 +2,15 @@ import { LedgerService } from "./ledger-service";
2
2
  import { LiveDataService } from "./live-data-service";
3
3
  import { SubscriptionService } from "./subscription-service";
4
4
  import { TransactionService } from "./transaction-service";
5
+ import { GasPriceCache } from "./gas-price-cache";
5
6
  export declare class SuiGrpcClient {
6
7
  ledgerService: LedgerService;
7
8
  transactionService: TransactionService;
8
9
  liveDataService: LiveDataService;
9
10
  subscriptionService: SubscriptionService;
11
+ gasPriceCache: GasPriceCache;
12
+ gas_price: number;
10
13
  constructor();
11
14
  init(): void;
15
+ get_gas_price(): Promise<number>;
12
16
  }
@@ -1,4 +1,13 @@
1
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.SuiGrpcClient = void 0;
4
13
  const grpc_connection_1 = require("./grpc-connection");
@@ -6,8 +15,10 @@ const ledger_service_1 = require("./ledger-service");
6
15
  const live_data_service_1 = require("./live-data-service");
7
16
  const subscription_service_1 = require("./subscription-service");
8
17
  const transaction_service_1 = require("./transaction-service");
18
+ const gas_price_cache_1 = require("./gas-price-cache");
9
19
  class SuiGrpcClient {
10
20
  constructor() {
21
+ this.gas_price = 0;
11
22
  this.init();
12
23
  }
13
24
  init() {
@@ -16,6 +27,12 @@ class SuiGrpcClient {
16
27
  this.transactionService = new transaction_service_1.TransactionService(grpcClient);
17
28
  this.liveDataService = new live_data_service_1.LiveDataService(grpcClient);
18
29
  this.subscriptionService = new subscription_service_1.SubscriptionService(grpcClient);
30
+ this.gasPriceCache = gas_price_cache_1.GasPriceCache.getInstance(this.ledgerService);
31
+ }
32
+ get_gas_price() {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ return yield this.gasPriceCache.getGasPrice();
35
+ });
19
36
  }
20
37
  }
21
38
  exports.SuiGrpcClient = SuiGrpcClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sui-common",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "Sui common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",
@@ -15,7 +15,7 @@
15
15
  "push": "npm run build && npm publish"
16
16
  },
17
17
  "dependencies": {
18
- "@clonegod/ttd-core": "2.0.61",
18
+ "@clonegod/ttd-core": "2.0.62",
19
19
  "@grpc/grpc-js": "^1.9.14",
20
20
  "@grpc/proto-loader": "^0.7.10",
21
21
  "@mysten/sui": "^1.37.5",