-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot.js
334 lines (293 loc) · 11.7 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
var Web3 = require('web3');
var abiDecoder = require('abi-decoder');
var _ = require('lodash');
var BigNumber = require('big-number');
const {PANCAKE_ROUTER_ADDRESS, PANCAKE_FACTORY_ADDRESS, PANCAKE_ROUTER_ABI, PANCAKE_FACTORY_ABI, PANCAKE_POOL_ABI, HTTP_PROVIDER_LINK, WEBSOCKET_PROVIDER_LINK, TOKEN_ADDRESSES, PRIVATE_KEY, MAX_AMOUNT, SLIPPAGE,GASPRICE, MINPROFIT} = require('./constants.js');
const INPUT_TOKEN_ADDRESS = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'; //TOKEN IN
const WBNB_TOKEN_ADDRESS = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
var pool_info = [];
let i;
var amount;
const maxAmount = MAX_AMOUNT;
var web3;
var web3Ws;
var pancakeRouter;
var pancakeFactory;
const ONE_GWEI = 1e9;
var attack_started = false;
var buyNonce;
var sellNonce
var subscription;
web3 = new Web3(new Web3.providers.HttpProvider(HTTP_PROVIDER_LINK));
web3Ws = new Web3(new Web3.providers.WebsocketProvider(WEBSOCKET_PROVIDER_LINK));
pancakeRouter = new web3.eth.Contract(PANCAKE_ROUTER_ABI, PANCAKE_ROUTER_ADDRESS);
pancakeFactory = new web3.eth.Contract(PANCAKE_FACTORY_ABI, PANCAKE_FACTORY_ADDRESS);
abiDecoder.addABI(PANCAKE_ROUTER_ABI);
const user_wallet = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);
const out_token_addresses = TOKEN_ADDRESSES;
const start = async() => {
buyNonce = await web3.eth.getTransactionCount(user_wallet.address);
sellNonce = buyNonce + 1
console.log(`buy nonce is ${buyNonce} and sell nonce is ${sellNonce}`)
const out_token_addresses = TOKEN_ADDRESSES;
for(var index = 0; index < out_token_addresses.length; index++) {
await getPoolInfo(WBNB_TOKEN_ADDRESS, out_token_addresses,index);
}
}
async function main() {
try {
web3Ws.onopen = function(evt) {
web3Ws.send(JSON.stringify({ method: "subscribe", topic: "transfers", address: user_wallet.address}));
console.log('connected')
}
subscription = web3Ws.eth.subscribe('pendingTransactions', function (error, result) {
}).on("data", async function (transactionHash) {
let transaction = await web3.eth.getTransaction(transactionHash);
if (transaction != null && transaction['to'] == PANCAKE_ROUTER_ADDRESS)
{
await handleTransaction(transaction, out_token_addresses, user_wallet);
}
})
} catch (error) {
console.log('failed to fetch mempool data:')
process.exit();
}
}
async function handleTransaction(transaction, out_token_addresses, user_wallet) {
if (await triggersFrontRun(transaction, out_token_addresses)) {
subscription.unsubscribe();
let gasPrice = parseInt(transaction['gasPrice']);
let newGasPrice = gasPrice + GASPRICE*ONE_GWEI;
if(amount > maxAmount){
amount = maxAmount
}
console.log('amount is' + amount)
var estimatedInput = ((amount*0.999)*(10**18));
var realInput = SLIPPAGE*(amount*(10**18));
var gasLimit = (300000);
var outputtoken = await getAmountOut(estimatedInput, pool_info[i].input_volumn, pool_info[i].output_volumn);
swap(newGasPrice, gasLimit, outputtoken, realInput, 0, out_token_addresses[i], user_wallet, transaction);
swap(gasPrice, gasLimit, outputtoken, 0, 1, out_token_addresses[i], user_wallet, transaction);
console.log('transaction attempted to frontrun is '+ transaction['hash']);
attack_started = false;
return execute();
}
}
async function triggersFrontRun(transaction, out_token_addresses) {
if(attack_started)
return false;
if(parseInt(transaction['gasPrice']) / 10**9 > 10 || parseInt(transaction['gasPrice'])/10**9 < 3 ){
return false;
}
console.log('gasPrice'+ transaction['gasPrice']/10**9);
let data = parseTx(transaction['input']);
let method = data[0];
let params = data[1];
console.log(method)
if(method == 'swapExactETHForTokens')
{
let path = params[1].value;
let in_token_addr = path[0];
let out_token_addr = path[path.length-1];
let recept_addr = params[2].value;
let deadline = params[3].value;
if(_.includes(out_token_addresses, out_token_addr))
{
i = _.indexOf(out_token_addresses, out_token_addr)
}else{
return false;
}
//reserves have to be divided by decimals
let in_amount = transaction.value;
let b = transaction.value/10**18;
let out = params[0].value/10**18;
await updatePoolInfo(i);
const x = pool_info[i].output_volumn/10**18;
const K = (pool_info[i].input_volumn/10**18)*(pool_info[i].output_volumn/10**18)
const y = (pool_info[i].input_volumn/10**18)
const a = (1/2)*(Math.sqrt((b/1.0025)*((b/1.0025)+4*(K/out)))-(b/1.0025)-2*y) //optimal amount for you to purchase
const firstnewX = K/(y+a);
const tokensReceived = x - firstnewX;
const secondnewX = K/(y+a+(b/1.0025))
const secondtokensReceived = firstnewX - secondnewX
const afterSellY = K/(secondnewX+tokensReceived);
const ethReceived = (y+a+(b/1.0025) - afterSellY);
const profit = (ethReceived-a)-(0.0025)*(ethReceived+a)
console.log('profit is...'+ profit)
if(profit>MINPROFIT && a>0)
{
amount = a;
attack_started = true;
return true;
}
else
{
return false;
}
}
else if(method == 'swapExactTokensForTokens')
{
let path = params[2].value;
let in_token_addr = path[path.length-2];
let out_token_addr = path[path.length-1];
let recept_addr = params[3].value;
let dead_line = params[4].value;
if(_.includes(out_token_addresses, out_token_addr))
{
i = _.indexOf(out_token_addresses, out_token_addr)
}else{
return false;
}
if(in_token_addr != INPUT_TOKEN_ADDRESS)
{
return false;
}
let b = params[0].value/10**18;
let out_min = params[1].value;
let out = params[1].value/10**18;
await updatePoolInfo(i);
const x = pool_info[i].output_volumn/10**18;
const K = (pool_info[i].input_volumn/10**18)*(pool_info[i].output_volumn/10**18)
const y = (pool_info[i].input_volumn/10**18)
const a = (1/2)*(Math.sqrt((b/1.0025)*((b/1.0025)+4*(K/out)))-(b/1.0025)-2*y)
const firstnewX = K/(y+a);
const tokensReceived = x - firstnewX;
const secondnewX = K/(y+a+(b/1.0025))
const secondtokensReceived = firstnewX - secondnewX
const afterSellY = K/(secondnewX+tokensReceived);
const ethReceived = (y+a+(b/1.0025) - afterSellY);
const profit = (ethReceived-a)-(0.0025)*(ethReceived+a)
console.log('profit is...'+ profit)
if(profit>MINPROFIT && a>0)
{
amount = a;
attack_started = true;
return true;
}
else
{
return false;
}
}
return false;
}
async function swap(gasPrice, gasLimit, outputtoken, outputeth, trade, out_token_address, user_wallet, transaction) {
var from = user_wallet;
var deadline;
var swap;
await web3.eth.getBlock('latest', (error, block) => {
deadline = block.timestamp + 300; // transaction expires in 300 seconds (5 minutes)
});
deadline = web3.utils.toHex(deadline);
if(trade == 0) {
swap = pancakeRouter.methods.swapETHForExactTokens(outputtoken.toString() , [INPUT_TOKEN_ADDRESS, out_token_address], from.address, deadline);
var encodedABI = swap.encodeABI();
var tx = {
from: from.address,
to: PANCAKE_ROUTER_ADDRESS,
gas: gasLimit,
gasPrice: gasPrice,
nonce: buyNonce.toString(),
data: encodedABI,
value: outputeth
};
} else {
swap = pancakeRouter.methods.swapExactTokensForETH(outputtoken.toString(), '0', [out_token_address, INPUT_TOKEN_ADDRESS], from.address, deadline);
var encodedABI = swap.encodeABI();
var tx = {
from: from.address,
to: PANCAKE_ROUTER_ADDRESS,
gas: gasLimit,
gasPrice: gasPrice,
nonce: sellNonce.toString(),
data: encodedABI,
value: 0*10**18
};
}
var signedTx = await from.signTransaction(tx);
if(trade == 0) {
let is_pending = await isPending(transaction['hash']);
if(!is_pending) {
console.log("The transaction you want to attack has already been completed!!!");
process.exit();
}else{
web3.eth.sendSignedTransaction(signedTx.rawTransaction)
}
}else{
web3.eth.sendSignedTransaction(signedTx.rawTransaction)
}
}
function parseTx(input) {
if (input == '0x')
return ['0x', []]
let decodedData = abiDecoder.decodeMethod(input);
let method = decodedData['name'];
let params = decodedData['params'];
return [method, params]
}
async function isPending(transactionHash) {
return await web3.eth.getTransactionReceipt(transactionHash) == null;
}
//useful structure
async function updatePoolInfo(i) {
try{
console.log('updating...')
console.log(i)
var reserves = await pool_info[i].contract.methods.getReserves().call();
if(pool_info[i].forward) {
var eth_balance = reserves[0];
var token_balance = reserves[1];
} else {
var eth_balance = reserves[1];
var token_balance = reserves[0];
}
pool_info[i].input_volumn = eth_balance;
pool_info[i].output_volumn = token_balance;
}catch (error) {
console.log('Failed To Get Pair Info');
return false;
}
}
async function getPoolInfo(input_token_address, out_token_addresses, index){
console.log(index)
var pool_address = await pancakeFactory.methods.getPair(input_token_address, out_token_addresses[index]).call();
var pool_contract = new web3.eth.Contract(PANCAKE_POOL_ABI, pool_address);
var reserves = await pool_contract.methods.getReserves().call();
var token0_address = await pool_contract.methods.token0().call();
if(token0_address == INPUT_TOKEN_ADDRESS) {
var forward = true;
var bnb_balance = reserves[0];
var token_balance = reserves[1];
} else {
var forward = false;
var bnb_balance = reserves[1];
var token_balance = reserves[0];
}
pool_info[index] = {'contract': pool_contract, 'forward': forward, 'input_volumn': bnb_balance, 'output_volumn': token_balance}
}
async function getAmountOut(aIn, reserveA, reserveB) {
const aInWithFee = BigNumber(aIn).multiply(997); //0.3% fee that's right
const numerator = BigNumber(aInWithFee).multiply(reserveB)
const denominator = BigNumber(aInWithFee).add(BigNumber(reserveA).multiply(1000));
const bOut = BigNumber(numerator).divide(denominator);
return bOut
}
//-----------------EXTRA FUNCTIONS getPairAddress of tokenA-tokenB pool offline.
const getUniv2PairAddress = (tokenA, tokenB) => {
const [token0, token1] = sortTokens(tokenA, tokenB);
const salt = ethers.utils.keccak256(token0 + token1.replace("0x", ""));
const address = ethers.utils.getCreate2Address(
"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f", // Factory address (contract creator)
salt,
"0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f" // init code hash
);
return address;
};
const execute = async() =>{
start().then(() => {
console.log('main...')
main()
});
}
execute();
//do not use the router, use swap