Exemple #1
0
        self.data_filter_set = data_filter_set
        if any(data_filter_set):
            self.data_filter_set_function = match_fn(data_filter_set)

    def is_valid_entry(self, entry):
        if not self.data_filter_set:
            return True
        return bool(self.data_filter_set_function(entry['data']))


def decode_utf8_bytes(value):
    return value.decode("utf-8")


not_text = complement(is_text)
normalize_to_text = apply_formatter_if(not_text, decode_utf8_bytes)


def normalize_data_values(type_string, data_value):
    """Decodes utf-8 bytes to strings for abi string values.

    eth-abi v1 returns utf-8 bytes for string values.
    This can be removed once eth-abi v2 is required.
    """
    _type = parse_type_string(type_string)
    if _type.base == "string":
        if _type.arrlist is not None:
            return tuple((normalize_to_text(value) for value in data_value))
        else:
            return normalize_to_text(data_value)
    return data_value
Exemple #2
0
 'getBlockByHash':
 null_if_block_not_found(call_eth_tester('get_block_by_hash')),
 'getBlockByNumber':
 null_if_block_not_found(call_eth_tester('get_block_by_number')),
 'getTransactionByHash':
 null_if_transaction_not_found(
     call_eth_tester('get_transaction_by_hash')),
 'getTransactionByBlockHashAndIndex':
 get_transaction_by_block_hash_and_index,
 'getTransactionByBlockNumberAndIndex':
 get_transaction_by_block_number_and_index,
 'getTransactionReceipt':
 null_if_transaction_not_found(
     compose(
         apply_formatter_if(
             compose(is_null, operator.itemgetter('block_number')),
             static_return(None),
         ),
         call_eth_tester('get_transaction_receipt'),
     )),
 'getUncleByBlockHashAndIndex':
 not_implemented,
 'getUncleByBlockNumberAndIndex':
 not_implemented,
 'getCompilers':
 not_implemented,
 'compileLLL':
 not_implemented,
 'compileSolidity':
 not_implemented,
 'compileSerpent':
 not_implemented,
Exemple #3
0
    hex_to_integer,
    integer_to_hex,
    is_array_of_dicts,
    is_array_of_strings,
    remove_key_if,
)

from .formatting import (
    construct_formatting_middleware, )


def bytes_to_ascii(value):
    return codecs.decode(value, 'ascii')


to_ascii_if_bytes = apply_formatter_if(is_bytes, bytes_to_ascii)
to_integer_if_hex = apply_formatter_if(is_string, hex_to_integer)
block_number_formatter = apply_formatter_if(is_integer, integer_to_hex)

is_false = partial(operator.is_, False)

is_not_false = complement(is_false)
is_not_null = complement(is_null)


@curry
def to_hexbytes(num_bytes, val, variable_length=False):
    if isinstance(val, (str, int, bytes)):
        result = HexBytes(val)
    else:
        raise TypeError("Cannot convert %r to HexBytes" % val)
Exemple #4
0
    integer_to_hex,
    is_array_of_dicts,
    is_array_of_strings,
    remove_key_if,
)

from .formatting import (
    construct_formatting_middleware,
)


def bytes_to_ascii(value):
    return codecs.decode(value, 'ascii')


to_ascii_if_bytes = apply_formatter_if(is_bytes, bytes_to_ascii)
to_integer_if_hex = apply_formatter_if(is_string, hex_to_integer)
block_number_formatter = apply_formatter_if(is_integer, integer_to_hex)


is_false = partial(operator.is_, False)

is_not_false = complement(is_false)
is_not_null = complement(is_null)


@curry
def to_hexbytes(num_bytes, val, variable_length=False):
    if isinstance(val, (str, int, bytes)):
        result = HexBytes(val)
    else:
Exemple #5
0
    block_normalizers = {
        **BLOCK_NORMALIZERS,
        **{
            "sealFields": identity,
            "author": encode_hex,
            "signature": encode_hex,
            "step": str,
        },
    }

    @classmethod
    def normalize_outbound_block(cls, block):
        return normalize_dict(block, cls.block_normalizers)


