@clonegod/ttd-base-common 1.0.26 → 1.1.2
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/appconfig/BaseQuoteAppConfig.d.ts +10 -0
- package/dist/appconfig/BaseQuoteAppConfig.js +36 -0
- package/dist/appconfig/BaseTradeAppConfig.d.ts +7 -0
- package/dist/appconfig/BaseTradeAppConfig.js +13 -0
- package/dist/appconfig/base_dex_env_args.d.ts +5 -0
- package/dist/appconfig/base_dex_env_args.js +68 -0
- package/dist/appconfig/base_env_args.d.ts +82 -0
- package/dist/appconfig/base_env_args.js +91 -0
- package/dist/appconfig/ensure_core_env.d.ts +1 -0
- package/dist/appconfig/ensure_core_env.js +18 -0
- package/dist/appconfig/index.d.ts +5 -0
- package/dist/appconfig/index.js +21 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/quote/depth/amm_depth_calculator.d.ts +19 -0
- package/dist/quote/depth/amm_depth_calculator.js +55 -0
- package/dist/quote/depth/clmm_depth_calculator.d.ts +28 -0
- package/dist/quote/depth/clmm_depth_calculator.js +176 -0
- package/dist/quote/depth/index.d.ts +51 -0
- package/dist/quote/depth/index.js +264 -0
- package/dist/quote/depth/tick_liquidity_snapshot.d.ts +58 -0
- package/dist/quote/depth/tick_liquidity_snapshot.js +143 -0
- package/dist/quote/event/index.d.ts +1 -0
- package/dist/quote/event/index.js +1 -0
- package/dist/quote/event/pool_event_listener.d.ts +5 -3
- package/dist/quote/event/pool_event_listener.js +128 -150
- package/dist/quote/event/swap_debouncer.d.ts +22 -0
- package/dist/quote/event/swap_debouncer.js +80 -0
- package/dist/quote/get_base_token_price.d.ts +6 -0
- package/dist/quote/get_base_token_price.js +90 -0
- package/dist/quote/index.d.ts +7 -0
- package/dist/quote/index.js +7 -0
- package/dist/quote/preload_token_prices.d.ts +2 -0
- package/dist/quote/preload_token_prices.js +37 -0
- package/dist/quote/price_feed_handler.d.ts +15 -0
- package/dist/quote/price_feed_handler.js +56 -0
- package/dist/quote/pricing/fee_helpers.d.ts +13 -0
- package/dist/quote/pricing/fee_helpers.js +68 -0
- package/dist/quote/pricing/index.d.ts +3 -1
- package/dist/quote/pricing/index.js +3 -1
- package/dist/quote/pricing/pool_state_initializer.d.ts +12 -0
- package/dist/quote/pricing/pool_state_initializer.js +191 -0
- package/dist/quote/pricing/sdk_token_factory.d.ts +2 -0
- package/dist/quote/pricing/sdk_token_factory.js +21 -0
- package/dist/quote/quote_amount.d.ts +4 -0
- package/dist/quote/quote_amount.js +24 -0
- package/dist/quote/tick/cached_tick_data_provider.d.ts +12 -0
- package/dist/quote/tick/cached_tick_data_provider.js +45 -0
- package/dist/quote/tick/clmm_tick_cache.d.ts +42 -0
- package/dist/quote/tick/clmm_tick_cache.js +236 -0
- package/dist/quote/tick/index.d.ts +4 -0
- package/dist/{ws → quote/tick}/index.js +4 -2
- package/dist/quote/tick/state_view_tick_loader.d.ts +17 -0
- package/dist/quote/tick/state_view_tick_loader.js +136 -0
- package/dist/quote/tick/tick_lens_loaders.d.ts +24 -0
- package/dist/quote/tick/tick_lens_loaders.js +158 -0
- package/dist/quote/verify/index.d.ts +2 -0
- package/dist/quote/verify/index.js +5 -0
- package/dist/quote/verify/quote_price_verify.d.ts +30 -0
- package/dist/quote/verify/quote_price_verify.js +240 -0
- package/dist/redis/redis_client.d.ts +3 -2
- package/dist/redis/redis_client.js +86 -116
- package/dist/send-tx/constants.d.ts +2 -0
- package/dist/send-tx/constants.js +6 -0
- package/dist/send-tx/index.d.ts +2 -0
- package/dist/{config → send-tx}/index.js +2 -1
- package/dist/send-tx/types.d.ts +4 -0
- package/dist/send-tx/types.js +2 -0
- package/dist/trade/abstract_dex_trade.d.ts +43 -21
- package/dist/trade/abstract_dex_trade.js +347 -133
- package/dist/trade/caller_manager.d.ts +31 -0
- package/dist/trade/caller_manager.js +202 -0
- package/dist/trade/check/abstract_tx_result_checker.d.ts +28 -0
- package/dist/trade/check/abstract_tx_result_checker.js +192 -0
- package/dist/trade/check/index.d.ts +1 -1
- package/dist/trade/check/index.js +1 -1
- package/dist/trade/index.d.ts +2 -2
- package/dist/trade/index.js +2 -2
- package/dist/trade/parse/base_parser.d.ts +1 -2
- package/dist/trade/parse/base_parser.js +36 -36
- package/dist/trade/trade_trace.d.ts +17 -0
- package/dist/trade/trade_trace.js +65 -0
- package/dist/types/config_types.d.ts +3 -3
- package/dist/types/event_types.d.ts +3 -3
- package/dist/types/pool_state.d.ts +140 -13
- package/dist/utils/ethers_compat.d.ts +13 -0
- package/dist/utils/ethers_compat.js +18 -0
- package/dist/utils/fast_signer.d.ts +1 -0
- package/dist/utils/fast_signer.js +87 -0
- package/dist/utils/gas_helper.d.ts +2 -2
- package/dist/utils/gas_helper.js +48 -60
- package/dist/utils/index.d.ts +5 -2
- package/dist/utils/index.js +6 -2
- package/dist/utils/pool_filter.d.ts +8 -0
- package/dist/utils/pool_filter.js +38 -0
- package/dist/utils/trade_direction.d.ts +14 -0
- package/dist/utils/trade_direction.js +23 -0
- package/package.json +3 -3
- package/dist/config/base_env_args.d.ts +0 -11
- package/dist/config/base_env_args.js +0 -19
- package/dist/config/index.d.ts +0 -1
- package/dist/quote/event/verify_clmm_swap_event.d.ts +0 -1
- package/dist/quote/event/verify_clmm_swap_event.js +0 -178
- package/dist/quote/pricing/token_price_cache.d.ts +0 -10
- package/dist/quote/pricing/token_price_cache.js +0 -40
- package/dist/trade/abstract_dex_trade_plus.d.ts +0 -43
- package/dist/trade/abstract_dex_trade_plus.js +0 -421
- package/dist/trade/check/tx_websocket_manager.d.ts +0 -23
- package/dist/trade/check/tx_websocket_manager.js +0 -119
- package/dist/trade/send/alchemy_base.d.ts +0 -5
- package/dist/trade/send/alchemy_base.js +0 -48
- package/dist/trade/send/ankr_base.d.ts +0 -5
- package/dist/trade/send/ankr_base.js +0 -48
- package/dist/trade/send/base_rpc.d.ts +0 -5
- package/dist/trade/send/base_rpc.js +0 -48
- package/dist/trade/send/blockpi_base.d.ts +0 -5
- package/dist/trade/send/blockpi_base.js +0 -48
- package/dist/trade/send/bloxroute_base.d.ts +0 -11
- package/dist/trade/send/bloxroute_base.js +0 -115
- package/dist/trade/send/chainstack_base.d.ts +0 -5
- package/dist/trade/send/chainstack_base.js +0 -48
- package/dist/trade/send/drpc_base.d.ts +0 -5
- package/dist/trade/send/drpc_base.js +0 -48
- package/dist/trade/send/getblock_base.d.ts +0 -5
- package/dist/trade/send/getblock_base.js +0 -48
- package/dist/trade/send/index.d.ts +0 -15
- package/dist/trade/send/index.js +0 -33
- package/dist/trade/send/infura_base.d.ts +0 -5
- package/dist/trade/send/infura_base.js +0 -48
- package/dist/trade/send/moralis_base.d.ts +0 -5
- package/dist/trade/send/moralis_base.js +0 -48
- package/dist/trade/send/onerpc_base.d.ts +0 -5
- package/dist/trade/send/onerpc_base.js +0 -48
- package/dist/trade/send/quicknode_base.d.ts +0 -5
- package/dist/trade/send/quicknode_base.js +0 -48
- package/dist/trade/send/send_tx.d.ts +0 -17
- package/dist/trade/send/send_tx.js +0 -163
- package/dist/ws/event_filter.d.ts +0 -8
- package/dist/ws/event_filter.js +0 -36
- package/dist/ws/index.d.ts +0 -2
- package/dist/ws/subscribe_v2_events.d.ts +0 -14
- package/dist/ws/subscribe_v2_events.js +0 -174
- package/dist/ws/subscribe_v3_events.d.ts +0 -14
- package/dist/ws/subscribe_v3_events.js +0 -174
|
@@ -13,9 +13,9 @@ export interface EvmContractEventData {
|
|
|
13
13
|
eventSignature: string;
|
|
14
14
|
args: any;
|
|
15
15
|
removeListener: () => void;
|
|
16
|
-
getBlock: () => Promise<
|
|
17
|
-
getTransaction: () => Promise<
|
|
18
|
-
getTransactionReceipt: () => Promise<
|
|
16
|
+
getBlock: () => Promise<any>;
|
|
17
|
+
getTransaction: () => Promise<any>;
|
|
18
|
+
getTransactionReceipt: () => Promise<any>;
|
|
19
19
|
decode: (data: string, topics?: string[]) => any;
|
|
20
20
|
}
|
|
21
21
|
export interface AmmPoolSyncEventData extends EvmContractEventData {
|
|
@@ -2,7 +2,7 @@ export interface OnchainPoolData {
|
|
|
2
2
|
address: string;
|
|
3
3
|
token0: string;
|
|
4
4
|
token1: string;
|
|
5
|
-
|
|
5
|
+
feeRateBps: number;
|
|
6
6
|
tickSpacing: number;
|
|
7
7
|
reserve0: string;
|
|
8
8
|
reserve1: string;
|
|
@@ -10,20 +10,147 @@ export interface OnchainPoolData {
|
|
|
10
10
|
sqrtPriceX96: string;
|
|
11
11
|
liquidity: string;
|
|
12
12
|
}
|
|
13
|
+
export interface AmmPoolState {
|
|
14
|
+
address: string;
|
|
15
|
+
token0: string;
|
|
16
|
+
token1: string;
|
|
17
|
+
protocolFeeBps: number;
|
|
18
|
+
reserve0: string;
|
|
19
|
+
reserve1: string;
|
|
20
|
+
stable?: boolean;
|
|
21
|
+
decimals0?: string;
|
|
22
|
+
decimals1?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface ClmmPoolState {
|
|
25
|
+
address: string;
|
|
26
|
+
token0: string;
|
|
27
|
+
token1: string;
|
|
28
|
+
poolFee: number;
|
|
29
|
+
tickSpacing: number;
|
|
30
|
+
tick: number;
|
|
31
|
+
sqrtPriceX96: string;
|
|
32
|
+
liquidity: string;
|
|
33
|
+
}
|
|
34
|
+
export interface InfinityPoolState {
|
|
35
|
+
address: string;
|
|
36
|
+
token0: string;
|
|
37
|
+
token1: string;
|
|
38
|
+
currency0: string;
|
|
39
|
+
currency1: string;
|
|
40
|
+
hooks: string;
|
|
41
|
+
poolManager: string;
|
|
42
|
+
parameters: string;
|
|
43
|
+
tickSpacing: number;
|
|
44
|
+
poolKeyFee: number;
|
|
45
|
+
tick: number;
|
|
46
|
+
sqrtPriceX96: string;
|
|
47
|
+
liquidity: string;
|
|
48
|
+
slot0LpFee?: number;
|
|
49
|
+
slot0ProtocolFee?: number;
|
|
50
|
+
lastSwapFee?: number;
|
|
51
|
+
lastSwapProtocolFee?: number;
|
|
52
|
+
}
|
|
13
53
|
export interface OnchainPoolChangeEvent {
|
|
54
|
+
provider_id?: string;
|
|
14
55
|
pool_address: string;
|
|
15
56
|
type: string;
|
|
16
57
|
event_time: number;
|
|
17
|
-
data:
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
58
|
+
data: OnchainEventDataMerged;
|
|
59
|
+
}
|
|
60
|
+
export interface OnchainEventDataMerged {
|
|
61
|
+
blockNumber: number;
|
|
62
|
+
transactionIndex: number;
|
|
63
|
+
transactionHash: string;
|
|
64
|
+
reserve0?: string;
|
|
65
|
+
reserve1?: string;
|
|
66
|
+
amount0?: string;
|
|
67
|
+
amount1?: string;
|
|
68
|
+
sqrtPriceX96?: string;
|
|
69
|
+
liquidity?: string;
|
|
70
|
+
tick?: number;
|
|
71
|
+
protocolFeesToken0?: string;
|
|
72
|
+
protocolFeesToken1?: string;
|
|
73
|
+
fee?: string;
|
|
74
|
+
protocolFee?: string;
|
|
75
|
+
tickLower?: number;
|
|
76
|
+
tickUpper?: number;
|
|
77
|
+
amount?: string;
|
|
78
|
+
liquidityDelta?: string;
|
|
79
|
+
token0Symbol?: string;
|
|
80
|
+
token1Symbol?: string;
|
|
81
|
+
askToken0InToken1?: string;
|
|
82
|
+
bidToken0InToken1?: string;
|
|
83
|
+
askToken1InToken0?: string;
|
|
84
|
+
bidToken1InToken0?: string;
|
|
85
|
+
}
|
|
86
|
+
export interface V2SwapEventData {
|
|
87
|
+
blockNumber: number;
|
|
88
|
+
transactionIndex: number;
|
|
89
|
+
transactionHash: string;
|
|
90
|
+
reserve0: string;
|
|
91
|
+
reserve1: string;
|
|
92
|
+
amount0?: string;
|
|
93
|
+
amount1?: string;
|
|
94
|
+
}
|
|
95
|
+
export interface V2LiquidityEventData {
|
|
96
|
+
blockNumber: number;
|
|
97
|
+
transactionIndex: number;
|
|
98
|
+
transactionHash: string;
|
|
99
|
+
amount0: string;
|
|
100
|
+
amount1: string;
|
|
101
|
+
}
|
|
102
|
+
export interface V3SwapEventData {
|
|
103
|
+
blockNumber: number;
|
|
104
|
+
transactionIndex: number;
|
|
105
|
+
transactionHash: string;
|
|
106
|
+
amount0: string;
|
|
107
|
+
amount1: string;
|
|
108
|
+
sqrtPriceX96: string;
|
|
109
|
+
liquidity: string;
|
|
110
|
+
tick: number;
|
|
111
|
+
protocolFeesToken0: string;
|
|
112
|
+
protocolFeesToken1: string;
|
|
113
|
+
}
|
|
114
|
+
export interface V3LiquidityEventData {
|
|
115
|
+
blockNumber: number;
|
|
116
|
+
transactionIndex: number;
|
|
117
|
+
transactionHash: string;
|
|
118
|
+
tickLower: number;
|
|
119
|
+
tickUpper: number;
|
|
120
|
+
amount: string;
|
|
121
|
+
amount0: string;
|
|
122
|
+
amount1: string;
|
|
123
|
+
}
|
|
124
|
+
export interface V4SwapEventData {
|
|
125
|
+
blockNumber: number;
|
|
126
|
+
transactionIndex: number;
|
|
127
|
+
transactionHash: string;
|
|
128
|
+
amount0: string;
|
|
129
|
+
amount1: string;
|
|
130
|
+
sqrtPriceX96: string;
|
|
131
|
+
liquidity: string;
|
|
132
|
+
tick: number;
|
|
133
|
+
fee: string;
|
|
134
|
+
protocolFee: string;
|
|
135
|
+
}
|
|
136
|
+
export interface V4LiquidityEventData {
|
|
137
|
+
blockNumber: number;
|
|
138
|
+
transactionIndex: number;
|
|
139
|
+
transactionHash: string;
|
|
140
|
+
tickLower: number;
|
|
141
|
+
tickUpper: number;
|
|
142
|
+
amount: string;
|
|
143
|
+
amount0: string;
|
|
144
|
+
amount1: string;
|
|
145
|
+
}
|
|
146
|
+
export interface PriceFeedEventData {
|
|
147
|
+
blockNumber: number;
|
|
148
|
+
transactionIndex: number;
|
|
149
|
+
transactionHash: string;
|
|
150
|
+
token0Symbol: string;
|
|
151
|
+
token1Symbol: string;
|
|
152
|
+
askToken0InToken1: string;
|
|
153
|
+
bidToken0InToken1: string;
|
|
154
|
+
askToken1InToken0: string;
|
|
155
|
+
bidToken1InToken0: string;
|
|
29
156
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ethersCompat = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const _ethers = ethers_1.ethers;
|
|
6
|
+
exports.ethersCompat = {
|
|
7
|
+
Interface: _ethers.utils?.Interface || _ethers.Interface,
|
|
8
|
+
JsonRpcProvider: _ethers.providers?.JsonRpcProvider || _ethers.JsonRpcProvider,
|
|
9
|
+
WebSocketProvider: _ethers.providers?.WebSocketProvider || _ethers.WebSocketProvider,
|
|
10
|
+
id: _ethers.utils?.id || _ethers.id,
|
|
11
|
+
formatUnits: _ethers.utils?.formatUnits || _ethers.formatUnits,
|
|
12
|
+
parseUnits: _ethers.utils?.parseUnits || _ethers.parseUnits,
|
|
13
|
+
formatEther: _ethers.utils?.formatEther || _ethers.formatEther,
|
|
14
|
+
parseEther: _ethers.utils?.parseEther || _ethers.parseEther,
|
|
15
|
+
keccak256: _ethers.utils?.keccak256 || _ethers.keccak256,
|
|
16
|
+
isAddress: _ethers.utils?.isAddress || _ethers.isAddress,
|
|
17
|
+
Zero: _ethers.constants?.Zero || BigInt(0),
|
|
18
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function patchEthersV5Signer(): boolean;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.patchEthersV5Signer = patchEthersV5Signer;
|
|
4
|
+
let patchedV5 = false;
|
|
5
|
+
let patchedV6 = false;
|
|
6
|
+
function tryRequire(moduleName) {
|
|
7
|
+
try {
|
|
8
|
+
return require(moduleName);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function patchEthersV5Signer() {
|
|
15
|
+
const secp = tryRequire('secp256k1');
|
|
16
|
+
if (!secp) {
|
|
17
|
+
console.warn('[fast_signer] secp256k1 native module not found, skipping patch');
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (patchEthersV5WithSecp(secp))
|
|
21
|
+
return true;
|
|
22
|
+
if (patchEthersV6WithSecp(secp))
|
|
23
|
+
return true;
|
|
24
|
+
console.log('[fast_signer] no compatible ethers version found, skipping patch');
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
function patchEthersV5WithSecp(secp) {
|
|
28
|
+
if (patchedV5)
|
|
29
|
+
return true;
|
|
30
|
+
const signingKeyModule = tryRequire('@ethersproject/signing-key');
|
|
31
|
+
const bytesModule = tryRequire('@ethersproject/bytes');
|
|
32
|
+
if (!signingKeyModule || !bytesModule)
|
|
33
|
+
return false;
|
|
34
|
+
const { SigningKey } = signingKeyModule;
|
|
35
|
+
const { arrayify, hexZeroPad, splitSignature } = bytesModule;
|
|
36
|
+
const originalSignDigest = SigningKey.prototype.signDigest;
|
|
37
|
+
SigningKey.prototype.signDigest = function (digest) {
|
|
38
|
+
const privKeyBytes = arrayify(this.privateKey);
|
|
39
|
+
const digestBytes = arrayify(digest);
|
|
40
|
+
if (digestBytes.length !== 32) {
|
|
41
|
+
return originalSignDigest.call(this, digest);
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
const { signature, recid } = secp.ecdsaSign(digestBytes, privKeyBytes);
|
|
45
|
+
const r = hexZeroPad('0x' + Buffer.from(signature.slice(0, 32)).toString('hex'), 32);
|
|
46
|
+
const s = hexZeroPad('0x' + Buffer.from(signature.slice(32, 64)).toString('hex'), 32);
|
|
47
|
+
return splitSignature({ recoveryParam: recid, r, s });
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
console.warn('[fast_signer v5] native sign failed, fallback', err);
|
|
51
|
+
return originalSignDigest.call(this, digest);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
patchedV5 = true;
|
|
55
|
+
console.log('[fast_signer] ethers v5 SigningKey patched with native secp256k1');
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
function patchEthersV6WithSecp(secp) {
|
|
59
|
+
if (patchedV6)
|
|
60
|
+
return true;
|
|
61
|
+
const ethersV6 = tryRequire('ethers');
|
|
62
|
+
if (!ethersV6 || !ethersV6.SigningKey || !ethersV6.Signature || !ethersV6.getBytes) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const { SigningKey, Signature, getBytes } = ethersV6;
|
|
66
|
+
const originalSign = SigningKey.prototype.sign;
|
|
67
|
+
SigningKey.prototype.sign = function (digest) {
|
|
68
|
+
const privKeyBytes = getBytes(this.privateKey);
|
|
69
|
+
const digestBytes = getBytes(digest);
|
|
70
|
+
if (digestBytes.length !== 32) {
|
|
71
|
+
return originalSign.call(this, digest);
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const { signature, recid } = secp.ecdsaSign(digestBytes, privKeyBytes);
|
|
75
|
+
const r = '0x' + Buffer.from(signature.slice(0, 32)).toString('hex').padStart(64, '0');
|
|
76
|
+
const s = '0x' + Buffer.from(signature.slice(32, 64)).toString('hex').padStart(64, '0');
|
|
77
|
+
return Signature.from({ r, s, v: recid ? 0x1c : 0x1b });
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
console.warn('[fast_signer v6] native sign failed, fallback', err);
|
|
81
|
+
return originalSign.call(this, digest);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
patchedV6 = true;
|
|
85
|
+
console.log('[fast_signer] ethers v6 SigningKey patched with native secp256k1');
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ethers } from 'ethers';
|
|
2
2
|
export declare class EVMGasHelper {
|
|
3
|
-
static wrapNativeToken(provider:
|
|
4
|
-
static checkNativeBalance(provider:
|
|
3
|
+
static wrapNativeToken(provider: any, wallet: ethers.Wallet, wrappedTokenAddress: string, amount: string, minReserve?: string, tokenSymbol?: string): Promise<string>;
|
|
4
|
+
static checkNativeBalance(provider: any, walletAddress: string): Promise<ethers.BigNumber>;
|
|
5
5
|
}
|
package/dist/utils/gas_helper.js
CHANGED
|
@@ -1,72 +1,60 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.EVMGasHelper = void 0;
|
|
13
4
|
const ethers_1 = require("ethers");
|
|
14
|
-
const
|
|
5
|
+
const ethers_compat_1 = require("./ethers_compat");
|
|
6
|
+
const ttd_core_1 = require("@clonegod/ttd-core");
|
|
15
7
|
class EVMGasHelper {
|
|
16
|
-
static wrapNativeToken(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
(0, dist_1.log_info)(`${tokenSymbol}余额不足最小保留额 ${minReserve} ${tokenSymbol},不执行转换`);
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
wrapAmount = nativeBalance.sub(minReserveBN);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
wrapAmount = ethers_1.ethers.utils.parseEther(amount);
|
|
32
|
-
const minReserveBN = ethers_1.ethers.utils.parseEther(minReserve);
|
|
33
|
-
if (nativeBalance.sub(wrapAmount).lt(minReserveBN)) {
|
|
34
|
-
(0, dist_1.log_info)(`转换后${tokenSymbol}余额将低于最小保留额 ${minReserve} ${tokenSymbol},调整转换金额`);
|
|
35
|
-
wrapAmount = nativeBalance.sub(minReserveBN);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (wrapAmount.lte(ethers_1.ethers.constants.Zero)) {
|
|
39
|
-
(0, dist_1.log_info)(`计算的转换金额为0或负数,不执行转换`);
|
|
8
|
+
static async wrapNativeToken(provider, wallet, wrappedTokenAddress, amount, minReserve = "0.1", tokenSymbol = "ETH") {
|
|
9
|
+
const nativeBalance = await provider.getBalance(wallet.address);
|
|
10
|
+
const nativeBalanceEth = ethers_compat_1.ethersCompat.formatEther(nativeBalance);
|
|
11
|
+
(0, ttd_core_1.log_info)(`当前${tokenSymbol}余额: ${nativeBalanceEth} ${tokenSymbol}`);
|
|
12
|
+
let wrapAmount;
|
|
13
|
+
if (amount === "max") {
|
|
14
|
+
const minReserveBN = ethers_compat_1.ethersCompat.parseEther(minReserve);
|
|
15
|
+
if (nativeBalance.lte(minReserveBN)) {
|
|
16
|
+
(0, ttd_core_1.log_info)(`${tokenSymbol}余额不足最小保留额 ${minReserve} ${tokenSymbol},不执行转换`);
|
|
40
17
|
return null;
|
|
41
18
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
gasLimit: 100000,
|
|
51
|
-
gasPrice: gasPrice
|
|
52
|
-
};
|
|
53
|
-
try {
|
|
54
|
-
const tx = yield wrappedTokenContract.deposit(txOptions);
|
|
55
|
-
(0, dist_1.log_info)(`${tokenSymbol}转换为W${tokenSymbol}交易已发送,txHash: ${tx.hash}`);
|
|
56
|
-
yield tx.wait();
|
|
57
|
-
(0, dist_1.log_info)(`${tokenSymbol}转换为W${tokenSymbol}交易已确认,txHash: ${tx.hash}`);
|
|
58
|
-
return tx.hash;
|
|
59
|
-
}
|
|
60
|
-
catch (error) {
|
|
61
|
-
(0, dist_1.log_error)(`${tokenSymbol}转换为W${tokenSymbol}失败: ${error.message}`, error);
|
|
62
|
-
throw error;
|
|
19
|
+
wrapAmount = nativeBalance.sub(minReserveBN);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
wrapAmount = ethers_compat_1.ethersCompat.parseEther(amount);
|
|
23
|
+
const minReserveBN = ethers_compat_1.ethersCompat.parseEther(minReserve);
|
|
24
|
+
if (nativeBalance.sub(wrapAmount).lt(minReserveBN)) {
|
|
25
|
+
(0, ttd_core_1.log_info)(`转换后${tokenSymbol}余额将低于最小保留额 ${minReserve} ${tokenSymbol},调整转换金额`);
|
|
26
|
+
wrapAmount = nativeBalance.sub(minReserveBN);
|
|
63
27
|
}
|
|
64
|
-
}
|
|
28
|
+
}
|
|
29
|
+
if (wrapAmount.lte(ethers_compat_1.ethersCompat.Zero)) {
|
|
30
|
+
(0, ttd_core_1.log_info)(`计算的转换金额为0或负数,不执行转换`);
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const wrappedTokenContract = new ethers_1.ethers.Contract(wrappedTokenAddress, [
|
|
34
|
+
"function deposit() external payable",
|
|
35
|
+
"function withdraw(uint wad) external"
|
|
36
|
+
], wallet);
|
|
37
|
+
(0, ttd_core_1.log_info)(`准备将 ${ethers_compat_1.ethersCompat.formatEther(wrapAmount)} ${tokenSymbol} 转换为 W${tokenSymbol}`);
|
|
38
|
+
const gasPrice = await provider.getGasPrice();
|
|
39
|
+
const txOptions = {
|
|
40
|
+
value: wrapAmount,
|
|
41
|
+
gasLimit: 100000,
|
|
42
|
+
gasPrice: gasPrice
|
|
43
|
+
};
|
|
44
|
+
try {
|
|
45
|
+
const tx = await wrappedTokenContract.deposit(txOptions);
|
|
46
|
+
(0, ttd_core_1.log_info)(`${tokenSymbol}转换为W${tokenSymbol}交易已发送,txHash: ${tx.hash}`);
|
|
47
|
+
await tx.wait();
|
|
48
|
+
(0, ttd_core_1.log_info)(`${tokenSymbol}转换为W${tokenSymbol}交易已确认,txHash: ${tx.hash}`);
|
|
49
|
+
return tx.hash;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
(0, ttd_core_1.log_error)(`${tokenSymbol}转换为W${tokenSymbol}失败: ${error.message}`, error);
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
65
55
|
}
|
|
66
|
-
static checkNativeBalance(provider, walletAddress) {
|
|
67
|
-
return
|
|
68
|
-
return yield provider.getBalance(walletAddress);
|
|
69
|
-
});
|
|
56
|
+
static async checkNativeBalance(provider, walletAddress) {
|
|
57
|
+
return await provider.getBalance(walletAddress);
|
|
70
58
|
}
|
|
71
59
|
}
|
|
72
60
|
exports.EVMGasHelper = EVMGasHelper;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { ethers } from "ethers";
|
|
2
1
|
import Decimal from "decimal.js";
|
|
3
2
|
export * from './gas_helper';
|
|
3
|
+
export * from './trade_direction';
|
|
4
|
+
export * from './pool_filter';
|
|
5
|
+
export * from './ethers_compat';
|
|
6
|
+
export * from './fast_signer';
|
|
4
7
|
export declare const sleep: (ms: number) => Promise<void>;
|
|
5
8
|
export declare const formatPrice: (price: number, precision?: number) => string;
|
|
6
|
-
export declare const formatUnits: (value:
|
|
9
|
+
export declare const formatUnits: (value: any, decimals: number) => Decimal;
|
|
7
10
|
export declare const formatNumberHighPrecision: (numStr: string, precision?: number) => string;
|
package/dist/utils/index.js
CHANGED
|
@@ -18,9 +18,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
20
|
exports.formatNumberHighPrecision = exports.formatUnits = exports.formatPrice = exports.sleep = void 0;
|
|
21
|
-
const ethers_1 = require("ethers");
|
|
22
21
|
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
22
|
+
const ethers_compat_1 = require("./ethers_compat");
|
|
23
23
|
__exportStar(require("./gas_helper"), exports);
|
|
24
|
+
__exportStar(require("./trade_direction"), exports);
|
|
25
|
+
__exportStar(require("./pool_filter"), exports);
|
|
26
|
+
__exportStar(require("./ethers_compat"), exports);
|
|
27
|
+
__exportStar(require("./fast_signer"), exports);
|
|
24
28
|
const sleep = (ms) => {
|
|
25
29
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
26
30
|
};
|
|
@@ -30,7 +34,7 @@ const formatPrice = (price, precision = 10) => {
|
|
|
30
34
|
};
|
|
31
35
|
exports.formatPrice = formatPrice;
|
|
32
36
|
const formatUnits = (value, decimals) => {
|
|
33
|
-
return new decimal_js_1.default(
|
|
37
|
+
return new decimal_js_1.default(ethers_compat_1.ethersCompat.formatUnits(value, decimals).toString());
|
|
34
38
|
};
|
|
35
39
|
exports.formatUnits = formatUnits;
|
|
36
40
|
const formatNumberHighPrecision = (numStr, precision = 12) => {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { StandardPoolInfoType } from '@clonegod/ttd-core';
|
|
2
|
+
export interface PoolFilterParams {
|
|
3
|
+
poolAddress?: string;
|
|
4
|
+
poolName?: string;
|
|
5
|
+
feeRate?: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function buildPoolFilterFromEnv(envArgs: any): PoolFilterParams;
|
|
8
|
+
export declare function filterPools(pools: StandardPoolInfoType[], filter: PoolFilterParams, label?: string): StandardPoolInfoType[];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildPoolFilterFromEnv = buildPoolFilterFromEnv;
|
|
4
|
+
exports.filterPools = filterPools;
|
|
5
|
+
const ttd_core_1 = require("@clonegod/ttd-core");
|
|
6
|
+
function buildPoolFilterFromEnv(envArgs) {
|
|
7
|
+
return {
|
|
8
|
+
poolAddress: envArgs.quote_pool_address || undefined,
|
|
9
|
+
poolName: envArgs.quote_pool_name || undefined,
|
|
10
|
+
feeRate: envArgs.quote_pool_fee_rate || undefined,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function filterPools(pools, filter, label = '') {
|
|
14
|
+
const prefix = label ? `[${label}] ` : '';
|
|
15
|
+
let result = pools;
|
|
16
|
+
if (filter.poolAddress) {
|
|
17
|
+
result = pools.filter(p => p.pool_address.toLowerCase() === filter.poolAddress.toLowerCase());
|
|
18
|
+
}
|
|
19
|
+
else if (filter.poolName || filter.feeRate) {
|
|
20
|
+
result = pools.filter(p => {
|
|
21
|
+
if (filter.poolName && p.pool_name !== filter.poolName)
|
|
22
|
+
return false;
|
|
23
|
+
if (filter.feeRate && p.fee_rate !== filter.feeRate)
|
|
24
|
+
return false;
|
|
25
|
+
return true;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (result.length === 0 && pools.length > 0) {
|
|
29
|
+
(0, ttd_core_1.log_warn)(`${prefix}Pool filter matched 0 pools from ${pools.length}`, {
|
|
30
|
+
filter,
|
|
31
|
+
available: pools.map(p => `${p.pool_name}(${p.pool_address}, fee=${p.fee_rate})`),
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
(0, ttd_core_1.log_info)(`${prefix}Pool filter: ${pools.length} → ${result.length}`, result.map(p => `${p.pool_name}(${p.pool_address}, fee=${p.fee_rate})`));
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface TradeDirectionResult<T = any> {
|
|
2
|
+
isBuy: boolean;
|
|
3
|
+
inputToken: T;
|
|
4
|
+
outputToken: T;
|
|
5
|
+
baseToken: T;
|
|
6
|
+
quoteToken: T;
|
|
7
|
+
}
|
|
8
|
+
export declare function resolveTradeDirection<T = any>(poolInfo: {
|
|
9
|
+
tokenA: T;
|
|
10
|
+
tokenB: T;
|
|
11
|
+
quote_token: string;
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}, isBuy: boolean): TradeDirectionResult<T>;
|
|
14
|
+
export declare function calculateStandardPrice(inputAmount: number, outputAmount: number, isBuy: boolean): string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveTradeDirection = resolveTradeDirection;
|
|
4
|
+
exports.calculateStandardPrice = calculateStandardPrice;
|
|
5
|
+
function resolveTradeDirection(poolInfo, isBuy) {
|
|
6
|
+
const { tokenA, tokenB, quote_token } = poolInfo;
|
|
7
|
+
const quoteToken = [tokenA, tokenB].find(t => t.symbol === quote_token);
|
|
8
|
+
const baseToken = [tokenA, tokenB].find(t => t.symbol !== quote_token);
|
|
9
|
+
if (!quoteToken || !baseToken) {
|
|
10
|
+
throw new Error(`Cannot resolve tokens: tokenA=${tokenA?.symbol}, tokenB=${tokenB?.symbol}, quote_token=${quote_token}`);
|
|
11
|
+
}
|
|
12
|
+
const inputToken = isBuy ? quoteToken : baseToken;
|
|
13
|
+
const outputToken = isBuy ? baseToken : quoteToken;
|
|
14
|
+
return { isBuy, inputToken, outputToken, baseToken, quoteToken };
|
|
15
|
+
}
|
|
16
|
+
function calculateStandardPrice(inputAmount, outputAmount, isBuy) {
|
|
17
|
+
if (isBuy) {
|
|
18
|
+
return (inputAmount / outputAmount).toFixed(18);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
return (outputAmount / inputAmount).toFixed(18);
|
|
22
|
+
}
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clonegod/ttd-base-common",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Base common library",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"push": "npm run build && npm publish"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@clonegod/ttd-core": "
|
|
18
|
-
"axios": "
|
|
17
|
+
"@clonegod/ttd-core": "3.1.75",
|
|
18
|
+
"axios": "1.15.0",
|
|
19
19
|
"dotenv": "^16.4.7",
|
|
20
20
|
"ethers": "^5.8.0"
|
|
21
21
|
},
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { EnvArgs } from "@clonegod/ttd-core/dist";
|
|
2
|
-
export declare class BaseEnvArgs extends EnvArgs {
|
|
3
|
-
large_trade_threshold_usd: number;
|
|
4
|
-
gas_price_gwei: number;
|
|
5
|
-
gas_limit: number;
|
|
6
|
-
tip_amount_gwei: number;
|
|
7
|
-
tx_status_check_interval_mills: number;
|
|
8
|
-
tx_cancel_timeout_ms: number;
|
|
9
|
-
tx_cancel_gas_boost_factor: number;
|
|
10
|
-
constructor();
|
|
11
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseEnvArgs = void 0;
|
|
4
|
-
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
5
|
-
const decimal_js_1 = require("decimal.js");
|
|
6
|
-
class BaseEnvArgs extends dist_1.EnvArgs {
|
|
7
|
-
constructor() {
|
|
8
|
-
super();
|
|
9
|
-
this.large_trade_threshold_usd = parseInt(process.env.LARGE_TRADE_THRESHOLD_USD || '500');
|
|
10
|
-
this.gas_price_gwei = new decimal_js_1.Decimal(process.env.GAS_PRICE_GWEI || '1').toNumber();
|
|
11
|
-
this.gas_limit = parseInt(process.env.GAS_LIMIT || '300000');
|
|
12
|
-
this.tip_amount_gwei = parseInt(process.env.EOA_TIP_AMOUNT_GWEI || '10000');
|
|
13
|
-
this.tx_status_check_interval_mills = parseInt(process.env.TX_STATUS_CHECK_INTERVAL_MILLS || '3000');
|
|
14
|
-
this.tx_cancel_timeout_ms = parseInt(process.env.TX_CANCEL_TIMEOUT_MS || '15000');
|
|
15
|
-
this.tx_cancel_gas_boost_factor = new decimal_js_1.Decimal(process.env.TX_CANCEL_GAS_PRICE_BOOST_FACTOR || '1.5').toNumber();
|
|
16
|
-
this.print();
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
exports.BaseEnvArgs = BaseEnvArgs;
|
package/dist/config/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './base_env_args';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|