@clonegod/ttd-bsc-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 +31 -0
  2. package/dist/common/abi.js +68 -0
  3. package/dist/common/constants.d.ts +21 -0
  4. package/dist/common/constants.js +37 -0
  5. package/dist/common/index.d.ts +2 -0
  6. package/dist/common/index.js +18 -0
  7. package/dist/config/bsc_env_args.d.ts +11 -0
  8. package/dist/config/bsc_env_args.js +19 -0
  9. package/dist/config/env_args.d.ts +13 -0
  10. package/dist/config/env_args.js +23 -0
  11. package/dist/config/index.d.ts +1 -0
  12. package/dist/config/index.js +17 -0
  13. package/dist/constants/networks.d.ts +10 -0
  14. package/dist/constants/networks.js +28 -0
  15. package/dist/index.d.ts +6 -0
  16. package/dist/index.js +22 -0
  17. package/dist/providers/index.d.ts +9 -0
  18. package/dist/providers/index.js +21 -0
  19. package/dist/quote/event/index.d.ts +1 -0
  20. package/dist/quote/event/index.js +17 -0
  21. package/dist/quote/event/pool_event_listener.d.ts +39 -0
  22. package/dist/quote/event/pool_event_listener.js +231 -0
  23. package/dist/quote/event/verify_clmm_swap_event.d.ts +1 -0
  24. package/dist/quote/event/verify_clmm_swap_event.js +178 -0
  25. package/dist/quote/index.d.ts +2 -0
  26. package/dist/quote/index.js +18 -0
  27. package/dist/quote/pricing/index.d.ts +2 -0
  28. package/dist/quote/pricing/index.js +18 -0
  29. package/dist/quote/pricing/pool.d.ts +13 -0
  30. package/dist/quote/pricing/pool.js +21 -0
  31. package/dist/quote/pricing/token_price_cache.d.ts +9 -0
  32. package/dist/quote/pricing/token_price_cache.js +40 -0
  33. package/dist/trade/abstract_dex_trade.d.ts +27 -0
  34. package/dist/trade/abstract_dex_trade.js +152 -0
  35. package/dist/trade/check/index.d.ts +1 -0
  36. package/dist/trade/check/index.js +17 -0
  37. package/dist/trade/check/tx_websocket_manager.d.ts +23 -0
  38. package/dist/trade/check/tx_websocket_manager.js +119 -0
  39. package/dist/trade/index.d.ts +4 -0
  40. package/dist/trade/index.js +20 -0
  41. package/dist/trade/parse/abstract_parser.d.ts +8 -0
  42. package/dist/trade/parse/abstract_parser.js +2 -0
  43. package/dist/trade/parse/base_parser.d.ts +30 -0
  44. package/dist/trade/parse/base_parser.js +108 -0
  45. package/dist/trade/parse/index.d.ts +1 -0
  46. package/dist/trade/parse/index.js +5 -0
  47. package/dist/trade/send/48club.d.ts +18 -0
  48. package/dist/trade/send/48club.js +97 -0
  49. package/dist/trade/send/blockrazor.d.ts +7 -0
  50. package/dist/trade/send/blockrazor.js +79 -0
  51. package/dist/trade/send/bsc_rpc.d.ts +6 -0
  52. package/dist/trade/send/bsc_rpc.js +43 -0
  53. package/dist/trade/send/index.d.ts +5 -0
  54. package/dist/trade/send/index.js +23 -0
  55. package/dist/trade/send/send_tx.d.ts +8 -0
  56. package/dist/trade/send/send_tx.js +85 -0
  57. package/dist/types/config_types.d.ts +28 -0
  58. package/dist/types/config_types.js +2 -0
  59. package/dist/types/event_types.d.ts +30 -0
  60. package/dist/types/event_types.js +2 -0
  61. package/dist/types/index.d.ts +4 -0
  62. package/dist/types/index.js +20 -0
  63. package/dist/types/quote_types.d.ts +27 -0
  64. package/dist/types/quote_types.js +2 -0
  65. package/dist/types/trade_types.d.ts +38 -0
  66. package/dist/types/trade_types.js +8 -0
  67. package/dist/utils/gas_helper.d.ts +5 -0
  68. package/dist/utils/gas_helper.js +72 -0
  69. package/dist/utils/index.d.ts +7 -0
  70. package/dist/utils/index.js +43 -0
  71. package/dist/utils/redis_lock.d.ts +14 -0
  72. package/dist/utils/redis_lock.js +100 -0
  73. package/package.json +30 -0
