-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.py
86 lines (66 loc) · 2.86 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
#!/usr/bin/env python3
# encoding: utf-8
import configparser
import os
import json
from web3 import Web3
import time
CONFIG_PATH = 'etc/config.conf'
def _ComposeContractBuildPath(truffle_build_path, target_contract_name):
json_filename = '{0}.json'.format(target_contract_name)
target_path = os.path.join(*[truffle_build_path, 'contracts', json_filename])
return target_path
def _GetBuildContractJsonFileAttribute(filepath, key):
with open(filepath) as f:
return json.load(f)[key]
def _DeploySmartContract(contract_path, file_ipc):
w3 = Web3(Web3.IPCProvider(file_ipc))
abi = _GetBuildContractJsonFileAttribute(contract_path, 'abi')
bytecode = _GetBuildContractJsonFileAttribute(contract_path, 'bytecode')
contract = w3.eth.contract(abi=abi, bytecode=bytecode)
tx_hash = contract.deploy(transaction={'from': w3.eth.accounts[0], 'gas': 910000})
tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
w3.miner.start(1)
retry_time = 0
while not tx_receipt and retry_time < 10:
print(' wait for miner!')
time.sleep(2)
tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
retry_time += 1
w3.miner.stop()
if not tx_receipt:
raise IOError('still cannot get contract result')
return tx_receipt, w3.eth.accounts[0]
def _DumpContractInfo(contract_path, contract_detail, contract_owner, file_path):
file_path = os.path.abspath(file_path)
dir_path = os.path.dirname(file_path)
if not os.path.isdir(dir_path):
os.makedirs(dir_path)
json_data = {
'abi': _GetBuildContractJsonFileAttribute(contract_path, 'abi'),
'address': contract_detail['contractAddress'],
'owner': contract_owner,
'detail': {k: v for k, v in contract_detail.items()}
}
with open(file_path, 'w') as f:
json.dump(json_data, f)
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
print('==== Deploy started ====')
contract_path = _ComposeContractBuildPath(config.get('Deploy', 'truffle_build_path'),
config.get('Deploy', 'target_contract_name'))
assert os.path.isfile(contract_path), 'file path {0} doesn\'t exist'.format(contract_path)
print('==== Deploy contract to private chain ====')
contract_detail, contract_owner = _DeploySmartContract(contract_path,
config.get('Ethereum', 'file_ipc'))
_DumpContractInfo(contract_path,
contract_detail,
contract_owner,
config.get('Output', 'file_path'))
print('==== Deploy finished ====')
print('Contract detail:')
for k, v in contract_detail.items():
print(' {0}: {1}'.format(k, v))
print('Contract owner:')
print(' owner: {0}'.format(contract_owner))