forked from topliceanu/IC3-dec-id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
123 lines (89 loc) · 4.09 KB
/
deploy.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
import json
from web3 import Web3, HTTPProvider
from eth_account import Account
from compile import compile_contract
def deploy_voting_contract(num_of_blocks: int = 10000) -> str:
# Setup infura project ID and endpoint
infura_url = INFURA_URL
# Connect to the Ethereum blockchain using Infura
w3 = Web3(HTTPProvider(infura_url))
# Set the private key of the account that will vote
private_key = '0x9c07c5b4e3ad30f78303fc1d73eb6e269aa1753bb2f4500fa7210b876156f467'
my_account = Account.from_key(private_key)
w3.eth.default_account = my_account.address
# Define the contract ABI and bytecode (these should be the ABI and bytecode of your contract)
contract_abi, contract_bytecode = compile_contract('Voting.sol', 'VotingContract')
with open(f'Voting.abi', 'w') as f:
json.dump(contract_abi, f)
with open('issuer_keys.json') as f:
issuer_pk = json.load(f)['issuer_pk']
verifier_contract_address = Web3.to_checksum_address('0x81480f02F452b912D38A76852A3ea78647659F22')
# Define the arguments for the contract constructor (if any)
constructor_args = (num_of_blocks, issuer_pk[0],issuer_pk[1], verifier_contract_address)
# Build a transaction to deploy the contract
contract_factory = w3.eth.contract(abi=contract_abi, bytecode=contract_bytecode)
contract = contract_factory.constructor(*constructor_args)
# Estimate the gas required to deploy the contract
gas_estimate = w3.eth.estimate_gas({
'from': w3.eth.default_account,
'data': contract.data_in_transaction,
'to': None,
'value': 0
})
tx = contract.build_transaction({
'from': w3.eth.default_account,
'nonce': w3.eth.get_transaction_count(w3.eth.default_account),
'gas': gas_estimate,
'gasPrice': w3.eth.gas_price,
})
# Sign the transaction
signed_tx = w3.eth.account.sign_transaction(tx, private_key)
# Send the transaction
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print(tx_hash.hex())
# Wait for the transaction to be mined
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash, 60)
address = tx_receipt['contractAddress']
with open(f'voting_contract_address.json', 'w') as f:
json.dump({'address': address}, f)
return tx_receipt['contractAddress']
def deploy_verifier_contract() -> str:
# Setup infura project ID and endpoint
infura_url = INFURA_URL
# Connect to the Ethereum blockchain using Infura
w3 = Web3(HTTPProvider(infura_url))
# Set the private key of the account that will vote
private_key = '0x9c07c5b4e3ad30f78303fc1d73eb6e269aa1753bb2f4500fa7210b876156f467'
my_account = Account.from_key(private_key)
w3.eth.default_account = my_account.address
# Define the contract ABI and bytecode (these should be the ABI and bytecode of your contract)
contract_abi, contract_bytecode = compile_contract('voting_verifier.sol', 'Groth16Verifier')
with open(f'Groth16Verifier.abi', 'w') as f:
json.dump(contract_abi, f)
# Build a transaction to deploy the contract
contract_factory = w3.eth.contract(abi=contract_abi, bytecode=contract_bytecode)
contract = contract_factory.constructor()
# Estimate the gas required to deploy the contract
gas_estimate = w3.eth.estimate_gas({
'from': w3.eth.default_account,
'data': contract.data_in_transaction,
'to': None,
'value': 0
})
tx = contract.build_transaction({
'from': w3.eth.default_account,
'nonce': w3.eth.get_transaction_count(w3.eth.default_account),
'gas': gas_estimate,
'gasPrice': w3.eth.gas_price,
})
# Sign the transaction
signed_tx = w3.eth.account.sign_transaction(tx, private_key)
# Send the transaction
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
# Wait for the transaction to be mined
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash, 60)
return tx_receipt['contractAddress']
if __name__ == '__main__':
tx = deploy_voting_contract(100000000)
# tx = deploy_verifier_contract()
print(tx)