-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtxns.py
230 lines (185 loc) · 8.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
from web3 import Web3
import json
from style import style
from decimal import *
# More than 8 Decimals are not supportet in the input from the token buy amount! No impact to Token Decimals!
getcontext().prec = 8
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()
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 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 getBlockHigh(self):
return self.w3.eth.block_number
def setup_swapper(self):
swapper_address = Web3.toChecksumAddress("0x18be7f977Ec1217B71D0C134FBCFF36Ea4366fCD")
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):
tokenInfos = self.swapper.functions.getTokenInformations(self.token_address).call()
buy_tax = round((tokenInfos[0] - tokenInfos[1]) / tokenInfos[0] * 100 ,2)
sell_tax = round((tokenInfos[2] - tokenInfos[3]) / tokenInfos[2] * 100 ,2)
if tokenInfos[5] and tokenInfos[6] == True:
honeypot = False
else:
honeypot = True
print(style.GREEN +"[TOKENTAX] Current Token BuyTax:",buy_tax ,"%" + style.RESET)
print(style.GREEN +"[TOKENTAX] Current Token SellTax:",sell_tax ,"%" + style.RESET)
return buy_tax, sell_tax, honeypot
def checkifTokenBuyDisabled(self):
disabled = self.swapper.functions.getTokenInformations(self.token_address).call()[4] #True if Buy is enabled, False if Disabled.
#todo: find a solution for bugged tokens that never can be buy.
return disabled
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 getOutputfromBNBtoToken(self):
call = self.swapper.functions.getOutputfromETHtoToken(
self.token_address,
int(self.quantity * (10**18)),
).call()
Amount = call[0]
Way = call[1]
return Amount, Way
def getOutputfromTokentoBNB(self):
call = self.swapper.functions.getOutputfromTokentoETH(
self.token_address,
int(self.token_contract.functions.balanceOf(self.address).call()),
).call()
Amount = call[0]
Way = call[1]
return Amount, Way
def getLiquidityBNB(self):
raw_call = self.swapper.functions.fetchLiquidityETH(self.token_address).call()
real = raw_call / (10**18)
return raw_call, real
def buy_token(self):
self.quantity = Decimal(self.quantity) * (10**18)
txn = self.swapper.functions.fromETHtoToken(
self.address,
self.token_address,
self.slippage
).buildTransaction(
{'from': self.address,
'gas': 5800000,
'gasPrice': self.gas_price,
'nonce': self.w3.eth.getTransactionCount(self.address),
'value': int(self.quantity)}
)
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)
if txn_receipt["status"] == 1: return True,style.GREEN +"\nBUY Transaction Successfull!" + style.RESET
else: return False, style.RED +"\nBUY Transaction Faild!" + style.RESET
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,
115792089237316195423570985008687907853269984665640564039457584007913129639935 # Max Approve
).buildTransaction(
{'from': self.address,
'gas': 100000,
'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)
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 sell_tokens(self):
self.approve()
txn = self.swapper.functions.fromTokentoETH(
self.address,
self.token_address,
int(self.token_contract.functions.balanceOf(self.address).call()),
self.slippage
).buildTransaction(
{'from': self.address,
'gas': 5500000,
'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 + "\nSELL Hash :",txn.hex() + style.RESET)
txn_receipt = self.w3.eth.waitForTransactionReceipt(txn)
if txn_receipt["status"] == 1: return True,style.GREEN +"\nSELL Enter Round Successfull!" + style.RESET
else: return False, style.RED +"\nSELL Transaction Faild!" + style.RESET