@clonegod/ttd-core 2.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.
Files changed (77) hide show
  1. package/README.md +3 -0
  2. package/dist/analyze/index.d.ts +3 -0
  3. package/dist/analyze/index.js +49 -0
  4. package/dist/app_config/app_config.d.ts +15 -0
  5. package/dist/app_config/app_config.js +50 -0
  6. package/dist/app_config/env_args.d.ts +16 -0
  7. package/dist/app_config/env_args.js +28 -0
  8. package/dist/app_config/index.d.ts +2 -0
  9. package/dist/app_config/index.js +18 -0
  10. package/dist/cache/arb_cache.d.ts +80 -0
  11. package/dist/cache/arb_cache.js +1010 -0
  12. package/dist/cache/arb_event_pub.d.ts +14 -0
  13. package/dist/cache/arb_event_pub.js +77 -0
  14. package/dist/cache/arb_event_sub.d.ts +17 -0
  15. package/dist/cache/arb_event_sub.js +166 -0
  16. package/dist/cache/index.d.ts +6 -0
  17. package/dist/cache/index.js +22 -0
  18. package/dist/cache/loading_cache.d.ts +62 -0
  19. package/dist/cache/loading_cache.js +233 -0
  20. package/dist/cache/redis_client.d.ts +3 -0
  21. package/dist/cache/redis_client.js +73 -0
  22. package/dist/cache/redis_cmd.d.ts +14 -0
  23. package/dist/cache/redis_cmd.js +83 -0
  24. package/dist/index.d.ts +216 -0
  25. package/dist/index.js +883 -0
  26. package/dist/market_price/estimate_token_amount.d.ts +4 -0
  27. package/dist/market_price/estimate_token_amount.js +109 -0
  28. package/dist/market_price/index.d.ts +1 -0
  29. package/dist/market_price/index.js +17 -0
  30. package/dist/pool/cache_pool_config.d.ts +2 -0
  31. package/dist/pool/cache_pool_config.js +116 -0
  32. package/dist/pool/index.d.ts +3 -0
  33. package/dist/pool/index.js +19 -0
  34. package/dist/pool/pool_util.d.ts +5 -0
  35. package/dist/pool/pool_util.js +65 -0
  36. package/dist/pool/types.d.ts +20 -0
  37. package/dist/pool/types.js +2 -0
  38. package/dist/quote/index.d.ts +1 -0
  39. package/dist/quote/index.js +17 -0
  40. package/dist/quote/log_quote_price.d.ts +2 -0
  41. package/dist/quote/log_quote_price.js +29 -0
  42. package/dist/quote/on_quote_response.d.ts +3 -0
  43. package/dist/quote/on_quote_response.js +28 -0
  44. package/dist/quote/publish_quote_price.d.ts +3 -0
  45. package/dist/quote/publish_quote_price.js +19 -0
  46. package/dist/quote/to_price_message.d.ts +5 -0
  47. package/dist/quote/to_price_message.js +103 -0
  48. package/dist/test/test_is_empty.d.ts +1 -0
  49. package/dist/test/test_is_empty.js +24 -0
  50. package/dist/test/test_log_level.d.ts +3 -0
  51. package/dist/test/test_log_level.js +29 -0
  52. package/dist/test/test_merge_property.d.ts +1 -0
  53. package/dist/test/test_merge_property.js +21 -0
  54. package/dist/token/cache_token_config.d.ts +2 -0
  55. package/dist/token/cache_token_config.js +68 -0
  56. package/dist/token/fixed_symbol_address.d.ts +2 -0
  57. package/dist/token/fixed_symbol_address.js +26 -0
  58. package/dist/token/index.d.ts +6 -0
  59. package/dist/token/index.js +22 -0
  60. package/dist/token/is_not_arb_token.d.ts +1 -0
  61. package/dist/token/is_not_arb_token.js +24 -0
  62. package/dist/token/price/gecko_terminal.d.ts +2 -0
  63. package/dist/token/price/gecko_terminal.js +69 -0
  64. package/dist/token/price/get_bsc_token_price.d.ts +2 -0
  65. package/dist/token/price/get_bsc_token_price.js +131 -0
  66. package/dist/token/price/get_solana_token_price.d.ts +2 -0
  67. package/dist/token/price/get_solana_token_price.js +17 -0
  68. package/dist/token/price/get_tron_token_price.d.ts +2 -0
  69. package/dist/token/price/get_tron_token_price.js +17 -0
  70. package/dist/token/price/index.d.ts +3 -0
  71. package/dist/token/price/index.js +19 -0
  72. package/dist/token/token_util.d.ts +3 -0
  73. package/dist/token/token_util.js +23 -0
  74. package/dist/token/types.d.ts +29 -0
  75. package/dist/token/types.js +2 -0
  76. package/package.json +47 -0
  77. package/types/index.d.ts +709 -0