def fix_web3_keys(block):
    return AttributeDict({
        **block,
        "receiptsRoot":
        HexBytes(block["receipts_root"]),
        "logsBloom":
        block["logs_bloom"],
    })


key_renaming_middleware = construct_formatting_middleware(
    result_formatters={
        "eth_getBlockByHash": apply_formatter_if(is_dict, fix_web3_keys),
        "eth_getBlockByNumber": apply_formatter_if(is_dict, fix_web3_keys),
    })
Exemple #6
0
        self.data_filter_set = data_filter_set
        if any(data_filter_set):
            self.data_filter_set_function = match_fn(data_filter_set)

    def is_valid_entry(self, entry):
        if not self.data_filter_set:
            return True
        return bool(self.data_filter_set_function(entry['data']))


def decode_utf8_bytes(value):
    return value.decode("utf-8")


not_text = complement(is_text)
normalize_to_text = apply_formatter_if(not_text, decode_utf8_bytes)


def normalize_data_values(type_string, data_value):
    """Decodes utf-8 bytes to strings for abi string values.

    eth-abi v1 returns utf-8 bytes for string values.
    This can be removed once eth-abi v2 is required.
    """
    _type = parse_type_string(type_string)
    if _type.base == "string":
        if _type.arrlist is not None:
            return tuple((normalize_to_text(value) for value in data_value))
        else:
            return normalize_to_text(data_value)
    return data_value
Exemple #7
0
    BaseProvider,
)
from .rpc import (
    HTTPProvider,
)


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
from eth_utils import (
    is_string,
)

from web3._utils.formatters import (
    apply_formatter_at_index,
    apply_formatter_if,
    apply_formatters_to_dict,
)

from .formatting import (
    construct_formatting_middleware,
)

FILTER_PARAM_NORMALIZERS = apply_formatters_to_dict({
    'address': apply_formatter_if(is_string, lambda x: [x])})

METHOD_NORMALIZERS = {
    'eth_getLogs': apply_formatter_at_index(FILTER_PARAM_NORMALIZERS, 0),
    'eth_newFilter': apply_formatter_at_index(FILTER_PARAM_NORMALIZERS, 0)
}

request_parameter_normalizer = construct_formatting_middleware(
    request_formatters=METHOD_NORMALIZERS,
)
Exemple #9
0
from .base import (
    BaseProvider, )
from .rpc import (
    HTTPProvider, )


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
Exemple #10
0
    else:
        return list()


@to_dict
def _build_argument_filters_from_event_abi(event_abi):
    for item in event_abi['inputs']:
        key = item['name']
        if item['indexed'] is True:
            value = TopicArgumentFilter(arg_type=item['type'])
        else:
            value = DataArgumentFilter(arg_type=item['type'])
        yield key, value


array_to_tuple = apply_formatter_if(is_list_like, tuple)


@to_tuple
def _normalize_match_values(match_values):
    for value in match_values:
        yield array_to_tuple(value)


class BaseArgumentFilter(ABC):
    _match_values = None
    _immutable = False

    def __init__(self, arg_type):
        self.arg_type = arg_type
Exemple #11
0
)
from web3.middleware import (
    construct_fixture_middleware,
    construct_formatting_middleware,
)


def is_named_block(value):
    return value in {"latest", "earliest", "pending"}


def is_hexstr(value):
    return is_string(value) and is_hex(value)


to_integer_if_hex = apply_formatter_if(is_hexstr, hex_to_integer)


is_not_named_block = complement(is_named_block)


TRANSACTION_KEY_MAPPINGS = {
    'block_hash': 'blockHash',
    'block_number': 'blockNumber',
    'gas_price': 'gasPrice',
    'transaction_hash': 'transactionHash',
    'transaction_index': 'transactionIndex',
}