@@ -0,0 +1,152 @@
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
+ ];
124
+ return nonceErrorKeywords.some(keyword => errorMessage.includes(keyword));
125
+ }
126
+ isNativeCurrency(symbol) {
127
+ return symbol.toUpperCase() === this.chainConfig.nativeCurrency;
128
+ }
129
+ getWrappedNativeAddress() {
130
+ return this.chainConfig.wrappedNativeCurrencyAddress;
131
+ }
132
+ buildTipTransferTx(to, transfer_amount_gwei, gas_price_gwei, transfer_nonce) {
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ let real_transfer_amount_gwei = Math.min(Number(transfer_amount_gwei), this.chainConfig.gasOptions.maxTipAmountGwei).toString();
135
+ let real_gas_price_gwei = Math.min(Number(gas_price_gwei), this.chainConfig.gasOptions.maxGasPriceGwei).toString();
136
+ let tx_data = {
137
+ from: this.wallet.address,
138
+ to,
139
+ value: ethers_1.ethers.utils.parseUnits(real_transfer_amount_gwei, 'gwei'),
140
+ gasLimit: 21000,
141
+ gasPrice: ethers_1.ethers.utils.parseUnits(real_gas_price_gwei, 'gwei'),
142
+ nonce: transfer_nonce,
143
+ chainId: this.chainConfig.chainId
144
+ };
145
+ let signedTx = yield this.wallet.signTransaction(tx_data);
146
+ (0, dist_1.log_info)(`构建转账交易: `, Object.assign(Object.assign({}, tx_data), { real_transfer_amount_gwei,
147
+ real_gas_price_gwei, txhash: ethers_1.ethers.utils.keccak256(signedTx) }));
148
+ return signedTx;
149
+ });
150
+ }
151
+ }
152
+ exports.AbstractEvmDexTrade = AbstractEvmDexTrade;
@@ -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,4 @@
1
+ export * from './send';
2
+ export * from './parse';
3
+ export * from './check';
4
+ export * from './abstract_dex_trade';
@@ -0,0 +1,20 @@
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);
@@ -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,30 @@
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
+ protected parseClmmSwapEvent(logs: any[], poolAddress: string): {
20
+ amount0: ethers.BigNumber;
21
+ amount1: ethers.BigNumber;
22
+ sqrtPriceX96: ethers.BigNumber;
23
+ liquidity: ethers.BigNumber;
24
+ tick: number;
25
+ protocolFeesToken0: ethers.BigNumber;
26
+ protocolFeesToken1: ethers.BigNumber;
27
+ sender?: string;
28
+ recipient?: string;
29
+ };
30
+ }
@@ -0,0 +1,108 @@
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.BaseTxParser = void 0;
13
+ const ethers_1 = require("ethers");
14
+ class BaseTxParser {
15
+ constructor(config) {
16
+ this.blockTimeCache = new Map();
17
+ this.CACHE_EXPIRY = 1000 * 60 * 60;
18
+ this.config = config;
19
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider(config.rpcEndpoint);
20
+ }
21
+ getBlockTime(blockNumber) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ if (process.env.NEED_BLOCK_TIME_INFO !== 'true') {
24
+ return 0;
25
+ }
26
+ try {
27
+ if (this.blockTimeCache.has(blockNumber)) {
28
+ return this.blockTimeCache.get(blockNumber);
29
+ }
30
+ const block = yield this.provider.getBlock(blockNumber);
31
+ const timestamp = block.timestamp * 1000;
32
+ this.blockTimeCache.set(blockNumber, timestamp);
33
+ setTimeout(() => {
34
+ this.blockTimeCache.delete(blockNumber);
35
+ }, this.CACHE_EXPIRY);
36
+ return timestamp;
37
+ }
38
+ catch (error) {
39
+ console.error('获取区块时间失败:', error);
40
+ return 0;
41
+ }
42
+ });
43
+ }
44
+ calculateGasFee(txReceipt) {
45
+ try {
46
+ const gasUsed = txReceipt.gasUsed || ethers_1.ethers.BigNumber.from(0);
47
+ const effectiveGasPrice = txReceipt.effectiveGasPrice || ethers_1.ethers.BigNumber.from(0);
48
+ const totalFeeBN = gasUsed.mul(effectiveGasPrice);
49
+ const totalFee = Number(ethers_1.ethers.utils.formatEther(totalFeeBN));
50
+ return {
51
+ base_fee: 0,
52
+ priority_fee: totalFee,
53
+ total_fee: totalFee
54
+ };
55
+ }
56
+ catch (error) {
57
+ console.error('计算 gas 费用失败:', error);
58
+ return {
59
+ base_fee: 0,
60
+ priority_fee: 0,
61
+ total_fee: 0
62
+ };
63
+ }
64
+ }
65
+ parseClmmSwapEvent(logs, poolAddress) {
66
+ const swapEventTopic = '0x19b47279256b2a23a1665c810c8d55a1758940ee09377d4f8d26497a3577dc83';
67
+ const swapLog = logs.find(log => log.address.toLowerCase() === poolAddress.toLowerCase() &&
68
+ log.topics[0].toLowerCase() === swapEventTopic.toLowerCase());
69
+ if (!swapLog) {
70
+ console.error('找不到Swap事件,所有日志:', logs.map(l => ({
71
+ address: l.address,
72
+ topics: l.topics
73
+ })));
74
+ throw new Error(`找不到池子 ${poolAddress} 的Swap事件`);
75
+ }
76
+ try {
77
+ const decodedData = ethers_1.ethers.utils.defaultAbiCoder.decode(['int256', 'int256', 'uint160', 'uint128', 'int24', 'uint128', 'uint128'], swapLog.data);
78
+ let sender = undefined;
79
+ let recipient = undefined;
80
+ if (swapLog.topics.length > 1) {
81
+ sender = ethers_1.ethers.utils.defaultAbiCoder.decode(['address'], ethers_1.ethers.utils.hexZeroPad(swapLog.topics[1], 32))[0];
82
+ }
83
+ if (swapLog.topics.length > 2) {
84
+ recipient = ethers_1.ethers.utils.defaultAbiCoder.decode(['address'], ethers_1.ethers.utils.hexZeroPad(swapLog.topics[2], 32))[0];
85
+ }
86
+ const [amount0, amount1, sqrtPriceX96, liquidity, tick, protocolFeesToken0, protocolFeesToken1] = decodedData;
87
+ return {
88
+ amount0,
89
+ amount1,
90
+ sqrtPriceX96,
91
+ liquidity,
92
+ tick,
93
+ protocolFeesToken0,
94
+ protocolFeesToken1,
95
+ sender,
96
+ recipient
97
+ };
98
+ }
99
+ catch (error) {
100
+ console.error('解析Swap事件失败:', error, {
101
+ logData: swapLog ? swapLog.data : 'No log',
102
+ logTopics: swapLog ? swapLog.topics : 'No log'
103
+ });
104
+ throw new Error(`解析Swap事件失败: ${error.message}`);
105
+ }
106
+ }
107
+ }
108
+ exports.BaseTxParser = BaseTxParser;
@@ -0,0 +1 @@
1
+ export { BaseTxParser } from './base_parser';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseTxParser = void 0;
4
+ var base_parser_1 = require("./base_parser");
5
+ Object.defineProperty(exports, "BaseTxParser", { enumerable: true, get: function () { return base_parser_1.BaseTxParser; } });
@@ -0,0 +1,18 @@
1
+ export declare const _48CLUB_BUILDER_CONTROL_EOA = "0x4848489f0b2BEdd788c696e2D79b6b69D7484848";
2
+ export interface BundleParams {
3
+ txs: string[];
4
+ maxBlockNumber?: number;
5
+ maxTimestamp?: number;
6
+ revertingTxHashes?: string[];
7
+ noMerge?: boolean;
8
+ backrunTarget?: string;
9
+ }
10
+ export declare class _48ClubTrade {
11
+ private rpcUrl;
12
+ private soulPointSignature?;
13
+ private provider;
14
+ constructor(soulPointSignature?: string);
15
+ sendPrivateTransaction(signedTx: string): Promise<string>;
16
+ _sendPrivateTransactionWith48SP(signedTx: string): Promise<string>;
17
+ sendBundle(params: BundleParams): Promise<string>;
18
+ }
@@ -0,0 +1,97 @@
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._48ClubTrade = exports._48CLUB_BUILDER_CONTROL_EOA = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const ethers_1 = require("ethers");
18
+ exports._48CLUB_BUILDER_CONTROL_EOA = '0x4848489f0b2BEdd788c696e2D79b6b69D7484848';
19
+ class _48ClubTrade {
20
+ constructor(soulPointSignature) {
21
+ this.rpcUrl = process.env._48CLUB_RPC_URL || 'https://puissant-builder.48.club/';
22
+ this.soulPointSignature = soulPointSignature;
23
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider(this.rpcUrl);
24
+ }
25
+ sendPrivateTransaction(signedTx) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ try {
28
+ const response = yield axios_1.default.post(this.rpcUrl, {
29
+ jsonrpc: "2.0",
30
+ id: "1",
31
+ method: "eth_sendRawTransaction",
32
+ params: [signedTx]
33
+ });
34
+ if (response.data.error) {
35
+ throw new Error(`RPC Error: ${response.data.error.message}`);
36
+ }
37
+ return response.data.result;
38
+ }
39
+ catch (error) {
40
+ if (error instanceof Error) {
41
+ throw new Error(`48club sendPrivateTransaction failed!!! ${error.message}`);
42
+ }
43
+ throw new Error('48club sendPrivateTransaction failed!!! Unknown error');
44
+ }
45
+ });
46
+ }
47
+ _sendPrivateTransactionWith48SP(signedTx) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ try {
50
+ if (!this.soulPointSignature) {
51
+ throw new Error('48 SoulPoint signature is required');
52
+ }
53
+ const response = yield axios_1.default.post(this.rpcUrl, {
54
+ jsonrpc: "2.0",
55
+ id: "1",
56
+ method: "eth_sendPrivateTransactionWith48SP",
57
+ params: [signedTx, this.soulPointSignature]
58
+ });
59
+ if (response.data.error) {
60
+ throw new Error(`RPC Error: ${response.data.error.message}`);
61
+ }
62
+ return response.data.result;
63
+ }
64
+ catch (error) {
65
+ if (error instanceof Error) {
66
+ throw new Error(`48club sendPrivateTransactionWith48SP failed!!! ${error.message}`);
67
+ }
68
+ throw new Error('48club sendPrivateTransactionWith48SP failed!!! Unknown error');
69
+ }
70
+ });
71
+ }
72
+ sendBundle(params) {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ try {
75
+ const currentTimestamp = Math.floor(Date.now() / 1000);
76
+ const requestParams = Object.assign({ txs: params.txs, maxBlockNumber: params.maxBlockNumber, maxTimestamp: params.maxTimestamp || currentTimestamp + 30, revertingTxHashes: params.revertingTxHashes, noMerge: params.noMerge, backrunTarget: params.backrunTarget }, (this.soulPointSignature ? { '48spSign': this.soulPointSignature } : {}));
77
+ const response = yield axios_1.default.post(this.rpcUrl, {
78
+ jsonrpc: "2.0",
79
+ id: "1",
80
+ method: "eth_sendBundle",
81
+ params: [requestParams]
82
+ });
83
+ if (response.data.error) {
84
+ throw new Error(`RPC Error: ${response.data.error.message}`);
85
+ }
86
+ return response.data.result;
87
+ }
88
+ catch (error) {
89
+ if (error instanceof Error) {
90
+ throw new Error(`48club sendBundle failed!!! ${error.message}`);
91
+ }
92
+ throw new Error('48club sendBundle failed!!! Unknown error');
93
+ }
94
+ });
95
+ }
96
+ }
97
+ exports._48ClubTrade = _48ClubTrade;
@@ -0,0 +1,7 @@
1
+ export declare class BlockRazorTrade {
2
+ private rpcUrl;
3
+ private authToken;
4
+ constructor();
5
+ sendPrivateTransaction(signedTx: string): Promise<string>;
6
+ sendBundle(transactions: string[], maxBlockNumber?: number, revertingTxHashes?: string[]): Promise<string>;
7
+ }