@clonegod/ttd-bsc-common 1.0.30 → 1.0.31

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.
@@ -0,0 +1,29 @@
1
+ export interface OnchainPoolData {
2
+ address: string;
3
+ token0: string;
4
+ token1: string;
5
+ fee: number;
6
+ tickSpacing: number;
7
+ reserve0: string;
8
+ reserve1: string;
9
+ tick: number;
10
+ sqrtPriceX96: string;
11
+ liquidity: string;
12
+ }
13
+ export interface OnchainPoolChangeEvent {
14
+ pool_address: string;
15
+ type: string;
16
+ event_time: number;
17
+ data: {
18
+ blockNumber: number;
19
+ transactionIndex: number;
20
+ transactionHash: string;
21
+ amount0: string;
22
+ amount1: string;
23
+ reserve0: BigInt;
24
+ reserve1: BigInt;
25
+ tick: number;
26
+ sqrtPriceX96: BigInt;
27
+ liquidity: BigInt;
28
+ };
29
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ export declare class EventFilter {
2
+ private static poolEventMap;
3
+ private static readonly CLEANUP_INTERVAL_MS;
4
+ private static readonly EVENT_EXPIRE_MS;
5
+ private static cleanupInitialized;
6
+ private static initializeCleanup;
7
+ static filterEvent<T>(poolAddress: string, blockNumber: number, eventType: string, callback: (data: T) => void, eventData: T): boolean;
8
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventFilter = void 0;
4
+ const dist_1 = require("@clonegod/ttd-core/dist");
5
+ class EventFilter {
6
+ static initializeCleanup() {
7
+ if (this.cleanupInitialized) {
8
+ return;
9
+ }
10
+ setInterval(() => {
11
+ const now = Date.now();
12
+ this.poolEventMap.forEach((timestamp, key) => {
13
+ if (now - timestamp > this.EVENT_EXPIRE_MS) {
14
+ this.poolEventMap.delete(key);
15
+ }
16
+ });
17
+ }, this.CLEANUP_INTERVAL_MS);
18
+ this.cleanupInitialized = true;
19
+ }
20
+ static filterEvent(poolAddress, blockNumber, eventType, callback, eventData) {
21
+ this.initializeCleanup();
22
+ const key = `${poolAddress}-${blockNumber}-${eventType.toUpperCase()}`;
23
+ if (this.poolEventMap.has(key)) {
24
+ (0, dist_1.log_warn)(`Event ${eventType} already occurred on pool: ${poolAddress}, block: ${blockNumber}`);
25
+ return false;
26
+ }
27
+ this.poolEventMap.set(key, Date.now());
28
+ callback(eventData);
29
+ return true;
30
+ }
31
+ }
32
+ exports.EventFilter = EventFilter;
33
+ EventFilter.poolEventMap = new Map();
34
+ EventFilter.CLEANUP_INTERVAL_MS = 10 * 1000;
35
+ EventFilter.EVENT_EXPIRE_MS = 10 * 60 * 1000;
36
+ EventFilter.cleanupInitialized = false;
@@ -0,0 +1,2 @@
1
+ export * from './subscribe_v2_events';
2
+ export * from './subscribe_v3_events';
@@ -0,0 +1,18 @@
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("./subscribe_v2_events"), exports);
18
+ __exportStar(require("./subscribe_v3_events"), exports);
@@ -0,0 +1,14 @@
1
+ export declare class UniswapV2AmmPoolEventsSubscriber {
2
+ private wsUrl;
3
+ private poolAddress;
4
+ private provider;
5
+ private poolContract;
6
+ private eventCallbacks;
7
+ private isConnected;
8
+ constructor(wsUrl: string, poolAddress: string);
9
+ initialize(): Promise<void>;
10
+ subscribe(eventType: string, callback: any): void;
11
+ unsubscribe(eventType: string): Promise<void>;
12
+ disconnect(): Promise<void>;
13
+ _reconnect(): void;
14
+ }
@@ -0,0 +1,174 @@
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.UniswapV2AmmPoolEventsSubscriber = void 0;
13
+ const ethers_1 = require("ethers");
14
+ const event_filter_1 = require("./event_filter");
15
+ const UNISWAP_V2_POOL_ABI = [
16
+ 'event Sync(uint112 reserve0, uint112 reserve1)',
17
+ 'event Mint(address indexed sender, uint amount0, uint amount1)',
18
+ 'event Burn(address indexed sender, uint amount0, uint amount1, address indexed to)'
19
+ ];
20
+ class UniswapV2AmmPoolEventsSubscriber {
21
+ constructor(wsUrl, poolAddress) {
22
+ this.wsUrl = wsUrl;
23
+ this.poolAddress = poolAddress;
24
+ this.provider = null;
25
+ this.poolContract = null;
26
+ this.eventCallbacks = {
27
+ sync: null,
28
+ mint: null,
29
+ burn: null
30
+ };
31
+ this.isConnected = false;
32
+ }
33
+ initialize() {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ try {
36
+ if (!this.wsUrl || !this.poolAddress) {
37
+ throw new Error(`wsUrl and poolAddress are required! wsUrl=${this.wsUrl} poolAddress=${this.poolAddress}`);
38
+ }
39
+ this.provider = new ethers_1.ethers.providers.WebSocketProvider(this.wsUrl);
40
+ this.poolContract = new ethers_1.ethers.Contract(this.poolAddress, UNISWAP_V2_POOL_ABI, this.provider);
41
+ this.isConnected = true;
42
+ console.log(`Connected to WebSocket: ${this.wsUrl} for pool: ${this.poolAddress}`);
43
+ this.provider._websocket.on('error', (error) => {
44
+ console.error('WebSocket Error:', error);
45
+ this.isConnected = false;
46
+ this._reconnect();
47
+ });
48
+ this.provider._websocket.on('close', (code, reason) => {
49
+ console.error('WebSocket Closed:', code, reason);
50
+ this.isConnected = false;
51
+ this._reconnect();
52
+ });
53
+ }
54
+ catch (error) {
55
+ console.error('Initialization Error:', error);
56
+ this.isConnected = false;
57
+ this._reconnect();
58
+ }
59
+ });
60
+ }
61
+ subscribe(eventType, callback) {
62
+ if (!this.poolContract) {
63
+ throw new Error('Contract not initialized. Call initialize() first.');
64
+ }
65
+ if (!['sync', 'mint', 'burn'].includes(eventType.toLowerCase())) {
66
+ throw new Error('Invalid event type. Use "sync", "mint", or "burn".');
67
+ }
68
+ this.eventCallbacks[eventType.toLowerCase()] = callback;
69
+ if (eventType.toLowerCase() === 'sync') {
70
+ this.poolContract.on('Sync', (reserve0, reserve1, event) => {
71
+ const data = {
72
+ pool_address: this.poolAddress,
73
+ type: 'sync',
74
+ event_time: Date.now(),
75
+ data: {
76
+ blockNumber: event.blockNumber,
77
+ transactionIndex: event.transactionIndex,
78
+ transactionHash: event.transactionHash,
79
+ amount0: '0',
80
+ amount1: '0',
81
+ reserve0: BigInt(reserve0.toString()),
82
+ reserve1: BigInt(reserve1.toString()),
83
+ tick: 0,
84
+ sqrtPriceX96: BigInt(0),
85
+ liquidity: BigInt(0),
86
+ }
87
+ };
88
+ callback(data);
89
+ });
90
+ }
91
+ else if (eventType.toLowerCase() === 'mint') {
92
+ this.poolContract.on('Mint', (sender, amount0, amount1, event) => {
93
+ const data = {
94
+ pool_address: this.poolAddress,
95
+ type: 'mint',
96
+ event_time: Date.now(),
97
+ data: {
98
+ blockNumber: event.blockNumber,
99
+ transactionIndex: event.transactionIndex,
100
+ transactionHash: event.transactionHash,
101
+ amount0: amount0.toString(),
102
+ amount1: amount1.toString(),
103
+ reserve0: BigInt(0),
104
+ reserve1: BigInt(0),
105
+ tick: 0,
106
+ sqrtPriceX96: BigInt(0),
107
+ liquidity: BigInt(0),
108
+ }
109
+ };
110
+ event_filter_1.EventFilter.filterEvent(this.poolAddress, event.blockNumber, 'mint', callback, data);
111
+ });
112
+ }
113
+ else if (eventType.toLowerCase() === 'burn') {
114
+ this.poolContract.on('Burn', (sender, amount0, amount1, to, event) => {
115
+ const data = {
116
+ pool_address: this.poolAddress,
117
+ type: 'burn',
118
+ event_time: Date.now(),
119
+ data: {
120
+ blockNumber: event.blockNumber,
121
+ transactionIndex: event.transactionIndex,
122
+ transactionHash: event.transactionHash,
123
+ amount0: amount0.toString(),
124
+ amount1: amount1.toString(),
125
+ reserve0: BigInt(0),
126
+ reserve1: BigInt(0),
127
+ tick: 0,
128
+ sqrtPriceX96: BigInt(0),
129
+ liquidity: BigInt(0),
130
+ }
131
+ };
132
+ event_filter_1.EventFilter.filterEvent(this.poolAddress, event.blockNumber, 'burn', callback, data);
133
+ });
134
+ }
135
+ }
136
+ unsubscribe(eventType) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ if (!this.poolContract) {
139
+ throw new Error('Contract not initialized.');
140
+ }
141
+ if (!['sync', 'mint', 'burn'].includes(eventType.toLowerCase())) {
142
+ throw new Error('Invalid event type. Use "sync", "mint", or "burn".');
143
+ }
144
+ this.poolContract.removeAllListeners(eventType);
145
+ this.eventCallbacks[eventType.toLowerCase()] = null;
146
+ console.log(`Unsubscribed from ${eventType} events`);
147
+ });
148
+ }
149
+ disconnect() {
150
+ return __awaiter(this, void 0, void 0, function* () {
151
+ if (this.provider) {
152
+ yield this.provider.destroy();
153
+ this.provider = null;
154
+ this.poolContract = null;
155
+ this.isConnected = false;
156
+ console.log('Disconnected from WebSocket');
157
+ }
158
+ });
159
+ }
160
+ _reconnect() {
161
+ if (!this.isConnected) {
162
+ console.log('Attempting to reconnect...');
163
+ setTimeout(() => __awaiter(this, void 0, void 0, function* () {
164
+ yield this.initialize();
165
+ for (const [eventType, callback] of Object.entries(this.eventCallbacks)) {
166
+ if (callback) {
167
+ this.subscribe(eventType, callback);
168
+ }
169
+ }
170
+ }), 2000);
171
+ }
172
+ }
173
+ }
174
+ exports.UniswapV2AmmPoolEventsSubscriber = UniswapV2AmmPoolEventsSubscriber;
@@ -0,0 +1,14 @@
1
+ export declare class UniswapV3PoolEventsSubscriber {
2
+ private wsUrl;
3
+ private poolAddress;
4
+ private provider;
5
+ private poolContract;
6
+ private eventCallbacks;
7
+ private isConnected;
8
+ constructor(wsUrl: string, poolAddress: string);
9
+ initialize(): Promise<void>;
10
+ subscribe(eventType: string, callback: any): void;
11
+ unsubscribe(eventType: string): Promise<void>;
12
+ disconnect(): Promise<void>;
13
+ _reconnect(): void;
14
+ }
@@ -0,0 +1,174 @@
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.UniswapV3PoolEventsSubscriber = void 0;
13
+ const ethers_1 = require("ethers");
14
+ const event_filter_1 = require("./event_filter");
15
+ const UNISWAP_V3_POOL_ABI = [
16
+ 'event Swap(address indexed sender, address indexed recipient, int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick)',
17
+ 'event Mint(address sender, address indexed owner, int24 indexed tickLower, int24 indexed tickUpper, uint128 amount, uint256 amount0, uint256 amount1)',
18
+ 'event Burn(address indexed owner, int24 indexed tickLower, int24 indexed tickUpper, uint128 amount, uint256 amount0, uint256 amount1)'
19
+ ];
20
+ class UniswapV3PoolEventsSubscriber {
21
+ constructor(wsUrl, poolAddress) {
22
+ this.wsUrl = wsUrl;
23
+ this.poolAddress = poolAddress;
24
+ this.provider = null;
25
+ this.poolContract = null;
26
+ this.eventCallbacks = {
27
+ swap: null,
28
+ mint: null,
29
+ burn: null
30
+ };
31
+ this.isConnected = false;
32
+ }
33
+ initialize() {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ try {
36
+ if (!this.wsUrl || !this.poolAddress) {
37
+ throw new Error(`wsUrl and poolAddress are required! wsUrl=${this.wsUrl} poolAddress=${this.poolAddress}`);
38
+ }
39
+ this.provider = new ethers_1.ethers.providers.WebSocketProvider(this.wsUrl);
40
+ this.poolContract = new ethers_1.ethers.Contract(this.poolAddress, UNISWAP_V3_POOL_ABI, this.provider);
41
+ this.isConnected = true;
42
+ console.log(`Connected to WebSocket: ${this.wsUrl} for pool: ${this.poolAddress}`);
43
+ this.provider._websocket.on('error', (error) => {
44
+ console.error('WebSocket Error:', error);
45
+ this.isConnected = false;
46
+ this._reconnect();
47
+ });
48
+ this.provider._websocket.on('close', (code, reason) => {
49
+ console.error('WebSocket Closed:', code, reason);
50
+ this.isConnected = false;
51
+ this._reconnect();
52
+ });
53
+ }
54
+ catch (error) {
55
+ console.error('Initialization Error:', error);
56
+ this.isConnected = false;
57
+ this._reconnect();
58
+ }
59
+ });
60
+ }
61
+ subscribe(eventType, callback) {
62
+ if (!this.poolContract) {
63
+ throw new Error('Contract not initialized. Call initialize() first.');
64
+ }
65
+ if (!['swap', 'mint', 'burn'].includes(eventType.toLowerCase())) {
66
+ throw new Error('Invalid event type. Use "swap", "mint", or "burn".');
67
+ }
68
+ this.eventCallbacks[eventType.toLowerCase()] = callback;
69
+ if (eventType.toLowerCase() === 'swap') {
70
+ this.poolContract.on('Swap', (sender, recipient, amount0, amount1, sqrtPriceX96, liquidity, tick, event) => {
71
+ const data = {
72
+ pool_address: this.poolAddress,
73
+ type: 'swap',
74
+ event_time: Date.now(),
75
+ data: {
76
+ blockNumber: event.blockNumber,
77
+ transactionIndex: event.transactionIndex,
78
+ transactionHash: event.transactionHash,
79
+ amount0: amount0.toString(),
80
+ amount1: amount1.toString(),
81
+ reserve0: BigInt(0),
82
+ reserve1: BigInt(0),
83
+ tick,
84
+ sqrtPriceX96: sqrtPriceX96.toString(),
85
+ liquidity: liquidity.toString(),
86
+ }
87
+ };
88
+ callback(data);
89
+ });
90
+ }
91
+ else if (eventType.toLowerCase() === 'mint') {
92
+ this.poolContract.on('Mint', (sender, owner, tickLower, tickUpper, amount, amount0, amount1, event) => {
93
+ const data = {
94
+ pool_address: this.poolAddress,
95
+ type: 'mint',
96
+ event_time: Date.now(),
97
+ data: {
98
+ blockNumber: event.blockNumber,
99
+ transactionIndex: event.transactionIndex,
100
+ transactionHash: event.transactionHash,
101
+ amount0: amount0.toString(),
102
+ amount1: amount1.toString(),
103
+ reserve0: BigInt(0),
104
+ reserve1: BigInt(0),
105
+ tick: 0,
106
+ sqrtPriceX96: BigInt(0),
107
+ liquidity: BigInt(0),
108
+ }
109
+ };
110
+ event_filter_1.EventFilter.filterEvent(this.poolAddress, event.blockNumber, 'mint', callback, data);
111
+ });
112
+ }
113
+ else if (eventType.toLowerCase() === 'burn') {
114
+ this.poolContract.on('Burn', (owner, tickLower, tickUpper, amount, amount0, amount1, event) => {
115
+ const data = {
116
+ pool_address: this.poolAddress,
117
+ type: 'burn',
118
+ event_time: Date.now(),
119
+ data: {
120
+ blockNumber: event.blockNumber,
121
+ transactionIndex: event.transactionIndex,
122
+ transactionHash: event.transactionHash,
123
+ amount0: amount0.toString(),
124
+ amount1: amount1.toString(),
125
+ reserve0: BigInt(0),
126
+ reserve1: BigInt(0),
127
+ tick: 0,
128
+ sqrtPriceX96: BigInt(0),
129
+ liquidity: BigInt(0),
130
+ }
131
+ };
132
+ event_filter_1.EventFilter.filterEvent(this.poolAddress, event.blockNumber, 'burn', callback, data);
133
+ });
134
+ }
135
+ }
136
+ unsubscribe(eventType) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ if (!this.poolContract) {
139
+ throw new Error('Contract not initialized.');
140
+ }
141
+ if (!['swap', 'mint', 'burn'].includes(eventType.toLowerCase())) {
142
+ throw new Error('Invalid event type. Use "swap", "mint", or "burn".');
143
+ }
144
+ this.poolContract.removeAllListeners(eventType);
145
+ this.eventCallbacks[eventType.toLowerCase()] = null;
146
+ console.log(`Unsubscribed from ${eventType} events`);
147
+ });
148
+ }
149
+ disconnect() {
150
+ return __awaiter(this, void 0, void 0, function* () {
151
+ if (this.provider) {
152
+ yield this.provider.destroy();
153
+ this.provider = null;
154
+ this.poolContract = null;
155
+ this.isConnected = false;
156
+ console.log('Disconnected from WebSocket');
157
+ }
158
+ });
159
+ }
160
+ _reconnect() {
161
+ if (!this.isConnected) {
162
+ console.log('Attempting to reconnect...');
163
+ setTimeout(() => __awaiter(this, void 0, void 0, function* () {
164
+ yield this.initialize();
165
+ for (const [eventType, callback] of Object.entries(this.eventCallbacks)) {
166
+ if (callback) {
167
+ this.subscribe(eventType, callback);
168
+ }
169
+ }
170
+ }), 2000);
171
+ }
172
+ }
173
+ }
174
+ exports.UniswapV3PoolEventsSubscriber = UniswapV3PoolEventsSubscriber;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-bsc-common",
3
- "version": "1.0.30",
3
+ "version": "1.0.31",
4
4
  "description": "BSC common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",