-
Notifications
You must be signed in to change notification settings - Fork 16
/
monitorNewTokens.ts
126 lines (109 loc) · 4.13 KB
/
monitorNewTokens.ts
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
import { rayFee, solanaConnection } from './constants';
import { storeData } from './utils';
import fs from 'fs';
import chalk from 'chalk';
import path from 'path';
import { Connection } from '@solana/web3.js';
import { MAINNET_PROGRAM_ID } from '@raydium-io/raydium-sdk';
const dataPath = path.join(__dirname, 'data', 'new_solana_tokens.json');
async function monitorNewTokens(connection: Connection) {
console.log(chalk.green(`monitoring new solana tokens...`));
try {
connection.onLogs(
rayFee,
async ({ logs, err, signature }) => {
try {
if (err) {
console.error(`connection contains error, ${err}`);
return;
}
console.log(chalk.bgGreen(`found new token signature: ${signature}`));
let signer = '';
let baseAddress = '';
let baseDecimals = 0;
let baseLpAmount = 0;
let quoteAddress = '';
let quoteDecimals = 0;
let quoteLpAmount = 0;
/**You need to use a RPC provider for getparsedtransaction to work properly.
* Check README.md for suggestions.
*/
const parsedTransaction = await connection.getParsedTransaction(
signature,
{
maxSupportedTransactionVersion: 0,
commitment: 'confirmed',
}
);
if (parsedTransaction && parsedTransaction?.meta.err == null) {
console.log(`successfully parsed transaction`);
signer =
parsedTransaction?.transaction.message.accountKeys[0].pubkey.toString();
console.log(`creator, ${signer}`);
const postTokenBalances = parsedTransaction?.meta.postTokenBalances;
const baseInfo = postTokenBalances?.find(
(balance) =>
balance.owner ===
'5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1' &&
balance.mint !== 'So11111111111111111111111111111111111111112'
);
if (baseInfo) {
baseAddress = baseInfo.mint;
baseDecimals = baseInfo.uiTokenAmount.decimals;
baseLpAmount = baseInfo.uiTokenAmount.uiAmount;
}
const quoteInfo = postTokenBalances.find(
(balance) =>
balance.owner ==
'5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1' &&
balance.mint == 'So11111111111111111111111111111111111111112'
);
if (quoteInfo) {
quoteAddress = quoteInfo.mint;
quoteDecimals = quoteInfo.uiTokenAmount.decimals;
quoteLpAmount = quoteInfo.uiTokenAmount.uiAmount;
}
}
const newTokenData = {
lpSignature: signature,
creator: signer,
timestamp: new Date().toISOString(),
baseInfo: {
baseAddress,
baseDecimals,
baseLpAmount,
},
quoteInfo: {
quoteAddress: quoteAddress,
quoteDecimals: quoteDecimals,
quoteLpAmount: quoteLpAmount,
},
logs: logs,
};
//store new tokens data in data folder
await storeData(dataPath, newTokenData);
} catch (error) {
const errorMessage = `error occured in new solana token log callback function, ${JSON.stringify(error, null, 2)}`;
console.log(chalk.red(errorMessage));
// Save error logs to a separate file
fs.appendFile(
'errorNewLpsLogs.txt',
`${errorMessage}\n`,
function (err) {
if (err) console.log('error writing errorlogs.txt', err);
}
);
}
},
'confirmed'
);
} catch (error) {
const errorMessage = `error occured in new sol lp monitor, ${JSON.stringify(error, null, 2)}`;
console.log(chalk.red(errorMessage));
// Save error logs to a separate file
fs.appendFile('errorNewLpsLogs.txt', `${errorMessage}\n`, function (err) {
if (err) console.log('error writing errorlogs.txt', err);
});
}
}
monitorNewTokens(solanaConnection);