-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.js
103 lines (96 loc) · 3.3 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
"use strict";
const env = require("./env.json");
Object.assign(process.env, env);
const ethers = require("ethers");
const purchaseToken = process.env.PURCHASE_TOKEN;
const purchaseAmount = ethers.utils.parseUnits(
process.env.PURCHASE_AMOUNT,
"ether"
);
const slippage = process.env.SLIPPAGE;
const wbnb = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"; // WBNB
const pcs = "0x10ED43C718714eb63d5aA57B78B54704E256024E"; // PCSv2 Router
const pcsAbi = new ethers.utils.Interface(require("./abi.json"));
const EXPECTED_PONG_BACK = 30000;
const KEEP_ALIVE_CHECK_INTERVAL = 15000;
const provider = new ethers.providers.WebSocketProvider(
process.env.BSC_NODE_WSS
);
const wallet = ethers.Wallet.fromMnemonic(process.env.MNEMONIC);
const account = wallet.connect(provider);
const router = new ethers.Contract(pcs, pcsAbi, account);
const startConnection = () => {
let pingTimeout = null;
let keepAliveInterval = null;
provider._websocket.on("open", () => {
console.log("txPool sniping has begun...\n");
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 (tx && tx.to) {
if (tx.to === pcs) {
let re = new RegExp("^0xf305d719");
if (re.test(tx.data)) {
const decodedInput = pcsAbi.parseTransaction({
data: tx.data,
value: tx.value,
});
if (purchaseToken === decodedInput.args[0]) {
await BuyToken(txHash);
}
}
}
}
});
});
});
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 (txHash) => {
const amounts = await router.getAmountsOut(purchaseAmount, [
wbnb,
purchaseToken,
]);
const amountOutMin = amounts[1].sub(amounts[1].div(slippage));
const tx = await router.swapExactETHForTokensSupportingFeeOnTransferTokens(
amountOutMin,
[wbnb, purchaseToken],
process.env.RECIPIENT,
Date.now() + 1000 * 60 * 5, //5 minutes
{
value: purchaseAmount,
gasLimit: 345684,
gasPrice: ethers.utils.parseUnits("6", "gwei"),
}
);
console.log("Waiting for Transaction reciept...");
const receipt = await tx.wait();
console.log("Token Purchase Complete");
console.log("Associated LP Event txHash: " + txHash);
console.log("Your txHash: " + receipt.transactionHash);
process.exit();
};
startConnection();