Esempio n. 1
0
def validate_abi(abi):
    """
    Helper function for validating an ABI
    """
    if not is_list_like(abi):
        raise ValueError("'abi' is not a list")

    if not all(is_dict(e) for e in abi):
        raise ValueError("'abi' is not a list of dictionaries")

    functions = filter_by_type('function', abi)
    selectors = groupby(compose(encode_hex, function_abi_to_4byte_selector),
                        functions)
    duplicates = valfilter(lambda funcs: len(funcs) > 1, selectors)
    if duplicates:
        raise ValueError('Abi contains functions with colliding selectors. '
                         'Functions {0}'.format(
                             _prepare_selector_collision_msg(duplicates)))
Esempio n. 2
0
    def sign_and_send_raw_middleware(make_request, w3):

        fill_tx = compose(fill_transaction_defaults(w3), fill_nonce(w3))

        def middleware(method, params):
            if method != "eth_sendTransaction":
                return make_request(method, params)
            else:
                transaction = fill_tx(params[0])

            if 'from' not in transaction:
                return make_request(method, params)
            elif transaction.get('from') not in accounts:
                return make_request(method, params)

            account = accounts[transaction['from']]
            raw_tx = account.signTransaction(transaction).rawTransaction

            return make_request("eth_sendRawTransaction", [raw_tx])

        return middleware
Esempio n. 3
0
from eth_utils.curried import (
    apply_formatters_to_dict,
    apply_key_map,
)
from hexbytes import (
    HexBytes, )

from vns_web3.middleware.formatting import (
    construct_formatting_middleware, )
from vns_web3.utils.toolz import (
    compose, )

remap_geth_poa_fields = apply_key_map({
    'extraData': 'proofOfAuthorityData',
})

pythonic_geth_poa = apply_formatters_to_dict({
    'proofOfAuthorityData': HexBytes,
})

geth_poa_cleanup = compose(pythonic_geth_poa, remap_geth_poa_fields)

geth_poa_middleware = construct_formatting_middleware(result_formatters={
    'eth_getBlockByHash':
    geth_poa_cleanup,
    'eth_getBlockByNumber':
    geth_poa_cleanup,
}, )
Esempio n. 4
0
def chain_id_validator(web3):
    return compose(apply_formatter_at_index(transaction_normalizer, 0),
                   transaction_param_validator(web3))
Esempio n. 5
0
def apply_formatters_to_args(*formatters):
    return compose(*(apply_formatter_at_index(formatter, index)
                     for index, formatter in enumerate(formatters)))
Esempio n. 6
0

def is_testrpc_available():
    try:
        import testrpc  # noqa: F401
        return True
    except ImportError:
        return False


to_integer_if_hex = apply_formatter_if(is_string, hex_to_integer)

TRANSACTION_FORMATTERS = {
    'to':
    apply_formatter_if(
        compose(complement(bool), decode_hex),
        static_return(None),
    ),
}


def ethtestrpc_string_middleware(make_request, web3):
    def middleware(method, params):
        return valmap(to_text, make_request(method, params))

    return middleware


ethtestrpc_middleware = construct_formatting_middleware(
    request_formatters={
        'eth_uninstallFilter': apply_formatter_at_index(to_integer_if_hex, 0),
Esempio n. 7
0
 def factory(cls, *args, **kwargs):
     return compose(cls, Contract.factory(*args, **kwargs))
Esempio n. 8
0
TRANSACTION_PARAMS_MAPPING = {
    'gasPrice': 'gas_price',
}

transaction_params_remapper = apply_key_map(TRANSACTION_PARAMS_MAPPING)

TRANSACTION_PARAMS_FORMATTERS = {
    'gas': to_integer_if_hex,
    'gasPrice': to_integer_if_hex,
    'value': to_integer_if_hex,
    'nonce': to_integer_if_hex,
}

transaction_params_formatter = compose(
    # remove nonce for now due to issue https://github.com/ethereum/eth-tester/issues/80
    remove_key_if('nonce', lambda _: True),
    apply_formatters_to_dict(TRANSACTION_PARAMS_FORMATTERS),
)

FILTER_PARAMS_MAPPINGS = {
    'fromBlock': 'from_block',
    'toBlock': 'to_block',
}

filter_params_remapper = apply_key_map(FILTER_PARAMS_MAPPINGS)

FILTER_PARAMS_FORMATTERS = {
    'fromBlock': to_integer_if_hex,
    'toBlock': to_integer_if_hex,
}
Esempio n. 9
0
block_formatter = apply_formatters_to_dict(BLOCK_FORMATTERS)

SYNCING_FORMATTERS = {
    'startingBlock': to_integer_if_hex,
    'currentBlock': to_integer_if_hex,
    'highestBlock': to_integer_if_hex,
    'knownStates': to_integer_if_hex,
    'pulledStates': to_integer_if_hex,
}

syncing_formatter = apply_formatters_to_dict(SYNCING_FORMATTERS)

TRANSACTION_POOL_CONTENT_FORMATTERS = {
    'pending': compose(
        keymap(to_ascii_if_bytes),
        valmap(transaction_formatter),
    ),
    'queued': compose(
        keymap(to_ascii_if_bytes),
        valmap(transaction_formatter),
    ),
}

transaction_pool_content_formatter = apply_formatters_to_dict(
    TRANSACTION_POOL_CONTENT_FORMATTERS)

TRANSACTION_POOL_INSPECT_FORMATTERS = {
    'pending': keymap(to_ascii_if_bytes),
    'queued': keymap(to_ascii_if_bytes),
}
Esempio n. 10
0
        eth_tester.unlock_account(transaction['from'], password)
        transaction_hash = eth_tester.send_transaction(transaction)
    finally:
        eth_tester.lock_account(transaction['from'])

    return transaction_hash


API_ENDPOINTS = {
    'web3': {
        'clientVersion':
        client_version,
        'sha3':
        compose(
            encode_hex,
            keccak,
            decode_hex,
            without_eth_tester(operator.itemgetter(0)),
        ),
    },
    'net': {
        'version': not_implemented,
        'peerCount': not_implemented,
        'listening': not_implemented,
    },
    'vns': {
        'protocolVersion':
        not_implemented,
        'syncing':
        not_implemented,
        'coinbase':
        compose(
Esempio n. 11
0
    apply_formatter_if, )
from vns_web3.utils.toolz import (
    compose, )
from vns_web3.utils.transactions import (
    fill_nonce,
    fill_transaction_defaults,
)

to_hexstr_from_eth_key = operator.methodcaller('to_hex')


def is_eth_key(value):
    return isinstance(value, PrivateKey)


key_normalizer = compose(
    apply_formatter_if(is_eth_key, to_hexstr_from_eth_key), )


@to_dict
def gen_normalized_accounts(val):
    if isinstance(val, (
            list,
            tuple,
            set,
    )):
        for i in val:
            account = to_account(i)
            yield account.address, account
    else:
        account = to_account(val)
        yield account.address, account