forked from Scott-778/coinmarketcap-sniper-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmcBot.js
413 lines (374 loc) · 15.7 KB
/
cmcBot.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
coinmarketcap-new-listings-sniper-bot
Coinmarketcap new listings sniper bot that uses
telegram notifications from this telegram channel
https://t.me/joinchat/b17jE6EbQX5kNWY8 use this link and subscribe.
Turn on two step verification in telegram.
Go to my.telegram.org and create App to get api_id and api_hash.
*/
const { TelegramClient } = require("telegram");
const { StringSession } = require("telegram/sessions");
const input = require("input");
const { NewMessage } = require('telegram/events');
const ethers = require('ethers');
const open = require('open');
require('dotenv').config();
const addresses = {
WBNB: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
pancakeRouter: '0x10ED43C718714eb63d5aA57B78B54704E256024E',
BUSD: '0xe9e7cea3dedca5984780bafc599bd69add087d56',
buyContract: '0xDC56800e179964C3C00a73f73198976397389d26',
recipient: process.env.recipient
}
const mnemonic = process.env.mnemonic;
const apiId = parseInt(process.env.apiId);
const apiHash = process.env.apiHash;
const stringSession = new StringSession("");
/*-----------Settings-----------*/
const numberOfTokensToBuy = 10; // number of different tokens you want to buy
const autoSell = true; // If you want to auto sell or not
const myGasPriceForApproval = ethers.utils.parseUnits('6', 'gwei');
const myGasLimit = 1500000;
const BUYALLTOKENS = true; // if true it will buy all tokens without stategies, change to false to use the strategy filters
/* if BUYALLTOKENS is true. Default Strategy to buy any token that we get notification for and liquidity is BNB */
const buyAllTokensStrategy = {
investmentAmount: '0.5', // Amount to invest per token in BNB
gasPrice: ethers.utils.parseUnits('10', 'gwei'),
profitMultiplier: 2.5, // 2.5X
stopLossMultiplier: 0.7, // 30% loss
percentOfTokensToSellProfit: 75, // sell 75% when profit is reached
percentOfTokensToSellLoss: 100 // sell 100% when stoploss is reached
}
/*------------Advanced Settings-------------*/
/* if BUYALLTOKENS is false it will filter tokens to buy based on strategies below, you can adjust these filters to your preference.
You can also use just one strategy, for example if I wanted to only use the low liquidity strategy I would change maxTax on lines 77 and 92 to a negative value ex -1 */
/* Strategy for buying low-liquid tokens */
const strategyLL =
{
investmentAmount: '0.1', // Investment amount per token in BNB
maxBuyTax: 20, // max buy slippage
maxSellTax: 20, // max sell slippage
maxLiquidity: 100, // max Liquidity BNB
minLiquidity: 10, // min Liquidity BNB
profitMultiplier: 2.5, // 2.5X
stopLossMultiplier: 0.7, // 30% loss
platform: "COINMARKETCAP", // Either COINMARKETCAP or COINGECKO
gasPrice: ethers.utils.parseUnits('8', 'gwei'), // Gas Price
percentOfTokensToSellProfit: 75, // sell 75% when profit is reached
percentOfTokensToSellLoss: 100 // sell 100% when stoploss is reached
}
/* Strategy for buying medium-liquid tokens */
const strategyML =
{
investmentAmount: '0.2', // Investment amount per token in BNB
maxBuyTax: 10, // max buy slippage
maxSellTax: 10, // max sell slippage
maxLiquidity: 250, // max Liquidity BNB
minLiquidity: 100, // min Liquidity BNB
profitMultiplier: 1.8, // 80% profit
stopLossMultiplier: 0.8, // 20% loss
platform: "COINGECKO", // Either COINMARKETCAP or COINGECKO
gasPrice: ethers.utils.parseUnits('7', 'gwei'),
percentOfTokensToSellProfit: 75, // sell 75% when profit is reached
percentOfTokensToSellLoss: 100 // sell 100% when stoploss is reached
}
/* Strategy for buying high-liquid tokens */
const strategyHL =
{
investmentAmount: '0.3', // Investment amount per token in BNB
maxBuyTax: 5, // max buy slippage
maxSellTax: 5, // max sell slippage
maxLiquidity: 1000, // max Liquidity BNB
minLiquidity: 250, // min Liquidity BNB
profitMultiplier: 1.5, // 50% profit
stopLossMultiplier: 0.9, // 10% loss
platform: "COINMARKETCAP", // Either COINMARKETCAP or COINGECKO
gasPrice: ethers.utils.parseUnits('6', 'gwei'),
percentOfTokensToSellProfit: 75, // sell 75% of tokens when profit is reached
percentOfTokensToSellLoss: 100 // sell 100% of tokens when stoploss is reached
}
/*-----------End Settings-----------*/
const node = 'https://bsc-dataseed.binance.org/';
const wallet = new ethers.Wallet.fromMnemonic(mnemonic);
const provider = new ethers.providers.JsonRpcProvider(node);
const account = wallet.connect(provider);
const pancakeAbi = [
'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)',
'function swapExactTokensForETHSupportingFeeOnTransferTokens(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline)'
];
const pancakeRouter = new ethers.Contract(addresses.pancakeRouter, pancakeAbi, account);
let tokenAbi = [
'function approve(address spender, uint amount) public returns(bool)',
'function balanceOf(address account) external view returns (uint256)',
'event Transfer(address indexed from, address indexed to, uint amount)',
'function name() view returns (string)',
'function buyTokens(address tokenAddress, address to) payable',
'function decimals() external view returns (uint8)'
];
const channelId = 1517585345;
let token = [];
var sellCount = 0;
var buyCount = 0;
const buyContract = new ethers.Contract(addresses.buyContract, tokenAbi, account);
async function buy() {
if (buyCount < numberOfTokensToBuy) {
const value = ethers.utils.parseUnits(token[buyCount].investmentAmount, 'ether').toString();
const tx = await buyContract.buyTokens(token[buyCount].tokenAddress, addresses.recipient,
{
value: value,
gasPrice: token[buyCount].gasPrice,
gasLimit: myGasLimit
});
const receipt = await tx.wait();
console.log("Buy Transaction Hash:",receipt.transactionHash);
const poocoinURL = new URL(token[buyCount].tokenAddress, 'https://poocoin.app/tokens/');
open(poocoinURL.href);
token[buyCount].didBuy = true;
buyCount++;
approve();
}
}
async function approve() {
let contract = token[buyCount - 1].contract;
const valueToApprove = ethers.constants.MaxUint256;
const tx = await contract.approve(
pancakeRouter.address,
valueToApprove, {
gasPrice: myGasPriceForApproval,
gasLimit: 210000
}
);
const receipt = await tx.wait();
console.log("Approve Transaction Hash:",receipt.transactionHash);
if (autoSell) {
token[buyCount - 1].checkProfit();
} else {
if (buyCount == numberOfTokensToBuy) {
process.exit();
}
}
}
async function checkForProfit(token) {
var sellAttempts = 0;
token.contract.on("Transfer", async (from, to, value, event) => {
const takeLoss = (parseFloat(token.investmentAmount) * (token.stopLossMultiplier - token.tokenSellTax / 100)).toFixed(18).toString();
const takeProfit = (parseFloat(token.investmentAmount) * (token.profitMultiplier + token.tokenSellTax / 100)).toFixed(18).toString();
const tokenName = await token.contract.name();
let bal = await token.contract.balanceOf(addresses.recipient);
const amount = await pancakeRouter.getAmountsOut(bal, token.sellPath);
const profitDesired = ethers.utils.parseUnits(takeProfit);
const stopLoss = ethers.utils.parseUnits(takeLoss);
let currentValue;
if (token.sellPath.length == 3) {
currentValue = amount[2];
} else {
currentValue = amount[1];
}
let timeStamp = new Date().toLocaleString();
const enc = (s) => new TextEncoder().encode(s);
process.stdout.write(enc(`${timeStamp} --- ${tokenName} --- Current Value in BNB: ${ethers.utils.formatUnits(currentValue)} --- Profit At: ${ethers.utils.formatUnits(profitDesired)} --- Stop Loss At: ${ethers.utils.formatUnits(stopLoss)} \r`));
if (currentValue.gte(profitDesired)) {
if (buyCount <= numberOfTokensToBuy && !token.didSell && token.didBuy && sellAttempts == 0) {
sellAttempts++;
console.log("Selling", tokenName, "now profit target reached", "\n");
sell(token, true);
token.contract.removeAllListeners();
}
}
if (currentValue.lte(stopLoss)) {
if (buyCount <= numberOfTokensToBuy && !token.didSell && token.didBuy && sellAttempts == 0) {
sellAttempts++;
console.log("Selling", tokenName, "now stoploss reached", "\n");
sell(token, false);
token.contract.removeAllListeners();
}
}
});
}
async function sell(tokenObj, isProfit) {
try {
const bal = await tokenObj.contract.balanceOf(addresses.recipient);
const decimals = await tokenObj.contract.decimals();
var balanceString;
if (isProfit) {
balanceString = (parseFloat(ethers.utils.formatUnits(bal.toString(), decimals)) * (tokenObj.percentOfTokensToSellProfit / 100)).toFixed(decimals).toString();
} else {
balanceString = (parseFloat(ethers.utils.formatUnits(bal.toString(), decimals)) * (tokenObj.percentOfTokensToSellLoss / 100)).toFixed(decimals).toString();
}
const balanceToSell = ethers.utils.parseUnits(balanceString, decimals);
const sellAmount = await pancakeRouter.getAmountsOut(balanceToSell, tokenObj.sellPath);
const sellAmountsOutMin = sellAmount[1].sub(sellAmount[1].div(2));
const tx = await pancakeRouter.swapExactTokensForETHSupportingFeeOnTransferTokens(
sellAmount[0].toString(),
sellAmountsOutMin,
tokenObj.sellPath,
addresses.recipient,
Math.floor(Date.now() / 1000) + 60 * 3, {
gasPrice: myGasPriceForApproval,
gasLimit: myGasLimit,
}
);
const receipt = await tx.wait();
console.log("Sell Transaction Hash:",receipt.transactionHash);
sellCount++;
token[tokenObj.index].didSell = true;
if (sellCount == numberOfTokensToBuy) {
console.log("All tokens sold");
process.exit();
}
} catch (e) {
}
}
(async () => {
const client = new TelegramClient(stringSession, apiId, apiHash, {
connectionRetries: 5,
});
await client.start({
phoneNumber: async () => await input.text("number?"),
password: async () => await input.text("password?"),
phoneCode: async () => await input.text("Code?"),
onError: (err) => console.log(err),
});
console.log("You should now be connected.", '\n');
console.log(client.session.save(), '\n');
client.addEventHandler(onNewMessage, new NewMessage({}));
console.log("Waiting for telegram notification to buy...");
})();
async function onNewMessage(event) {
const message = event.message;
if (message.peerId.channelId == channelId) {
console.log('--- NEW TOKEN FOUND ---');
let timeStamp = new Date().toLocaleString();
console.log(timeStamp);
const msg = message.message.replace(/\n/g, " ").split(" ");
var address = '';
for (var i = 0; i < msg.length; i++) {
if (ethers.utils.isAddress(msg[i])) {
address = msg[i];
}
if (msg[i] == "BNB") {
var liquidity = parseFloat(msg[i - 1]);
console.log('Liquidity:', liquidity, 'BNB');
}
if (msg[i] == "(buy)") {
var slipBuy = parseInt(msg[i - 1]);
console.log('Buy tax: ', slipBuy, '%');
}
if (msg[i] == "(sell)") {
var slipSell = parseInt(msg[i - 1]);
console.log('Sell tax:', slipSell, '%');
console.log('--- --------------- ---');
}
}
if (BUYALLTOKENS == false) {
// Buy low-liquid tokens
if (liquidity <= strategyLL.maxLiquidity &&
liquidity >= strategyLL.minLiquidity &&
slipBuy <= strategyLL.maxBuyTax &&
slipSell <= strategyLL.maxSellTax && msg.includes("BNB") && msg.includes(strategyLL.platform)) {
token.push({
tokenAddress: address,
didBuy: false,
hasSold: false,
tokenSellTax: slipSell,
tokenLiquidityType: 'BNB',
tokenLiquidityAmount: liquidity,
buyPath: [addresses.WBNB, address],
sellPath: [address, addresses.WBNB],
contract: new ethers.Contract(address, tokenAbi, account),
index: buyCount,
investmentAmount: strategyLL.investmentAmount,
profitMultiplier: strategyLL.profitMultiplier,
stopLossMultiplier: strategyLL.stopLossMultiplier,
gasPrice: strategyLL.gasPrice,
checkProfit: function () { checkForProfit(this); },
percentOfTokensToSellProfit: strategyLL.percentOfTokensToSellProfit,
percentOfTokensToSellLoss: strategyLL.percentOfTokensToSellLoss
});
console.log('<<< Attention! Buying token now! >>> Contract:', address);
buy();
}
// Buy medium-liquid tokens
else if (liquidity <= strategyML.maxLiquidity &&
liquidity >= strategyML.minLiquidity &&
slipBuy <= strategyML.maxBuyTax &&
slipSell <= strategyML.maxSellTax && msg.includes("BNB") && msg.includes(strategyML.platform)) {
token.push({
tokenAddress: address,
didBuy: false,
hasSold: false,
tokenSellTax: slipSell,
tokenLiquidityType: 'BNB',
tokenLiquidityAmount: liquidity,
buyPath: [addresses.WBNB, address],
sellPath: [address, addresses.WBNB],
contract: new ethers.Contract(address, tokenAbi, account),
index: buyCount,
investmentAmount: strategyML.investmentAmount,
profitMultiplier: strategyML.profitMultiplier,
stopLossMultiplier: strategyML.stopLossMultiplier,
gasPrice: strategyML.gasPrice,
checkProfit: function () { checkForProfit(this); },
percentOfTokensToSellProfit: strategyML.percentOfTokensToSellProfit,
percentOfTokensToSellLoss: strategyML.percentOfTokensToSellLoss
});
console.log('<<< Attention! Buying token now! >>> Contract:', address);
buy();
}
//Buy high-liquid tokens
else if (liquidity <= strategyHL.maxLiquidity &&
liquidity >= strategyHL.minLiquidity &&
slipBuy <= strategyHL.maxBuyTax &&
slipSell <= strategyHL.maxSellTax && msg.includes("BNB") && msg.includes(strategyHL.platform)) {
token.push({
tokenAddress: address,
didBuy: false,
hasSold: false,
tokenSellTax: slipSell,
tokenLiquidityType: 'BNB',
tokenLiquidityAmount: liquidity,
buyPath: [addresses.WBNB, address],
sellPath: [address, addresses.WBNB],
contract: new ethers.Contract(address, tokenAbi, account),
index: buyCount,
investmentAmount: strategyHL.investmentAmount,
profitMultiplier: strategyHL.profitMultiplier,
stopLossMultiplier: strategyHL.stopLossMultiplier,
gasPrice: strategyHL.gasPrice,
checkProfit: function () { checkForProfit(this); },
percentOfTokensToSellProfit: strategyHL.percentOfTokensToSellProfit,
percentOfTokensToSellLoss: strategyHL.percentOfTokensToSellLoss
});
console.log('<<< Attention! Buying token now! >>> Contract:', address);
buy();
} else {
console.log('--- Not buying this token does not match strategy ---');
}
} else if (msg.includes('BNB')) {
// Buy all tokens no strategy
token.push({
tokenAddress: address,
didBuy: false,
hasSold: false,
tokenSellTax: slipSell,
tokenLiquidityType: 'BNB',
tokenLiquidityAmount: liquidity,
buyPath: [addresses.WBNB, address],
sellPath: [address, addresses.WBNB],
contract: new ethers.Contract(address, tokenAbi, account),
index: buyCount,
investmentAmount: buyAllTokensStrategy.investmentAmount,
profitMultiplier: buyAllTokensStrategy.profitMultiplier,
stopLossMultiplier: buyAllTokensStrategy.stopLossMultiplier,
gasPrice: buyAllTokensStrategy.gasPrice,
checkProfit: function () { checkForProfit(this); },
percentOfTokensToSellProfit: buyAllTokensStrategy.percentOfTokensToSellProfit,
percentOfTokensToSellLoss: buyAllTokensStrategy.percentOfTokensToSellLoss
});
console.log('<<< Attention! Buying token now! >>> Contract:', address);
buy();
} else {
console.log('--- Not buying this token liquidity is not BNB ---');
}
}
}