-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.py
93 lines (79 loc) · 3.11 KB
/
connect.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
import sys
from web3 import Web3
from console import print_error, print_info
class Connect:
__instance = None
@staticmethod
def get_instance():
if Connect.__instance == None:
Connect()
return Connect.__instance
def __init__(self):
if Connect.__instance == None:
Connect.__instance = self
self.variables = {}
self.w3 = None
def add_connection(self, values={}):
for k, v in values.items():
self.variables[k] = v
self.create_connection()
def create_connection(self):
if self.w3 == None:
# connection = self.variables["provider"] + \
# ":" + self.variables["port"]
connection = self.variables["provider"]
self.w3 = Web3(Web3.HTTPProvider(connection))
if self.w3.isConnected():
print("Connection created with Blockchain")
else:
print("Connection error")
self.del_connection()
else:
print("Connection already exists")
def has_connection(self):
return self.w3 != None
def del_connection(self):
self.w3 = None
self.variables = {}
def deploy_contract(self, abi, bytecode, opcode, address, pkey):
if self.has_connection():
contract = self.w3.eth.contract(abi=abi, opcodes=opcode, bytecode=bytecode)
nonce = self.w3.eth.getTransactionCount(address)
gas_price = self.w3.eth.gas_price
params_transaction = {
"from": address,
"nonce": nonce,
"gasPrice": gas_price,
"chainId": int(self.variables["chainid"]),
}
transaction = contract.constructor().build_transaction(params_transaction)
# sign transaction
signed = self.w3.eth.account.sign_transaction(transaction, private_key=pkey)
hash = self.w3.eth.send_raw_transaction(signed.rawTransaction)
receipt = self.w3.eth.wait_for_transaction_receipt(hash)
print_info("Contract deployed!")
print_info("Contract Address: {}".format(receipt.contractAddress))
return receipt.contractAddress
else:
return {"No connection to blockchain"}
def get_bytecode(self, address):
if self.has_connection():
address = Web3.toChecksumAddress(address)
bytecode = self.w3.eth.get_code(address).hex()
return bytecode
else:
print_error("No connection to blockchain")
def get_variables(self):
return self.variables
def show_variables(self):
print_info(" Connection data on Blockchain")
print(" -----------------------------")
flag = 0
for key, value in self.variables.items():
flag += 1
if flag > 1:
print(" |")
sys.stdout.write(" |_")
sys.stdout.write("%s" % key)
sys.stdout.write(" = %s \n" % (value))
print("")