@@ -0,0 +1,14 @@
1
+ import { RedisCommand, RedisConfigChangeEventMessageType, RedisOrderEventMessageType, RedisQuoteEventMessageType, TradeResponseType } from "../../types";
2
+ import { CHAIN_ID, DEX_ID } from "../index";
3
+ import { ArbCache } from "./arb_cache";
4
+ export declare class ArbEventPublisher {
5
+ arb_cache: ArbCache;
6
+ chain_id: CHAIN_ID;
7
+ redis_cmd: RedisCommand;
8
+ constructor(arb_cache: ArbCache);
9
+ publish_config_change_event(message: RedisConfigChangeEventMessageType): Promise<void>;
10
+ publish_quote_price_event(chain_id: string, message: RedisQuoteEventMessageType): Promise<void>;
11
+ publish_order_event(group_id: string, pair: string, dex_id: DEX_ID, message: RedisOrderEventMessageType): Promise<void>;
12
+ publish_wallet_raw_tx_event(group_id: string, wallet: string, message: any): Promise<void>;
13
+ publish_tx_result_event(group_id: string, pair: string, message: TradeResponseType): Promise<void>;
14
+ }
@@ -0,0 +1,77 @@
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.ArbEventPublisher = void 0;
13
+ const index_1 = require("../index");
14
+ class ArbEventPublisher {
15
+ constructor(arb_cache) {
16
+ this.arb_cache = arb_cache;
17
+ this.redis_cmd = arb_cache.redis_cmd;
18
+ this.chain_id = arb_cache.chain_id;
19
+ }
20
+ publish_config_change_event(message) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ let channel = (0, index_1.get_config_channel_name)(this.chain_id);
23
+ yield this.redis_cmd.publish(channel, JSON.stringify(message));
24
+ (0, index_1.log_info)('publish_config_change_event', { channel, message });
25
+ });
26
+ }
27
+ publish_quote_price_event(chain_id, message) {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ const channel = (0, index_1.get_quote_channel_name)(chain_id);
30
+ yield this.redis_cmd.publish(channel, JSON.stringify(message));
31
+ (0, index_1.log_debug)(`publish_quote_event, success`, {
32
+ channel, message
33
+ });
34
+ });
35
+ }
36
+ publish_order_event(group_id, pair, dex_id, message) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ const channel = (0, index_1.get_order_channel_name)(group_id, pair, dex_id);
39
+ yield this.redis_cmd.publish(channel, JSON.stringify(message));
40
+ (0, index_1.log_info)(`publish_order_event, success`, {
41
+ channel, message
42
+ });
43
+ });
44
+ }
45
+ publish_wallet_raw_tx_event(group_id, wallet, message) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const channel = (0, index_1.get_wallet_raw_tx_channel_name)(group_id, wallet);
48
+ let data = JSON.stringify(message);
49
+ yield this.redis_cmd.publish(channel, data);
50
+ if (index_1.LOG.trace) {
51
+ (0, index_1.log_info)(`publish_wallet_raw_tx_event, success`, {
52
+ channel,
53
+ txid: message === null || message === void 0 ? void 0 : message.txid,
54
+ message
55
+ });
56
+ return;
57
+ }
58
+ (0, index_1.log_info)(`publish_wallet_raw_tx_event, success`, {
59
+ channel,
60
+ txid: message === null || message === void 0 ? void 0 : message.txid
61
+ });
62
+ });
63
+ }
64
+ publish_tx_result_event(group_id, pair, message) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ const channel = (0, index_1.get_tx_result_channel_name)(group_id, pair);
67
+ yield this.redis_cmd.publish(channel, JSON.stringify(message));
68
+ (0, index_1.log_debug)(`publish_tx_result_event, success`, {
69
+ channel,
70
+ group_id,
71
+ pair,
72
+ txid: message.data.txid
73
+ });
74
+ });
75
+ }
76
+ }
77
+ exports.ArbEventPublisher = ArbEventPublisher;
@@ -0,0 +1,17 @@
1
+ import { ArbCache, RedisCommand, RedisConfigChangeEventMessageType } from "../../types";
2
+ import { CHAIN_ID, DEX_ID } from "../index";
3
+ export declare class ArbEventSubscriber {
4
+ arb_cache: ArbCache;
5
+ chain_id: CHAIN_ID;
6
+ redis_cmd: RedisCommand;
7
+ constructor(arb_cache: ArbCache);
8
+ pre_load_config_cache(): Promise<void>;
9
+ subscribe_config_change_event(callback?: Function): Promise<void>;
10
+ on_config_token_change_event(message: RedisConfigChangeEventMessageType): Promise<boolean>;
11
+ on_config_pool_change_event(message: RedisConfigChangeEventMessageType): Promise<boolean>;
12
+ on_config_group_change_event(message: RedisConfigChangeEventMessageType): Promise<boolean>;
13
+ subscribe_quote_event(chain_id: string, callback: Function): Promise<void>;
14
+ subscribe_order_event(group_id: string, pair: string, dex_id: DEX_ID, callback: Function): Promise<void>;
15
+ subscribe_wallet_raw_tx_result(group_id: string, wallet: string, callback: Function): Promise<void>;
16
+ subscribe_tx_result(group_id: string, pair: string, callback: Function): Promise<void>;
17
+ }
@@ -0,0 +1,166 @@
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.ArbEventSubscriber = void 0;
13
+ const index_1 = require("../index");
14
+ class ArbEventSubscriber {
15
+ constructor(arb_cache) {
16
+ this.arb_cache = arb_cache;
17
+ this.redis_cmd = arb_cache.redis_cmd;
18
+ this.chain_id = arb_cache.chain_id;
19
+ }
20
+ pre_load_config_cache() {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ (0, index_1.new_line)(5);
23
+ setTimeout(() => {
24
+ console.log(`=================================================================================`);
25
+ console.log(`| PRE-LOAD CONFIG CACHE |`);
26
+ console.log(`=================================================================================`);
27
+ }, 0);
28
+ if ((0, index_1.isEmpty)(this.arb_cache.token_list)) {
29
+ yield this.on_config_token_change_event(null);
30
+ }
31
+ else {
32
+ (0, index_1.log_info)(`skip pre-load token_list: arb_cache.token_list not empty, length=${this.arb_cache.token_list.length}`);
33
+ }
34
+ if ((0, index_1.isEmpty)(this.arb_cache.pool_list)) {
35
+ yield this.on_config_pool_change_event(null);
36
+ }
37
+ else {
38
+ (0, index_1.log_info)(`skip pre-load pool_list, arb_cache.pool_list not empty, length=${this.arb_cache.pool_list.length}`);
39
+ }
40
+ let group_ids = yield this.arb_cache.get_trade_group_id_list();
41
+ (0, index_1.log_info)(`pre_load_config_cache, all group ids in cache:`, group_ids);
42
+ for (let group_id of group_ids) {
43
+ yield this.on_config_group_change_event({
44
+ event_type: index_1.REDIS_EVENT_TYPE_CONFIG_CHANGE.GROUP_CONFIG_CHANGE,
45
+ event_time: Date.now(),
46
+ group_id
47
+ });
48
+ yield (0, index_1.sleep)(1000);
49
+ }
50
+ });
51
+ }
52
+ subscribe_config_change_event() {
53
+ return __awaiter(this, arguments, void 0, function* (callback = () => { }) {
54
+ (0, index_1.new_line)(5);
55
+ setTimeout(() => {
56
+ console.log(`=================================================================================`);
57
+ console.log(`| SUBSCRIBE CONFIG CHANGE |`);
58
+ console.log(`=================================================================================`);
59
+ }, 0);
60
+ let channel = (0, index_1.get_config_channel_name)(this.chain_id);
61
+ const listener = (message, _channel) => {
62
+ (0, index_1.log_debug)('--------------------->>> subscribe_config_change_event => on event', { channel, message });
63
+ let event = JSON.parse(message);
64
+ switch (event.event_type) {
65
+ case index_1.REDIS_EVENT_TYPE_CONFIG_CHANGE.TOKEN_CONFIG_CHANGE:
66
+ this.on_config_token_change_event(event)
67
+ .then((is_sussess) => callback(is_sussess, event))
68
+ .catch(err => (0, index_1.log_error)(`handle TOKEN_CONFIG_CHANGE event error!!!`, err));
69
+ break;
70
+ case index_1.REDIS_EVENT_TYPE_CONFIG_CHANGE.POOL_CONFIG_CHANGE:
71
+ this.on_config_pool_change_event(event)
72
+ .then((is_sussess) => callback(is_sussess, event))
73
+ .catch(err => (0, index_1.log_error)(`handle POOL_CONFIG_CHANGE event error!!!`, err));
74
+ break;
75
+ case index_1.REDIS_EVENT_TYPE_CONFIG_CHANGE.GROUP_CONFIG_CHANGE:
76
+ this.on_config_group_change_event(event)
77
+ .then((is_sussess) => callback(is_sussess, event))
78
+ .catch(err => (0, index_1.log_error)(`handle GROUP_CONFIG_CHANGE event error!!!`, err));
79
+ break;
80
+ default:
81
+ (0, index_1.log_warn)(`subscribe_config_change receive unkown type of message: ${message} `);
82
+ }
83
+ };
84
+ yield this.redis_cmd.subscribe(channel, listener);
85
+ (0, index_1.log_debug)(`subscribe_config_change_event, success`, { channel });
86
+ });
87
+ }
88
+ on_config_token_change_event(message) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ (0, index_1.log_debug)(`on_config_token_change_event, refresh local token cache, start`, { message });
91
+ yield this.arb_cache.get_token_list_no_cache();
92
+ (0, index_1.log_debug)(`on_config_token_change_event, refresh local token cache, success`);
93
+ return true;
94
+ });
95
+ }
96
+ on_config_pool_change_event(message) {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ (0, index_1.log_debug)(`on_config_pool_change_event, refresh local pool cache, start`, { message });
99
+ yield this.arb_cache.get_pool_list_no_cache();
100
+ (0, index_1.log_debug)(`on_config_pool_change_event, refresh local pool cache, success`);
101
+ return true;
102
+ });
103
+ }
104
+ on_config_group_change_event(message) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ let group_id_set_in_env = process.env.GROUP_ID;
107
+ let { group_id } = message;
108
+ if (group_id_set_in_env && group_id_set_in_env != group_id) {
109
+ (0, index_1.log_info)(`on_config_group_change_event, group_id not match, skip`, {
110
+ group_id_set_in_env,
111
+ group_id
112
+ });
113
+ return false;
114
+ }
115
+ (0, index_1.log_debug)(`on_config_group_change_event, refresh local group cache, start`, { message });
116
+ yield this.arb_cache.get_trade_service_by_group_id_no_cache(message.group_id);
117
+ (0, index_1.log_debug)(`on_config_group_change_event, refresh local group cache, success`);
118
+ return true;
119
+ });
120
+ }
121
+ subscribe_quote_event(chain_id, callback) {
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ const channel = (0, index_1.get_quote_channel_name)(chain_id);
124
+ const listener = (message, _channel) => {
125
+ (0, index_1.log_debug)('subscribe_quote_event => on event', { channel, message });
126
+ callback(message);
127
+ };
128
+ yield this.redis_cmd.subscribe(channel, listener);
129
+ (0, index_1.log_info)(`subscribe_quote_event, success`, { channel });
130
+ });
131
+ }
132
+ subscribe_order_event(group_id, pair, dex_id, callback) {
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ const channel = (0, index_1.get_order_channel_name)(group_id, pair, dex_id);
135
+ const listener = (message, _channel) => {
136
+ (0, index_1.log_info)('subscribe_order_event => on event', { channel, message });
137
+ callback(message);
138
+ };
139
+ yield this.redis_cmd.subscribe(channel, listener);
140
+ (0, index_1.log_info)(`subscribe_order_event, success`, { channel });
141
+ });
142
+ }
143
+ subscribe_wallet_raw_tx_result(group_id, wallet, callback) {
144
+ return __awaiter(this, void 0, void 0, function* () {
145
+ const channel = (0, index_1.get_wallet_raw_tx_channel_name)(group_id, wallet);
146
+ const listener = (message, _channel) => {
147
+ (0, index_1.log_debug)('subscribe_wallet_raw_tx_result => on event', { channel, message });
148
+ callback(message);
149
+ };
150
+ yield this.redis_cmd.subscribe(channel, listener);
151
+ (0, index_1.log_info)(`subscribe_wallet_raw_tx_result, success`, { channel });
152
+ });
153
+ }
154
+ subscribe_tx_result(group_id, pair, callback) {
155
+ return __awaiter(this, void 0, void 0, function* () {
156
+ const channel = (0, index_1.get_tx_result_channel_name)(group_id, pair);
157
+ const listener = (message, _channel) => {
158
+ (0, index_1.log_info)('subscribe_tx_result => on event', { channel, message });
159
+ callback(message);
160
+ };
161
+ yield this.redis_cmd.subscribe(channel, listener);
162
+ (0, index_1.log_info)(`subscribe_tx_result, success`, { channel });
163
+ });
164
+ }
165
+ }
166
+ exports.ArbEventSubscriber = ArbEventSubscriber;
@@ -0,0 +1,6 @@
1
+ export * from './redis_client';
2
+ export * from './redis_cmd';
3
+ export * from './loading_cache';
4
+ export * from './arb_cache';
5
+ export * from './arb_event_pub';
6
+ export * from './arb_event_sub';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./redis_client"), exports);
18
+ __exportStar(require("./redis_cmd"), exports);
19
+ __exportStar(require("./loading_cache"), exports);
20
+ __exportStar(require("./arb_cache"), exports);
21
+ __exportStar(require("./arb_event_pub"), exports);
22
+ __exportStar(require("./arb_event_sub"), exports);
@@ -0,0 +1,62 @@
1
+ import { RedisClientType } from "redis";
2
+ import { CHAIN_ID } from "../index";
3
+ export declare class LoadingCache {
4
+ redis_client: RedisClientType;
5
+ local_cache: Map<string, string>;
6
+ refresh_cache_timer: Map<string, any>;
7
+ constructor();
8
+ init(): Promise<void>;
9
+ get_key(chain_id: CHAIN_ID, cache_type: string, id: string): string;
10
+ set(chain_id: CHAIN_ID, cache_type: string, id: string, value: string): Promise<{
11
+ key: string;
12
+ res: string;
13
+ }>;
14
+ get(chain_id: CHAIN_ID, cache_type: string, id: string, skip_local?: boolean, auto_refresh?: boolean, refresh_interval_seconds?: number): Promise<{
15
+ key: string;
16
+ value: string;
17
+ }>;
18
+ refresh(key: string, interval_seconds: number): Promise<void>;
19
+ set_ex(chain_id: CHAIN_ID, cache_type: string, id: string, value: string, ttl: number): Promise<{
20
+ key: string;
21
+ res: string;
22
+ ttl: number;
23
+ }>;
24
+ get_hash_key(chain_id: CHAIN_ID, cache_type: string): string;
25
+ hset(chain_id: CHAIN_ID, cache_type: string, field: string, value: string): Promise<{
26
+ key: string;
27
+ field: string;
28
+ res: number[];
29
+ ttl: number;
30
+ }>;
31
+ hset_ex(chain_id: CHAIN_ID, cache_type: string, field: string, value: string, ttl?: number): Promise<{
32
+ key: string;
33
+ field: string;
34
+ res: number[];
35
+ ttl: number;
36
+ }>;
37
+ hdel(chain_id: CHAIN_ID, cache_type: string, field: string): Promise<number>;
38
+ hlen(chain_id: CHAIN_ID, cache_type: string): Promise<{
39
+ key: string;
40
+ value: number;
41
+ }>;
42
+ hkeys(chain_id: CHAIN_ID, cache_type: string): Promise<{
43
+ key: string;
44
+ value: string[];
45
+ }>;
46
+ hget(chain_id: CHAIN_ID, cache_type: string, field: string): Promise<{
47
+ key: string;
48
+ field: string;
49
+ value: string;
50
+ }>;
51
+ hmget(chain_id: CHAIN_ID, cache_type: string, fields: string[]): Promise<{
52
+ key: string;
53
+ fields: string[];
54
+ value: string[];
55
+ }>;
56
+ hget_all(chain_id: CHAIN_ID, cache_type: string): Promise<{
57
+ key: string;
58
+ value: {
59
+ [x: string]: string;
60
+ };
61
+ }>;
62
+ }
@@ -0,0 +1,233 @@
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.LoadingCache = void 0;
13
+ const index_1 = require("../index");
14
+ const redis_client_1 = require("./redis_client");
15
+ class LoadingCache {
16
+ constructor() {
17
+ }
18
+ init() {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ (0, index_1.log_info)('init redis client start');
21
+ this.local_cache = new Map();
22
+ this.refresh_cache_timer = new Map();
23
+ yield (0, index_1.sleep)(1000);
24
+ this.redis_client = yield (0, redis_client_1.getCache)('LoadingCache');
25
+ (0, index_1.log_info)('init redis client done');
26
+ });
27
+ }
28
+ get_key(chain_id, cache_type, id) {
29
+ if ((0, index_1.isEmpty)(chain_id) || (0, index_1.isEmpty)(cache_type) || (0, index_1.isEmpty)(id)) {
30
+ throw new Error(`set cache fialed! chain_id, cache_type, id is null or empty`);
31
+ }
32
+ return (0, index_1.get_cache_key)(chain_id, cache_type, id);
33
+ }
34
+ set(chain_id, cache_type, id, value) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ const key = this.get_key(chain_id, cache_type, id);
37
+ this.local_cache.set(key, value);
38
+ let res = yield this.redis_client.set(key, value);
39
+ return {
40
+ key,
41
+ res
42
+ };
43
+ });
44
+ }
45
+ get(chain_id_1, cache_type_1, id_1) {
46
+ return __awaiter(this, arguments, void 0, function* (chain_id, cache_type, id, skip_local = false, auto_refresh = false, refresh_interval_seconds = 600) {
47
+ const key = this.get_key(chain_id, cache_type, id);
48
+ let value = '';
49
+ if (!skip_local) {
50
+ value = this.local_cache.get(key);
51
+ }
52
+ if (!value) {
53
+ if (index_1.LOG.debug) {
54
+ (0, index_1.log_debug)(`!!! local cache missed, key=${key}`);
55
+ }
56
+ value = yield this.redis_client.get(key);
57
+ let ttl = yield this.redis_client.ttl(key);
58
+ if (ttl === -1) {
59
+ this.local_cache.set(key, value);
60
+ }
61
+ }
62
+ if (!value) {
63
+ if (index_1.LOG.debug) {
64
+ (0, index_1.log_debug)(`!!! redis cache missed, key=${key}`);
65
+ }
66
+ }
67
+ return {
68
+ key,
69
+ value
70
+ };
71
+ });
72
+ }
73
+ refresh(key, interval_seconds) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ if (this.refresh_cache_timer.has(key)) {
76
+ return;
77
+ }
78
+ let interval = Math.max(10, interval_seconds) * 1000;
79
+ let timeout_id = setInterval(() => __awaiter(this, void 0, void 0, function* () {
80
+ try {
81
+ this.redis_client.get(key).then((new_value) => __awaiter(this, void 0, void 0, function* () {
82
+ if (!new_value) {
83
+ (0, index_1.log_debug)(`refresh cache: failed! key=${key}, get value from redis is empty! ignored`);
84
+ return;
85
+ }
86
+ let pre_value = this.local_cache.get(key);
87
+ let is_same = pre_value === new_value;
88
+ (0, index_1.log_debug)(`refresh cache exeute! key=${key}, is_same=${is_same}, refresh interval=${interval}ms`);
89
+ if (!is_same) {
90
+ this.local_cache.set(key, new_value);
91
+ (0, index_1.log_info)(`refresh cache: success! key=${key}`, { pre_value, new_value, is_same });
92
+ }
93
+ }));
94
+ }
95
+ catch (err) {
96
+ (0, index_1.log_error)(`refresh cache: ${key} error`, err);
97
+ }
98
+ }), interval);
99
+ this.refresh_cache_timer.set(key, timeout_id);
100
+ });
101
+ }
102
+ set_ex(chain_id, cache_type, id, value, ttl) {
103
+ return __awaiter(this, void 0, void 0, function* () {
104
+ if (ttl <= 0) {
105
+ throw new Error(`Invalid param: ttl=${ttl}! ttl must > 0`);
106
+ }
107
+ const key = this.get_key(chain_id, cache_type, id);
108
+ let res = yield this.redis_client.set(key, value, { EX: ttl });
109
+ return {
110
+ key,
111
+ res,
112
+ ttl
113
+ };
114
+ });
115
+ }
116
+ get_hash_key(chain_id, cache_type) {
117
+ if ((0, index_1.isEmpty)(chain_id) || (0, index_1.isEmpty)(cache_type)) {
118
+ throw new Error(`set cache fialed! chain_id, cache_type, id is null or empty`);
119
+ }
120
+ return (0, index_1.get_cache_key)(chain_id, cache_type, undefined);
121
+ }
122
+ hset(chain_id, cache_type, field, value) {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ return yield this.hset_ex(chain_id, cache_type, field, value);
125
+ });
126
+ }
127
+ hset_ex(chain_id_1, cache_type_1, field_1, value_1) {
128
+ return __awaiter(this, arguments, void 0, function* (chain_id, cache_type, field, value, ttl = -1) {
129
+ const hkey = (0, index_1.get_cache_key)(chain_id, cache_type, '');
130
+ let res = [];
131
+ let res1 = yield this.redis_client.hSet(hkey, field, value);
132
+ res.push(res1);
133
+ if (ttl > 0) {
134
+ try {
135
+ let res2 = yield this.redis_client.hExpire(hkey, field, ttl);
136
+ res.push(res2);
137
+ }
138
+ catch (err) {
139
+ }
140
+ }
141
+ let data = {
142
+ key: hkey,
143
+ field,
144
+ res,
145
+ ttl
146
+ };
147
+ if (index_1.LOG.trace) {
148
+ (0, index_1.log_trace)('execute hSet, res=', data);
149
+ }
150
+ return data;
151
+ });
152
+ }
153
+ hdel(chain_id, cache_type, field) {
154
+ return __awaiter(this, void 0, void 0, function* () {
155
+ const hkey = (0, index_1.get_cache_key)(chain_id, cache_type, '');
156
+ let res = yield this.redis_client.hDel(hkey, field);
157
+ return res;
158
+ });
159
+ }
160
+ hlen(chain_id, cache_type) {
161
+ return __awaiter(this, void 0, void 0, function* () {
162
+ const hkey = (0, index_1.get_cache_key)(chain_id, cache_type, '');
163
+ let value = yield this.redis_client.hLen(hkey);
164
+ let data = {
165
+ key: hkey,
166
+ value
167
+ };
168
+ if (index_1.LOG.trace) {
169
+ (0, index_1.log_trace)('execute hLen, res=', data);
170
+ }
171
+ return data;
172
+ });
173
+ }
174
+ hkeys(chain_id, cache_type) {
175
+ return __awaiter(this, void 0, void 0, function* () {
176
+ const hkey = (0, index_1.get_cache_key)(chain_id, cache_type, '');
177
+ let value = yield this.redis_client.hKeys(hkey);
178
+ let data = {
179
+ key: hkey,
180
+ value
181
+ };
182
+ if (index_1.LOG.trace) {
183
+ (0, index_1.log_trace)('execute hKeys, res=', data);
184
+ }
185
+ return data;
186
+ });
187
+ }
188
+ hget(chain_id, cache_type, field) {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ const hkey = (0, index_1.get_cache_key)(chain_id, cache_type, '');
191
+ let value = yield this.redis_client.hGet(hkey, field);
192
+ let data = {
193
+ key: hkey,
194
+ field,
195
+ value
196
+ };
197
+ if (index_1.LOG.trace) {
198
+ (0, index_1.log_trace)('execute hGet, res=', data);
199
+ }
200
+ return data;
201
+ });
202
+ }
203
+ hmget(chain_id, cache_type, fields) {
204
+ return __awaiter(this, void 0, void 0, function* () {
205
+ const hkey = (0, index_1.get_cache_key)(chain_id, cache_type, '');
206
+ let value = yield this.redis_client.hmGet(hkey, fields);
207
+ let data = {
208
+ key: hkey,
209
+ fields,
210
+ value
211
+ };
212
+ if (index_1.LOG.trace) {
213
+ (0, index_1.log_trace)('execute hGet, res=', data);
214
+ }
215
+ return data;
216
+ });
217
+ }
218
+ hget_all(chain_id, cache_type) {
219
+ return __awaiter(this, void 0, void 0, function* () {
220
+ const hkey = (0, index_1.get_cache_key)(chain_id, cache_type, '');
221
+ let value = yield this.redis_client.hGetAll(hkey);
222
+ let data = {
223
+ key: hkey,
224
+ value
225
+ };
226
+ if (index_1.LOG.trace) {
227
+ (0, index_1.log_trace)('execute hGetAll, res=', data);
228
+ }
229
+ return data;
230
+ });
231
+ }
232
+ }
233
+ exports.LoadingCache = LoadingCache;
@@ -0,0 +1,3 @@
1
+ import type { RedisClientType } from 'redis';
2
+ declare function getCache(module_name?: string): Promise<RedisClientType>;
3
+ export { getCache, };