@clonegod/ttd-sol-common 1.0.41 → 1.0.43

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.
@@ -32,16 +32,24 @@ class AppConfig extends events_1.EventEmitter {
32
32
  this.arb_cache = (0, dist_1.getArbCache)(this.env_args);
33
33
  yield this.arb_cache.init();
34
34
  this.arb_cache.listen_trade_result(this);
35
- this.connection = new web3_js_1.Connection(this.env_args.quicknode_endpoint, {
35
+ this.connection = new web3_js_1.Connection(this.env_args.rpc_endpoint, {
36
36
  commitment: common_1.COMMITMENT_LEVEL.CONFIRMED,
37
37
  });
38
+ let common_service_config = yield this.arb_cache.get_common_service();
39
+ if ((0, dist_1.isEmpty)(common_service_config)) {
40
+ throw new Error(`get_common_service from cache failed! read from cache=${(0, dist_1.to_json_str)(common_service_config)}`);
41
+ }
42
+ let { config_center } = common_service_config;
43
+ this.query_token_market_price_url = this.env_args.token_price_url
44
+ .replace('${config_center_http_port}', config_center.http_port + '')
45
+ .replace('${chain_id}', this.env_args.chain_id);
38
46
  });
39
47
  }
40
48
  init_trade_runtime() {
41
49
  return __awaiter(this, void 0, void 0, function* () {
42
50
  try {
43
- let { chain_id, input_group_id, input_dex_id, input_pair } = this.env_args.trade_cmd_args;
44
- this.trade_runtime = yield this.arb_cache.create_trade_runtime(chain_id, input_group_id, input_dex_id, input_pair);
51
+ let { chain_id, dex_id_list, input_group_id, input_pair } = this.env_args;
52
+ this.trade_runtime = yield this.arb_cache.create_trade_runtime(chain_id, input_group_id, dex_id_list[0], input_pair);
45
53
  let secret_key = new Uint8Array(bs58_1.default.decode(this.trade_runtime.wallet.private_key));
46
54
  this.keypair = web3_js_1.Keypair.fromSecretKey(secret_key);
47
55
  }
@@ -56,6 +64,7 @@ class AppConfig extends events_1.EventEmitter {
56
64
  return __awaiter(this, void 0, void 0, function* () {
57
65
  this.arb_event_subscriber = (0, dist_1.getArbEventSubscriber)(this.arb_cache);
58
66
  yield this.arb_event_subscriber.pre_load_config_cache();
67
+ yield this.arb_event_subscriber.subscribe_config_change_event();
59
68
  const refresh_trade_runtime = (is_sussess, event) => __awaiter(this, void 0, void 0, function* () {
60
69
  (0, dist_1.log_info)(`subscribe_config_change_event, callback`, { is_sussess, event });
61
70
  if (event.event_type === dist_1.REDIS_EVENT_TYPE_CONFIG_CHANGE.GROUP_CONFIG_CHANGE) {
@@ -1,14 +1,19 @@
1
- import { TradeCommandLineArgs } from "@clonegod/ttd-common";
2
1
  import { CHAIN_ID } from "@clonegod/ttd-common/dist";
3
2
  export declare class EnvArgs {
4
3
  chain_id: CHAIN_ID;
5
- dex_id_list: string[];
6
4
  app_name: string;
7
5
  redis_host: string;
8
6
  redis_port: string;
9
- quicknode_endpoint: string;
7
+ dex_id_list: string[];
8
+ input_group_id: string;
9
+ input_pair: string;
10
+ rpc_endpoint: string;
10
11
  helius_api_key: string;
11
- trade_cmd_args: TradeCommandLineArgs;
12
+ geyser_ws_url: string;
13
+ token_price_url: string;
14
+ auto_quote_enable: boolean;
15
+ auto_quote_interval_seconds: number;
16
+ quote_pair_list: string[];
12
17
  constructor();
13
18
  init(): void;
14
19
  }
@@ -7,18 +7,23 @@ class EnvArgs {
7
7
  this.init();
8
8
  }
9
9
  init() {
10
- this.trade_cmd_args = (0, dist_1.parse_trade_cmdline_args)();
11
- let { input_pair, input_dex_id, input_group_id } = this.trade_cmd_args;
12
- this.app_name = `${input_pair}_${input_dex_id}_${input_group_id}`;
10
+ let dex_id_list = (process.env.DEX_ID_LIST || '').split(',');
11
+ let input_pair = process.env.INPUT_PAIR;
12
+ let input_group_id = process.env.INPUT_GROUP_ID;
13
+ this.app_name = `${input_pair}_${dex_id_list[0]}_${input_group_id}`;
13
14
  this.chain_id = process.env.CHAIN_ID.toUpperCase();
14
15
  if ((0, dist_1.isEmpty)(this.chain_id)) {
15
16
  throw new Error(`environment: CHAIN_ID is empty!!!`);
16
17
  }
17
- this.dex_id_list = [process.env.INPUT_DEX_ID];
18
18
  this.redis_host = process.env.REDIS_HOST;
19
19
  this.redis_port = process.env.REDIS_PORT;
20
- this.quicknode_endpoint = process.env.QUICKNODE_ENDPOINT;
20
+ this.rpc_endpoint = process.env.RPC_ENDPOINT;
21
21
  this.helius_api_key = process.env.HELIUS_API_KEY;
22
+ this.geyser_ws_url = `wss://atlas-mainnet.helius-rpc.com/?api-key=${process.env.HELIUS_API_KEY}`;
23
+ this.quote_pair_list = (process.env.QUOTE_PAIR_LIST || '').split(',');
24
+ this.auto_quote_enable = process.env.AUTO_QUOTE_ENABLE === 'true';
25
+ this.auto_quote_interval_seconds = parseInt(process.env.AUTO_QUOTE_INTERVAL_SECONDS || '60');
26
+ this.token_price_url = process.env.TOKEN_PRICE_URL;
22
27
  (0, dist_1.log_info)('Env args init finish', this);
23
28
  }
24
29
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export * from './common';
2
1
  export * from './appconfig';
2
+ export * from './common';
3
3
  export * from './helius_geyser_ws';
4
+ export * from './quote';
4
5
  export * from './send_tx';
5
6
  export * from './trade';
package/dist/index.js CHANGED
@@ -14,8 +14,9 @@ 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
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./common"), exports);
18
17
  __exportStar(require("./appconfig"), exports);
18
+ __exportStar(require("./common"), exports);
19
19
  __exportStar(require("./helius_geyser_ws"), exports);
20
+ __exportStar(require("./quote"), exports);
20
21
  __exportStar(require("./send_tx"), exports);
21
22
  __exportStar(require("./trade"), exports);
@@ -0,0 +1,3 @@
1
+ export * from './log_quote_price';
2
+ export * from './publish_quote_price';
3
+ export * from './to_price_message';
@@ -0,0 +1,19 @@
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("./log_quote_price"), exports);
18
+ __exportStar(require("./publish_quote_price"), exports);
19
+ __exportStar(require("./to_price_message"), exports);
@@ -0,0 +1,2 @@
1
+ import { PriceMessageType } from "@clonegod/ttd-common";
2
+ export declare function log_quote_price(event_source: string, price_message: PriceMessageType, tx_hash: string): Promise<void>;
@@ -0,0 +1,31 @@
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.log_quote_price = void 0;
13
+ const dist_1 = require("@clonegod/ttd-common/dist");
14
+ function log_quote_price(event_source, price_message, tx_hash) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ const { unique_orderbook_id, pool_name, pair, fee_rate, price_id, quote_amount_usd, ask, bid, time } = price_message;
17
+ let price_time = new Date().getTime();
18
+ let price_time_str = (0, dist_1.parseToDateTime)(price_time);
19
+ let times = [
20
+ time.stream_time - time.block_time,
21
+ time.quote_end_time - time.quote_start_time,
22
+ time.price_time - time.stream_time,
23
+ time.price_time - time.block_time,
24
+ ];
25
+ setTimeout(() => {
26
+ console.log(`> [${event_source}] - ${tx_hash}`);
27
+ console.log(price_time_str, ' - ', unique_orderbook_id, ' - ', String(price_id).padEnd(32), ' - ', pool_name.padEnd(12), ' - ', pair.padEnd(12), ' - ', String(quote_amount_usd).padEnd(4), ' - ', String(ask.price).padEnd(8), ' - ', String(bid.price).padEnd(8), ' - ', `${times.join(',')} ms`, '\n');
28
+ }, 0);
29
+ });
30
+ }
31
+ exports.log_quote_price = log_quote_price;
@@ -0,0 +1,3 @@
1
+ import { PriceMessageType } from "@clonegod/ttd-common";
2
+ import { AppConfig } from "../appconfig";
3
+ export declare const publish_quote_price: (appConfig: AppConfig, price_msg: PriceMessageType) => void;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.publish_quote_price = void 0;
4
+ const dist_1 = require("@clonegod/ttd-common/dist");
5
+ const publish_quote_price = (appConfig, price_msg) => {
6
+ const chain_id = appConfig.env_args.chain_id;
7
+ let price_id = price_msg.price_id;
8
+ let event = {
9
+ event_type: dist_1.REDIS_EVENT_TYPE_QUOTE.NEW_QUOTE_PRICE,
10
+ event_time: Date.now(),
11
+ chain_id,
12
+ data: price_msg
13
+ };
14
+ appConfig.arb_cache.redis_event_publisher.publish_quote_price_event(chain_id, event);
15
+ appConfig.arb_cache.cache_price_message(price_msg)
16
+ .then(() => (0, dist_1.log_debug)(`cache price success, price_id=${price_id}`))
17
+ .catch(err => (0, dist_1.log_error)(`cache price error! price_id=${price_id}`, err));
18
+ };
19
+ exports.publish_quote_price = publish_quote_price;
@@ -0,0 +1,4 @@
1
+ import { PriceMessageType, QuoteTimeInfoType, StandardPoolInfoType } from '@clonegod/ttd-common';
2
+ import { AppConfig } from '../appconfig';
3
+ export declare function to_price_message(appConfig: AppConfig, quote_amount_usd: number, tx_price: number, quote_ask_price: number, quote_bid_price: number, pool_info: StandardPoolInfoType, time: QuoteTimeInfoType): Promise<PriceMessageType>;
4
+ export declare function normalize_pair_name(priceMessage: PriceMessageType): void;
@@ -0,0 +1,90 @@
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.normalize_pair_name = exports.to_price_message = void 0;
16
+ const decimal_js_1 = __importDefault(require("decimal.js"));
17
+ const uuid_1 = require("uuid");
18
+ const dist_1 = require("@clonegod/ttd-common/dist");
19
+ function to_price_message(appConfig, quote_amount_usd, tx_price, quote_ask_price, quote_bid_price, pool_info, time) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ let { pool_address, tokenA, tokenB } = pool_info;
22
+ let { dex_id, pool_name, fee_rate, is_reverse_token } = yield appConfig.arb_cache.get_one_pool_info(pool_address);
23
+ let _dex_id = dex_id;
24
+ let price_id = (0, uuid_1.v4)().replace(/-/gi, '');
25
+ let chain_id = appConfig.env_args.chain_id;
26
+ let unique_orderbook_id = (0, dist_1.format_unique_orderbook_id)(chain_id, _dex_id, pool_address, fee_rate);
27
+ time.price_time = new Date().getTime();
28
+ let fee_rate_bps = fee_rate / 100100;
29
+ let ask_price = getAskPrice(new decimal_js_1.default(quote_ask_price), fee_rate_bps * 0);
30
+ let bid_price = getBidPrice(new decimal_js_1.default(quote_bid_price), fee_rate_bps * 0);
31
+ let price_message = {
32
+ chain_id,
33
+ dex_id,
34
+ price_id,
35
+ pool_id: pool_address,
36
+ pool_name,
37
+ fee_rate,
38
+ pair: pool_name,
39
+ unique_orderbook_id,
40
+ is_reverse_token,
41
+ tokenA: {
42
+ symbol: tokenA.symbol,
43
+ address: tokenA.address,
44
+ address_hex: tokenA.address_hex,
45
+ decimals: tokenA.decimals,
46
+ name: tokenA.name,
47
+ enable: true,
48
+ },
49
+ tokenB: {
50
+ symbol: tokenB.symbol,
51
+ address: tokenB.address,
52
+ address_hex: tokenB.address_hex,
53
+ decimals: tokenB.decimals,
54
+ name: tokenB.name,
55
+ enable: true,
56
+ },
57
+ tx_price: tx_price === null || tx_price === void 0 ? void 0 : tx_price.toFixed(8),
58
+ quote_amount_usd,
59
+ ask: {
60
+ price: ask_price,
61
+ quantity: quote_amount_usd
62
+ },
63
+ bid: {
64
+ price: bid_price,
65
+ quantity: quote_amount_usd
66
+ },
67
+ time
68
+ };
69
+ normalize_pair_name(price_message);
70
+ return price_message;
71
+ });
72
+ }
73
+ exports.to_price_message = to_price_message;
74
+ const getAskPrice = (price, feeRate) => {
75
+ return price.mul(1 + feeRate).toFixed(8);
76
+ };
77
+ const getBidPrice = (price, feeRate) => {
78
+ return price.mul(1 - feeRate).toFixed(8);
79
+ };
80
+ function normalize_pair_name(priceMessage) {
81
+ let { pool_name, is_reverse_token } = priceMessage;
82
+ if (is_reverse_token) {
83
+ pool_name = pool_name.split('/')[1] + '/' + pool_name.split('/')[0];
84
+ }
85
+ const dex_pair_names = pool_name.split('/');
86
+ let cex_base_token = (0, dist_1.format_symbol_name)(dex_pair_names[0]);
87
+ let cex_qutoe_token = (0, dist_1.format_symbol_name)(dex_pair_names[1]);
88
+ priceMessage.pair = cex_base_token.toUpperCase() + '/' + cex_qutoe_token.toUpperCase();
89
+ }
90
+ exports.normalize_pair_name = normalize_pair_name;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sol-common",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -13,7 +13,7 @@
13
13
  "push": "npm run build && npm publish"
14
14
  },
15
15
  "dependencies": {
16
- "@clonegod/ttd-common": "1.0.147",
16
+ "@clonegod/ttd-common": "^1.0.147",
17
17
  "@solana/web3.js": "1.91.6",
18
18
  "@irys/sdk": "^0.2.10",
19
19
  "@metaplex-foundation/mpl-token-metadata": "^2.5.2",