@clonegod/ttd-base-common 1.0.8 → 1.0.9

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.
@@ -4,7 +4,9 @@ import { DexConfig, EvmChainConfig } from "../types";
4
4
  export declare abstract class AbstractEvmDexTradePlus extends AbastrcatTrade {
5
5
  protected appConfig: AppConfig;
6
6
  protected group_wallets: ethers.Wallet[];
7
+ protected wallet: ethers.Wallet;
7
8
  protected provider: ethers.providers.JsonRpcProvider;
9
+ protected walletMode: 'single' | 'multi';
8
10
  protected approvedTokens: Map<string, Map<string, boolean>>;
9
11
  protected pairContracts: Map<string, ethers.Contract>;
10
12
  protected tokenContracts: Map<string, ethers.Contract>;
@@ -14,6 +16,9 @@ export declare abstract class AbstractEvmDexTradePlus extends AbastrcatTrade {
14
16
  constructor(appConfig: AppConfig);
15
17
  protected abstract initConfigs(): void;
16
18
  init(): Promise<void>;
19
+ protected getWalletMode(): 'single' | 'multi';
20
+ protected getSingleWallet(): ethers.Wallet;
21
+ protected getGroupWallets(): ethers.Wallet[];
17
22
  protected getTokenContract(tokenAddress: string): ethers.Contract;
18
23
  protected getTokenContractWithWallet(tokenAddress: string, wallet: ethers.Wallet): ethers.Contract;
19
24
  protected getGasPriceGwei(context: TradeContext): string;
@@ -17,6 +17,7 @@ class AbstractEvmDexTradePlus extends dist_1.AbastrcatTrade {
17
17
  constructor(appConfig) {
18
18
  super();
19
19
  this.appConfig = appConfig;
20
+ this.walletMode = 'multi';
20
21
  this.approvedTokens = new Map();
21
22
  this.pairContracts = new Map();
22
23
  this.tokenContracts = new Map();
@@ -27,18 +28,37 @@ class AbstractEvmDexTradePlus extends dist_1.AbastrcatTrade {
27
28
  }
28
29
  init() {
29
30
  return __awaiter(this, void 0, void 0, function* () {
30
- var _a;
31
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 环境变量来启用多钱包功能');
32
+ const walletGroupIds = process.env.WALLET_GROUP_IDS || '';
33
+ this.walletMode = walletGroupIds && walletGroupIds.trim().split(',').length > 0 ? 'multi' : 'single';
34
+ if (this.walletMode === 'multi') {
35
+ let wallet_infos = (0, dist_1.load_wallet_multi)(walletGroupIds.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
+ }
39
+ else {
40
+ this.wallet = new ethers_1.ethers.Wallet(this.appConfig.trade_runtime.wallet.private_key, this.provider);
41
+ (0, dist_1.log_info)(`单钱包已初始化,walletAddress= ${this.wallet.address}`);
34
42
  }
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
43
  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}`);
44
+ (0, dist_1.log_info)(`${this.dexConfig.dexName} Router已初始化, routerAddress= ${this.dexConfig.routerAddress}`);
40
45
  });
41
46
  }
47
+ getWalletMode() {
48
+ return this.walletMode;
49
+ }
50
+ getSingleWallet() {
51
+ if (this.walletMode !== 'single' || !this.wallet) {
52
+ throw new Error('Single wallet not available in multi-wallet mode or not initialized');
53
+ }
54
+ return this.wallet;
55
+ }
56
+ getGroupWallets() {
57
+ if (this.walletMode !== 'multi' || !this.group_wallets) {
58
+ throw new Error('Group wallets not available in single-wallet mode or not initialized');
59
+ }
60
+ return this.group_wallets;
61
+ }
42
62
  getTokenContract(tokenAddress) {
43
63
  if (!this.tokenContracts.has(tokenAddress)) {
44
64
  const tokenContract = new ethers_1.ethers.Contract(tokenAddress, common_1.ERC20_ABI, this.provider);
@@ -68,37 +88,70 @@ class AbstractEvmDexTradePlus extends dist_1.AbastrcatTrade {
68
88
  }
69
89
  preApproveAllWallets() {
70
90
  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) {
91
+ if (this.walletMode === 'single') {
92
+ if (!this.wallet) {
93
+ (0, dist_1.log_info)('Single wallet not available for pre-approval');
94
+ return;
95
+ }
96
+ if (!token_list || token_list.length === 0) {
97
+ token_list = Array.from(this.tokenContracts.keys());
98
+ }
99
+ (0, dist_1.log_info)(`Pre-approve: single wallet ${this.wallet.address}, ${token_list.length} tokens`, token_list);
100
+ const tokenAddresses = new Set();
101
+ for (const token_address of token_list) {
102
+ tokenAddresses.add(token_address.toLowerCase());
103
+ }
104
+ const tokens = Array.from(tokenAddresses);
85
105
  try {
86
- (0, dist_1.log_info)(`Pre-approve: wallet ${wallet.address}`);
106
+ (0, dist_1.log_info)(`Pre-approve: wallet ${this.wallet.address}`);
87
107
  for (const tokenAddress of tokens) {
88
108
  try {
89
- yield this.approveTokenIfNeeded(wallet, tokenAddress);
109
+ yield this.approveTokenIfNeeded(this.wallet, tokenAddress);
90
110
  }
91
111
  catch (error) {
92
- console.warn(`Failed to pre-approve token ${tokenAddress} for wallet ${wallet.address}:`, error);
112
+ console.warn(`Failed to pre-approve token ${tokenAddress} for wallet ${this.wallet.address}:`, error);
93
113
  }
94
114
  }
95
- (0, dist_1.log_info)(`Pre-approve: wallet ${wallet.address} completed`);
115
+ (0, dist_1.log_info)(`Pre-approve: wallet ${this.wallet.address} completed`);
96
116
  }
97
117
  catch (error) {
98
- console.warn(`Failed to pre-approve tokens for wallet ${wallet.address}:`, error);
118
+ console.warn(`Failed to pre-approve tokens for wallet ${this.wallet.address}:`, error);
119
+ }
120
+ (0, dist_1.log_info)('Pre-approve: single wallet completed');
121
+ }
122
+ else {
123
+ if (!this.group_wallets || this.group_wallets.length === 0) {
124
+ (0, dist_1.log_info)('No wallets available for pre-approval');
125
+ return;
126
+ }
127
+ if (!token_list || token_list.length === 0) {
128
+ token_list = Array.from(this.tokenContracts.keys());
129
+ }
130
+ (0, dist_1.log_info)(`Pre-approve: ${this.group_wallets.length} wallets, ${token_list.length} tokens`, token_list);
131
+ const tokenAddresses = new Set();
132
+ for (const token_address of token_list) {
133
+ tokenAddresses.add(token_address.toLowerCase());
134
+ }
135
+ const tokens = Array.from(tokenAddresses);
136
+ for (const wallet of this.group_wallets) {
137
+ try {
138
+ (0, dist_1.log_info)(`Pre-approve: wallet ${wallet.address}`);
139
+ for (const tokenAddress of tokens) {
140
+ try {
141
+ yield this.approveTokenIfNeeded(wallet, tokenAddress);
142
+ }
143
+ catch (error) {
144
+ console.warn(`Failed to pre-approve token ${tokenAddress} for wallet ${wallet.address}:`, error);
145
+ }
146
+ }
147
+ (0, dist_1.log_info)(`Pre-approve: wallet ${wallet.address} completed`);
148
+ }
149
+ catch (error) {
150
+ console.warn(`Failed to pre-approve tokens for wallet ${wallet.address}:`, error);
151
+ }
99
152
  }
153
+ (0, dist_1.log_info)('Pre-approve: all wallets completed');
100
154
  }
101
- (0, dist_1.log_info)('Pre-approve: all wallets completed');
102
155
  });
103
156
  }
104
157
  approveTokenIfNeeded(wallet, tokenAddress) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-base-common",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Base common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",