forked from TheCryptoFarm/txPoolSniper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
183 lines (173 loc) · 5.65 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
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
"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 tokenAbi = new ethers.utils.Interface(require("./tokenABI.json"));
const EXPECTED_PONG_BACK = 30000;
const KEEP_ALIVE_CHECK_INTERVAL = 15000;
let pingTimeout = null;
let keepAliveInterval = null;
let provider;
let explorer;
let wallet;
let account;
let router;
let grasshopper;
let swapEth;
let purchaseAmount;
async function Wait(seconds) {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}
const startConnection = () => {
provider = new ethers.providers.WebSocketProvider(process.env.BSC_NODE_WSS);
explorer = process.env.EXPLORER;
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", async () => {
console.log(
"🏗️ txPool sniping has begun...patience is a virtue, my grasshopper..."
);
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);
const WETH = await router.WETH();
if (ethers.utils.getAddress(tokens.pair[0]) === ethers.utils.getAddress(WETH)) {
swapEth = 1;
purchaseAmount = ethers.utils.parseUnits(tokens.purchaseAmount, "ether");
} else {
await Approve();
}
});
provider.on("pending", async (txHash) => {
provider
.getTransaction(txHash)
.then(async (tx) => {
if (grasshopper === 0) {
console.log("🚧 And, Yes..I am actually working...trust me...");
grasshopper = 1;
}
if (tx && tx.to) {
if (ethers.utils.getAddress(tx.to) ===
ethers.utils.getAddress(tokens.router)){
const re1 = new RegExp("^0xf305d719");
const re2 = new RegExp("^0xe8e33700");
if (re1.test(tx.data) || re2.test(tx.data)) {
const decodedInput = pcsAbi.parseTransaction({
data: tx.data,
value: tx.value,
});
if (
ethers.utils.getAddress(tokens.pair[1]) ===
ethers.utils.getAddress(decodedInput.args[0]) ||
ethers.utils.getAddress(tokens.pair[1]) ===
ethers.utils.getAddress(decodedInput.args[1])
) {
provider.off("pending");
if (tokens.buyDelay > 0) {
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 Approve = async () => {
const contract = new ethers.Contract(
tokens.pair[0],
tokenAbi,
account
);
const tokenName = await contract.name();
const tokenDecimals = await contract.decimals();
purchaseAmount = ethers.utils.parseUnits(tokens.purchaseAmount, tokenDecimals);
const allowance = await contract.allowance(process.env.RECIPIENT, tokens.router);
if (allowance._hex === "0x00") {
const tx = await contract.approve(tokens.router, ethers.constants.MaxUint256);
const receipt = await tx.wait();
console.log(`🎟️ Approved ${tokenName} for swapping... ${receipt.transactionHash}`);
}
};
const BuyToken = async (txLP) => {
const tx = await retry(
async () => {
const amountOutMin = 0; // I don't like this but it works
if (swapEth) {
const receipt = await router.swapExactETHForTokens(
amountOutMin,
tokens.pair,
process.env.RECIPIENT,
Date.now() + 1000 * tokens.deadline,
{
value: purchaseAmount,
gasLimit: tokens.gasLimit,
gasPrice: txLP.gasPrice,
}
);
return receipt;
} else {
const receipt = await router.swapExactTokensForTokens(
purchaseAmount,
amountOutMin,
tokens.pair,
process.env.RECIPIENT,
Date.now() + 1000 * tokens.deadline,
{
gasLimit: tokens.gasLimit,
gasPrice: txLP.gasPrice,
}
);
return receipt;
}
},
{
retries: tokens.buyRetries,
minTimeout: tokens.retryMinTimeout,
maxTimeout: tokens.retryMaxTimeout,
onRetry: (err, number) => {
console.log("💰 Buy Failed - Retrying", number);
console.log(err);
if (number === tokens.buyRetries) {
console.log("☢️ Sniping has failed...");
process.exit();
}
},
}
);
console.log("💰 LP: " + txLP.hash);
console.log(`🔫 Sniped: ${explorer}/tx/${tx.hash}`);
process.exit();
};
startConnection();