forked from TrendingTechnology/Pancakeswap_BSC_Sniper_Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txns.py
355 lines (325 loc) · 13.5 KB
/
txns.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
from web3 import Web3, constants
import json
import time
from style import style
class TXN():
def __init__(self, token_address, quantity):
self.w3 = self.connect()
self.address, self.private_key = self.setup_address()
self.token_address = Web3.toChecksumAddress(token_address)
self.token_contract = self.setup_token()
self.swapper_address, self.swapper = self.setup_swapper()
self.slippage = self.setupSlippage()
self.quantity = quantity
self.MaxGasInBNB, self.gas_price = self.setupGas()
self.initSettings()
def connect(self):
with open("./Settings.json") as f:
keys = json.load(f)
if keys["RPC"][:2].lower() == "ws":
w3 = Web3(Web3.WebsocketProvider(keys["RPC"]))
else:
w3 = Web3(Web3.HTTPProvider(keys["RPC"]))
return w3
def initSettings(self):
with open("./Settings.json") as f:
keys = json.load(f)
self.timeout = keys["timeout"]
self.safeGas = keys["SaveGasCost"]
def setupGas(self):
with open("./Settings.json") as f:
keys = json.load(f)
return keys['MaxTXFeeBNB'], int(keys['GWEI_GAS'] * (10**9))
def setup_address(self):
with open("./Settings.json") as f:
keys = json.load(f)
if len(keys["metamask_address"]) <= 41:
print(style.RED + "Set your Address in the keys.json file!" + style.RESET)
raise SystemExit
if len(keys["metamask_private_key"]) <= 42:
print(style.RED + "Set your PrivateKey in the keys.json file!" + style.RESET)
raise SystemExit
return keys["metamask_address"], keys["metamask_private_key"]
def setupSlippage(self):
with open("./Settings.json") as f:
keys = json.load(f)
return keys['Slippage']
def get_token_decimals(self):
return self.token_contract.functions.decimals().call()
def get_token_Name(self):
return self.token_contract.functions.name().call()
def get_token_Symbol(self):
return self.token_contract.functions.symbol().call()
def getBlockHigh(self):
return self.w3.eth.block_number
def setup_swapper(self):
swapper_address = Web3.toChecksumAddress(
"0x2D4e39B07117937b2CB51b8a7ab8189b50D41184")
with open("./abis/BSC_Swapper.json") as f:
contract_abi = json.load(f)
swapper = self.w3.eth.contract(
address=swapper_address, abi=contract_abi)
return swapper_address, swapper
def setup_token(self):
with open("./abis/bep20_abi_token.json") as f:
contract_abi = json.load(f)
token_contract = self.w3.eth.contract(
address=self.token_address, abi=contract_abi)
return token_contract
def get_token_balance(self):
return self.token_contract.functions.balanceOf(self.address).call() / (10 ** self.token_contract.functions.decimals().call())
def checkToken(self):
# uint256 BuyEstimateOutput,
# uint256 BuyRealOutput,
# uint256 SellEstimateOutput,
# uint256 SellRealOutput,
# bool Buy,
# bool Approve,
# bool Sell,
# bool SellOutputArrive
call = self.swapper.functions.getTokenInformations(
self.token_address).call()
buy_tax = round(
((call[0] - call[1]) / (call[0]) * 100) - 0.7)
sell_tax = round(
((call[2] - call[3]) / (call[2]) * 100) - 0.7)
if call[4] and call[5] and call[6] and call[7] == True:
honeypot = False
else:
honeypot = True
return buy_tax, sell_tax, honeypot
def checkifTokenBuyDisabled(self):
try:
self.swapper.functions.snipeETHtoToken(
self.token_address,
int(self.slippage * 10),
self.address
).buildTransaction(
{
'from': self.address,
'gasPrice': self.gas_price,
'nonce': self.w3.eth.getTransactionCount(self.address),
'value': int(self.quantity * (10**18))
}
)
return True
except Exception as e:
return False
def estimateGas(self, txn):
gas = self.w3.eth.estimateGas({
"from": txn['from'],
"to": txn['to'],
"value": txn['value'],
"data": txn['data']})
gas = gas + (gas / 10) # Adding 1/10 from gas to gas!
maxGasBNB = Web3.fromWei(gas * self.gas_price, "ether")
print(style.GREEN + "\nMax Transaction cost " +
str(maxGasBNB) + " BNB" + style.RESET)
if maxGasBNB > self.MaxGasInBNB:
print(style.RED + "\nTx cost exceeds your settings, exiting!")
raise SystemExit
return gas
def getOutputTokenToBNB(self, percent: int = 100):
TokenBalance = int(
self.token_contract.functions.balanceOf(self.address).call())
if TokenBalance > 0:
AmountForInput = int((TokenBalance / 100) * percent)
if percent == 100:
AmountForInput = TokenBalance
Amount, Way, DexWay = self.fetchOutputTokentoBNB(AmountForInput)
return Amount, Way, DexWay
def fetchOutputBNBtoToken(self):
call = self.swapper.functions.fetchOutputETHtoToken(
self.token_address,
int(self.quantity * (10**18)),
).call()
Amount = call[0]
Way = call[1]
DexWay = call[2]
return Amount, Way, DexWay
def fetchOutputTokentoBNB(self, quantity: int):
call = self.swapper.functions.fetchOutputTokentoETH(
self.token_address,
quantity
).call()
Amount = call[0]
Way = call[1]
DexWay = call[2]
return Amount, Way, DexWay
def getLiquidityUSD(self):
raw_call = self.swapper.functions.getLiquidityUSD(
self.token_address).call()
real = round(raw_call[-1] / (10**18), 2)
return raw_call, real
def is_approve(self):
Approve = self.token_contract.functions.allowance(
self.address, self.swapper_address).call()
Aproved_quantity = self.token_contract.functions.balanceOf(
self.address).call()
if int(Approve) <= int(Aproved_quantity):
return False
else:
return True
def approve(self):
if self.is_approve() == False:
txn = self.token_contract.functions.approve(
self.swapper_address,
Web3.toInt(hexstr=constants.MAX_INT)
).buildTransaction(
{'from': self.address,
'gasPrice': self.gas_price,
'nonce': self.w3.eth.getTransactionCount(self.address),
'value': 0}
)
txn.update({'gas': int(self.estimateGas(txn))})
signed_txn = self.w3.eth.account.sign_transaction(
txn,
self.private_key
)
txn = self.w3.eth.sendRawTransaction(signed_txn.rawTransaction)
print(style.GREEN + "\nApprove Hash:", txn.hex()+style.RESET)
txn_receipt = self.w3.eth.waitForTransactionReceipt(
txn, timeout=self.timeout)
if txn_receipt["status"] == 1:
return True, style.GREEN + "\nApprove Successfull!" + style.RESET
else:
return False, style.RED + "\nApprove Transaction Faild!" + style.RESET
else:
return True, style.GREEN + "\nAllready approved!" + style.RESET
def buy_token(self, retry: int = 1):
if self.safeGas != True:
return self.buy_token_fast(retry)
else:
return self.buy_token_cheap(retry)
def buy_token_fast(self, trys):
while trys:
try:
txn = self.swapper.functions.snipeETHtoToken(
self.token_address,
self.slippage * 10,
self.address
).buildTransaction(
{'from': self.address,
'gasPrice': self.gas_price,
'nonce': self.w3.eth.getTransactionCount(self.address),
'value': int(self.quantity * (10**18))}
)
txn.update({'gas': int(self.estimateGas(txn))})
signed_txn = self.w3.eth.account.sign_transaction(
txn,
self.private_key
)
txn = self.w3.eth.sendRawTransaction(signed_txn.rawTransaction)
print(style.GREEN + "\nBUY Hash:", txn.hex() + style.RESET)
txn_receipt = self.w3.eth.waitForTransactionReceipt(
txn, timeout=self.timeout)
if txn_receipt["status"] == 1:
return True, style.GREEN + "\nBUY Transaction Successfull!" + style.RESET
else:
return False, style.RED + "\nBUY Transaction Faild!" + style.RESET
except Exception as e:
print(e)
trys -= 1
print(style.RED + "\nBUY Transaction Faild!" + style.RESET)
time.sleep(1)
def buy_token_cheap(self, trys):
while trys:
try:
Amount, TokenWay, DexWay = self.fetchOutputBNBtoToken()
minAmount = int(Amount - ((Amount / 100) * self.slippage))
txn = self.swapper.functions.fromETHtoToken(
minAmount,
TokenWay,
DexWay,
self.address
).buildTransaction(
{'from': self.address,
'gasPrice': self.gas_price,
'nonce': self.w3.eth.getTransactionCount(self.address),
'value': int(self.quantity * (10**18))}
)
txn.update({'gas': int(self.estimateGas(txn))})
signed_txn = self.w3.eth.account.sign_transaction(
txn,
self.private_key
)
txn = self.w3.eth.sendRawTransaction(signed_txn.rawTransaction)
print(style.GREEN + "\nBUY Hash:", txn.hex() + style.RESET)
txn_receipt = self.w3.eth.waitForTransactionReceipt(
txn, timeout=self.timeout)
if txn_receipt["status"] == 1:
return True, style.GREEN + "\nBUY Transaction Successfull!" + style.RESET
else:
return False, style.RED + "\nBUY Transaction Faild!" + style.RESET
except Exception as e:
print(e)
trys -= 1
print(style.RED + "\nBUY Transaction Faild!" + style.RESET)
time.sleep(1)
def sell_tokens(self, percent: int = 100):
self.approve()
TokenBalance = int(
self.token_contract.functions.balanceOf(self.address).call())
if TokenBalance > 0:
AmountForSell = int((TokenBalance / 100) * percent)
if percent == 100:
AmountForSell = TokenBalance
if self.safeGas != True:
return self.sell_tokens_fast(AmountForSell)
else:
return self.sell_tokens_cheap(AmountForSell)
else:
print(style.RED + "\nYou dont have any tokens to sell!" + style.RESET)
def sell_tokens_fast(self, Amount: int):
txn = self.swapper.functions.snipeTokentoETH(
Amount,
self.token_address,
self.slippage * 10,
self.address
).buildTransaction(
{'from': self.address,
'gasPrice': self.gas_price,
'nonce': self.w3.eth.getTransactionCount(self.address)}
)
txn.update({'gas': int(self.estimateGas(txn))})
signed_txn = self.w3.eth.account.sign_transaction(
txn,
self.private_key
)
txn = self.w3.eth.sendRawTransaction(signed_txn.rawTransaction)
print(style.GREEN + "\nSELL Hash :", txn.hex() + style.RESET)
txn_receipt = self.w3.eth.waitForTransactionReceipt(
txn, timeout=self.timeout)
if txn_receipt["status"] == 1:
return True, style.GREEN + "\nSELL Transaction Successfull!" + style.RESET
else:
return False, style.RED + "\nSELL Transaction Faild!" + style.RESET
def sell_tokens_cheap(self, Amount: int):
AmountOut, TokenWay, DexWay = self.fetchOutputTokentoBNB(Amount)
minAmount = int(AmountOut - ((AmountOut / 100) * self.slippage))
txn = self.swapper.functions.fromTokentoETH(
Amount,
minAmount,
TokenWay,
DexWay,
self.address
).buildTransaction(
{
'from': self.address,
'gasPrice': self.gas_price,
'nonce': self.w3.eth.getTransactionCount(self.address)
}
)
txn.update({'gas': int(self.estimateGas(txn))})
signed_txn = self.w3.eth.account.sign_transaction(
txn,
self.private_key
)
txn = self.w3.eth.sendRawTransaction(signed_txn.rawTransaction)
print(style.GREEN + "\nSELL Hash :", txn.hex() + style.RESET)
txn_receipt = self.w3.eth.waitForTransactionReceipt(
txn, timeout=self.timeout)
if txn_receipt["status"] == 1:
return True, style.GREEN + "\nSELL Transaction Successfull!" + style.RESET
else:
return False, style.RED + "\nSELL Transaction Faild!" + style.RESET