@clonegod/ttd-base-common 1.0.0

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 (73) hide show
  1. package/dist/common/abi.d.ts +1 -0
  2. package/dist/common/abi.js +14 -0
  3. package/dist/common/constants.d.ts +18 -0
  4. package/dist/common/constants.js +34 -0
  5. package/dist/common/index.d.ts +2 -0
  6. package/dist/common/index.js +18 -0
  7. package/dist/config/base_env_args.d.ts +11 -0
  8. package/dist/config/base_env_args.js +19 -0
  9. package/dist/config/bsc_env_args.d.ts +11 -0
  10. package/dist/config/bsc_env_args.js +19 -0
  11. package/dist/config/index.d.ts +1 -0
  12. package/dist/config/index.js +17 -0
  13. package/dist/index.d.ts +7 -0
  14. package/dist/index.js +23 -0
  15. package/dist/quote/event/index.d.ts +1 -0
  16. package/dist/quote/event/index.js +17 -0
  17. package/dist/quote/event/pool_event_listener.d.ts +53 -0
  18. package/dist/quote/event/pool_event_listener.js +334 -0
  19. package/dist/quote/event/verify_clmm_swap_event.d.ts +1 -0
  20. package/dist/quote/event/verify_clmm_swap_event.js +178 -0
  21. package/dist/quote/index.d.ts +2 -0
  22. package/dist/quote/index.js +18 -0
  23. package/dist/quote/pricing/index.d.ts +2 -0
  24. package/dist/quote/pricing/index.js +18 -0
  25. package/dist/quote/pricing/pool.d.ts +13 -0
  26. package/dist/quote/pricing/pool.js +21 -0
  27. package/dist/quote/pricing/token_price_cache.d.ts +10 -0
  28. package/dist/quote/pricing/token_price_cache.js +40 -0
  29. package/dist/redis/index.d.ts +1 -0
  30. package/dist/redis/index.js +17 -0
  31. package/dist/redis/redis_client.d.ts +21 -0
  32. package/dist/redis/redis_client.js +155 -0
  33. package/dist/trade/abstract_dex_trade.d.ts +27 -0
  34. package/dist/trade/abstract_dex_trade.js +153 -0
  35. package/dist/trade/abstract_dex_trade_plus.d.ts +30 -0
  36. package/dist/trade/abstract_dex_trade_plus.js +227 -0
  37. package/dist/trade/check/index.d.ts +1 -0
  38. package/dist/trade/check/index.js +17 -0
  39. package/dist/trade/check/tx_websocket_manager.d.ts +23 -0
  40. package/dist/trade/check/tx_websocket_manager.js +119 -0
  41. package/dist/trade/index.d.ts +5 -0
  42. package/dist/trade/index.js +21 -0
  43. package/dist/trade/parse/abstract_parser.d.ts +8 -0
  44. package/dist/trade/parse/abstract_parser.js +2 -0
  45. package/dist/trade/parse/base_parser.d.ts +19 -0
  46. package/dist/trade/parse/base_parser.js +69 -0
  47. package/dist/trade/parse/index.d.ts +1 -0
  48. package/dist/trade/parse/index.js +5 -0
  49. package/dist/trade/send/48club.d.ts +18 -0
  50. package/dist/trade/send/48club.js +97 -0
  51. package/dist/trade/send/blockrazor.d.ts +7 -0
  52. package/dist/trade/send/blockrazor.js +79 -0
  53. package/dist/trade/send/bsc_rpc.d.ts +6 -0
  54. package/dist/trade/send/bsc_rpc.js +43 -0
  55. package/dist/trade/send/index.d.ts +5 -0
  56. package/dist/trade/send/index.js +23 -0
  57. package/dist/trade/send/send_tx.d.ts +8 -0
  58. package/dist/trade/send/send_tx.js +91 -0
  59. package/dist/types/config_types.d.ts +28 -0
  60. package/dist/types/config_types.js +2 -0
  61. package/dist/types/event_types.d.ts +30 -0
  62. package/dist/types/event_types.js +2 -0
  63. package/dist/types/index.d.ts +4 -0
  64. package/dist/types/index.js +20 -0
  65. package/dist/types/quote_types.d.ts +27 -0
  66. package/dist/types/quote_types.js +2 -0
  67. package/dist/types/trade_types.d.ts +38 -0
  68. package/dist/types/trade_types.js +8 -0
  69. package/dist/utils/gas_helper.d.ts +5 -0
  70. package/dist/utils/gas_helper.js +72 -0
  71. package/dist/utils/index.d.ts +7 -0
  72. package/dist/utils/index.js +43 -0
  73. package/package.json +30 -0
