-
Notifications
You must be signed in to change notification settings - Fork 10
/
deployer.py
149 lines (111 loc) · 5.13 KB
/
deployer.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import json
from solc import compile_standard
from web3 import Web3, HTTPProvider
from web3.contract import ConciseContract
OUTPUT_DIR = 'contract_data'
class Deployer(object):
def __init__(self, contracts_dir, w3=Web3(HTTPProvider('http://localhost:8545'))):
self.contracts_dir = contracts_dir
self.w3 = w3
def get_solc_input(self):
"""Walks the contract directory and returns a Solidity input dict
Learn more about Solidity input JSON here: https://goo.gl/7zKBvj
Returns:
dict: A Solidity input JSON object as a dict
"""
solc_input = {
'language': 'Solidity',
'sources': {
file_name: {
'urls': [os.path.realpath(os.path.join(r, file_name))]
} for r, d, f in os.walk(self.contracts_dir) for file_name in f
},
'settings': {
'outputSelection': {
"*": {
"": [
"legacyAST",
"ast"
],
"*": [
"abi",
"evm.bytecode.object",
"evm.bytecode.sourceMap",
"evm.deployedBytecode.object",
"evm.deployedBytecode.sourceMap"
]
}
}
}
}
return solc_input
def compile_all(self):
"""Compiles all of the contracts in the /contracts directory
Creates {contract name}.json files in /build that contain
the build output for each contract.
"""
# Solidity input JSON
solc_input = self.get_solc_input()
# Compile the contracts
compilation_result = compile_standard(solc_input, allow_paths=self.contracts_dir)
# Create the output folder if it doesn't already exist
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Write the contract ABI to output files
compiled_contracts = compilation_result['contracts']
for contract_file in compiled_contracts:
for contract in compiled_contracts[contract_file]:
contract_name = contract.split('.')[0]
contract_data = compiled_contracts[contract_file][contract_name]
contract_data_path = OUTPUT_DIR + '/{0}.json'.format(contract_name)
with open(contract_data_path, "w+") as contract_data_file:
json.dump(contract_data, contract_data_file)
@staticmethod
def get_contract_data(contract_name):
"""Returns the contract data for a given contract
Args:
contract_name (str): Name of the contract to return.
Returns:
str, str: ABI and bytecode of the contract
"""
contract_data_path = OUTPUT_DIR + '/{0}.json'.format(contract_name)
with open(contract_data_path, 'r') as contract_data_file:
contract_data = json.load(contract_data_file)
abi = contract_data['abi']
bytecode = contract_data['evm']['bytecode']['object']
return abi, bytecode
def deploy_contract(self, contract_name, gas=5000000, args=(), concise=True):
"""Deploys a contract to the given Ethereum network using Web3
Args:
contract_name (str): Name of the contract to deploy. Must already be compiled.
provider (HTTPProvider): The Web3 provider to deploy with.
gas (int): Amount of gas to use when creating the contract.
args (obj): Any additional arguments to include with the contract creation.
concise (bool): Whether to return a Contract or ConciseContract instance.
Returns:
Contract: A Web3 contract instance.
"""
abi, bytecode = self.get_contract_data(contract_name)
contract = self.w3.eth.contract(abi=abi, bytecode=bytecode)
# Get transaction hash from deployed contract
tx_hash = contract.deploy(transaction={
'from': self.w3.eth.accounts[0],
'gas': gas
}, args=args)
# Get tx receipt to get contract address
tx_receipt = self.w3.eth.getTransactionReceipt(tx_hash)
contract_address = tx_receipt['contractAddress']
contract_instance = self.w3.eth.contract(address=contract_address, abi=abi)
return ConciseContract(contract_instance) if concise else contract_instance
def get_contract_at_address(self, contract_name, address, concise=True):
"""Returns a Web3 instance of the given contract at the given address
Args:
contract_name (str): Name of the contract. Must already be compiled.
address (str): Address of the contract.
concise (bool): Whether to return a Contract or ConciseContract instance.
Returns:
Contract: A Web3 contract instance.
"""
abi, _ = self.get_contract_data(contract_name)
contract_instance = self.w3.eth.contract(abi=abi, address=address)
return ConciseContract(contract_instance) if concise else contract_instance