-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotify.ts
337 lines (314 loc) · 10.4 KB
/
notify.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
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
import lo from "lodash";
import { struct, u8, u64, publicKey } from "@raydium-io/raydium-sdk";
import { PublicKey } from "@solana/web3.js";
import axios from "axios";
import { Metaplex } from "@metaplex-foundation/js";
import { Raydium, RaydiumAuthority, connection } from "./config/config";
import { IPairInfo } from "./utils/types";
const LOG_TYPE = struct([u8("log_type")]);
const RAY_IX_TYPE = {
CREATE_POOL: 0,
ADD_LIQUIDITY: 1,
BURN_LIQUIDITY: 2,
SWAP: 3,
};
const INIT_LOG = struct([
u8("log_type"),
u64("time"),
u8("pc_decimals"),
u8("coin_decimals"),
u64("pc_lot_size"),
u64("coin_lot_size"),
u64("pc_amount"),
u64("coin_amount"),
publicKey("market"),
]);
const ACTION_TYPE = {
DEPOSIT: "deposit",
WITHDRAW: "withdraw",
MINT: "mint",
BURN: "burn",
CREATE: "create",
DEFAULT: "unknown",
};
const parseCreateTransaction = async (input_data: any, sig: string) => {
try {
const tx = await connection.getParsedTransaction(sig, {
commitment: "confirmed",
maxSupportedTransactionVersion: 2,
});
// if (tx) {
const feePayer = tx?.transaction.message.accountKeys[0].pubkey.toBase58();
const ixs = tx?.transaction.message.instructions;
let ix_index = -1;
if (ixs?.length) {
for (let i = 0; i < ixs.length; i++) {
if (ixs[i].programId.toBase58() == Raydium.toBase58()) {
ix_index = i;
break;
}
}
const inner_ixs = tx?.meta?.innerInstructions;
if (ix_index == -1) {
console.error(`Could not parse activity from ix in ${sig}`);
return [
{
user: feePayer,
signature: sig,
},
];
}
let result: any[] = [];
inner_ixs
?.filter((inner_ixs) => inner_ixs.index === ix_index)[0]
.instructions.slice(-3)
.map((inn_ix) => {
let bIsSending = ACTION_TYPE.DEFAULT;
// @ts-ignore
const owner = inn_ix.parsed?.info?.authority;
// @ts-ignore
if (owner === feePayer || inn_ix.parsed?.type === "mintTo")
bIsSending = ACTION_TYPE.DEPOSIT;
else if (
owner === RaydiumAuthority.toBase58() ||
// @ts-ignore
inn_ix.parsed?.type == "mintTo"
)
bIsSending = ACTION_TYPE.MINT;
result.push({
mode: bIsSending,
user: feePayer,
// @ts-ignore
amount: inn_ix.parsed?.info?.amount,
mintOrAta:
// @ts-ignore
inn_ix.parsed?.info?.mint || inn_ix.parsed?.info?.destination,
signature: sig,
});
});
return result;
}
// }
} catch (error) {
console.error(error);
return [
{
signature: sig,
},
];
}
}
const fetchTokenInfo = async (info: any[]) => {
let result = info;
if (!info[0].mintOrAta) return result;
let data: IPairInfo | undefined;
try {
data = await getTokenAddress(
result.map((r) => new PublicKey(r.mintOrAta))
);
} catch (err) {
console.error("Getting token address occur errors");
return;
}
for (let i = 0; i < result.length; i++) {
if (data) result[i].token = data[result[i].mintOrAta];
}
return result;
}
const getTokenAddress = async (accounts: PublicKey[]) => {
try {
// Get the pool account info
let poolAccountInfo = await connection.getMultipleParsedAccounts(accounts);
let mintInfo: IPairInfo = {};
poolAccountInfo.value.map((info, idx) => {
const account = accounts[idx].toBase58();
// @ts-ignore
if (info?.data.space === 165) {
// @ts-ignore
const mint = info.data.parsed.info?.mint;
mintInfo[account] = {
address: mint,
// @ts-ignore
decimals: info.data.parsed?.info?.tokenAmount?.decimals,
};
// @ts-ignore
} else if (info?.data.space === 82) {
mintInfo[account] = {
address: account,
// @ts-ignore
decimals: info.data.parsed?.info?.decimals,
};
}
});
// Retrieve the token address from the token account data
return await getMetadata(mintInfo);
} catch (error) {
console.error("Error:", error);
}
};
const getMetadata = async (tokenInfos: IPairInfo) => {
const metaplex = Metaplex.make(connection);
let tokens: any[] = await metaplex.nfts().findAllByMintList({
mints: Object.values(tokenInfos).map((info: any) => new PublicKey(info.address)),
});
for (let i = 0; i < tokens.length; i++) {
if (!tokens[i]?.uri) {
const tokenInfo = Object.values(tokenInfos)
const addr = tokenInfo[i].address
const offTokenMetaAPI = `https://token-list-api.solana.cloud/v1/search?query=${addr}&start=0&limit=1&chainId=101`
const res = await axios
.get(offTokenMetaAPI)
.then((res) => res.data)
.catch((e) => {
console.error(
`Could not get token meta for ${addr}`
);
return {
content: [],
};
});
if (res.content.length > 0)
tokens[i] = {
mintAddress: new PublicKey(addr),
name: res.content[0].name,
symbol: res.content[0].symbol,
uri: res.content[0].logoURI,
image: res.content[0].logoURI,
};
else
tokens[i] = {
mintAddress: new PublicKey(addr),
name: "Unregistered Token",
symbol: addr,
uri: "",
};
} else if (!tokens[i]?.image) {
// TODO: fetch uri json and read image url.
const response = await axios.get(tokens[i]?.uri);
tokens[i].image = response.data.image;
tokens[i].description = response.data.description;
}
}
let result = tokenInfos;
tokens.filter((info) => info !== null)
.map((info: any) => {
const mint = info?.mintAddress.toBase58();
const idx = Object.values(result)
// @ts-ignore
.map((info) => info.address)
.indexOf(mint);
const account = Object.keys(result)[idx];
result[account] = {
...result[account],
name: info.name,
symbol: info.symbol,
uri: info.uri,
image: info.json?.image || info.image,
desc: info.json?.description || info?.description,
};
});
return result;
};
const parseAmountWithDecimal = async (strAmount: any, decimals: any) => {
const amount = parseFloat(strAmount) / 10 ** decimals;
return amount.toString();
}
const getData = async (msg: any[] | undefined, type: number, poolId: PublicKey) => {
if (!msg || !msg[1] || !msg[0] || !msg[1].token || !msg[0].token) return;
if (msg[0].token) {
let vaultAmount;
try {
vaultAmount = await connection.getTokenAccountBalance(new PublicKey(msg[1].mintOrAta));
} catch (err) {
console.error("Getting vaultAmount occur errors");
return;
}
let initMint;
try {
initMint = await connection.getParsedAccountInfo(new PublicKey(msg[0].token.address));
} catch (err) {
console.error("Getting initMint occur errors");
return;
}
// @ts-ignore
const initMintToken = initMint.value?.data.parsed.info;
const pairName = `${msg[0].token.symbol} / ${msg[1].token.symbol}`; //
const mintAuthority = initMintToken.mintAuthority //
const freezeAuthority = initMintToken.freezeAuthority //
const description = msg[0].token.desc; //
const links = {
Transaction: `https://solscan.io/tx/${msg[0].signature}`,
// Birdeye: `https://birdeye.so/token/${msg[0].token.address}/${msg[1].token.address}?chain=solana`,
}; //
const image = msg[0].token.image
const poolCreateAAmount = await parseAmountWithDecimal(msg[0].amount, msg[0].token.decimals) + ` ${msg[0].token.symbol}`; //
const poolCreateBAmount = await parseAmountWithDecimal(msg[1].amount, msg[1].token.decimals) + ` ${msg[1].token.symbol}`;//
return { pairName, mintAuthority, freezeAuthority, description, links, image, poolCreateAAmount, poolCreateBAmount, poolId }
}
}
export const trackNewPool = async () => {
connection.onLogs(
Raydium,
async (x) => {
try {
const log = x.logs;
const signature = x.signature;
const error = x.err;
const ray_log_row = lo.find(log, (y) => y.includes("ray_log"));
if (!error && ray_log_row) {
try {
const match = ray_log_row.match(/ray_log: (.*)/)
if (match?.length) {
const ray_data = Buffer.from(
match[1],
"base64"
);
const log_type = LOG_TYPE.decode(ray_data).log_type;
if (log_type == RAY_IX_TYPE.CREATE_POOL) {
const tx = await connection.getParsedTransaction(signature, {
maxSupportedTransactionVersion: 0,
});
if (tx) {
const instructions = tx.transaction.message.instructions;
const raydiumInstruction = instructions.find((instruction) => { return instruction.programId.toString() == "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8" });
if (raydiumInstruction) {
if ('accounts' in raydiumInstruction) {
const poolId = raydiumInstruction.accounts[4];
const ray_input = INIT_LOG.decode(ray_data);
let info: any[] | undefined = [];
try {
info = await parseCreateTransaction(ray_input, signature);
} catch (error) {
return;
}
let res
let result
if (info && info?.length) {
res = info.map((data) => ({ ...data, token: undefined }));
try {
result = await fetchTokenInfo(res);
}
catch (error) {
console.error("fetchTokenInfo error", error);
return;
}
// send data using socket connection
const returnValue = await getData(result, RAY_IX_TYPE.CREATE_POOL, poolId)
console.log(returnValue)
}
}
}
}
}
}
} catch (ex) {
console.error(ex);
}
}
} catch (ex) {
console.error(ex);
}
},
"confirmed"
);
}