Created
September 7, 2016 12:49
-
-
Save konradkonrad/1fecf9ed0416922997f68c4d92d86edc to your computer and use it in GitHub Desktop.
Create genesis['alloc'] dump of predeployed contracts with (py)ethereums `tester.py`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import json | |
from ethereum import tester | |
from ethereum.utils import remove_0x_head | |
def strip_0x(value): | |
if isinstance(value, basestring): | |
return remove_0x_head(value) | |
return value | |
def dump(contract_paths): | |
"""Create a state dump after deploying all solidity contracts from `contract_paths` | |
including only the deployed accounts. | |
Note: if you have library dependencies, all prior contracts are supplied as | |
`--libraries` argument to `solc`, so make sure the order is right. | |
Args: | |
contract_paths (list): list of absolute paths to solidity files. | |
Returns: | |
dump (dict): dictionary containing account state of contracts to be used in genesis['alloc'] | |
""" | |
state = tester.state(num_accounts=1) | |
state.block.number = 1158001 | |
deployed = [] | |
libraries = dict() | |
for path in contract_paths: | |
contract = state.abi_contract( | |
None, | |
path=path, | |
language='solidity', | |
libraries=libraries | |
) | |
state.mine(number_of_blocks=1) | |
libraries[os.path.split(path)[-1].split('.')[0]] = contract.address.encode('hex') | |
deployed.append(contract.address.encode('hex')) | |
alloc = dict() | |
for account in deployed: | |
alloc[account] = {key: strip_0x(value) | |
for key, value in state.block.account_to_dict(account).items()} | |
return alloc | |
if __name__ == '__main__': | |
if len(sys.argv) < 2 or '-h' in sys.argv: | |
print "Usage:\n\ttester_dump.py <solidity_contract_path>..." | |
else: | |
print json.dumps(dump(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment