@clonegod/ttd-core 2.0.41 → 2.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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +2 -2
- package/dist/app_config/app_config.d.ts +0 -16
- package/dist/app_config/app_config.js +0 -66
- package/dist/app_config/app_trade_config.d.ts +0 -15
- package/dist/app_config/app_trade_config.js +0 -69
- package/dist/app_config/env_args.d.ts +0 -27
- package/dist/app_config/env_args.js +0 -40
- package/dist/evm/index.d.ts +0 -9
- package/dist/evm/index.js +0 -13
- package/dist/token/price/cached_price.d.ts +0 -3
- package/dist/token/price/cached_price.js +0 -115
- package/dist/token/price/get_base_token_price.d.ts +0 -2
- package/dist/token/price/get_base_token_price.js +0 -162
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * from './pool';
|
|
|
9
9
|
export * from './quote';
|
|
10
10
|
export * from './token';
|
|
11
11
|
export * from './trade';
|
|
12
|
+
export * from './chains';
|
|
12
13
|
export declare const FAILED = "FAILED";
|
|
13
14
|
export declare const SUCCESS = "SUCCESS";
|
|
14
15
|
export declare const NOT_FOUND = "NOT_FOUND";
|
package/dist/index.js
CHANGED
|
@@ -89,6 +89,7 @@ __exportStar(require("./pool"), exports);
|
|
|
89
89
|
__exportStar(require("./quote"), exports);
|
|
90
90
|
__exportStar(require("./token"), exports);
|
|
91
91
|
__exportStar(require("./trade"), exports);
|
|
92
|
+
__exportStar(require("./chains"), exports);
|
|
92
93
|
const short = require('short-uuid');
|
|
93
94
|
const exec = require('util').promisify(require('child_process').exec);
|
|
94
95
|
exports.FAILED = 'FAILED';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clonegod/ttd-core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.43",
|
|
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",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"license": "ISC",
|
|
10
10
|
"scripts": {
|
|
11
11
|
"clean": "rm -rf dist node_modules",
|
|
12
|
-
"build": "
|
|
12
|
+
"build": "yarn tsc --outDir ./dist",
|
|
13
13
|
"push": "npm run build && npm publish"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import EventEmitter from "events";
|
|
2
|
-
import { StandardTokenInfoType, TradeRuntimeType } from "../../types";
|
|
3
|
-
import { ArbCache, ArbEventSubscriber } from "../cache";
|
|
4
|
-
import { EnvArgs } from "./env_args";
|
|
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
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
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 env_args_1 = require("./env_args");
|
|
19
|
-
class AppConfig extends events_1.default {
|
|
20
|
-
constructor() {
|
|
21
|
-
super();
|
|
22
|
-
this.env_args = new env_args_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;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { TradeResponseType } from "../../types";
|
|
2
|
-
import { AbastrcatTrade } from "../trade/abstract_trade";
|
|
3
|
-
import { AbstractTransactionResultCheck } from "../trade/abstract_tx_check";
|
|
4
|
-
import { AppConfig } from "./app_config";
|
|
5
|
-
export declare class TradeAppConfig 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
|
-
subscribe_wallet_raw_txn_event(): void;
|
|
14
|
-
cache_trade_result(trade_response: TradeResponseType): Promise<void>;
|
|
15
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
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.TradeAppConfig = void 0;
|
|
13
|
-
const __1 = require("..");
|
|
14
|
-
const handle_order_message_1 = require("../trade/handle_order_message");
|
|
15
|
-
const app_config_1 = require("./app_config");
|
|
16
|
-
class TradeAppConfig extends app_config_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
|
-
subscribe_wallet_raw_txn_event() {
|
|
55
|
-
throw new Error('not implemented');
|
|
56
|
-
}
|
|
57
|
-
cache_trade_result(trade_response) {
|
|
58
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
-
try {
|
|
60
|
-
(0, __1.log_info)(`cache_trade_result`, trade_response);
|
|
61
|
-
yield this.arb_cache.cache_trade_result(trade_response);
|
|
62
|
-
}
|
|
63
|
-
catch (err) {
|
|
64
|
-
(0, __1.log_error)(`cache_trade_result error!`, err);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
exports.TradeAppConfig = TradeAppConfig;
|
|
@@ -1,27 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
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;
|
package/dist/evm/index.d.ts
DELETED
package/dist/evm/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EVM_CHAIN_ID = void 0;
|
|
4
|
-
var EVM_CHAIN_ID;
|
|
5
|
-
(function (EVM_CHAIN_ID) {
|
|
6
|
-
EVM_CHAIN_ID[EVM_CHAIN_ID["ETHEREUM"] = 1] = "ETHEREUM";
|
|
7
|
-
EVM_CHAIN_ID[EVM_CHAIN_ID["BSC"] = 56] = "BSC";
|
|
8
|
-
EVM_CHAIN_ID[EVM_CHAIN_ID["BASE"] = 8453] = "BASE";
|
|
9
|
-
EVM_CHAIN_ID[EVM_CHAIN_ID["ARBITRUM"] = 42161] = "ARBITRUM";
|
|
10
|
-
EVM_CHAIN_ID[EVM_CHAIN_ID["AVALANCHE"] = 43114] = "AVALANCHE";
|
|
11
|
-
EVM_CHAIN_ID[EVM_CHAIN_ID["POLYGON"] = 137] = "POLYGON";
|
|
12
|
-
EVM_CHAIN_ID[EVM_CHAIN_ID["OPTIMISM"] = 10] = "OPTIMISM";
|
|
13
|
-
})(EVM_CHAIN_ID || (exports.EVM_CHAIN_ID = EVM_CHAIN_ID = {}));
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { FormattedTokenPrice } from "../..";
|
|
2
|
-
export declare const fetchPriceFromCache: (token_address_list: string[]) => Promise<Map<string, FormattedTokenPrice>>;
|
|
3
|
-
export declare function cache_new_market_price(token_address: string, market_price: string, market_source: string): Promise<void>;
|
|
@@ -1,115 +0,0 @@
|
|
|
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.fetchPriceFromCache = void 0;
|
|
13
|
-
exports.cache_new_market_price = cache_new_market_price;
|
|
14
|
-
const __1 = require("../..");
|
|
15
|
-
const check_token_price_timeout = (token_info, token_price_timeout_seconds) => {
|
|
16
|
-
if ((0, __1.isEmpty)(token_info.market_price) || (0, __1.isEmpty)(token_info.update_time)) {
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
let last_update_time = token_info.update_time;
|
|
20
|
-
let diff_seconds = Math.floor((Date.now() - (0, __1.parseDateTimeStrToMills)(last_update_time)) / 1000);
|
|
21
|
-
return diff_seconds > token_price_timeout_seconds;
|
|
22
|
-
};
|
|
23
|
-
const get_diff_seconds = (last_update_time, now) => {
|
|
24
|
-
return Math.floor((now - (0, __1.parseDateTimeStrToMills)(last_update_time)) / 1000);
|
|
25
|
-
};
|
|
26
|
-
const fetchPriceFromCache = (token_address_list) => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
-
let result = new Map();
|
|
28
|
-
let global_app_config;
|
|
29
|
-
try {
|
|
30
|
-
global_app_config = (0, __1.getGlobalAppConfig)();
|
|
31
|
-
}
|
|
32
|
-
catch (err) {
|
|
33
|
-
(0, __1.log_warn)('global_app_config is not set, skip get token price from cache');
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
let token_price_timeout_seconds = 600;
|
|
37
|
-
let all_token_list = yield global_app_config.arb_cache.get_token_list();
|
|
38
|
-
let token_list = all_token_list.filter(e => token_address_list.includes(e.address) && !(0, __1.isEmpty)(e.market_price) && !(0, __1.isEmpty)(e.update_time));
|
|
39
|
-
let not_found_token_list = [];
|
|
40
|
-
for (let token_info of token_list) {
|
|
41
|
-
try {
|
|
42
|
-
if (check_token_price_timeout(token_info, token_price_timeout_seconds)) {
|
|
43
|
-
not_found_token_list.push(token_info);
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
let diff_seconds = get_diff_seconds(token_info.update_time, Date.now());
|
|
47
|
-
result.set(token_info.address, {
|
|
48
|
-
address: token_info.address,
|
|
49
|
-
price: token_info.market_price,
|
|
50
|
-
update_time: token_info.update_time,
|
|
51
|
-
});
|
|
52
|
-
(0, __1.log_info)(`get token price from cache(token_info) success: ${token_info.symbol} ${token_info.address}, price=${token_info.market_price} (${diff_seconds}s ago)`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
catch (err) {
|
|
56
|
-
(0, __1.log_error)(`get token price from cache failed`, err);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
for (let token_info of not_found_token_list) {
|
|
60
|
-
let price_info = yield global_app_config.get_token_market_price(token_info.symbol);
|
|
61
|
-
if (!price_info || check_token_price_timeout(price_info, token_price_timeout_seconds)) {
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
result.set(token_info.address, {
|
|
65
|
-
address: token_info.address,
|
|
66
|
-
price: price_info.market_price,
|
|
67
|
-
update_time: price_info.update_time,
|
|
68
|
-
});
|
|
69
|
-
let diff_seconds = get_diff_seconds(price_info.update_time, Date.now());
|
|
70
|
-
(0, __1.log_info)(`get token price from cache(market_price) success: ${token_info.symbol} ${token_info.address}, price=${price_info.market_price} (${diff_seconds}s ago)`);
|
|
71
|
-
}
|
|
72
|
-
return result;
|
|
73
|
-
});
|
|
74
|
-
exports.fetchPriceFromCache = fetchPriceFromCache;
|
|
75
|
-
function cache_new_market_price(token_address, market_price, market_source) {
|
|
76
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
-
let global_app_config;
|
|
78
|
-
try {
|
|
79
|
-
global_app_config = (0, __1.getGlobalAppConfig)();
|
|
80
|
-
}
|
|
81
|
-
catch (err) {
|
|
82
|
-
(0, __1.log_warn)('global_app_config is not set, skip get token price from cache');
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
let ttl = global_app_config.env_args.token_price_cache_seconds;
|
|
86
|
-
let tokenInfo = global_app_config.arb_cache.token_address_map.get(token_address);
|
|
87
|
-
if (!tokenInfo) {
|
|
88
|
-
tokenInfo = (yield global_app_config.arb_cache.get_token_list_no_cache()).find(e => e.address === token_address);
|
|
89
|
-
}
|
|
90
|
-
if (!tokenInfo) {
|
|
91
|
-
(0, __1.log_warn)(`tokenInfo is not found in cache: token_address=${token_address}`);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
let { symbol, address } = tokenInfo;
|
|
95
|
-
if (market_price && Number(market_price) > 0) {
|
|
96
|
-
let clone_token_info = (0, __1.deep_clone)(tokenInfo);
|
|
97
|
-
clone_token_info.market_price = market_price;
|
|
98
|
-
global_app_config.cache_token_market_price(clone_token_info, ttl)
|
|
99
|
-
.then(() => {
|
|
100
|
-
(0, __1.log_info)(`cache new market price success: ${symbol} ${address}, price=${market_price} (${ttl}s), source=${market_source}`);
|
|
101
|
-
})
|
|
102
|
-
.catch(err => {
|
|
103
|
-
(0, __1.log_error)(`cache new market price failed: ${symbol} ${address}, price=${market_price} (${ttl}s)`, err);
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
(0, __1.log_warn)(`skip cache invalid market_price from ${market_source}`, {
|
|
108
|
-
symbol,
|
|
109
|
-
address,
|
|
110
|
-
market_price,
|
|
111
|
-
market_source,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}
|
|
@@ -1,162 +0,0 @@
|
|
|
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.get_base_token_price_info = get_base_token_price_info;
|
|
16
|
-
require('dotenv').config();
|
|
17
|
-
const axios_1 = __importDefault(require("axios"));
|
|
18
|
-
const index_1 = require("../../index");
|
|
19
|
-
const gecko_terminal_1 = require("./gecko_terminal");
|
|
20
|
-
const price_cache_1 = require("./price_cache");
|
|
21
|
-
function get_base_token_price_info(addresses) {
|
|
22
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
addresses = addresses.map(addr => addr.toLowerCase());
|
|
24
|
-
const result = new Map();
|
|
25
|
-
const PRICE_CHANNELS = [
|
|
26
|
-
{
|
|
27
|
-
name: 'CachedPrice',
|
|
28
|
-
fetchFn: price_cache_1.fetchPriceFromCache,
|
|
29
|
-
batchSize: 100,
|
|
30
|
-
batchDelay: 1000,
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
name: 'GeckoTerminal',
|
|
34
|
-
fetchFn: (address_list) => (0, gecko_terminal_1.fetchPriceFromGeckoTerminal)(index_1.CHAIN_ID.BASE, address_list),
|
|
35
|
-
batchSize: 10,
|
|
36
|
-
batchDelay: 2000,
|
|
37
|
-
},
|
|
38
|
-
];
|
|
39
|
-
try {
|
|
40
|
-
for (const channel of PRICE_CHANNELS) {
|
|
41
|
-
if (addresses.length === 0)
|
|
42
|
-
break;
|
|
43
|
-
(0, index_1.log_debug)(`[get_token_price_info] Processing ${addresses.length} tokens using ${channel.name}`);
|
|
44
|
-
const batches = (0, index_1.chunkArray)(addresses, channel.batchSize);
|
|
45
|
-
(0, index_1.log_debug)(`[get_token_price_info] Split into ${batches.length} batches of size ${channel.batchSize}`);
|
|
46
|
-
let remainingAddresses = [...addresses];
|
|
47
|
-
for (let i = 0; i < batches.length; i++) {
|
|
48
|
-
const batch = batches[i];
|
|
49
|
-
if (batch.length === 0)
|
|
50
|
-
continue;
|
|
51
|
-
(0, index_1.log_debug)(`[get_token_price_info] Processing batch ${i + 1}/${batches.length} (${batch.length} tokens) with ${channel.name}`);
|
|
52
|
-
try {
|
|
53
|
-
const channelResult = yield channel.fetchFn(batch);
|
|
54
|
-
for (const [address, priceInfo] of channelResult.entries()) {
|
|
55
|
-
result.set(address, priceInfo);
|
|
56
|
-
remainingAddresses = remainingAddresses.filter(addr => addr !== address);
|
|
57
|
-
if (channel.name !== 'CachedPrice') {
|
|
58
|
-
(0, price_cache_1.cache_new_market_price)(address, priceInfo.price, channel.name);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
(0, index_1.log_warn)(`[get_token_price_info] Error processing batch ${i + 1} with ${channel.name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
64
|
-
}
|
|
65
|
-
if (i < batches.length - 1) {
|
|
66
|
-
yield (0, index_1.sleep)(channel.batchDelay);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
addresses = [...remainingAddresses];
|
|
70
|
-
if (addresses.length === 0) {
|
|
71
|
-
(0, index_1.log_debug)(`[get_token_price_info] All token prices retrieved using ${channel.name}`);
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
if (result.size === 0) {
|
|
76
|
-
throw new Error(`Unable to get price information for any token: ${addresses.join(', ')}`);
|
|
77
|
-
}
|
|
78
|
-
if (addresses.length > 0) {
|
|
79
|
-
(0, index_1.log_warn)(`[get_token_price_info] Failed to get prices for ${addresses.length} tokens after trying all channels: ${addresses.join(', ')}`);
|
|
80
|
-
}
|
|
81
|
-
(0, index_1.log_debug)(`[get_token_price_info] Completed price fetching for ${result.size} tokens`);
|
|
82
|
-
return result;
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
throw new Error(`Failed to get token price information: ${error instanceof Error ? error.message : String(error)}`);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
function fetchPriceFromUniSwap(addresses) {
|
|
90
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
-
var _a, _b, _c, _d;
|
|
92
|
-
const result = new Map();
|
|
93
|
-
const currentTime = (0, index_1.getCurDateTime)();
|
|
94
|
-
const query = `
|
|
95
|
-
query Tokens($contracts: [ContractInput!]!) {
|
|
96
|
-
tokens(contracts: $contracts) {
|
|
97
|
-
id
|
|
98
|
-
address
|
|
99
|
-
symbol
|
|
100
|
-
name
|
|
101
|
-
decimals
|
|
102
|
-
market(currency: USD) {
|
|
103
|
-
price { value }
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
`;
|
|
108
|
-
const variables = {
|
|
109
|
-
contracts: addresses.map(address => ({
|
|
110
|
-
address: address,
|
|
111
|
-
chain: 'BASE'
|
|
112
|
-
}))
|
|
113
|
-
};
|
|
114
|
-
const headers = {
|
|
115
|
-
'Content-Type': 'application/json',
|
|
116
|
-
'Origin': 'https://app.uniswap.org',
|
|
117
|
-
'Referer': 'https://app.uniswap.org/',
|
|
118
|
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
|
119
|
-
};
|
|
120
|
-
try {
|
|
121
|
-
const response = yield axios_1.default.post('https://interface.gateway.uniswap.org/v1/graphql', {
|
|
122
|
-
query,
|
|
123
|
-
variables
|
|
124
|
-
}, {
|
|
125
|
-
headers,
|
|
126
|
-
timeout: 30000
|
|
127
|
-
});
|
|
128
|
-
if ((_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.tokens) {
|
|
129
|
-
for (const token of response.data.data.tokens) {
|
|
130
|
-
if ((_d = (_c = token.market) === null || _c === void 0 ? void 0 : _c.price) === null || _d === void 0 ? void 0 : _d.value) {
|
|
131
|
-
result.set(token.address.toLowerCase(), {
|
|
132
|
-
symbol: token.symbol || '',
|
|
133
|
-
address: token.address.toLowerCase(),
|
|
134
|
-
price: token.market.price.value.toString(),
|
|
135
|
-
update_time: currentTime
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
(0, index_1.log_debug)(`[fetchPriceFromUniSwap] Retrieved prices for ${result.size}/${addresses.length} tokens`);
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
(0, index_1.log_debug)(`[fetchPriceFromUniSwap] Invalid response from Uniswap for ${addresses.length} tokens`);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
catch (error) {
|
|
146
|
-
(0, index_1.log_warn)(`[fetchPriceFromUniSwap] Request failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
147
|
-
}
|
|
148
|
-
return result;
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
if (require.main === module) {
|
|
152
|
-
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
153
|
-
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
154
|
-
let addresses = [
|
|
155
|
-
"0x4200000000000000000000000000000000000006",
|
|
156
|
-
"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
|
|
157
|
-
"0x940181a94A35A4569E4529A3CDfB74e38FD98631",
|
|
158
|
-
];
|
|
159
|
-
console.log(yield get_base_token_price_info(addresses));
|
|
160
|
-
}))();
|
|
161
|
-
}))();
|
|
162
|
-
}
|