This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
deployer.py
64 lines (50 loc) · 2.77 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
import json
import os
from ethereum.tools import tester as t
from solc import compile_standard
from web3.contract import ConciseContract
from web3 import Web3, HTTPProvider
from plasma.config import plasma_config
OWN_DIR = os.path.dirname(os.path.realpath(__file__))
class Deployer(object):
def __init__(self, provider=HTTPProvider('http://localhost:8545')):
self.w3 = Web3(provider)
def get_dirs(self, path):
abs_contract_path = os.path.realpath(os.path.join(OWN_DIR, 'contracts'))
extra_args = [[file, [os.path.realpath(os.path.join(r, file))]] for r, d, f in os.walk(abs_contract_path) for file in f]
contracts = {}
for contract in extra_args:
contracts[contract[0]] = {'urls': contract[1]}
path = '{}/{}'.format(abs_contract_path, path)
return path, contracts
def compile_contract(self, path, args=()):
file_name = path.split('/')[1]
contract_name = file_name.split('.')[0]
path, contracts = self.get_dirs(path)
compiled_sol = compile_standard({'language': 'Solidity',
'sources': {**{path.split('/')[-1]: {'urls': [path]}}, **contracts}}, # Noqa E999
allow_paths=OWN_DIR + "/contracts")
abi = compiled_sol['contracts'][file_name][contract_name]['abi']
bytecode = compiled_sol['contracts'][file_name][contract_name]['evm']['bytecode']['object']
# Create the contract_data folder if it doesn't already exist
os.makedirs('contract_data', exist_ok=True)
contract_file = open('contract_data/%s.json' % (file_name.split('.')[0]), "w+")
json.dump(abi, contract_file)
contract_file.close()
return abi, bytecode, contract_name
def create_contract(self, path, args=(), gas=4410000, sender=t.k0):
abi, bytecode, contract_name = self.compile_contract(path, args)
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 in concise mode
contract_instance = self.w3.eth.contract(abi, contract_address, ContractFactoryClass=ConciseContract)
print("Successfully deployed {} contract!".format(contract_name))
return contract_instance
def get_contract(self, path):
file_name = path.split('/')[1]
abi = json.load(open('contract_data/%s.json' % (file_name.split('.')[0])))
return self.w3.eth.contract(abi, plasma_config['ROOT_CHAIN_CONTRACT_ADDRESS'])