transaction_key_remapper = apply_key_map(TRANSACTION_KEY_MAPPINGS)
Exemple #12
0
 'getCode': call_eth_tester('get_code'),
 'sign': not_implemented,
 'sendTransaction': call_eth_tester('send_transaction'),
 'sendRawTransaction': call_eth_tester('send_raw_transaction'),
 'call': call_eth_tester('call'),  # TODO: untested
 'estimateGas': call_eth_tester('estimate_gas'),  # TODO: untested
 'getBlockByHash': null_if_block_not_found(call_eth_tester('get_block_by_hash')),
 'getBlockByNumber': null_if_block_not_found(call_eth_tester('get_block_by_number')),
 'getTransactionByHash': null_if_transaction_not_found(
     call_eth_tester('get_transaction_by_hash')
 ),
 'getTransactionByBlockHashAndIndex': get_transaction_by_block_hash_and_index,
 'getTransactionByBlockNumberAndIndex': get_transaction_by_block_number_and_index,
 'getTransactionReceipt': null_if_transaction_not_found(compose(
     apply_formatter_if(
         compose(is_null, operator.itemgetter('block_number')),
         static_return(None),
     ),
     call_eth_tester('get_transaction_receipt'),
 )),
 'getUncleByBlockHashAndIndex': not_implemented,
 'getUncleByBlockNumberAndIndex': not_implemented,
 'getCompilers': not_implemented,
 'compileLLL': not_implemented,
 'compileSolidity': not_implemented,
 'compileSerpent': not_implemented,
 'newFilter': create_log_filter,
 'newBlockFilter': call_eth_tester('create_block_filter'),
 'newPendingTransactionFilter': call_eth_tester('create_pending_transaction_filter'),
 'uninstallFilter': excepts(
     FilterNotFound,
     compose(
Exemple #13
0
    fill_transaction_defaults,
)

from .abi import (
    STANDARD_NORMALIZERS,
)

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
        return

Exemple #14
0
    else:
        return list()


@to_dict
def _build_argument_filters_from_event_abi(event_abi):
    for item in event_abi['inputs']:
        key = item['name']
        if item['indexed'] is True:
            value = TopicArgumentFilter(arg_type=item['type'])
        else:
            value = DataArgumentFilter(arg_type=item['type'])
        yield key, value


array_to_tuple = apply_formatter_if(is_list_like, tuple)


@to_tuple
def _normalize_match_values(match_values):
    for value in match_values:
        yield array_to_tuple(value)


class BaseArgumentFilter(ABC):
    _match_values = None
    _immutable = False

    def __init__(self, arg_type):
        self.arg_type = arg_type
Exemple #15
0
)
from web3.middleware import (
    construct_fixture_middleware,
    construct_formatting_middleware,
)


def is_named_block(value):
    return value in {"latest", "earliest", "pending"}


def is_hexstr(value):
    return is_string(value) and is_hex(value)


to_integer_if_hex = apply_formatter_if(is_hexstr, hex_to_integer)

is_not_named_block = complement(is_named_block)

TRANSACTION_KEY_MAPPINGS = {
    'block_hash': 'blockHash',
    'block_number': 'blockNumber',
    'gas_price': 'gasPrice',
    'transaction_hash': 'transactionHash',
    'transaction_index': 'transactionIndex',
}

transaction_key_remapper = apply_key_map(TRANSACTION_KEY_MAPPINGS)

LOG_KEY_MAPPINGS = {
    'log_index': 'logIndex',
Exemple #16
0
    fill_nonce,
    fill_transaction_defaults,
)

from .abi import (
    STANDARD_NORMALIZERS, )

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
Exemple #17
0
)
from web3._utils.toolz.curried import (
    keymap,
    valmap,
)

from .formatting import (
    construct_formatting_middleware,
)


def bytes_to_ascii(value):
    return codecs.decode(value, 'ascii')


to_ascii_if_bytes = apply_formatter_if(is_bytes, bytes_to_ascii)
to_integer_if_hex = apply_formatter_if(is_string, hex_to_integer)
block_number_formatter = apply_formatter_if(is_integer, integer_to_hex)


is_false = partial(operator.is_, False)

is_not_false = complement(is_false)
is_not_null = complement(is_null)


@curry
def to_hexbytes(num_bytes, val, variable_length=False):
    if isinstance(val, (str, int, bytes)):
        result = HexBytes(val)
    else: