@clonegod/ttd-bsc-common 3.1.67 → 3.1.69

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.
@@ -6,4 +6,3 @@ export * from './verify';
6
6
  export * from './price_feed_handler';
7
7
  export * from './quote_amount';
8
8
  export * from './preload_token_prices';
9
- export * from './hook_fee_monitor';
@@ -22,4 +22,3 @@ __exportStar(require("./verify"), exports);
22
22
  __exportStar(require("./price_feed_handler"), exports);
23
23
  __exportStar(require("./quote_amount"), exports);
24
24
  __exportStar(require("./preload_token_prices"), exports);
25
- __exportStar(require("./hook_fee_monitor"), exports);
@@ -0,0 +1,13 @@
1
+ import { AmmPoolState, ClmmPoolState, InfinityPoolState } from '../../types/pool_state';
2
+ export declare const V4_DYNAMIC_FEE_FLAG = 8388608;
3
+ export declare function calculateSwapFeePpm(protocolFeePpm: number, lpFeePpm: number): number;
4
+ export declare function decodeV4ProtocolFee(encodedProtocolFee: number, zeroForOne: boolean): number;
5
+ export declare function v2EffectiveFeeBps(state: AmmPoolState): number;
6
+ export declare function v3EffectiveFeeBps(state: ClmmPoolState): number;
7
+ export declare function v4EffectiveFeeBps(state: InfinityPoolState): number;
8
+ export declare function v4EffectiveFeePpm(state: InfinityPoolState): number;
9
+ export declare function v4DirectionalFeePpm(state: InfinityPoolState, zeroForOne: boolean): number;
10
+ export declare function buildInfinityPoolSdkFee(state: InfinityPoolState): {
11
+ fee: number;
12
+ protocolFee: number;
13
+ };
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.V4_DYNAMIC_FEE_FLAG = void 0;
4
+ exports.calculateSwapFeePpm = calculateSwapFeePpm;
5
+ exports.decodeV4ProtocolFee = decodeV4ProtocolFee;
6
+ exports.v2EffectiveFeeBps = v2EffectiveFeeBps;
7
+ exports.v3EffectiveFeeBps = v3EffectiveFeeBps;
8
+ exports.v4EffectiveFeeBps = v4EffectiveFeeBps;
9
+ exports.v4EffectiveFeePpm = v4EffectiveFeePpm;
10
+ exports.v4DirectionalFeePpm = v4DirectionalFeePpm;
11
+ exports.buildInfinityPoolSdkFee = buildInfinityPoolSdkFee;
12
+ exports.V4_DYNAMIC_FEE_FLAG = 0x800000;
13
+ function calculateSwapFeePpm(protocolFeePpm, lpFeePpm) {
14
+ return protocolFeePpm + lpFeePpm - Math.floor(protocolFeePpm * lpFeePpm / 1000000);
15
+ }
16
+ function decodeV4ProtocolFee(encodedProtocolFee, zeroForOne) {
17
+ if (zeroForOne)
18
+ return encodedProtocolFee & 0xFFF;
19
+ return (encodedProtocolFee >> 12) & 0xFFF;
20
+ }
21
+ function v2EffectiveFeeBps(state) {
22
+ return state.protocolFeeBps;
23
+ }
24
+ function v3EffectiveFeeBps(state) {
25
+ return state.poolFee / 100;
26
+ }
27
+ function v4EffectiveFeeBps(state) {
28
+ return v4EffectiveFeePpm(state) / 100;
29
+ }
30
+ function v4EffectiveFeePpm(state) {
31
+ if (state.lastSwapFee != null) {
32
+ return state.lastSwapFee;
33
+ }
34
+ if (state.poolKeyFee !== exports.V4_DYNAMIC_FEE_FLAG) {
35
+ return state.poolKeyFee;
36
+ }
37
+ const lp = state.slot0LpFee ?? 0;
38
+ const proto = state.slot0ProtocolFee ?? 0;
39
+ const proto0to1 = proto & 0xFFF;
40
+ const proto1to0 = (proto >> 12) & 0xFFF;
41
+ const fee0to1 = calculateSwapFeePpm(proto0to1, lp);
42
+ const fee1to0 = calculateSwapFeePpm(proto1to0, lp);
43
+ return Math.max(fee0to1, fee1to0);
44
+ }
45
+ function v4DirectionalFeePpm(state, zeroForOne) {
46
+ if (state.lastSwapFee != null) {
47
+ return state.lastSwapFee;
48
+ }
49
+ if (state.poolKeyFee !== exports.V4_DYNAMIC_FEE_FLAG) {
50
+ return state.poolKeyFee;
51
+ }
52
+ const lp = state.slot0LpFee ?? 0;
53
+ const proto = state.slot0ProtocolFee ?? 0;
54
+ const directionalProto = decodeV4ProtocolFee(proto, zeroForOne);
55
+ return calculateSwapFeePpm(directionalProto, lp);
56
+ }
57
+ function buildInfinityPoolSdkFee(state) {
58
+ if (state.lastSwapFee != null) {
59
+ return { fee: state.lastSwapFee, protocolFee: 0 };
60
+ }
61
+ if (state.poolKeyFee !== exports.V4_DYNAMIC_FEE_FLAG) {
62
+ return { fee: state.poolKeyFee, protocolFee: 0 };
63
+ }
64
+ return {
65
+ fee: state.slot0LpFee ?? 0,
66
+ protocolFee: state.slot0ProtocolFee ?? 0,
67
+ };
68
+ }
@@ -1,3 +1,4 @@
1
1
  export * from './pool';
