# Path: hydrachain/native_contracts.py
# class Registry(object):
# class NativeContractBase(object):
# class CreateNativeContractInstance(NativeContractBase):
# class NativeABIContract(NativeContractBase):
# class ABIEvent(object):
#     class cproxy(object):
#     class cproxy(object):
# class TypedStorage(object):
# class Scalar(TypedStorage):
# class List(TypedStorage):
# class Dict(List):
# class IterableDict(Dict):
# class Struct(TypedStorage):
# class TypedStorageContract(NativeContractBase):
# class NativeContract(NativeABIContract, TypedStorageContract):
#     def __init__(self):
#     def mk_instance_address(self, native_contract, sender, nonce):
#     def is_instance_address(self, address):
#     def address_to_native_contract_class(self, address):
#     def register(self, contract):
#     def unregister(self, contract):
#     def abi_contracts(self):
#     def __contains__(self, address):
#     def __getitem__(self, address):
#     def __init__(self, ext, msg):
#     def _on_msg(cls, ext, msg):
#     def _get_storage_data(self, key):
#     def _set_storage_data(self, key, value):
#     def _safe_call(self):
#     def _safe_call(self):
# def _abi_decode_single_patch(typ, val):
# def abi_encode_args(method, args):
# def abi_decode_args(method, data):
# def abi_encode_return_vals(method, vals):
# def abi_decode_return_vals(method, data):
# def constant(f):
#     def __init__(self, ext, msg):
#         def mk_event_method(ctx, evt):
#             def m(*args):
#     def balance(self):  # can change during subcalls
#     def suicide(self, address):
#     def call(self, to, data='', **kargs):
#     def call_abi(self, to, abi_contract_method, *args, **kargs):
#     def _get_method_abi(cls, method):
#     def json_abi(cls):
#     def _abi_methods(cls):
#     def _find_method(cls, method_id):
#     def default_method(self):
#     def _safe_call(self):
#     def __setattr__(self, key, value):
#     def arg_types(cls):
#     def arg_names(cls):
#     def event_id(cls):
#     def __init__(self, ctx, *args):
#     def listen(cls, log_, address=None, callback=None):
# def listen_logs(state, event, address=None, callback=None):
# def tester_call_method(state, sender, method, *args):
# def tester_nac(state, sender, address, value=0):
#     def mk_method(method):
#         def m(s, *args):
# def test_call(block, sender, to, data='', gasprice=0, value=0):
# def chain_nac_proxy(chain, sender, contract_address, value=0):
#     def mk_method(method):
#         def m(s, *args):
# def tester_create_native_contract_instance(state, sender, contract, value=0):
#     def __init__(self, value_type):
#     def setup(self, prefix, getter, setter):
#     def _db_decode_type(cls, value_type, data):
#     def _db_encode_type(cls, value_type, val):
#     def _key(self, k):
#     def set(self, k=b'', v=None, value_type=None):
#             def _set(ts_k, v):
#     def get(self, k=b'', value_type=None):
#             def _set(ts_k, v):
#     def markstorage(self, k):
#     def __getitem__(self, i):
#     def __setitem__(self, i, v):
#     def updatelen(self, i, v):
#     def markstorage(self, i):
#     def __len__(self):
#     def append(self, v):
#     def __contains__(self, idx):
#     def __iter__(self):
#     def __getitem__(self, k):
#     def __setitem__(self, k, v):
#     def markstorage(self, k):
#     def __contains__(self, k):
#     def __len__(self):
#     def __getitem__(self, k):
#     def _ckey(self, idx):
#     def __setitem__(self, k, v):
#     def updatelen(self, k):
#     def markstorage(self, k):
#     def __contains__(self, idx):
#     def keys(self):
#     def values(self):
#     def items(self):
#     def __len__(self):
#     def __init__(self, **kwargs):
#     def __getattribute__(self, k, *default):
#     def _ckey(self, idx):
#     def __setattr__(self, k, v):
#     def setup(self, prefix, getter, setter):
#     def __init__(self, ext, msg):
#     def _prepare_storage(self, get_storage_data, set_storage_data):
#         def slots():
#                 def _mk_property(skalar):


