-
Notifications
You must be signed in to change notification settings - Fork 0
/
pancake bot
119 lines (99 loc) · 3.3 KB
/
pancake bot
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
put your wallet address in
put your private key in
put your wss provider in
add bnb and wbnb to your wallet and have fun buying all of the shit coins every 3 secs…
const ethers = require(‘ethers’);
const addresses = {
WBNB: ‘0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c’,
BUSD: ‘0xe9e7cea3dedca5984780bafc599bd69add087d56’,
factory: ‘0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73’,
router: ‘0x10ed43c718714eb63d5aa57b78b54704e256024e’,
recipient: ‘’
}
//First address of this mnemonic must have enough BNB to pay for tx fess
const privateKey = ‘’;
const mygasPrice = ethers.utils.parseUnits(‘5’, ‘gwei’);
const provider = new ethers.providers.WebSocketProvider(‘wss://muddy-young-frost.bsc.quiknode.pro/’);
const wallet = new ethers.Wallet(privateKey);
const account = wallet.connect(provider);
const factory = new ethers.Contract(
addresses.factory,
[‘event PairCreated(address indexed token0, address indexed token1, address pair, uint)’],
account
);
const router = new ethers.Contract(
addresses.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 wbnb = new ethers.Contract(
addresses.WBNB,
[
‘function approve(address spender, uint amount) public returns(bool)’,
],
account
);
console.log(Before Approve);
const valueToapprove = ethers.utils.parseUnits(‘0.1’, ‘ether’);
const init = async () => {
const tx = await wbnb.approve(
router.address,
valueToapprove,
{
gasPrice: mygasPrice,
gasLimit: ‘162445’
}
);
console.log(After Approve);
const receipt = await tx.wait();
console.log(‘Transaction receipt’);
console.log(receipt);
}
factory.on(‘PairCreated’, async (token0, token1, pairAddress) => {
console.log(after factory.on:);
console.log( New pair detected ================= token0: ${token0} token1: ${token1} pairAddress: ${pairAddress} );
//The quote currency needs to be WBNB (we will pay with WBNB)
let tokenIn, tokenOut;
if(token0 === addresses.WBNB) {
tokenIn = token0;
tokenOut = token1;
}
if(token1 == addresses.WBNB) {
tokenIn = token1;
tokenOut = token0;
}
//The quote currency is not WBNB
if(typeof tokenIn === ‘undefined’) {
return;
}
//We buy for 0.1 BNB of the new token
//ethers was originally created for Ethereum, both also work for BSC
//‘ether’ === ‘bnb’ on BSC
console.log(line 87);
const amountIn = ethers.utils.parseUnits(‘0.01’, ‘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(10));
console.log(line 92);
console.log( Buying new token ================= tokenIn: ${amountIn} ${tokenIn} (WBNB) tokenOut: ${amountOutMin} ${tokenOut} );
console.log(line 101);
const tx = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
addresses.recipient,
Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time
{
gasPrice: mygasPrice,
gasLimit: 162445
}
);
console.log(line 117);
const receipt = await tx.wait();
console.log(‘Transaction receipt’);
console.log(receipt);
});
init();