forked from TheCryptoFarm/txPoolSniper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
135 lines (126 loc) · 4.07 KB
/
app.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
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
"use strict";
const env = require("./env.json");
Object.assign(process.env, env);
const ethers = require("ethers");
const retry = require("async-retry");
const tokens = require("./tokens.js");
const pcsAbi = new ethers.utils.Interface(require("./abi.json"));
const purchaseAmount = ethers.utils.parseUnits(tokens.purchaseAmount, "ether");
const EXPECTED_PONG_BACK = 30000;
const KEEP_ALIVE_CHECK_INTERVAL = 15000;
let pingTimeout = null;
let keepAliveInterval = null;
let provider;
let wallet;
let account;
let router;
let grasshopper;
async function Wait(seconds) {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}
const startConnection = () => {
provider = new ethers.providers.WebSocketProvider(process.env.BSC_NODE_WSS);
wallet = new ethers.Wallet(process.env.PRIVATE_KEY);
account = wallet.connect(provider);
router = new ethers.Contract(tokens.router, pcsAbi, account);
grasshopper = 0;
provider._websocket.on("open", () => {
console.log(
"txPool sniping has begun...patience is a virtue, my grasshopper..."
);
tokens.router = ethers.utils.getAddress(tokens.router);
keepAliveInterval = setInterval(() => {
provider._websocket.ping();
// Use `WebSocket#terminate()`, which immediately destroys the connection,
// instead of `WebSocket#close()`, which waits for the close timer.
// Delay should be equal to the interval at which your server
// sends out pings plus a conservative assumption of the latency.
pingTimeout = setTimeout(() => {
provider._websocket.terminate();
}, EXPECTED_PONG_BACK);
}, KEEP_ALIVE_CHECK_INTERVAL);
provider.on("pending", async (txHash) => {
provider
.getTransaction(txHash)
.then(async (tx) => {
if (grasshopper === 0) {
console.log("And, Yes..I am working...");
grasshopper = 1;
}
if (tx && tx.to) {
if (tx.to === tokens.router) {
const re1 = new RegExp("^0xf305d719");
if (re1.test(tx.data)) {
const decodedInput = pcsAbi.parseTransaction({
data: tx.data,
value: tx.value,
});
if (
ethers.utils.getAddress(tokens.pair[1]) ===
decodedInput.args[0]
) {
provider.off("pending");
await Wait(tokens.buyDelay);
await BuyToken(tx);
}
}
}
}
})
.catch(() => {});
});
});
provider._websocket.on("close", () => {
console.log("WebSocket Closed...Reconnecting...");
clearInterval(keepAliveInterval);
clearTimeout(pingTimeout);
startConnection();
});
provider._websocket.on("error", () => {
console.log("Error. Attemptiing to Reconnect...");
clearInterval(keepAliveInterval);
clearTimeout(pingTimeout);
startConnection();
});
provider._websocket.on("pong", () => {
clearInterval(pingTimeout);
});
};
const BuyToken = async (txLP) => {
const tx = await retry(
async () => {
const amountOutMin = 0; // I don't like this but it works
let buyConfirmation = await router.swapExactETHForTokens(
amountOutMin,
tokens.pair,
process.env.RECIPIENT,
Date.now() + 1000 * tokens.deadline,
{
value: purchaseAmount,
gasLimit: tokens.gasLimit,
gasPrice: ethers.utils.parseUnits(tokens.gasPrice, "gwei"),
}
);
return buyConfirmation;
},
{
retries: tokens.buyRetries,
minTimeout: tokens.retryMinTimeout,
maxTimeout: tokens.retryMaxTimeout,
onRetry: (err, number) => {
console.log("Buy Failed - Retrying", number);
console.log("Error", err);
if (number === tokens.buyRetries) {
console.log("Sniping has failed...");
process.exit();
}
},
}
);
console.log("Associated LP Event txHash: " + txLP.hash);
console.log("Your [pending] txHash: " + tx.hash);
process.exit();
};
startConnection();