2
2
  export * from './sdk_token_factory';
3
3
  export * from './pool_state_initializer';
4
+ export * from './fee_helpers';
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./pool"), exports);
18
18
  __exportStar(require("./sdk_token_factory"), exports);
19
19
  __exportStar(require("./pool_state_initializer"), exports);
20
+ __exportStar(require("./fee_helpers"), exports);
@@ -55,17 +55,17 @@ class PoolStateInitializer {
55
55
  poolContract.token0(),
56
56
  poolContract.token1(),
57
57
  ]);
58
- const feeRateBps = AMM_FEE_MAP[dexId] || AMM_FEE_MAP[dexId.replace('-', '_')] || 30;
58
+ const protocolFeeBps = AMM_FEE_MAP[dexId] || AMM_FEE_MAP[dexId.replace('-', '_')] || 30;
59
59
  const state = {
60
60
  address: poolAddress,
61
61
  token0: token0.toLowerCase(),
62
62
  token1: token1.toLowerCase(),
63
- feeRateBps,
63
+ protocolFeeBps,
64
64
  reserve0: reserves[0].toString(),
65
65
  reserve1: reserves[1].toString(),
66
66
  };
67
67
  (0, ttd_core_1.log_info)(`[PoolStateInitializer] AMM pool initialized: ${poolAddress}`, {
68
- token0: state.token0, token1: state.token1, feeRateBps: state.feeRateBps,
68
+ token0: state.token0, token1: state.token1, protocolFeeBps: state.protocolFeeBps,
69
69
  });
70
70
  return state;
71
71
  }
@@ -87,13 +87,11 @@ class PoolStateInitializer {
87
87
  poolContract.liquidity(),
88
88
  ]);
89
89
  const [sqrtPriceX96, tick] = slot0;
