-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
142 lines (124 loc) · 6.33 KB
/
index.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
import { getAssociatedTokenAddressSync } from "@solana/spl-token";
import { Connection, ParsedTransactionWithMeta, PublicKey } from "@solana/web3.js";
import axios from "axios";
import dotenv from 'dotenv'
import fs from 'fs'
dotenv.config()
const solanaConnection = new Connection(process.env.RPC_URL!, { wsEndpoint: process.env.WSS_URL! });
enum Filter {
Receive,
Send,
Both
}
const dexscreenapi = 'https://api.dexscreener.com/latest/dex/tokens/'
const getHistory = async (pubkey: PublicKey, mint: PublicKey, filter: Filter) => {
const history_result: Array<{
signature: string,
from: string,
to: string,
date: string,
amount: string,
symbol: string
type: string
}> = []
const mintData = await solanaConnection.getParsedAccountInfo(mint)
// @ts-ignore
const decimals: number = mintData.value?.data.parsed.info.decimals
const ata = getAssociatedTokenAddressSync(mint, pubkey)
const history = await solanaConnection.getSignaturesForAddress(ata)
const symbol = await getSymbol(mint)
for (let i = 0; i < history.length; i++) {
if (history[i].err == null) {
const decoded = await solanaConnection.getParsedTransaction(history[i].signature, {
maxSupportedTransactionVersion: 0,
commitment: 'confirmed'
})
if (decoded) {
const data = await getInfo(decoded, pubkey, ata, solanaConnection, decimals, filter)
if (data) {
history_result.push({ signature: history[i].signature, from: data.from, to: data.to, date: formatDate(history[i].blockTime! * 1000), amount: data.amount, symbol, type: data.type })
}
}
}
}
fs.writeFileSync('history.json', JSON.stringify(history_result, null, 4))
}
const getInfo = async (decoded: ParsedTransactionWithMeta, pubkey: PublicKey, ata: PublicKey, solanaConnection: Connection, decimals: number, filter: Filter) => {
if (decoded?.meta?.logMessages?.toString().includes('TransferChecked')) {
const inx = decoded.transaction.message?.instructions
for (const item of inx) {
// @ts-ignore
if (item.parsed && item.parsed.type == 'transferChecked') {
// @ts-ignore
const { destination, source, tokenAmount } = item.parsed.info
const amount = tokenAmount.uiAmount
if (destination.toString() == ata.toString() && (filter == Filter.Receive || filter == Filter.Both)) {
const sourceAcc = await solanaConnection.getParsedAccountInfo(new PublicKey(source))
// @ts-ignore
const owner = sourceAcc.value?.data.parsed.info.owner
if (owner) {
// type: 'receive'
return { type: 'receive', from: owner, to: pubkey.toString(), amount }
}
else return undefined
} else if (source.toString() == ata.toString() && (filter == Filter.Send || filter == Filter.Both)) {
const destinationAcc = await solanaConnection.getParsedAccountInfo(new PublicKey(destination))
// @ts-ignore
const owner = destinationAcc.value?.data.parsed.info.owner
if (owner) {
// type: 'send'
return { type: 'send', from: pubkey.toString(), to: owner, amount }
}
else return undefined
} else return undefined
}
}
} else if (decoded?.meta?.logMessages?.toString().includes('Transfer')) {
const inx = decoded.transaction.message?.instructions
for (const item of inx) {
// @ts-ignore
if (item.parsed && item.parsed.type == 'transfer') {
// @ts-ignore
const { destination, source, amount } = item.parsed.info
if (destination.toString() == ata.toString() && (filter == Filter.Receive || filter == Filter.Both)) {
const sourceAcc = await solanaConnection.getParsedAccountInfo(new PublicKey(source))
// @ts-ignore
const owner = sourceAcc.value?.data.parsed.info.owner
if (owner) {
// type: 'receive'
return { type: 'receive', from: owner, to: pubkey.toString(), amount: Number(amount) / Math.pow(10, decimals) }
}
else return undefined
} else if (source.toString() == ata.toString() && (filter == Filter.Send || filter == Filter.Both)) {
const destinationAcc = await solanaConnection.getParsedAccountInfo(new PublicKey(destination))
// @ts-ignore
const owner = destinationAcc.value?.data.parsed.info.owner
if (owner) {
// type: 'send'
return { type: 'send', from: pubkey.toString(), to: owner, amount: Number(amount) / Math.pow(10, decimals) }
}
else return undefined
} else return undefined
}
}
} else return undefined
}
const getSymbol = async (mint: PublicKey) => {
const data = await axios.get(`${dexscreenapi}${mint.toString()}`)
const pair = data.data.pairs[0]
// console.log(pair)
if (pair.baseToken.address = mint.toString()) return pair.baseToken.symbol
if (pair.quoteToken.address = mint.toString()) return pair.quoteToken.symbol
}
function formatDate(timestamp: number): string {
const date = new Date(timestamp);
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const month = months[date.getUTCMonth()];
const day = date.getUTCDate();
const year = date.getUTCFullYear();
const hours = date.getUTCHours().toString().padStart(2, '0');
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
return `${month} ${day}, ${year} ${hours}:${minutes}:${seconds} +UTC`;
}
getHistory(new PublicKey('spaxkvvazpDRzDMMPze12HJeg2Q5yY5y7jEtmxN7WXL'), new PublicKey('G9tt98aYSznRk7jWsfuz9FnTdokxS6Brohdo9hSmjTRB'), Filter.Both)