-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
50 lines (41 loc) · 1.55 KB
/
deploy.py
File metadata and controls
50 lines (41 loc) · 1.55 KB
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
from web3 import Web3
import json
import time
try:
# 1. Connect to Ganache
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545'))
assert w3.is_connected(), "Failed to connect to Ganache"
# 2. Set deployer account (first Ganache account)
w3.eth.default_account = w3.eth.accounts[0]
print(f"Deployer: {w3.eth.default_account}")
# 3. Load contract artifacts
with open('blockchain/contract_abi.json') as f:
abi = json.load(f)
with open('blockchain/crypto_identifier.bin', 'r') as f:
bytecode = f.read().strip() # Remove any newlines
# 4. Deploy with sufficient gas
contract = w3.eth.contract(abi=abi, bytecode=bytecode)
tx_hash = contract.constructor().transact({
'gas': 3000000,
'from': w3.eth.default_account
})
# 5. Wait for receipt (up to 2 minutes)
start_time = time.time()
while time.time() - start_time < 120:
try:
tx_receipt = w3.eth.get_transaction_receipt(tx_hash)
if tx_receipt is not None:
break
except:
pass
time.sleep(2)
if not tx_receipt:
raise TimeoutError("Deployment timed out after 2 minutes")
print(f"Contract deployed at: {tx_receipt.contractAddress}")
print(f"Block: {tx_receipt.blockNumber}")
print(f"Gas used: {tx_receipt.gasUsed}")
except Exception as e:
print(f"Error: {type(e).__name__}: {str(e)}")
if 'tx_hash' in locals():
print(f"Transaction hash: {tx_hash.hex()}")
print(f"Check Ganache GUI for details")