90
- const feeRatePpm = Number(fee);
91
90
  const state = {
92
91
  address: poolAddress,
93
92
  token0: token0.toLowerCase(),
94
93
  token1: token1.toLowerCase(),
95
- feeRateBps: feeRatePpm / 100,
96
- feeRatePpm,
94
+ poolFee: Number(fee),
97
95
  tickSpacing: Number(tickSpacing),
98
96
  tick: Number(tick),
99
97
  sqrtPriceX96: sqrtPriceX96.toString(),
@@ -101,7 +99,7 @@ class PoolStateInitializer {
101
99
  };
102
100
  (0, ttd_core_1.log_info)(`[PoolStateInitializer] CLMM pool initialized: ${poolAddress}`, {
103
101
  token0: state.token0, token1: state.token1,
104
- feeRateBps: state.feeRateBps, feeRatePpm: state.feeRatePpm,
102
+ poolFee: state.poolFee,
105
103
  tickSpacing: state.tickSpacing, tick: state.tick,
106
104
  });
107
105
  return state;
@@ -126,25 +124,24 @@ class PoolStateInitializer {
126
124
  address: poolKey,
127
125
  token0: currency0.toLowerCase ? currency0.toLowerCase() : currency0,
128
126
  token1: currency1.toLowerCase ? currency1.toLowerCase() : currency1,
129
- feeRateBps: (Number(lpFee) + Number(protocolFee)) / 100,
130
- feeRatePpm: Number(fee),
127
+ currency0,
128
+ currency1,
129
+ hooks,
130
+ poolManager,
131
+ parameters,
131
132
  tickSpacing,
133
+ poolKeyFee: Number(fee),
132
134
  tick: Number(tick),
133
135
  sqrtPriceX96: sqrtPriceX96.toString(),
134
136
  liquidity: liquidity.toString(),
135
- hooks,
136
- poolManager,
137
- parameters,
138
- currency0,
139
- currency1,
140
- lpFee: Number(lpFee),
141
- protocolFee: Number(protocolFee),
137
+ slot0LpFee: Number(lpFee),
138
+ slot0ProtocolFee: Number(protocolFee),
142
139
  };
143
140
  (0, ttd_core_1.log_info)(`[PoolStateInitializer] Infinity pool initialized: ${poolKey}`, {
144
141
  currency0: state.currency0, currency1: state.currency1,
145
- feeRateBps: state.feeRateBps, feeRatePpm: state.feeRatePpm,
142
+ poolKeyFee: state.poolKeyFee,
143
+ slot0LpFee: state.slot0LpFee, slot0ProtocolFee: state.slot0ProtocolFee,
146
144
  tickSpacing: state.tickSpacing, tick: state.tick,
147
- lpFee: state.lpFee, protocolFee: state.protocolFee,
148
145
  });
149
146
  return state;
150
147
  }
