forked from richizo/frontrun-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcherz.js
94 lines (89 loc) · 2.86 KB
/
watcherz.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
require("dotenv").config();
const Web3 = require("web3");
const ethers = require("ethers");
const abiDecoder = require("abi-decoder");
const exchangeABI = require("./abi").exchangeABI;
abiDecoder.addABI(exchangeABI);
let web3;
const {
SELL_TOKEN_ADDRESS,
SWAP_TO_TOKEN_1_ADDRESS,
SWAP_TO_TOKEN_2_ADDRESS,
SWAP_TO_TOKEN_1_TICKER,
SWAP_TO_TOKEN_2_TICKER
} = process.env;
function initialize() {
web3 = new Web3("wss://bsc-ws-node.nariox.org:443");
}
async function startTracking() {
// REMOVED
console.log("Listening...")
}
// REMOVED
let alerting = false;
async function watcherz(transaction) {
let transactionInput = transaction.input;
if(!transactionInput.includes("xxxxxxxxxxxxxxxxxxxx")) return;
const trxMethod = transactionInput.toLowerCase().substring(0, 10);
if (
trxMethod === "0x18cbafe5" /*swapExactTokensForETH */ ||
trxMethod === "0x38ed1739" /*swapExactTokensForTokens */
) {
const decodedData = abiDecoder.decodeMethod(transactionInput);
const path = decodedData.params.filter((el) => el.name == "path")[0].value;
const tokenSell = path[0].toLowerCase();
const tokenBuy = path[path.length - 1].toLowerCase();
if (tokenSell === SELL_TOKEN_ADDRESS.toLowerCase()) {
let priceLimit;
let ticker;
switch (tokenBuy) {
case SWAP_TO_TOKEN_1_ADDRESS.toLowerCase():
ticker = SWAP_TO_TOKEN_1_TICKER;
break;
case SWAP_TO_TOKEN_2_ADDRESS.toLowerCase():
ticker = SWAP_TO_TOKEN_2_TICKER;
break;
default:
priceLimit = null;
}
if (priceLimit != null) {
let transactionReceipt = null;
while (transactionReceipt === null) {
transactionReceipt = await web3.eth
.getTransactionReceipt(transaction.hash)
.catch(() => {});
}
if(alerting) return;
let transfers = [];
for (let i = 0; i < transactionReceipt.logs.length; i++) {
if (
transactionReceipt.logs[i].topics[0] ==
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" //keccak for Transfer(address,address,uint256)
) {
transfers.push(transactionReceipt.logs[i]);
}
}
let quantityBuy = null,
quantitySell = null;
for(let i=0;i<transfers.length;i++){
const el = transfers[i];
if (el.address.toLowerCase() == tokenBuy) {
quantityBuy = parseInt(el.data, 16);
} else if (el.address.toLowerCase() == tokenSell) {
quantitySell = parseInt(el.data, 16);
}
}
// REMOVED
if (quantityBuy != null && quantitySell != null) {
const price = quantityBuy/(quantitySell);
console.log(price+" "+ticker+" txHash: "+transaction.hash);
}
}
}
}
}
async function run() {
initialize();
startTracking();
}
run();