@clonegod/ttd-base-common 1.0.11 → 1.0.13
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,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;
|
|
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.UniswapV2AmmPoolEventsSubscriber = void 0;
|
|
13
13
|
const ethers_1 = require("ethers");
|
|
14
|
+
const event_filter_1 = require("./event_filter");
|
|
14
15
|
const UNISWAP_V2_POOL_ABI = [
|
|
15
16
|
'event Sync(uint112 reserve0, uint112 reserve1)',
|
|
16
17
|
'event Mint(address indexed sender, uint amount0, uint amount1)',
|
|
@@ -67,7 +68,7 @@ class UniswapV2AmmPoolEventsSubscriber {
|
|
|
67
68
|
this.eventCallbacks[eventType.toLowerCase()] = callback;
|
|
68
69
|
if (eventType.toLowerCase() === 'sync') {
|
|
69
70
|
this.poolContract.on('Sync', (reserve0, reserve1, event) => {
|
|
70
|
-
const
|
|
71
|
+
const event_data = {
|
|
71
72
|
pool_address: this.poolAddress,
|
|
72
73
|
type: 'sync',
|
|
73
74
|
event_time: Date.now(),
|
|
@@ -84,12 +85,12 @@ class UniswapV2AmmPoolEventsSubscriber {
|
|
|
84
85
|
liquidity: BigInt(0),
|
|
85
86
|
}
|
|
86
87
|
};
|
|
87
|
-
callback(
|
|
88
|
+
callback(event_data);
|
|
88
89
|
});
|
|
89
90
|
}
|
|
90
91
|
else if (eventType.toLowerCase() === 'mint') {
|
|
91
92
|
this.poolContract.on('Mint', (sender, amount0, amount1, event) => {
|
|
92
|
-
const
|
|
93
|
+
const event_data = {
|
|
93
94
|
pool_address: this.poolAddress,
|
|
94
95
|
type: 'mint',
|
|
95
96
|
event_time: Date.now(),
|
|
@@ -106,12 +107,12 @@ class UniswapV2AmmPoolEventsSubscriber {
|
|
|
106
107
|
liquidity: BigInt(0),
|
|
107
108
|
}
|
|
108
109
|
};
|
|
109
|
-
callback
|
|
110
|
+
event_filter_1.EventFilter.filterEvent(this.poolAddress, event.blockNumber, 'mint', callback, event_data);
|
|
110
111
|
});
|
|
111
112
|
}
|
|
112
113
|
else if (eventType.toLowerCase() === 'burn') {
|
|
113
114
|
this.poolContract.on('Burn', (sender, amount0, amount1, to, event) => {
|
|
114
|
-
const
|
|
115
|
+
const event_data = {
|
|
115
116
|
pool_address: this.poolAddress,
|
|
116
117
|
type: 'burn',
|
|
117
118
|
event_time: Date.now(),
|
|
@@ -128,7 +129,7 @@ class UniswapV2AmmPoolEventsSubscriber {
|
|
|
128
129
|
liquidity: BigInt(0),
|
|
129
130
|
}
|
|
130
131
|
};
|
|
131
|
-
callback
|
|
132
|
+
event_filter_1.EventFilter.filterEvent(this.poolAddress, event.blockNumber, 'burn', callback, event_data);
|
|
132
133
|
});
|
|
133
134
|
}
|
|
134
135
|
}
|
|
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.UniswapV3PoolEventsSubscriber = void 0;
|
|
13
13
|
const ethers_1 = require("ethers");
|
|
14
|
+
const event_filter_1 = require("./event_filter");
|
|
14
15
|
const UNISWAP_V3_POOL_ABI = [
|
|
15
16
|
'event Swap(address indexed sender, address indexed recipient, int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick)',
|
|
16
17
|
'event Mint(address sender, address indexed owner, int24 indexed tickLower, int24 indexed tickUpper, uint128 amount, uint256 amount0, uint256 amount1)',
|
|
@@ -67,7 +68,7 @@ class UniswapV3PoolEventsSubscriber {
|
|
|
67
68
|
this.eventCallbacks[eventType.toLowerCase()] = callback;
|
|
68
69
|
if (eventType.toLowerCase() === 'swap') {
|
|
69
70
|
this.poolContract.on('Swap', (sender, recipient, amount0, amount1, sqrtPriceX96, liquidity, tick, event) => {
|
|
70
|
-
const
|
|
71
|
+
const event_data = {
|
|
71
72
|
pool_address: this.poolAddress,
|
|
72
73
|
type: 'swap',
|
|
73
74
|
event_time: Date.now(),
|
|
@@ -84,12 +85,12 @@ class UniswapV3PoolEventsSubscriber {
|
|
|
84
85
|
liquidity: liquidity.toString(),
|
|
85
86
|
}
|
|
86
87
|
};
|
|
87
|
-
callback(
|
|
88
|
+
callback(event_data);
|
|
88
89
|
});
|
|
89
90
|
}
|
|
90
91
|
else if (eventType.toLowerCase() === 'mint') {
|
|
91
92
|
this.poolContract.on('Mint', (sender, owner, tickLower, tickUpper, amount, amount0, amount1, event) => {
|
|
92
|
-
const
|
|
93
|
+
const event_data = {
|
|
93
94
|
pool_address: this.poolAddress,
|
|
94
95
|
type: 'mint',
|
|
95
96
|
event_time: Date.now(),
|
|
@@ -106,12 +107,12 @@ class UniswapV3PoolEventsSubscriber {
|
|
|
106
107
|
liquidity: BigInt(0),
|
|
107
108
|
}
|
|
108
109
|
};
|
|
109
|
-
callback
|
|
110
|
+
event_filter_1.EventFilter.filterEvent(this.poolAddress, event.blockNumber, 'mint', callback, event_data);
|
|
110
111
|
});
|
|
111
112
|
}
|
|
112
113
|
else if (eventType.toLowerCase() === 'burn') {
|
|
113
114
|
this.poolContract.on('Burn', (owner, tickLower, tickUpper, amount, amount0, amount1, event) => {
|
|
114
|
-
const
|
|
115
|
+
const event_data = {
|
|
115
116
|
pool_address: this.poolAddress,
|
|
116
117
|
type: 'burn',
|
|
117
118
|
event_time: Date.now(),
|
|
@@ -128,7 +129,7 @@ class UniswapV3PoolEventsSubscriber {
|
|
|
128
129
|
liquidity: BigInt(0),
|
|
129
130
|
}
|
|
130
131
|
};
|
|
131
|
-
callback
|
|
132
|
+
event_filter_1.EventFilter.filterEvent(this.poolAddress, event.blockNumber, 'burn', callback, event_data);
|
|
132
133
|
});
|
|
133
134
|
}
|
|
134
135
|
}
|