-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.js
90 lines (80 loc) · 2.67 KB
/
main.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
const config = require('./config');
const { ethers } = require('ethers');
const tgBot = require('node-telegram-bot-api');
const channel_id = config.channel_id;
const bot_id = config.bot_token;
infura_http = new ethers.providers.JsonRpcProvider(config.infura_mainnet_http, "homestead");
const tg_bot = new tgBot(bot_id, {polling: true});
function parseList() {
const csv = require('csv-parser');
const fs = require('fs');
let addresses = [];
return new Promise((resolve, reject) => {
fs.createReadStream('whaleList.csv')
.pipe(csv())
.on('data', (row) => {
addresses.push(row);
})
.on('end', () => {
resolve(addresses);
})
})
};
async function parseBlock(block_with_transactions,address_list) {
let caught_whales = [];
const transactions = block_with_transactions['transactions'];
for (let tx of transactions) {
const address = tx['from'];
for (let whale of address_list) {
try {
let whale_wallet = ethers.utils.getAddress(whale['Wallet'])
if (whale_wallet == address) {
whale['tx'] = tx['hash'];
caught_whales.push(whale);
}
} catch {
console.log("Error checking whale addresses");
}
}
}
return caught_whales;
};
async function post(block,addresses) {
try {
const whale_list = await parseBlock(block,addresses);
for (whale of whale_list) {
let tx_receipt = await infura_http.getTransactionReceipt(whale['tx']);
if (tx_receipt['status'] != 0 ) {
const default_msg = whale['Name'] + " spotted!\n\n" +
"TX: " + "https://etherscan.io/tx/" + whale['tx'] + '\n\n' +
"Recent ERC20 Transactions: " + "https://etherscan.io/address/" + whale['Wallet']+ "#tokentxns";
tg_bot.sendMessage(channel_id, default_msg);
} else {
console.log("Failed tx detected! Ignoring...");
}
}
} catch (e) {
console.log("Error sending transaction post: " + e);
}
};
let main = async function () {
let eth_websocket = new ethers.providers.WebSocketProvider(config.infura_mainnet_ws);
const address_list = await parseList();
eth_websocket.on("block", (blk) => {
eth_websocket.getBlockWithTransactions(blk).then(function (block) {
post(block,address_list);
});
});
eth_websocket._websocket.on("error", async () => {
console.log(`Unable to connect to ${ep.subdomain} retrying in 3s...`);
setTimeout(main, 3000);
});
eth_websocket._websocket.on("close", async (code) => {
console.log(
`Connection lost with code ${code}! Attempting reconnect in 3s...`
);
eth_websocket._websocket.terminate();
setTimeout(main, 3000);
});
};
main();