-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathbot.js
104 lines (84 loc) · 3.6 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
import ethers from 'ethers';
import express from 'express';
import chalk from 'chalk';
const app = express();
const data = {
WBNB: '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c', //wbnb
to_PURCHASE: '0xe9e7cea3dedca5984780bafc599bd69add087d56', // token to purchase = BUSD for test
factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73', //PancakeSwap V2 factory
router: '0x10ED43C718714eb63d5aA57B78B54704E256024E', //PancakeSwap V2 router
recipient: '', //wallet address,
AMOUNT_OF_WBNB : '0.0002',
Slippage : '3', //in Percentage
gasPrice : '5', //in gwei
gasLimit : '345684' //at least 21000
}
let initialLiquidityDetected = false;
const bscMainnetUrl = 'https://bsc-dataseed.binance.org/'; //ankr or quiknode
const privatekey = ''; //without 0
const provider = new ethers.providers.JsonRpcProvider(bscMainnetUrl)
const wallet = new ethers.Wallet(privatekey);
const account = wallet.connect(provider);
const factory = new ethers.Contract(
data.factory,
['function getPair(address tokenA, address tokenB) external view returns (address pair)'],
account
);
const router = new ethers.Contract(
data.router,
[
'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
],
account
);
const run = async () => {
const tokenIn = data.WBNB;
const tokenOut = data.to_PURCHASE;
const pairAddress = await factory.getPair(tokenIn, tokenOut);
console.log(pairAddress);
const pair = new ethers.Contract(pairAddress, ['event Mint(address indexed sender, uint amount0, uint amount1)'], account);
pair.on('Mint', async (sender, amount0, amount1) => {
if(initialLiquidityDetected === true) {
return;
}
initialLiquidityDetected = true;
//We buy x amount of the new token for our wbnb
const amountIn = ethers.utils.parseUnits(`${data.AMOUNT_OF_WBNB}`, 'ether');
const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);
//Our execution price will be a bit different, we need some flexbility
const amountOutMin = amounts[1].sub(amounts[1].div(`${data.Slippage}`));
console.log(
chalk.green.inverse(`Liquidity Addition Detected\n`)
+
`Buying Token
=================
tokenIn: ${amountIn.toString()} ${tokenIn} (WBNB)
tokenOut: ${amountOutMin.toString()} ${tokenOut}
`);
console.log('Processing Transaction.....');
console.log(chalk.yellow(`amountIn: ${amountIn}`));
console.log(chalk.yellow(`amountOutMin: ${amountOutMin}`));
console.log(chalk.yellow(`tokenIn: ${tokenIn}`));
console.log(chalk.yellow(`tokenOut: ${tokenOut}`));
console.log(chalk.yellow(`data.recipient: ${data.recipient}`));
console.log(chalk.yellow(`data.gasLimit: ${data.gasLimit}`));
console.log(chalk.yellow(`data.gasPrice: ${ethers.utils.parseUnits(`${data.gasPrice}`, 'gwei')}`));
const tx = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
data.recipient,
Date.now() + 1000 * 60 * 10, //10 minutes
{
'gasLimit': data.gasLimit,
'gasPrice': ethers.utils.parseUnits(`${data.gasPrice}`, 'gwei')
});
const receipt = await tx.wait();
console.log('Transaction receipt');
console.log(receipt);
});
}
run();
const PORT = 5000;
app.listen(PORT, (console.log(chalk.yellow(`Listening for Liquidity Addition to token ${data.to_PURCHASE}`))));