-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings.py
135 lines (120 loc) · 5.54 KB
/
settings.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
import time
import requests
from decouple import config
from termcolor import cprint
from web3 import Web3
from abi import hexAbi
class MyAccount:
"""
BLUEPRINT OF ETH ACCOUNT TO BE MANAGED BY SCRIPT
"""
ethContractAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"
hexContractAddress = "0x2b591e99afE9f32eAA6214f7B7629768c40Eeb39"
def __init__(self, ethWalletAddress, walletBAddress, chain='eth'):
self.privateKey = config(f'{ethWalletAddress}')
self.ethWalletAddress = ethWalletAddress
self.walletBAddress = walletBAddress
if chain == 'eth':
self.chain = config('INFURA_KEY')
self.chainId = 1
elif chain == 'pulsechain':
self.chain = config('pulsechain')
self.chainId = 941
def createConnection(self):
"""
CREATES CONNECTION TO ETH MAINNET VIA INFURA API
:return: <Web3>
"""
w3 = Web3(Web3.HTTPProvider(self.chain))
if w3.isConnected() :
return w3
else:
raise Exception("Connection to web3 is unsuccessful")
def getWalletBalance(self):
"""
GET ACCOUNT BALANCE OF ETH WALLET
:return: <float>
"""
w3 = self.createConnection()
balance = w3.eth.getBalance(w3.toChecksumAddress(self.ethWalletAddress))
balance = w3.fromWei(balance, 'ether')
return balance
def signTransaction(self, transaction):
"""
SIGN TRANSACTION AND BROADCAST TO ETH MAINNET
:param transaction: <dict> transaction to be signed
:return: void
"""
w3 = self.createConnection()
cprint("[+] Signing transaction", "yellow")
try:
signed = w3.eth.account.signTransaction(transaction, self.privateKey)
except TypeError:
cprint("[+] Invalid key for address \n[+] Signing transaction failed ", "red")
tx = None
else:
cprint("[+] Signing transaction successful", "green")
cprint("[+] Broadcasting signed transaction", "yellow")
try:
tx = w3.eth.sendRawTransaction(signed.rawTransaction)
except Exception as e:
print(e)
cprint("[+] Broadcasting signed transaction failed", "red")
tx = None
else:
cprint("[+] Broadcasting signed transaction successful \n Transaction Hash : " + str(tx.hex()), "green")
time.sleep(60)
return tx
def getHexBalance(self):
"""
GET BALANCE OF DAI ADDRESS ASSOCIATED WITH ETH ACCOUNT
:return:<float> or None
"""
w3 = self.createConnection()
contract_instance = w3.eth.contract(address=Web3.toChecksumAddress(self.hexContractAddress), abi=hexAbi)
balance = contract_instance.functions.balanceOf(Web3.toChecksumAddress(self.ethWalletAddress)).call()
return balance
def get_fast_gas_price(self):
response = requests.get("https://ethgasstation.info/json/ethgasAPI.json")
gas_price = response.json()['fastest'] / 10
return gas_price
def send_max_eth(self, recipient_wallet):
w3 = self.createConnection()
b = self.getWalletBalance()
gas_price = self.get_fast_gas_price()
balance = w3.toWei(b, 'ether')
gwei = w3.toWei(gas_price, 'gwei')
b = w3.fromWei(balance, 'ether')
g = w3.fromWei(gwei*21000, 'ether')
cprint(f'[+] {b} eth found', 'yellow')
if balance > gwei * 21000:
max_send = b - g
transaction = {"to": Web3.toChecksumAddress(recipient_wallet),
'chainId': self.chainId,
"value": w3.toWei(max_send, 'ether'),
"gas": 21000,
"gasPrice": gwei,
"nonce": w3.eth.getTransactionCount(self.ethWalletAddress)}
cprint(transaction, 'blue')
return self.signTransaction(transaction)
else:
cprint(f"[+] Not enough gas", 'red')
def send_hex(self, recipient_wallet):
w3 = self.createConnection()
hexbalance = self.getHexBalance()
ebalance = self.getWalletBalance()
ethbalance = w3.toWei(ebalance, 'gwei')
nonce = w3.eth.getTransactionCount(self.ethWalletAddress)
gas_price = self.get_fast_gas_price()
cprint(f'[+] Found {hexbalance} hex', 'yellow')
cprint(f'[+] {ebalance} eth found for gas', 'yellow')
if ethbalance > gas_price * 80000 and hexbalance > 1:
contract_instance = w3.eth.contract(address=Web3.toChecksumAddress(self.hexContractAddress), abi=hexAbi)
txn = contract_instance.functions.transfer(Web3.toChecksumAddress(recipient_wallet), hexbalance).buildTransaction({'chainId': self.chainId,
'gas': 80000,
'maxFeePerGas': w3.toWei(gas_price, 'gwei'),
'maxPriorityFeePerGas': w3.toWei(gas_price, 'gwei'),
'nonce': nonce, })
return self.signTransaction(txn)
else:
cprint(f'[+] Not enough eth or hex', 'red')