-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbot.js
296 lines (257 loc) · 9.16 KB
/
bot.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// FRONT RUNNING BOT FOR PANCACKE SWAP (Using Router V2)
require("dotenv").config();
const ethers = require("ethers");
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000;
// PancakeSwap router.
const pancakeRouterABI = require("./ABIs/pancakeRouterABI.json");
const erc20ABI = require("./ABIs/erc20ABI.json");
const pancakeSwapRouterV2Address = "0x10ED43C718714eb63d5aA57B78B54704E256024E";
const BNBaddress = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c";
// Function to calculate the gas fee to pay. To sandwich the high value transaction.
function calculateGasPrice(action, value) {
if (action === "buy") {
return ethers.formatUnits(value++, "gwei");
} else {
// If it is a sell substract 1
return ethers.formatUnits(value--, "gwei");
}
}
// Router function to interact with PancakeSwap Router
function router(ourWallet) {
return new ethers.Contract(
pancakeSwapRouterV2Address,
pancakeRouterABI,
ourWallet
);
}
// Function to interact with the ERC20
function erc20(ourWallet, tokenAddress) {
return new ethers.Contract(tokenAddress, erc20ABI, ourWallet);
}
// Buy function (BNB -> ERC20)
async function buy(ourWallet, tokenAddress, gasLimit, gasPrice) {
// How much we are going to buy. (BNB to Gwei)
const budget = 0.01;
const buyAmount = ethers.parseUnits(budget.toString(), "ether");
// The difference between the expected price of the trade and the final price of the trade when executed.
const slippage = 1;
// How many tokens we are going to receive.
let amountOutMin = 0;
if (slippage !== 0) {
const amounts = await router(ourWallet).getAmountsOut(buyAmount, [
BNBaddress,
tokenAddress,
]);
// amountOutMin = amounts[1] - (amounts[1] / 100n) * BigInt(slippage);
amountOutMin = amounts[1] - amounts[1] * (BigInt(slippage) / BigInt(100));
}
// Fill up the transaction Swap
const tx = await router(
ourWallet
).swapExactETHForTokensSupportingFeeOnTransferTokens(
amountOutMin,
[BNBaddress, tokenAddress],
ourWallet.address,
Date.now() + 1000 * 60 * 10,
{
'value': buyAmount.toString(),
'gasLimit': ethers.parseUnits(gasLimit.toString(), "gwei"),
'gasPrice': ethers.parseUnits(gasPrice.toString(), "gwei"),
}
);
const receipt = await tx.wait();
// 0 = failed \\ 1 == success
if (receipt && receipt.blockNumber && receipt.status === 1) {
console.log(
`Transaction https://bscscan.com/tx/${receipt.transactionHash} mined, status: success`
);
} else if (receipt && receipt.blockNumber && receipt.status === 0) {
console.log(
`Transaction https://bscscan.com/tx/${receipt.transactionHash} mined, status: failed`
);
} else {
console.log(
`Transaction https://bscscan.com/tx/${receipt.transactionHash} not mined`
);
}
}
// Sell function (ERC20 -> BNB)
async function sell(ourWallet, tokenAddress, gasLimit, gasPrice) {
const slippage = 1;
// % to sell from our balance needs to be 99% as maybe the 100% cannot be executed.
const amountToken = 99;
//Instance Swap contract for selling.
const sellTokenContract = new ethers.Contract(
pancakeSwapRouterV2Address,
pancakeRouterABI,
ourWallet
);
// Get our balance in the Token contract after purchase
const tokenBalance = await erc20(ourWallet, tokenAddress).balanceOf(
ourWallet.address
);
//99% of our balance will be sold.
const sellAmount = tokenBalance * (amountToken / 100);
let amountOutMin = 0;
const amounts = await router(ourWallet).getAmountsOut(sellAmount, [
tokenAddress,
BNBaddress,
]);
if (slippage !== 0) {
amountOutMin = amounts[1] - amounts[1] * (BigInt(slippage) / BigInt(100));
} else {
amountOutMin = amounts[1];
}
// Approve Pancacke swap to manage our tokens.
const approve = await sellTokenContract.approve(
pancakeSwapRouterV2Address,
sellAmount
);
const receipt_approve = await approve.wait();
if (
receipt_approve &&
receipt_approve.blockNumber &&
receipt_approve.status === 1
) {
console.log(
`Approved https://bscscan.com/tx/${receipt_approve.transactionHash}`
);
// Execute the transaction once the token management is approved.
const swap_txn =
await contract.swapExactTokensForETHSupportingFeeOnTransferTokens(
sellAmount,
amountOutMin,
[tokenAddress, BNBaddress],
ourWallet.address,
Date.now() + 1000 * 60 * 10,
{
'gasLimit': ethers.parseUnits(gasLimit.toString(), "gwei"),
'gasPrice': ethers.parseUnits(gasPrice.toString(), "gwei"),
}
);
const receipt = await swap_txn.wait();
// 0 - failed, 1 - success
if (receipt && receipt.blockNumber && receipt.status === 1) {
console.log(
`Transaction https://bscscan.com/tx/${receipt.transactionHash} mined, status: success`
);
} else if (receipt && receipt.blockNumber && receipt.status === 0) {
console.log(
`Transaction https://bscscan.com/tx/${receipt.transactionHash} mined, status: failed`
);
} else {
console.log(
`Transaction https://bscscan.com/tx/${receipt.transactionHash} not mined`
);
}
}
}
const main = async () => {
// Instance JsonRpcProvider.
const provider = new ethers.WebSocketProvider(process.env.WSS);
// our Wallet
const privateKey = new ethers.Wallet(process.env.PRIVATE_KEY);
const ourWallet = privateKey.connect(provider);
// Instance Interface
const Interface = new ethers.Interface([
"function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)",
"function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)",
"function swapExactETHForTokensSupportingFeeOnTransferTokens(uint amountOutMin, address[] calldata path, address to, uint deadline",]);
// Listen to the pending transactions
provider.on("pending", (tx) => {
provider.getTransaction(tx).then(async (transaction) => {
// now we will only listen for pending transaction on pancakesswap factory
if (transaction && transaction.to === pancakeSwapRouterV2Address) {
const value = ethers.formatEther(transaction.value);
const gasPrice = ethers.formatEther(transaction.gasPrice);
const gasLimit = ethers.formatEther(transaction.gasLimit);
// for example we will be only showing transaction that are higher than 30 bnb
if (value > 1) {
console.log(`\nValue BNB: ${value}`);
console.log(`gasPrice: ${gasPrice}`);
console.log(`gasLimit: ${gasLimit}`);
// Sender
console.log(`From: ${transaction.from}`);
let result = [];
//we will use try and catch to handle the error and decode the data of the function used to swap the token
try {
result = Interface.decodeFunctionData(
"swapExactETHForTokens",
transaction.data
);
} catch (err) {
try {
result = Interface.decodeFunctionData(
"swapExactETHForTokensSupportingFeeOnTransferTokens",
transaction.data
);
} catch (err) {
try {
result = Interface.decodeFunctionData(
"swapETHForExactTokens",
transaction.data
);
} catch (err) {
if (err) {
console.log(err);
console.log(`\nFinal error: ${transaction}`);
}
}
}
}
if (result.length > 0) {
let tokenAddress = "";
if (result[1].length > 0) {
tokenAddress = result[1][1];
console.log(`Token Address: ${tokenAddress}`);
// Calculate the gas price for buying and selling
const buyGasPrice = calculateGasPrice(
"buy",
transaction.gasPrice
);
const sellGasPrice = calculateGasPrice(
"sell",
transaction.gasPrice
);
// Execute the BUY! (BNB -> ERC20)
console.log("\n-Let's buy!💰");
await buy(
ourWallet,
tokenAddress,
transaction.gasLimit,
buyGasPrice
);
// Execute the SELL! (ERC20 -> BNB)
await sell(
ourWallet,
tokenAddress,
transaction.gasLimit,
sellGasPrice
);
}
console.log(`Result: ${result}`);
}
}
}
});
});
// Handle Node disconnection & errors.
provider.websocket.on("error", (ep) => {
console.log(`Unable to connect to ${ep.subdomain} retrying in 3s...`);
setTimeout(main, 3000);
});
provider.websocket.on("close", (code) => {
console.log(
`Connection lost with code ${code}! Attempting reconnect in 3s...`
);
provider.websocket.terminate();
setTimeout(main, 3000);
});
};
main();
// Server created with Express.
app.listen(PORT, (err) => {
console.log(`Listening on port http://localhost:${PORT}`);
});