-
Notifications
You must be signed in to change notification settings - Fork 0
/
poolMonitor.js
430 lines (383 loc) · 15.7 KB
/
poolMonitor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// poolMonitor.js
//import { ethers } from 'ethers';
const { ethers, formatUnits, parseEther, JsonRpcProvider } = require('ethers');
const pool = require('./db.js');
const notifier = require('./notifier.js');
class PoolMonitor {
constructor(poolConfig, provider, thresholds) {
this.poolAddress = poolConfig.address;
this.provider = provider;
this.priceTokenPreference = poolConfig.priceToken; // "token0" or "token1"
this.priceChangeThreshold = thresholds.priceChange;
this.liquidityChangeThreshold = thresholds.liquidityChange;
this.priceAlertSent = false;
this.liquidityAlertSent = false;
this.lastPriceAlertTime = 0;
this.lastLiquidityAlertTime = 0;
this.poolAbi = [
'function getReserves() view returns (uint256 _reserve0, uint256 _reserve1, uint256 _blockTimestampLast)',
'function token0() view returns (address)',
'function token1() view returns (address)',
'event Swap(address indexed sender, address indexed to, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out)',
];
this.erc20Abi = [
'function decimals() view returns (uint8)',
'function symbol() view returns (string)',
];
this.poolContract = new ethers.Contract(this.poolAddress, this.poolAbi, provider);
this.token0 = null;
this.token1 = null;
}
async init() {
await this.getTokenDetails();
}
async getTokenDetails() {
const token0Address = await this.poolContract.token0();
const token1Address = await this.poolContract.token1();
const token0Contract = new ethers.Contract(token0Address, this.erc20Abi, this.provider);
const token1Contract = new ethers.Contract(token1Address, this.erc20Abi, this.provider);
const [decimals0, decimals1, symbol0, symbol1] = await Promise.all([
token0Contract.decimals(),
token1Contract.decimals(),
token0Contract.symbol(),
token1Contract.symbol(),
]);
this.token0 = {
address: token0Address,
decimals: decimals0,
symbol: symbol0,
contract: token0Contract,
};
this.token1 = {
address: token1Address,
decimals: decimals1,
symbol: symbol1,
contract: token1Contract,
};
}
async insertHistoricalData(data) {
try {
const query = `
INSERT INTO historical_data (
pool_address, timestamp, token0_symbol, token1_symbol,
price, price_change, liquidity, liquidity_change
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
`;
const values = [
this.poolAddress,
data.timestamp,
this.token0.symbol,
this.token1.symbol,
data.price,
data.priceChange,
data.liquidity,
data.liquidityChange,
];
await pool.query(query, values);
} catch (error) {
console.error('Error inserting historical data:', error);
}
}
async getMaxChanges(currentTime, currentPrice, currentLiquidity) {
try {
const oneDayAgo = currentTime - 24 * 60 * 60 * 1000;
// For price changes
const priceQuery = `
SELECT
((($1 - price) / price) * 100) AS price_change,
timestamp
FROM historical_data
WHERE pool_address = $2 AND timestamp BETWEEN $3 AND $4
ORDER BY price_change DESC
LIMIT 1
`;
const priceDecreaseQuery = `
SELECT
((($1 - price) / price) * 100) AS price_change,
timestamp
FROM historical_data
WHERE pool_address = $2 AND timestamp BETWEEN $3 AND $4
ORDER BY price_change ASC
LIMIT 1
`;
// For liquidity changes
const liquidityQuery = `
SELECT
((($1 - liquidity) / liquidity) * 100) AS liquidity_change,
timestamp
FROM historical_data
WHERE pool_address = $2 AND timestamp BETWEEN $3 AND $4
ORDER BY liquidity_change DESC
LIMIT 1
`;
const liquidityDecreaseQuery = `
SELECT
((($1 - liquidity) / liquidity) * 100) AS liquidity_change,
timestamp
FROM historical_data
WHERE pool_address = $2 AND timestamp BETWEEN $3 AND $4
ORDER BY liquidity_change ASC
LIMIT 1
`;
const priceValues = [currentPrice, this.poolAddress, oneDayAgo, currentTime];
const liquidityValues = [currentLiquidity, this.poolAddress, oneDayAgo, currentTime];
const [maxPriceIncreaseRes, maxPriceDecreaseRes, maxLiquidityIncreaseRes, maxLiquidityDecreaseRes] = await Promise.all([
pool.query(priceQuery, priceValues),
pool.query(priceDecreaseQuery, priceValues),
pool.query(liquidityQuery, liquidityValues),
pool.query(liquidityDecreaseQuery, liquidityValues),
]);
return {
max_price_increase: maxPriceIncreaseRes.rows[0]?.price_change || 0,
max_price_increase_timestamp: maxPriceIncreaseRes.rows[0]?.timestamp || null,
max_price_decrease: maxPriceDecreaseRes.rows[0]?.price_change || 0,
max_price_decrease_timestamp: maxPriceDecreaseRes.rows[0]?.timestamp || null,
max_liquidity_increase: maxLiquidityIncreaseRes.rows[0]?.liquidity_change || 0,
max_liquidity_increase_timestamp: maxLiquidityIncreaseRes.rows[0]?.timestamp || null,
max_liquidity_decrease: maxLiquidityDecreaseRes.rows[0]?.liquidity_change || 0,
max_liquidity_decrease_timestamp: maxLiquidityDecreaseRes.rows[0]?.timestamp || null,
};
} catch (error) {
console.error('Error fetching maximum changes:', error);
return null;
}
}
async getDataPast24Hours(currentTime) {
try {
const oneDayAgo = currentTime - 24 * 60 * 60 * 1000;
const query = `
SELECT *
FROM historical_data
WHERE pool_address = $1 AND timestamp BETWEEN $2 AND $3
`;
const values = [
this.poolAddress,
oneDayAgo,
currentTime,
];
const res = await pool.query(query, values);
return res.rows;
} catch (error) {
console.error('Error fetching historical data:', error);
return [];
}
}
async deleteOldData(currentTime) {
try {
const oneDayAgo = currentTime - 24 * 60 * 60 * 1000;
const query = `
DELETE FROM historical_data
WHERE pool_address = $1 AND timestamp < $2
`;
await pool.query(query, [this.poolAddress, oneDayAgo]);
} catch (error) {
console.error('Error deleting old data:', error);
}
}
calculatePercentageChange(currentValue, pastValue) {
return ((currentValue - pastValue) / pastValue) * 100;
}
async getPoolData() {
try {
const [_reserve0, _reserve1] = await this.poolContract.getReserves();
const token0Reserve = parseFloat(
ethers.formatUnits(_reserve0, this.token0.decimals)
);
const token1Reserve = parseFloat(
ethers.formatUnits(_reserve1, this.token1.decimals)
);
let price;
let baseTokenSymbol;
let quoteTokenSymbol;
// Determine which token to use as the base and quote
if (this.priceTokenPreference === 'token0') {
// Price of token0 in terms of token1 (token1 per token0)
price = token1Reserve / token0Reserve;
baseTokenSymbol = this.token0.symbol;
quoteTokenSymbol = this.token1.symbol;
} else if (this.priceTokenPreference === 'token1') {
// Price of token1 in terms of token0 (token0 per token1)
price = token0Reserve / token1Reserve;
baseTokenSymbol = this.token1.symbol;
quoteTokenSymbol = this.token0.symbol;
} else {
throw new Error(`Invalid priceTokenPreference: ${this.priceTokenPreference}`);
}
const liquidity = token0Reserve + token1Reserve;
const currentTime = Date.now();
// Get maximum changes and timestamps from the database
const maxChanges = await this.getMaxChanges(currentTime, price, liquidity);
let maxPriceChange = 0;
let maxPriceChangeTimestamp = null;
let priceChangeDirection = '';
let maxLiquidityChange = 0;
let maxLiquidityChangeTimestamp = null;
let liquidityChangeDirection = '';
if (maxChanges) {
// Extract data
const maxPriceIncrease = parseFloat(maxChanges.max_price_increase) || 0;
const maxPriceIncreaseTimestamp = parseInt(maxChanges.max_price_increase_timestamp) || null;
const maxPriceDecrease = parseFloat(maxChanges.max_price_decrease) || 0;
const maxPriceDecreaseTimestamp = parseInt(maxChanges.max_price_decrease_timestamp) || null;
const maxLiquidityIncrease = parseFloat(maxChanges.max_liquidity_increase) || 0;
const maxLiquidityIncreaseTimestamp = parseInt(maxChanges.max_liquidity_increase_timestamp) || null;
const maxLiquidityDecrease = parseFloat(maxChanges.max_liquidity_decrease) || 0;
const maxLiquidityDecreaseTimestamp = parseInt(maxChanges.max_liquidity_decrease_timestamp) || null;
// Determine maximum price change and direction
if (Math.abs(maxPriceIncrease) >= Math.abs(maxPriceDecrease)) {
maxPriceChange = maxPriceIncrease;
maxPriceChangeTimestamp = maxPriceIncreaseTimestamp;
priceChangeDirection = 'increased';
} else {
maxPriceChange = maxPriceDecrease;
maxPriceChangeTimestamp = maxPriceDecreaseTimestamp;
priceChangeDirection = 'decreased';
}
// Determine maximum liquidity change and direction
if (Math.abs(maxLiquidityIncrease) >= Math.abs(maxLiquidityDecrease)) {
maxLiquidityChange = maxLiquidityIncrease;
maxLiquidityChangeTimestamp = maxLiquidityIncreaseTimestamp;
liquidityChangeDirection = 'increased';
} else {
maxLiquidityChange = maxLiquidityDecrease;
maxLiquidityChangeTimestamp = maxLiquidityDecreaseTimestamp;
liquidityChangeDirection = 'decreased';
}
}
// Insert current data into the database
await this.insertHistoricalData({
timestamp: currentTime,
price,
priceChange: null, // Not applicable here
liquidity,
liquidityChange: null, // Not applicable here
baseTokenSymbol,
quoteTokenSymbol,
});
// Delete old data
await this.deleteOldData(currentTime);
// Define ANSI color codes
const RESET = '\x1b[0m';
const BOLD = '\x1b[1m';
const FG_RED = '\x1b[31m';
const FG_GREEN = '\x1b[32m';
const FG_YELLOW = '\x1b[33m';
const FG_BLUE = '\x1b[34m';
// Output current data with colors
console.log(`${BOLD}\nPool: ${this.poolAddress}${RESET}`);
console.log(`${FG_BLUE}Token0 (${this.token0.symbol}) Reserve: ${token0Reserve}${RESET}`);
console.log(`${FG_BLUE}Token1 (${this.token1.symbol}) Reserve: ${token1Reserve}${RESET}`);
console.log(`${FG_GREEN}Price (${quoteTokenSymbol} per ${baseTokenSymbol}): ${price}${RESET}`);
// Output maximum percentage changes
console.log(`${FG_YELLOW}Maximum price change over the past 24 hours: ${maxPriceChange.toFixed(2)}% (${priceChangeDirection})${RESET}`);
if (maxPriceChangeTimestamp) {
console.log(`Occurred at: ${new Date(maxPriceChangeTimestamp).toLocaleString()}`);
}
console.log(`${FG_YELLOW}Maximum liquidity change over the past 24 hours: ${maxLiquidityChange.toFixed(2)}% (${liquidityChangeDirection})${RESET}`);
if (maxLiquidityChangeTimestamp) {
console.log(`Occurred at: ${new Date(maxLiquidityChangeTimestamp).toLocaleString()}`);
}
const THREE_HOURS = 3 * 60 * 60 * 1000;
// Check if maximum changes exceed thresholds and send alerts
if (Math.abs(maxPriceChange) >= this.priceChangeThreshold) {
const currentTime = Date.now();
if (
!this.priceAlertSent ||
(currentTime - this.lastPriceAlertTime) >= THREE_HOURS
) {
const alertMessage = `⚠️ *Price Alert*\n` +
`*Pool*: ${this.token0.symbol}/${this.token1.symbol}\n` +
`*Price has ${priceChangeDirection} by*: *${Math.abs(maxPriceChange).toFixed(2)}%*\n` +
`*Threshold*: ${this.priceChangeThreshold}%\n` +
`*Current Price* (${quoteTokenSymbol} per ${baseTokenSymbol}): ${price}\n` +
`*Time*: ${new Date().toLocaleString()}`;
console.log(`${FG_RED}${alertMessage}${RESET}`);
await notifier.sendTelegramMessage(alertMessage);
this.priceAlertSent = true;
this.lastPriceAlertTime = currentTime;
}
} else {
if (this.priceAlertSent) {
const alertMessage = `ℹ️ *Price Update*\n` +
`Price change is back below threshold for pool ${this.token0.symbol}/${this.token1.symbol}\n` +
`*Current Price* (${quoteTokenSymbol} per ${baseTokenSymbol}): ${price}\n` +
`*Time*: ${new Date().toLocaleString()}`;
console.log(`${FG_GREEN}${alertMessage}${RESET}`);
// Optionally send a notification when the condition returns to normal
await notifier.sendTelegramMessage(alertMessage);
this.priceAlertSent = false;
this.lastPriceAlertTime = 0;
}
}
if (Math.abs(maxLiquidityChange) >= this.liquidityChangeThreshold) {
const currentTime = Date.now();
if (
!this.liquidityAlertSent ||
(currentTime - this.lastLiquidityAlertTime) >= THREE_HOURS
) {
const alertMessage = `⚠️ *Liquidity Alert*\n` +
`*Pool*: ${this.token0.symbol}/${this.token1.symbol}\n` +
`*Liquidity has ${liquidityChangeDirection} by*: *${Math.abs(maxLiquidityChange).toFixed(2)}%*\n` +
`*Threshold*: ${this.liquidityChangeThreshold}%\n` +
`*Current Liquidity*: ${liquidity}\n` +
`*Time*: ${new Date().toLocaleString()}`;
console.log(`${FG_RED}${alertMessage}${RESET}`);
await notifier.sendTelegramMessage(alertMessage);
this.liquidityAlertSent = true;
this.lastLiquidityAlertTime = currentTime;
}
} else {
if (this.liquidityAlertSent) {
const alertMessage = `ℹ️ *Liquidity Update*\n` +
`Liquidity change is back below threshold for pool ${this.token0.symbol}/${this.token1.symbol}\n` +
`*Current Liquidity*: ${liquidity}\n` +
`*Time*: ${new Date().toLocaleString()}`;
console.log(`${FG_GREEN}${alertMessage}${RESET}`);
// Optionally send a notification when the condition returns to normal
await notifier.sendTelegramMessage(alertMessage);
this.liquidityAlertSent = false;
this.lastLiquidityAlertTime = 0;
}
}
} catch (error) {
console.error(`Error fetching data for pool ${this.poolAddress}:`, error);
}
}
// Optional: Listen to Swap events
listenToEvents() {
this.poolContract.on(
'Swap',
async (sender, to, amount0In, amount1In, amount0Out, amount1Out, event) => {
console.log(`Swap event in pool ${this.poolAddress}:`);
console.log(`Sender: ${sender}`);
console.log(`To: ${to}`);
console.log(
`Amount ${this.token0.symbol} In: ${ethers.formatUnits(
amount0In,
this.token0.decimals
)}`
);
console.log(
`Amount ${this.token1.symbol} In: ${ethers.formatUnits(
amount1In,
this.token1.decimals
)}`
);
console.log(
`Amount ${this.token0.symbol} Out: ${ethers.formatUnits(
amount0Out,
this.token0.decimals
)}`
);
console.log(
`Amount ${this.token1.symbol} Out: ${ethers.formatUnits(
amount1Out,
this.token1.decimals
)}`
);
}
);
}
}
module.exports = PoolMonitor;