@@ -199,8 +199,9 @@ class AbstractDexTrade extends ttd_core_1.AbastrcatTrade {
199
199
  trace.set('gasType', mainGasFields.type === 2 ? 'EIP1559' : 'Legacy');
200
200
  trace.set('tip', transfer_amount_gwei);
201
201
  const only_bundle = order_msg.is_dex_maker;
202
- await this.transactionSender.sendTransaction(signedMainTx, tipTxMap, order_trace_id, pair, only_bundle, trace);
203
202
  trace.mark('sent');
203
+ await this.transactionSender.sendTransaction(signedMainTx, tipTxMap, order_trace_id, pair, only_bundle, trace);
204
+ trace.mark('send_complete');
204
205
  trace.flush();
205
206
  context._execution_marks = trace.getAbsoluteMarks();
206
207
  context._execution_start_time = trace.getStartTime();
@@ -14,7 +14,7 @@ export interface AmmPoolState {
14
14
  address: string;
15
15
  token0: string;
16
16
  token1: string;
17
- feeRateBps: number;
17
+ protocolFeeBps: number;
18
18
  reserve0: string;
19
19
  reserve1: string;
20
20
  }
@@ -22,47 +22,132 @@ export interface ClmmPoolState {
22
22
  address: string;
23
23
  token0: string;
24
24
  token1: string;
25
- feeRateBps: number;
26
- feeRatePpm: number;
25
+ poolFee: number;
27
26
  tickSpacing: number;
28
27
  tick: number;
29
28
  sqrtPriceX96: string;
30
29
  liquidity: string;
31
30
  }
32
- export interface InfinityPoolState extends ClmmPoolState {
31
+ export interface InfinityPoolState {
32
+ address: string;
33
+ token0: string;
34
+ token1: string;
35
+ currency0: string;
36
+ currency1: string;
33
37
  hooks: string;
34
38
  poolManager: string;
35
39
  parameters: string;
36
- currency0: string;
37
- currency1: string;
38
- lpFee?: number;
39
- protocolFee?: number;
40
+ tickSpacing: number;
41
+ poolKeyFee: number;
42
+ tick: number;
43
+ sqrtPriceX96: string;
44
+ liquidity: string;
45
+ slot0LpFee?: number;
46
+ slot0ProtocolFee?: number;
47
+ lastSwapFee?: number;
48
+ lastSwapProtocolFee?: number;
40
49
  }
41
50
  export interface OnchainPoolChangeEvent {
42
51
  provider_id?: string;
43
52
  pool_address: string;
44
53
  type: string;
45
54
  event_time: number;
46
- data: {
47
- blockNumber: number;
48
- transactionIndex: number;
49
- transactionHash: string;
50
- amount0: string;
51
- amount1: string;
52
- reserve0: BigInt | string;
53
- reserve1: BigInt | string;
54
- tick: number;
55
- sqrtPriceX96: BigInt | string;
56
- liquidity: BigInt | string;
57
- tickLower?: number;
58
- tickUpper?: number;
59
- liquidityDelta?: string;
60
- amount?: string;
61
- token0Symbol?: string;
62
- token1Symbol?: string;
63
- askToken0InToken1?: string;
64
- bidToken0InToken1?: string;
65
- askToken1InToken0?: string;
66
- bidToken1InToken0?: string;
67
- };
55
+ data: OnchainEventDataMerged;
56
+ }
57
+ export interface OnchainEventDataMerged {
58
+ blockNumber: number;
59
+ transactionIndex: number;
60
+ transactionHash: string;
61
+ reserve0?: string;
62
+ reserve1?: string;
63
+ amount0?: string;
64
+ amount1?: string;
65
+ sqrtPriceX96?: string;
66
+ liquidity?: string;
67
+ tick?: number;
68
+ protocolFeesToken0?: string;
69
+ protocolFeesToken1?: string;
70
+ fee?: string;
71
+ protocolFee?: string;
72
+ tickLower?: number;
73
+ tickUpper?: number;
74
+ amount?: string;
75
+ liquidityDelta?: string;
76
+ token0Symbol?: string;
77
+ token1Symbol?: string;
78
+ askToken0InToken1?: string;
79
+ bidToken0InToken1?: string;
80
+ askToken1InToken0?: string;
81
+ bidToken1InToken0?: string;
82
+ }
83
+ export interface V2SwapEventData {
84
+ blockNumber: number;
85
+ transactionIndex: number;
86
+ transactionHash: string;
87
+ reserve0: string;
88
+ reserve1: string;
89
+ amount0?: string;
90
+ amount1?: string;
91
+ }
92
+ export interface V2LiquidityEventData {
93
+ blockNumber: number;
94
+ transactionIndex: number;
95
+ transactionHash: string;
96
+ amount0: string;
97
+ amount1: string;
98
+ }
99
+ export interface V3SwapEventData {
100
+ blockNumber: number;
101
+ transactionIndex: number;
102
+ transactionHash: string;
103
+ amount0: string;
104
+ amount1: string;
105
+ sqrtPriceX96: string;
106
+ liquidity: string;
107
+ tick: number;
108
+ protocolFeesToken0: string;
109
+ protocolFeesToken1: string;
110
+ }
111
+ export interface V3LiquidityEventData {
112
+ blockNumber: number;
113
+ transactionIndex: number;
114
+ transactionHash: string;
115
+ tickLower: number;
116
+ tickUpper: number;
117
+ amount: string;
118
+ amount0: string;
119
+ amount1: string;
120
+ }
121
+ export interface V4SwapEventData {
122
+ blockNumber: number;
123
+ transactionIndex: number;
124
+ transactionHash: string;
125
+ amount0: string;
126
+ amount1: string;
127
+ sqrtPriceX96: string;
128
+ liquidity: string;
129
+ tick: number;
130
+ fee: string;
131
+ protocolFee: string;
132
+ }
133
+ export interface V4LiquidityEventData {
134
+ blockNumber: number;
135
+ transactionIndex: number;
136
+ transactionHash: string;
137
+ tickLower: number;
138
+ tickUpper: number;
139
+ amount: string;
140
+ amount0: string;
141
+ amount1: string;
142
+ }
143
+ export interface PriceFeedEventData {
144
+ blockNumber: number;
145
+ transactionIndex: number;
146
+ transactionHash: string;
147
+ token0Symbol: string;
148
+ token1Symbol: string;
149
+ askToken0InToken1: string;
150
+ bidToken0InToken1: string;
151
+ askToken1InToken0: string;
152
+ bidToken1InToken0: string;
68
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-bsc-common",
3
- "version": "3.1.67",
3
+ "version": "3.1.69",
4
4
  "description": "BSC common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",
@@ -14,7 +14,7 @@
14
14
  "push": "npm run build && npm publish"
15
15
  },
16
16
  "dependencies": {
17
- "@clonegod/ttd-core": "3.1.57",
17
+ "@clonegod/ttd-core": "3.1.60",
18
18
  "axios": "1.15.0",
19
19
  "dotenv": "^16.4.7",
20
20
  "ethers": "^5.8.0",