from ethereum.utils import zpad
from ethereum import transactions
from pyethapp.rpc_client import ABIContract
from hydrachain import native_contracts as nc
import gevent.event
import ethereum.slogging as slogging
    """

    chain = app.services.chain

    # setup new block callbacks and events
    new_block_evt = gevent.event.Event()

    def _on_new_block(app):
        log.DEV('new block mined')
        new_block_evt.set()
    chain.on_new_head_cbs.append(_on_new_block)

    def wait_next_block():
        bn = chain.chain.head.number
        chain.consensus_manager.log('waiting for new block', block=bn)
        new_block_evt.wait(timeout)
        new_block_evt.clear()
        if chain.chain.head.number > bn:
            chain.consensus_manager.log('new block event', block=chain.chain.head.number)
        elif chain.chain.head.number == bn:
            chain.consensus_manager.log('wait_next_block timed out', block=bn)

    return wait_next_block


def create_contract_instance(app, sender, contract_template):
    log.DEV("creating instance", klass=contract_template)
    to_ = nc.CreateNativeContractInstance.address
    call_data = contract_template.address[-4:]
    tx = transact(app, sender, to_, data=call_data)

# Path: hydrachain/native_contracts.py
# class Registry(object):
# class NativeContractBase(object):
# class CreateNativeContractInstance(NativeContractBase):
# class NativeABIContract(NativeContractBase):
# class ABIEvent(object):
#     class cproxy(object):
#     class cproxy(object):
# class TypedStorage(object):
# class Scalar(TypedStorage):
# class List(TypedStorage):
# class Dict(List):
# class IterableDict(Dict):
# class Struct(TypedStorage):
# class TypedStorageContract(NativeContractBase):
# class NativeContract(NativeABIContract, TypedStorageContract):
#     def __init__(self):
#     def mk_instance_address(self, native_contract, sender, nonce):
#     def is_instance_address(self, address):
#     def address_to_native_contract_class(self, address):
#     def register(self, contract):
#     def unregister(self, contract):
#     def abi_contracts(self):
#     def __contains__(self, address):
#     def __getitem__(self, address):
#     def __init__(self, ext, msg):
#     def _on_msg(cls, ext, msg):
#     def _get_storage_data(self, key):
#     def _set_storage_data(self, key, value):
#     def _safe_call(self):
#     def _safe_call(self):
# def _abi_decode_single_patch(typ, val):
# def abi_encode_args(method, args):
# def abi_decode_args(method, data):
# def abi_encode_return_vals(method, vals):
# def abi_decode_return_vals(method, data):
# def constant(f):
#     def __init__(self, ext, msg):
#         def mk_event_method(ctx, evt):
#             def m(*args):
#     def balance(self):  # can change during subcalls
#     def suicide(self, address):
#     def call(self, to, data='', **kargs):
#     def call_abi(self, to, abi_contract_method, *args, **kargs):
#     def _get_method_abi(cls, method):
#     def json_abi(cls):
#     def _abi_methods(cls):
#     def _find_method(cls, method_id):
#     def default_method(self):
#     def _safe_call(self):
#     def __setattr__(self, key, value):
#     def arg_types(cls):
#     def arg_names(cls):
#     def event_id(cls):
#     def __init__(self, ctx, *args):
#     def listen(cls, log_, address=None, callback=None):
# def listen_logs(state, event, address=None, callback=None):
# def tester_call_method(state, sender, method, *args):
# def tester_nac(state, sender, address, value=0):
#     def mk_method(method):
#         def m(s, *args):
# def test_call(block, sender, to, data='', gasprice=0, value=0):
# def chain_nac_proxy(chain, sender, contract_address, value=0):
#     def mk_method(method):
#         def m(s, *args):
# def tester_create_native_contract_instance(state, sender, contract, value=0):
#     def __init__(self, value_type):
#     def setup(self, prefix, getter, setter):
#     def _db_decode_type(cls, value_type, data):
#     def _db_encode_type(cls, value_type, val):
#     def _key(self, k):
#     def set(self, k=b'', v=None, value_type=None):
#             def _set(ts_k, v):
#     def get(self, k=b'', value_type=None):
#             def _set(ts_k, v):
#     def markstorage(self, k):
#     def __getitem__(self, i):
#     def __setitem__(self, i, v):
#     def updatelen(self, i, v):
#     def markstorage(self, i):
#     def __len__(self):
#     def append(self, v):
#     def __contains__(self, idx):
#     def __iter__(self):
#     def __getitem__(self, k):
#     def __setitem__(self, k, v):
#     def markstorage(self, k):
#     def __contains__(self, k):
#     def __len__(self):
#     def __getitem__(self, k):
#     def _ckey(self, idx):
#     def __setitem__(self, k, v):
#     def updatelen(self, k):
#     def markstorage(self, k):
#     def __contains__(self, idx):
#     def keys(self):
#     def values(self):
#     def items(self):
#     def __len__(self):
#     def __init__(self, **kwargs):
#     def __getattribute__(self, k, *default):
#     def _ckey(self, idx):
#     def __setattr__(self, k, v):
#     def setup(self, prefix, getter, setter):
#     def __init__(self, ext, msg):
#     def _prepare_storage(self, get_storage_data, set_storage_data):
#         def slots():
#                 def _mk_property(skalar):


# Path: hydrachain/nc_utils.py
from ethereum.utils import zpad
from ethereum import transactions
from pyethapp.rpc_client import ABIContract
from hydrachain import native_contracts as nc
import gevent.event
import ethereum.slogging as slogging

    """

    chain = app.services.chain

    # setup new block callbacks and events
    new_block_evt = gevent.event.Event()

    def _on_new_block(app):
        log.DEV('new block mined')
        new_block_evt.set()
    chain.on_new_head_cbs.append(_on_new_block)

    def wait_next_block():
        bn = chain.chain.head.number
        chain.consensus_manager.log('waiting for new block', block=bn)
        new_block_evt.wait(timeout)
        new_block_evt.clear()
        if chain.chain.head.number > bn:
            chain.consensus_manager.log('new block event', block=chain.chain.head.number)
        elif chain.chain.head.number == bn:
            chain.consensus_manager.log('wait_next_block timed out', block=bn)

    return wait_next_block


def create_contract_instance(app, sender, contract_template):
    log.DEV("creating instance", klass=contract_template)
    to_ = nc.CreateNativeContractInstance.address
    call_data = contract_template.address[-4:]
    tx = transact(app, sender, to_, data=call_data)

    instance_address = nc.registry.mk_instance_address(contract_template, sender, tx.nonce)