@@ -0,0 +1,153 @@
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.AbstractEvmDexTrade = void 0;
13
+ const dist_1 = require("@clonegod/ttd-core/dist");
14
+ const ethers_1 = require("ethers");
15
+ const common_1 = require("../common");
16
+ class AbstractEvmDexTrade extends dist_1.AbastrcatTrade {
17
+ constructor(appConfig) {
18
+ super();
19
+ this.appConfig = appConfig;
20
+ this.approvedTokens = new Map();
21
+ this.pairContracts = new Map();
22
+ this.tokenContracts = new Map();
23
+ this.approvedTokens = new Map();
24
+ this.pairContracts = new Map();
25
+ this.tokenContracts = new Map();
26
+ this.initConfigs();
27
+ }
28
+ init() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider(this.chainConfig.rpcEndpoint);
31
+ this.wallet = new ethers_1.ethers.Wallet(this.appConfig.trade_runtime.wallet.private_key, this.provider);
32
+ (0, dist_1.log_info)(`钱包已初始化,地址: ${this.wallet.address}`);
33
+ this.routerContract = new ethers_1.ethers.Contract(this.dexConfig.routerAddress, this.dexConfig.routerAbi, this.wallet);
34
+ (0, dist_1.log_info)(`${this.dexConfig.dexName} Router已初始化, 地址: ${this.dexConfig.routerAddress}`);
35
+ });
36
+ }
37
+ getTokenContract(tokenAddress) {
38
+ if (!this.tokenContracts.has(tokenAddress)) {
39
+ const tokenContract = new ethers_1.ethers.Contract(tokenAddress, common_1.ERC20_ABI, this.wallet);
40
+ this.tokenContracts.set(tokenAddress, tokenContract);
41
+ }
42
+ return this.tokenContracts.get(tokenAddress);
43
+ }
44
+ getGasPriceGwei(context) {
45
+ let { evm_gas_limit, evm_gas_price_gwei, evm_tip_amount_gwei } = context.trade_runtime.settings.strategy;
46
+ if (evm_gas_price_gwei === undefined || evm_gas_price_gwei === null || evm_gas_price_gwei <= 0) {
47
+ evm_gas_price_gwei = this.chainConfig.gasOptions.defaultGasPriceGwei;
48
+ }
49
+ (0, dist_1.log_info)(`getGasPriceGwei: ${evm_gas_price_gwei} Gwei`);
50
+ return evm_gas_price_gwei.toString();
51
+ }
52
+ getBuilderTipAmoutGwei(context) {
53
+ let { evm_gas_limit, evm_gas_price_gwei, evm_tip_amount_gwei } = context.trade_runtime.settings.strategy;
54
+ if (evm_tip_amount_gwei === undefined || evm_tip_amount_gwei === null || evm_tip_amount_gwei <= 0) {
55
+ evm_tip_amount_gwei = this.chainConfig.gasOptions.defaultTipAmountGwei;
56
+ }
57
+ (0, dist_1.log_info)(`getGasTipAmoutGwei: ${evm_tip_amount_gwei} Gwei`);
58
+ return evm_tip_amount_gwei.toString();
59
+ }
60
+ checkTradeTokenApprove(context, routerAddress) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const { pool_info } = context;
63
+ const { tokenA, tokenB } = pool_info;
64
+ const router = routerAddress || this.dexConfig.routerAddress;
65
+ yield Promise.all([tokenA, tokenB].map((_a) => __awaiter(this, [_a], void 0, function* ({ symbol, address }) {
66
+ const tokenContract = this.getTokenContract(address);
67
+ yield this.checkTokenApprove(address, symbol, tokenContract, router);
68
+ })));
69
+ });
70
+ }
71
+ checkTokenApprove(tokenAddress, tokenSymbol, tokenContract, spenderAddress) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ if (this.approvedTokens.get(tokenAddress)) {
74
+ (0, dist_1.log_info)(`代币 ${tokenSymbol} 已授权,跳过授权检查`);
75
+ return true;
76
+ }
77
+ const allowance = yield tokenContract.allowance(this.wallet.address, spenderAddress);
78
+ const maxAllowance = ethers_1.ethers.constants.MaxUint256.div(2);
79
+ (0, dist_1.log_info)(`代币授权检查:`, {
80
+ tokenSymbol,
81
+ currentAllowance: allowance.toString(),
82
+ maxAllowance: maxAllowance.toString(),
83
+ isSufficient: allowance.gte(maxAllowance)
84
+ });
85
+ if (allowance.lt(maxAllowance)) {
86
+ const maxApprovalAmount = ethers_1.ethers.utils.formatUnits(ethers_1.ethers.constants.MaxUint256, yield tokenContract.decimals());
87
+ const tokenSymbolDisplay = tokenSymbol || `${tokenAddress.substring(0, 6)}...`;
88
+ (0, dist_1.log_info)(`正在为合约授权代币 ${tokenSymbolDisplay}`, {
89
+ tokenAddress,
90
+ tokenSymbol: tokenSymbolDisplay,
91
+ currentAllowance: ethers_1.ethers.utils.formatUnits(allowance, yield tokenContract.decimals()),
92
+ newAllowance: maxApprovalAmount
93
+ });
94
+ const tx = yield tokenContract.approve(spenderAddress, ethers_1.ethers.constants.MaxUint256, {
95
+ gasLimit: this.chainConfig.gasOptions.gasLimit
96
+ });
97
+ yield tx.wait();
98
+ this.approvedTokens.set(tokenAddress, true);
99
+ (0, dist_1.log_info)(`代币 ${tokenSymbolDisplay} 授权完成,数量: ${maxApprovalAmount} ${tokenSymbolDisplay}, 交易哈希: ${tx.hash}`);
100
+ return true;
101
+ }
102
+ else {
103
+ this.approvedTokens.set(tokenAddress, true);
104
+ return true;
105
+ }
106
+ });
107
+ }
108
+ isNonceRelatedError(error) {
109
+ if (!error)
110
+ return false;
111
+ const errorMessage = error.message ? error.message.toLowerCase() : '';
112
+ const nonceErrorKeywords = [
113
+ 'nonce',
114
+ 'nonce too low',
115
+ 'nonce too high',
116
+ 'nonce has already been used',
117
+ 'already known',
118
+ 'replacement transaction underpriced',
119
+ 'transaction with same nonce',
120
+ 'transaction nonce is too low',
121
+ 'invalid transaction nonce',
122
+ 'insufficient funds',
123
+ 'sign bundle to get failure details',
124
+ ];
125
+ return nonceErrorKeywords.some(keyword => errorMessage.includes(keyword));
126
+ }
127
+ isNativeCurrency(symbol) {
128
+ return symbol.toUpperCase() === this.chainConfig.nativeCurrency;
129
+ }
130
+ getWrappedNativeAddress() {
131
+ return this.chainConfig.wrappedNativeCurrencyAddress;
132
+ }
133
+ buildTipTransferTx(to, transfer_amount_gwei, gas_price_gwei, transfer_nonce) {
134
+ return __awaiter(this, void 0, void 0, function* () {
135
+ let real_transfer_amount_gwei = Math.min(Number(transfer_amount_gwei), this.chainConfig.gasOptions.maxTipAmountGwei).toString();
136
+ let real_gas_price_gwei = Math.min(Number(gas_price_gwei), this.chainConfig.gasOptions.maxGasPriceGwei).toString();
137
+ let tx_data = {
138
+ from: this.wallet.address,
139
+ to,
140
+ value: ethers_1.ethers.utils.parseUnits(real_transfer_amount_gwei, 'gwei'),
141
+ gasLimit: 21000,
142
+ gasPrice: ethers_1.ethers.utils.parseUnits(real_gas_price_gwei, 'gwei'),
143
+ nonce: transfer_nonce,
144
+ chainId: this.chainConfig.chainId
145
+ };
146
+ let signedTx = yield this.wallet.signTransaction(tx_data);
147
+ (0, dist_1.log_info)(`构建转账交易: `, Object.assign(Object.assign({}, tx_data), { real_transfer_amount_gwei,
148
+ real_gas_price_gwei, txhash: ethers_1.ethers.utils.keccak256(signedTx) }));
149
+ return signedTx;
150
+ });
151
+ }
152
+ }
153
+ exports.AbstractEvmDexTrade = AbstractEvmDexTrade;
@@ -0,0 +1,30 @@
1
+ import { AbastrcatTrade, AppConfig, TradeContext } from "@clonegod/ttd-core/dist";
2
+ import { ethers } from "ethers";
3
+ import { DexConfig, EvmChainConfig } from "../types";
4
+ export declare abstract class AbstractEvmDexTradePlus extends AbastrcatTrade {
5
+ protected appConfig: AppConfig;
6
+ protected group_wallets: ethers.Wallet[];
7
+ protected provider: ethers.providers.JsonRpcProvider;
8
+ protected approvedTokens: Map<string, Map<string, boolean>>;
9
+ protected pairContracts: Map<string, ethers.Contract>;
10
+ protected tokenContracts: Map<string, ethers.Contract>;
11
+ protected routerContract: ethers.Contract;
12
+ protected chainConfig: EvmChainConfig;
13
+ protected dexConfig: DexConfig;
14
+ constructor(appConfig: AppConfig);
15
+ protected abstract initConfigs(): void;
16
+ init(): Promise<void>;
17
+ protected getTokenContract(tokenAddress: string): ethers.Contract;
18
+ protected getTokenContractWithWallet(tokenAddress: string, wallet: ethers.Wallet): ethers.Contract;
19
+ protected getGasPriceGwei(context: TradeContext): string;
20
+ protected getBuilderTipAmoutGwei(context: TradeContext): string;
21
+ preApproveAllWallets(token_list?: string[]): Promise<void>;
22
+ private approveTokenIfNeeded;
23
+ protected checkTradeTokenApprove(context: TradeContext, wallet: ethers.Wallet, routerAddress?: string): Promise<void>;
24
+ protected checkTokenApprove(tokenAddress: string, tokenSymbol: string, tokenContract: ethers.Contract, spenderAddress: string, wallet: ethers.Wallet): Promise<boolean>;
25
+ protected isNonceRelatedError(error: any): boolean;
26
+ protected isNativeCurrency(symbol: string): boolean;
27
+ protected getWrappedNativeAddress(): string;
28
+ abstract execute(context: TradeContext, retryCount?: number): Promise<string>;
29
+ protected buildTipTransferTx(to: string, transfer_amount_gwei: string, gas_price_gwei: string, transfer_nonce: number, wallet: ethers.Wallet): Promise<string>;
30
+ }
@@ -0,0 +1,227 @@
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.AbstractEvmDexTradePlus = void 0;
13
+ const dist_1 = require("@clonegod/ttd-core/dist");
14
+ const ethers_1 = require("ethers");
15
+ const common_1 = require("../common");
16
+ class AbstractEvmDexTradePlus extends dist_1.AbastrcatTrade {
17
+ constructor(appConfig) {
18
+ super();
19
+ this.appConfig = appConfig;
20
+ this.approvedTokens = new Map();
21
+ this.pairContracts = new Map();
22
+ this.tokenContracts = new Map();
23
+ this.approvedTokens = new Map();
24
+ this.pairContracts = new Map();
25
+ this.tokenContracts = new Map();
26
+ this.initConfigs();
27
+ }
28
+ init() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ var _a;
31
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider(this.chainConfig.rpcEndpoint);
32
+ if (!process.env.WALLET_GROUP_IDS) {
33
+ throw new Error('必须配置 WALLET_GROUP_IDS 环境变量来启用多钱包功能');
34
+ }
35
+ let wallet_infos = (0, dist_1.load_wallet_multi)(((_a = process.env.WALLET_GROUP_IDS) === null || _a === void 0 ? void 0 : _a.split(',')) || [], false);
36
+ this.group_wallets = wallet_infos.map(info => new ethers_1.ethers.Wallet(info.private_key, this.provider));
37
+ (0, dist_1.log_info)(`组钱包已初始化,数量: ${this.group_wallets.length}`, this.group_wallets.map(e => e.address));
38
+ this.routerContract = new ethers_1.ethers.Contract(this.dexConfig.routerAddress, this.dexConfig.routerAbi, this.provider);
39
+ (0, dist_1.log_info)(`${this.dexConfig.dexName} Router已初始化, 地址: ${this.dexConfig.routerAddress}, 钱包数量: ${this.group_wallets.length}`);
40
+ });
41
+ }
42
+ getTokenContract(tokenAddress) {
43
+ if (!this.tokenContracts.has(tokenAddress)) {
44
+ const tokenContract = new ethers_1.ethers.Contract(tokenAddress, common_1.ERC20_ABI, this.provider);
45
+ this.tokenContracts.set(tokenAddress, tokenContract);
46
+ }
47
+ return this.tokenContracts.get(tokenAddress);
48
+ }
49
+ getTokenContractWithWallet(tokenAddress, wallet) {
50
+ const tokenContract = this.getTokenContract(tokenAddress);
51
+ return tokenContract.connect(wallet);
52
+ }
53
+ getGasPriceGwei(context) {
54
+ let { evm_gas_limit, evm_gas_price_gwei, evm_tip_amount_gwei } = context.trade_runtime.settings.strategy;
55
+ if (evm_gas_price_gwei === undefined || evm_gas_price_gwei === null || evm_gas_price_gwei <= 0) {
56
+ evm_gas_price_gwei = this.chainConfig.gasOptions.defaultGasPriceGwei;
57
+ }
58
+ (0, dist_1.log_info)(`getGasPriceGwei: ${evm_gas_price_gwei} Gwei`);
59
+ return evm_gas_price_gwei.toString();
60
+ }
61
+ getBuilderTipAmoutGwei(context) {
62
+ let { evm_gas_limit, evm_gas_price_gwei, evm_tip_amount_gwei } = context.trade_runtime.settings.strategy;
63
+ if (evm_tip_amount_gwei === undefined || evm_tip_amount_gwei === null || evm_tip_amount_gwei <= 0) {
64
+ evm_tip_amount_gwei = this.chainConfig.gasOptions.defaultTipAmountGwei;
65
+ }
66
+ (0, dist_1.log_info)(`getGasTipAmoutGwei: ${evm_tip_amount_gwei} Gwei`);
67
+ return evm_tip_amount_gwei.toString();
68
+ }
69
+ preApproveAllWallets() {
70
+ return __awaiter(this, arguments, void 0, function* (token_list = []) {
71
+ if (!this.group_wallets || this.group_wallets.length === 0) {
72
+ (0, dist_1.log_info)('No wallets available for pre-approval');
73
+ return;
74
+ }
75
+ if (!token_list || token_list.length === 0) {
76
+ token_list = Array.from(this.tokenContracts.keys());
77
+ }
78
+ (0, dist_1.log_info)(`Pre-approve: ${this.group_wallets.length} wallets, ${token_list.length} tokens`, token_list);
79
+ const tokenAddresses = new Set();
80
+ for (const token_address of token_list) {
81
+ tokenAddresses.add(token_address.toLowerCase());
82
+ }
83
+ const tokens = Array.from(tokenAddresses);
84
+ for (const wallet of this.group_wallets) {
85
+ try {
86
+ (0, dist_1.log_info)(`Pre-approve: wallet ${wallet.address}`);
87
+ for (const tokenAddress of tokens) {
88
+ try {
89
+ yield this.approveTokenIfNeeded(wallet, tokenAddress);
90
+ }
91
+ catch (error) {
92
+ console.warn(`Failed to pre-approve token ${tokenAddress} for wallet ${wallet.address}:`, error);
93
+ }
94
+ }
95
+ (0, dist_1.log_info)(`Pre-approve: wallet ${wallet.address} completed`);
96
+ }
97
+ catch (error) {
98
+ console.warn(`Failed to pre-approve tokens for wallet ${wallet.address}:`, error);
99
+ }
100
+ }
101
+ (0, dist_1.log_info)('Pre-approve: all wallets completed');
102
+ });
103
+ }
104
+ approveTokenIfNeeded(wallet, tokenAddress) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ try {
107
+ const tokenContract = this.getTokenContract(tokenAddress);
108
+ const routerAddress = this.dexConfig.routerAddress;
109
+ const currentAllowance = yield tokenContract.allowance(wallet.address, routerAddress);
110
+ if (currentAllowance.gt(0)) {
111
+ (0, dist_1.log_info)(`Pre-approve: token ${tokenAddress} already approved`);
112
+ return;
113
+ }
114
+ const maxAllowance = ethers_1.ethers.constants.MaxUint256;
115
+ const approveTx = yield tokenContract.connect(wallet).approve(routerAddress, maxAllowance);
116
+ (0, dist_1.log_info)(`Pre-approve: token ${tokenAddress} approve success, tx: ${approveTx.hash}`);
117
+ yield approveTx.wait();
118
+ (0, dist_1.log_info)(`Pre-approve: token ${tokenAddress} approved successfully`);
119
+ }
120
+ catch (error) {
121
+ console.warn(`Failed to approve token ${tokenAddress} for wallet ${wallet.address}:`, error);
122
+ throw error;
123
+ }
124
+ });
125
+ }
126
+ checkTradeTokenApprove(context, wallet, routerAddress) {
127
+ return __awaiter(this, void 0, void 0, function* () {
128
+ const { pool_info } = context;
129
+ const { tokenA, tokenB } = pool_info;
130
+ const router = routerAddress || this.dexConfig.routerAddress;
131
+ if (!this.approvedTokens.has(wallet.address)) {
132
+ this.approvedTokens.set(wallet.address, new Map());
133
+ }
134
+ yield Promise.all([tokenA, tokenB].map((_a) => __awaiter(this, [_a], void 0, function* ({ symbol, address }) {
135
+ const tokenContract = this.getTokenContractWithWallet(address, wallet);
136
+ yield this.checkTokenApprove(address, symbol, tokenContract, router, wallet);
137
+ })));
138
+ });
139
+ }
140
+ checkTokenApprove(tokenAddress, tokenSymbol, tokenContract, spenderAddress, wallet) {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ if (!this.approvedTokens.has(wallet.address)) {
143
+ this.approvedTokens.set(wallet.address, new Map());
144
+ }
145
+ if (this.approvedTokens.get(wallet.address).get(tokenAddress)) {
146
+ (0, dist_1.log_info)(`钱包 ${wallet.address} 的代币 ${tokenSymbol} 已授权,跳过授权检查`);
147
+ return true;
148
+ }
149
+ const allowance = yield tokenContract.allowance(wallet.address, spenderAddress);
150
+ const maxAllowance = ethers_1.ethers.constants.MaxUint256.div(2);
151
+ (0, dist_1.log_info)(`代币授权检查:`, {
152
+ walletAddress: wallet.address,
153
+ tokenSymbol,
154
+ currentAllowance: allowance.toString(),
155
+ maxAllowance: maxAllowance.toString(),
156
+ isSufficient: allowance.gte(maxAllowance)
157
+ });
158
+ if (allowance.lt(maxAllowance)) {
159
+ const maxApprovalAmount = ethers_1.ethers.utils.formatUnits(ethers_1.ethers.constants.MaxUint256, yield tokenContract.decimals());
160
+ const tokenSymbolDisplay = tokenSymbol || `${tokenAddress.substring(0, 6)}...`;
161
+ (0, dist_1.log_info)(`正在为钱包 ${wallet.address} 授权代币 ${tokenSymbolDisplay}`, {
162
+ walletAddress: wallet.address,
163
+ tokenAddress,
164
+ tokenSymbol: tokenSymbolDisplay,
165
+ currentAllowance: ethers_1.ethers.utils.formatUnits(allowance, yield tokenContract.decimals()),
166
+ newAllowance: maxApprovalAmount
167
+ });
168
+ const tx = yield tokenContract.approve(spenderAddress, ethers_1.ethers.constants.MaxUint256, {
169
+ gasLimit: this.chainConfig.gasOptions.gasLimit
170
+ });
171
+ yield tx.wait();
172
+ this.approvedTokens.get(wallet.address).set(tokenAddress, true);
173
+ (0, dist_1.log_info)(`钱包 ${wallet.address} 的代币 ${tokenSymbolDisplay} 授权完成,数量: ${maxApprovalAmount} ${tokenSymbolDisplay}, 交易哈希: ${tx.hash}`);
174
+ return true;
175
+ }
176
+ else {
177
+ this.approvedTokens.get(wallet.address).set(tokenAddress, true);
178
+ return true;
179
+ }
180
+ });
181
+ }
182
+ isNonceRelatedError(error) {
183
+ if (!error)
184
+ return false;
185
+ const errorMessage = error.message ? error.message.toLowerCase() : '';
186
+ const nonceErrorKeywords = [
187
+ 'nonce',
188
+ 'nonce too low',
189
+ 'nonce too high',
190
+ 'nonce has already been used',
191
+ 'already known',
192
+ 'replacement transaction underpriced',
193
+ 'transaction with same nonce',
194
+ 'transaction nonce is too low',
195
+ 'invalid transaction nonce',
196
+ 'insufficient funds',
197
+ 'sign bundle to get failure details',
198
+ ];
199
+ return nonceErrorKeywords.some(keyword => errorMessage.includes(keyword));
200
+ }
201
+ isNativeCurrency(symbol) {
202
+ return symbol.toUpperCase() === this.chainConfig.nativeCurrency;
203
+ }
204
+ getWrappedNativeAddress() {
205
+ return this.chainConfig.wrappedNativeCurrencyAddress;
206
+ }
207
+ buildTipTransferTx(to, transfer_amount_gwei, gas_price_gwei, transfer_nonce, wallet) {
208
+ return __awaiter(this, void 0, void 0, function* () {
209
+ let real_transfer_amount_gwei = Math.min(Number(transfer_amount_gwei), this.chainConfig.gasOptions.maxTipAmountGwei).toString();
210
+ let real_gas_price_gwei = Math.min(Number(gas_price_gwei), this.chainConfig.gasOptions.maxGasPriceGwei).toString();
211
+ let tx_data = {
212
+ from: wallet.address,
213
+ to,
214
+ value: ethers_1.ethers.utils.parseUnits(real_transfer_amount_gwei, 'gwei'),
215
+ gasLimit: 21000,
216
+ gasPrice: ethers_1.ethers.utils.parseUnits(real_gas_price_gwei, 'gwei'),
217
+ nonce: transfer_nonce,
218
+ chainId: this.chainConfig.chainId
219
+ };
220
+ let signedTx = yield wallet.signTransaction(tx_data);
221
+ (0, dist_1.log_info)(`构建转账交易: `, Object.assign(Object.assign({}, tx_data), { real_transfer_amount_gwei,
222
+ real_gas_price_gwei, txhash: ethers_1.ethers.utils.keccak256(signedTx) }));
223
+ return signedTx;
224
+ });
225
+ }
226
+ }
227
+ exports.AbstractEvmDexTradePlus = AbstractEvmDexTradePlus;
@@ -0,0 +1 @@
1
+ export * from "./tx_websocket_manager";
@@ -0,0 +1,17 @@
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("./tx_websocket_manager"), exports);
@@ -0,0 +1,23 @@
1
+ import { ethers } from 'ethers';
2
+ export declare class WebSocketManager {
3
+ private static instance;
4
+ private wsProvider;
5
+ private wsConnections;
6
+ private reconnectAttempts;
7
+ private readonly MAX_RECONNECT_ATTEMPTS;
8
+ private readonly RECONNECT_DELAY;
9
+ private reconnecting;
10
+ private eventListeners;
11
+ private constructor();
12
+ static getInstance(): WebSocketManager;
13
+ getWsProvider(wsEndpoint: string, network: {
14
+ name: string;
15
+ chainId: number;
16
+ }): ethers.providers.WebSocketProvider | null;
17
+ private handleWsError;
18
+ private handleWsDisconnection;
19
+ incrementConnections(): void;
20
+ decrementConnections(): void;
21
+ listenToEvent(eventName: string, handler: Function): void;
22
+ removeEventListener(eventName: string, handler: Function): void;
23
+ }
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebSocketManager = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const dist_1 = require("@clonegod/ttd-core/dist");
6
+ class WebSocketManager {
7
+ constructor() {
8
+ this.wsProvider = null;
9
+ this.wsConnections = 0;
10
+ this.reconnectAttempts = 0;
11
+ this.MAX_RECONNECT_ATTEMPTS = 5;
12
+ this.RECONNECT_DELAY = 2000;
13
+ this.reconnecting = false;
14
+ this.eventListeners = new Map();
15
+ }
16
+ static getInstance() {
17
+ if (!WebSocketManager.instance) {
18
+ WebSocketManager.instance = new WebSocketManager();
19
+ }
20
+ return WebSocketManager.instance;
21
+ }
22
+ getWsProvider(wsEndpoint, network) {
23
+ if (!wsEndpoint) {
24
+ (0, dist_1.log_warn)('WebSocket endpoint not configured');
25
+ return null;
26
+ }
27
+ if (!this.wsProvider && !this.reconnecting) {
28
+ (0, dist_1.log_info)('Creating new WebSocket provider');
29
+ try {
30
+ this.wsProvider = new ethers_1.ethers.providers.WebSocketProvider(wsEndpoint, network);
31
+ this.wsProvider.on('error', (error) => {
32
+ this.handleWsError(error, wsEndpoint, network);
33
+ });
34
+ this.wsProvider._websocket.on('close', () => {
35
+ (0, dist_1.log_warn)('WebSocket connection closed');
36
+ this.handleWsDisconnection(wsEndpoint, network);
37
+ });
38
+ this.reconnectAttempts = 0;
39
+ }
40
+ catch (error) {
41
+ (0, dist_1.log_error)('Failed to create WebSocket provider', error);
42
+ this.wsProvider = null;
43
+ }
44
+ }
45
+ return this.wsProvider;
46
+ }
47
+ handleWsError(error, wsEndpoint, network) {
48
+ (0, dist_1.log_error)('WebSocket provider error', error);
49
+ this.handleWsDisconnection(wsEndpoint, network);
50
+ }
51
+ handleWsDisconnection(wsEndpoint, network) {
52
+ if (this.reconnecting)
53
+ return;
54
+ if (this.wsProvider) {
55
+ this.wsProvider.removeAllListeners();
56
+ this.wsProvider = null;
57
+ }
58
+ if (this.wsConnections > 0 &&
59
+ this.reconnectAttempts < this.MAX_RECONNECT_ATTEMPTS) {
60
+ this.reconnecting = true;
61
+ this.reconnectAttempts++;
62
+ (0, dist_1.log_info)(`Attempting to reconnect WebSocket (${this.reconnectAttempts}/${this.MAX_RECONNECT_ATTEMPTS})`);
63
+ setTimeout(() => {
64
+ this.reconnecting = false;
65
+ this.getWsProvider(wsEndpoint, network);
66
+ }, this.RECONNECT_DELAY * this.reconnectAttempts);
67
+ }
68
+ else if (this.reconnectAttempts >= this.MAX_RECONNECT_ATTEMPTS) {
69
+ (0, dist_1.log_error)(`Maximum WebSocket reconnection attempts (${this.MAX_RECONNECT_ATTEMPTS}) reached`, new Error('Max reconnect attempts'));
70
+ this.wsConnections = 0;
71
+ this.reconnectAttempts = 0;
72
+ this.reconnecting = false;
73
+ }
74
+ }
75
+ incrementConnections() {
76
+ this.wsConnections++;
77
+ (0, dist_1.log_info)(`WebSocket active connections: ${this.wsConnections}`);
78
+ }
79
+ decrementConnections() {
80
+ if (this.wsConnections > 0) {
81
+ this.wsConnections--;
82
+ }
83
+ (0, dist_1.log_info)(`WebSocket active connections: ${this.wsConnections}`);
84
+ if (this.wsConnections === 0 && this.wsProvider) {
85
+ (0, dist_1.log_info)('No active connections, closing WebSocket');
86
+ this.wsProvider.removeAllListeners();
87
+ this.wsProvider = null;
88
+ this.reconnectAttempts = 0;
89
+ }
90
+ }
91
+ listenToEvent(eventName, handler) {
92
+ var _a;
93
+ if (!this.wsProvider) {
94
+ (0, dist_1.log_warn)('WebSocket provider not available for event listener');
95
+ return;
96
+ }
97
+ if (!this.eventListeners.has(eventName)) {
98
+ this.eventListeners.set(eventName, new Set());
99
+ }
100
+ (_a = this.eventListeners.get(eventName)) === null || _a === void 0 ? void 0 : _a.add(handler);
101
+ this.wsProvider.once(eventName, handler);
102
+ this.incrementConnections();
103
+ }
104
+ removeEventListener(eventName, handler) {
105
+ if (!this.wsProvider)
106
+ return;
107
+ this.wsProvider.removeListener(eventName, handler);
108
+ const handlers = this.eventListeners.get(eventName);
109
+ if (handlers) {
110
+ handlers.delete(handler);
111
+ if (handlers.size === 0) {
112
+ this.eventListeners.delete(eventName);
113
+ }
114
+ }
115
+ this.decrementConnections();
116
+ }
117
+ }
118
+ exports.WebSocketManager = WebSocketManager;
119
+ WebSocketManager.instance = null;
@@ -0,0 +1,5 @@
1
+ export * from './send';
2
+ export * from './parse';
3
+ export * from './check';
4
+ export * from './abstract_dex_trade';
5
+ export * from './abstract_dex_trade_plus';
@@ -0,0 +1,21 @@
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("./send"), exports);
18
+ __exportStar(require("./parse"), exports);
19
+ __exportStar(require("./check"), exports);
20
+ __exportStar(require("./abstract_dex_trade"), exports);
21
+ __exportStar(require("./abstract_dex_trade_plus"), exports);
@@ -0,0 +1,8 @@
1
+ import { StandardPoolInfoType, StandardSwapDetailType } from '@clonegod/ttd-core';
2
+ import { TransactionReceipt } from '@ethersproject/providers';
3
+ export interface ITxParser {
4
+ parseTransaction(txReceipt: TransactionReceipt, poolInfo: StandardPoolInfoType): Promise<StandardSwapDetailType>;
5
+ }
6
+ export interface ITxParserFactory {
7
+ createParser(version: string): ITxParser;
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,19 @@
1
+ import { StandardPoolInfoType, StandardSwapDetailType } from "@clonegod/ttd-core";
2
+ import { TransactionReceipt } from '@ethersproject/providers';
3
+ import { ethers } from 'ethers';
4
+ import { ITxParser } from "./abstract_parser";
5
+ import { ParserConfig } from "../../types";
6
+ export declare abstract class BaseTxParser implements ITxParser {
7
+ protected config: ParserConfig;
8
+ protected provider: ethers.providers.JsonRpcProvider;
9
+ protected blockTimeCache: Map<number, number>;
10
+ protected readonly CACHE_EXPIRY: number;
11
+ constructor(config: ParserConfig);
12
+ abstract parseTransaction(txReceipt: TransactionReceipt, poolInfo: StandardPoolInfoType): Promise<StandardSwapDetailType>;
13
+ protected getBlockTime(blockNumber: number): Promise<number>;
14
+ protected calculateGasFee(txReceipt: TransactionReceipt): {
15
+ base_fee: number;
16
+ priority_fee: number;
17
+ total_fee: number;
18
+ };
19
+ }