@clonegod/ttd-core 2.0.15 → 2.0.16

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,15 @@
1
+ import { TradeResponseType } from "../../types";
2
+ import { AbastrcatTrade } from "../trade/abstract_trade";
3
+ import { AbstractTransactionResultCheck } from "../trade/abstract_tx_check";
4
+ import { AppConfig } from "./AppConfig";
5
+ export declare abstract class AbstractTradeAppConfig extends AppConfig {
6
+ is_already_subscribe_wallet_raw_txn: boolean;
7
+ trade_instance: AbastrcatTrade;
8
+ tx_result_checker: AbstractTransactionResultCheck;
9
+ constructor();
10
+ init(): Promise<void>;
11
+ init_trade_runtime(): Promise<void>;
12
+ subscribe_order_event(trade_instance: AbastrcatTrade, tx_result_checker: AbstractTransactionResultCheck): Promise<void>;
13
+ abstract subscribe_wallet_raw_txn_event(): void;
14
+ cache_trade_result(trade_response: TradeResponseType): Promise<void>;
15
+ }
@@ -0,0 +1,66 @@
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.AbstractTradeAppConfig = void 0;
13
+ const __1 = require("..");
14
+ const handle_order_message_1 = require("../trade/handle_order_message");
15
+ const AppConfig_1 = require("./AppConfig");
16
+ class AbstractTradeAppConfig extends AppConfig_1.AppConfig {
17
+ constructor() {
18
+ super();
19
+ this.is_already_subscribe_wallet_raw_txn = false;
20
+ }
21
+ init() {
22
+ const _super = Object.create(null, {
23
+ init: { get: () => super.init }
24
+ });
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ yield _super.init.call(this);
27
+ this.arb_cache.listen_trade_result(this);
28
+ });
29
+ }
30
+ init_trade_runtime() {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ try {
33
+ let { chain_id, dex_id, trade_group_id, trade_pair } = this.env_args;
34
+ this.trade_runtime = yield this.arb_cache.create_trade_runtime(chain_id, trade_group_id, dex_id, trade_pair);
35
+ this.trade_runtime.wallet_token_accounts = new Map();
36
+ }
37
+ catch (err) {
38
+ (0, __1.log_error)(`create_trade_runtime error!`, err);
39
+ yield (0, __1.sleep)(1000);
40
+ process.exit(0);
41
+ }
42
+ });
43
+ }
44
+ subscribe_order_event(trade_instance, tx_result_checker) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ let handler = (message) => (0, handle_order_message_1.handle_order_message)(this, trade_instance, tx_result_checker, message);
47
+ let group_id = this.trade_runtime.group.id;
48
+ let pair_name = this.trade_runtime.pair_name;
49
+ let dex_id = this.trade_runtime.dex_id;
50
+ yield this.arb_event_subscriber.subscribe_order_event(group_id, pair_name, dex_id, handler);
51
+ (0, __1.log_info)(`subscribe_order_event, success`, { dex_id, group_id, pair_name });
52
+ });
53
+ }
54
+ cache_trade_result(trade_response) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ try {
57
+ (0, __1.log_info)(`cache_trade_result`, trade_response);
58
+ yield this.arb_cache.cache_trade_result(trade_response);
59
+ }
60
+ catch (err) {
61
+ (0, __1.log_error)(`cache_trade_result error!`, err);
62
+ }
63
+ });
64
+ }
65
+ }
66
+ exports.AbstractTradeAppConfig = AbstractTradeAppConfig;
@@ -0,0 +1,16 @@
1
+ import EventEmitter from "events";
2
+ import { StandardTokenInfoType, TradeRuntimeType } from "../../types";
3
+ import { ArbCache, ArbEventSubscriber } from "../cache";
4
+ import { EnvArgs } from "./EnvArgs";
5
+ export declare class AppConfig extends EventEmitter {
6
+ env_args: EnvArgs;
7
+ app_full_name: string;
8
+ arb_cache: ArbCache;
9
+ arb_event_subscriber: ArbEventSubscriber;
10
+ trade_runtime: TradeRuntimeType;
11
+ constructor();
12
+ init(): Promise<void>;
13
+ subscribe_config_change(): Promise<void>;
14
+ cache_token_market_price(token_info_with_price: StandardTokenInfoType, ttl?: number): Promise<void>;
15
+ get_token_market_price(symbol: string): Promise<StandardTokenInfoType>;
16
+ }
@@ -0,0 +1,66 @@
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.AppConfig = void 0;
16
+ const events_1 = __importDefault(require("events"));
17
+ const __1 = require("..");
18
+ const EnvArgs_1 = require("./EnvArgs");
19
+ class AppConfig extends events_1.default {
20
+ constructor() {
21
+ super();
22
+ this.env_args = new EnvArgs_1.EnvArgs();
23
+ let { chain_id, app_name, dex_id } = this.env_args;
24
+ this.app_full_name = `${chain_id}_${dex_id}_${app_name}`;
25
+ global.app_config = this;
26
+ }
27
+ init() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ this.arb_cache = (0, __1.getArbCache)(this.env_args);
30
+ yield this.arb_cache.init();
31
+ let common_service_config = yield this.arb_cache.get_common_service();
32
+ if ((0, __1.isEmpty)(common_service_config)) {
33
+ throw new Error(`get_common_service from cache failed! read from cache=${(0, __1.to_json_str)(common_service_config)}`);
34
+ }
35
+ });
36
+ }
37
+ subscribe_config_change() {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ this.arb_event_subscriber = (0, __1.getArbEventSubscriber)(this.arb_cache);
40
+ yield this.arb_event_subscriber.pre_load_config_cache();
41
+ const refresh_trade_runtime = (is_sussess, event) => __awaiter(this, void 0, void 0, function* () {
42
+ (0, __1.log_info)(`subscribe_config_change_event, callback`, { is_sussess, event });
43
+ if (event.event_type === __1.REDIS_EVENT_TYPE_CONFIG_CHANGE.GROUP_CONFIG_CHANGE) {
44
+ if (!this.trade_runtime) {
45
+ (0, __1.log_warn)(`refresh_trade_runtime, skip...`);
46
+ return;
47
+ }
48
+ this.trade_runtime = yield this.arb_cache.create_trade_runtime(this.trade_runtime.chain_id, this.trade_runtime.group.id, this.trade_runtime.dex_id, this.trade_runtime.pair_name);
49
+ }
50
+ });
51
+ yield this.arb_event_subscriber.subscribe_config_change_event(refresh_trade_runtime);
52
+ });
53
+ }
54
+ cache_token_market_price(token_info_with_price_1) {
55
+ return __awaiter(this, arguments, void 0, function* (token_info_with_price, ttl = -1) {
56
+ token_info_with_price.update_time = (0, __1.getCurDateTime)();
57
+ yield this.arb_cache.cache_token_market_price(token_info_with_price, ttl);
58
+ });
59
+ }
60
+ get_token_market_price(symbol) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ return yield this.arb_cache.get_token_market_price(symbol);
63
+ });
64
+ }
65
+ }
66
+ exports.AppConfig = AppConfig;
@@ -0,0 +1,27 @@
1
+ import { CHAIN_ID, DEX_ID } from "..";
2
+ export declare class EnvArgs {
3
+ app_name: string;
4
+ app_base_dir: string;
5
+ chain_id: CHAIN_ID;
6
+ dex_id: DEX_ID;
7
+ redis_host: string;
8
+ redis_port: string;
9
+ config_center_host: string;
10
+ trade_analyze_url: string;
11
+ rpc_endpoint: string;
12
+ ws_endpoint: string;
13
+ grpc_endpoint: string;
14
+ grpc_token: string;
15
+ auto_quote_enable: boolean;
16
+ auto_quote_interval_mills: number;
17
+ quote_pair: string;
18
+ quote_amount_usd: number;
19
+ quote_pool_address: string;
20
+ quote_pool_name: string;
21
+ quote_pool_fee_rate: number;
22
+ trade_group_id: string;
23
+ trade_pair: string;
24
+ token_price_cache_seconds: number;
25
+ constructor(chain_id?: CHAIN_ID, dex_id?: DEX_ID);
26
+ print(): void;
27
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EnvArgs = void 0;
4
+ const __1 = require("..");
5
+ class EnvArgs {
6
+ constructor(chain_id, dex_id) {
7
+ var _a, _b;
8
+ this.app_name = process.env.APP_NAME || '';
9
+ this.app_base_dir = process.env.APP_BASE_DIR || '';
10
+ this.chain_id = chain_id || ((_a = process.env.CHAIN_ID) === null || _a === void 0 ? void 0 : _a.toUpperCase());
11
+ if ((0, __1.isEmpty)(this.chain_id)) {
12
+ throw new Error(`environment: CHAIN_ID is empty!!!`);
13
+ }
14
+ this.dex_id = dex_id || ((_b = process.env.DEX_ID) === null || _b === void 0 ? void 0 : _b.toUpperCase());
15
+ if ((0, __1.isEmpty)(this.dex_id)) {
16
+ }
17
+ this.redis_host = process.env.REDIS_HOST || '127.0.0.1';
18
+ this.redis_port = process.env.REDIS_PORT || '6379';
19
+ this.config_center_host = process.env.CONFIG_CENTER_HOST || '';
20
+ this.trade_analyze_url = process.env.TRADE_ANALYZE_URL || '';
21
+ this.rpc_endpoint = process.env.RPC_ENDPOINT || '';
22
+ this.ws_endpoint = process.env.WS_ENDPOINT || '';
23
+ this.grpc_endpoint = process.env.GRPC_ENDPOINT || '';
24
+ this.grpc_token = process.env.GRPC_TOKEN || '';
25
+ this.auto_quote_enable = process.env.AUTO_QUOTE_ENABLE === 'true';
26
+ this.auto_quote_interval_mills = parseInt(process.env.AUTO_QUOTE_INTERVAL_MILLS || '60000');
27
+ this.quote_pair = process.env.QUOTE_PAIR || '';
28
+ this.quote_amount_usd = parseInt(process.env.QUOTE_AMOUNT_USD || '100');
29
+ this.quote_pool_address = process.env.QUOTE_POOL_ADDRESS || '';
30
+ this.quote_pool_name = process.env.QUOTE_POOL_NAME || '';
31
+ this.quote_pool_fee_rate = parseInt(process.env.QUOTE_POOL_FEE_RATE || '0');
32
+ this.trade_group_id = process.env.TRADE_GROUP_ID || '';
33
+ this.trade_pair = process.env.TRADE_PAIR || '';
34
+ this.token_price_cache_seconds = parseInt(process.env.TOKEN_PRICE_CACHE_SECONDS || '600');
35
+ }
36
+ print() {
37
+ console.log('>>> EnvArgs', this);
38
+ }
39
+ }
40
+ exports.EnvArgs = EnvArgs;
@@ -1,5 +1,5 @@
1
- import { AppConfig } from './app_config';
2
- export * from './env_args';
3
- export * from './app_config';
4
- export * from './app_trade_config';
1
+ import { AppConfig } from './AppConfig';
2
+ export * from './EnvArgs';
3
+ export * from './AppConfig';
4
+ export * from './AbstractTradeAppConfig';
5
5
  export declare const getGlobalAppConfig: () => AppConfig;
@@ -15,9 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.getGlobalAppConfig = void 0;
18
- __exportStar(require("./env_args"), exports);
19
- __exportStar(require("./app_config"), exports);
20
- __exportStar(require("./app_trade_config"), exports);
18
+ __exportStar(require("./EnvArgs"), exports);
19
+ __exportStar(require("./AppConfig"), exports);
20
+ __exportStar(require("./AbstractTradeAppConfig"), exports);
21
21
  const getGlobalAppConfig = () => {
22
22
  let app_config = global.app_config;
23
23
  if (!app_config) {
@@ -1,6 +1,5 @@
1
- import { AppConfig } from "../app_config";
2
- import { TradeAppConfig } from "../app_config/app_trade_config";
1
+ import { AbstractTradeAppConfig, AppConfig } from "../app_config";
3
2
  import { AbastrcatTrade } from "./abstract_trade";
4
3
  import { AbstractTransactionResultCheck } from "./abstract_tx_check";
5
4
  export declare const try_lock_order_msg: (appConfig: AppConfig, order_msg: any) => Promise<boolean>;
6
- export declare const handle_order_message: (trade_appconfig: TradeAppConfig, trade_instance: AbastrcatTrade, tx_result_checker: AbstractTransactionResultCheck, recv_order_msg: string) => Promise<void>;
5
+ export declare const handle_order_message: (trade_appconfig: AbstractTradeAppConfig, trade_instance: AbastrcatTrade, tx_result_checker: AbstractTransactionResultCheck, recv_order_msg: string) => Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "2.0.15",
3
+ "version": "2.0.16",
4
4
  "description": "Common types and utilities for trading systems - use `npm run push` to publish",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",