Esempio n. 1
0
    hex_to_integer,
    integer_to_hex,
    is_array_of_dicts,
    is_array_of_strings,
)

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:
Esempio n. 2
0
    static_result,
)

from .base import BaseProvider  # noqa: E402
from .rpc import HTTPProvider  # noqa: E402


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 force_obj_to_text(make_request(method, params))

    return middleware
Esempio n. 3
0
    apply_formatter_to_array,
    apply_formatters_to_args,
    apply_formatters_to_dict,
    apply_key_map,
    hex_to_integer,
    integer_to_hex,
    is_array_of_dicts,
    static_return,
)


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


to_integer_if_hex = apply_formatter_if(is_string, 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)
Esempio n. 4
0
    apply_formatter_at_index,
    apply_one_of_formatters,
    hex_to_integer,
    integer_to_hex,
)

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)


def is_array_of_strings(value):
    if not is_list_like(value):
        return False
    return all((is_string(item) for item in value))
Esempio n. 5
0
    signed_transaction = Transaction(v=v, r=r, s=s, **chain_naive_transaction)
    return rlp.encode(signed_transaction)


TRANSACTION_DEFAULTS = {
    'gasPrice': lambda web3: web3.eth.gasPrice,
    'value': 0,
    'data': b'',
    'chainId': lambda web3: int(web3.net.version),
}

TRANSACTION_FORMATTERS = {
    'nonce': hexstr_if_str(to_int),
    'gasPrice': hexstr_if_str(to_int),
    'gas': hexstr_if_str(to_int),
    'to': apply_formatter_if(is_string, decode_hex),
    'value': hexstr_if_str(to_int),
    'data': apply_formatter_if(is_string, decode_hex),
    'v': hexstr_if_str(to_int),
    'r': hexstr_if_str(to_int),
    's': hexstr_if_str(to_int),
}


def chain_id_to_v(transaction_dict):
    # See EIP 155
    chain_id = transaction_dict.pop('chainId')
    if chain_id is None:
        return transaction_dict
    else:
        return dict(transaction_dict, v=chain_id, r=0, s=0)
Esempio n. 6
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

Esempio n. 7
0
    hex_to_integer,
    apply_formatter_if,
    apply_formatters_to_dict,
    apply_formatter_to_array,
    apply_formatter_at_index,
)

from .formatting import (
    construct_formatting_middleware, )


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


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

is_not_null = complement(is_null)

# TODO: decide what inputs this allows.
TRANSACTION_PARAMS_FORMATTERS = {
    'value': hex,
    'gas': hex,
    'gasPrice': hex,
    'nonce': hex,
}

transaction_params_formatter = apply_formatters_to_dict(
    TRANSACTION_PARAMS_FORMATTERS)
Esempio n. 8
0
    apply_formatter_to_array,
    apply_formatters_to_args,
    apply_formatters_to_dict,
    apply_key_map,
    hex_to_integer,
    integer_to_hex,
    is_array_of_dicts,
    static_return,
)


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


to_integer_if_hex = apply_formatter_if(is_string, 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',
Esempio n. 9
0
    apply_formatter_if,
    apply_formatters_to_args,
    apply_formatter_to_array,
    apply_formatters_to_dict,
    apply_key_map,
    hex_to_integer,
    integer_to_hex,
    static_return,
)


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


to_integer_if_hex = apply_formatter_if(hex_to_integer, is_string)

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',
Esempio n. 10
0
    integer_to_hex,
    is_array_of_dicts,
    remove_key_if,
    static_return,
)


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)
Esempio n. 11
0
    signed_transaction = Transaction(v=v, r=r, s=s, **chain_naive_transaction)
    return rlp.encode(signed_transaction)


TRANSACTION_DEFAULTS = {
    'gasPrice': lambda web3: web3.eth.gasPrice,
    'value': 0,
    'data': b'',
    'chainId': lambda web3: int(web3.net.version),
}

TRANSACTION_FORMATTERS = {
    'nonce': hexstr_if_str(to_decimal),
    'gasPrice': hexstr_if_str(to_decimal),
    'gas': hexstr_if_str(to_decimal),
    'to': apply_formatter_if(decode_hex, is_string),
    'value': hexstr_if_str(to_decimal),
    'data': apply_formatter_if(decode_hex, is_string),
    'v': hexstr_if_str(to_decimal),
    'r': hexstr_if_str(to_decimal),
    's': hexstr_if_str(to_decimal),
}


def chain_id_to_v(transaction_dict):
    # See EIP 155
    chain_id = transaction_dict.pop('chainId')
    if chain_id is None:
        return transaction_dict
    else:
        return dict(transaction_dict, v=chain_id, r=0, s=0)
Esempio n. 12
0
    integer_to_hex,
)
from web3.utils.normalizers import (
    abi_int_to_hex,
    abi_bytes_to_hex,
)

from .formatting import (
    construct_formatting_middleware, )


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


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

is_false = partial(operator.is_, False)

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

# TODO: decide what inputs this allows.
TRANSACTION_PARAMS_FORMATTERS = {
    'value': integer_to_hex,
    'gas': integer_to_hex,
    'gasPrice': integer_to_hex,
    'nonce': integer_to_hex,
}
Esempio n. 13
0
    apply_formatter_if,
    apply_formatters_to_args,
    apply_formatter_to_array,
    apply_formatters_to_dict,
    apply_key_map,
    hex_to_integer,
    integer_to_hex,
    static_return,
)


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


to_integer_if_hex = apply_formatter_if(is_string, 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)
Esempio n. 14
0
from web3.utils.formatters import (
    apply_formatter_if,
    apply_formatters_to_args,
    apply_formatters_to_dict,
    apply_key_map,
    hex_to_integer,
    integer_to_hex,
    static_return,
)


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


to_integer_if_hex = apply_formatter_if(hex_to_integer, is_string)


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)
Esempio n. 15
0
    return rlp.encode(signed_transaction)


TRANSACTION_DEFAULTS = {
    'value': 0,
    'data': b'',
    'gas': lambda web3, tx: web3.eth.estimateGas(tx),
    'gasPrice': lambda web3, tx: web3.eth.generateGasPrice(tx) or web3.eth.gasPrice,
    'chainId': lambda web3, tx: int(web3.net.version),
}

TRANSACTION_FORMATTERS = {
    'nonce': hexstr_if_str(to_int),
    'gasPrice': hexstr_if_str(to_int),
    'gas': hexstr_if_str(to_int),
    'to': apply_formatter_if(is_string, decode_hex),
    'value': hexstr_if_str(to_int),
    'data': apply_formatter_if(is_string, decode_hex),
    'v': hexstr_if_str(to_int),
    'r': hexstr_if_str(to_int),
    's': hexstr_if_str(to_int),
}


def chain_id_to_v(transaction_dict):
    # See EIP 155
    chain_id = transaction_dict.pop('chainId')
    if chain_id is None:
        return transaction_dict
    else:
        return dict(transaction_dict, v=chain_id, r=0, s=0)
Esempio n. 16
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(
Esempio n. 17
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(
Esempio n. 18
0
    apply_one_of_formatters,
    hex_to_integer,
    integer_to_hex,
    is_array_of_dicts,
    is_array_of_strings,
)

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)
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, )
Esempio n. 20
0
    spawn,
)

from .base import BaseProvider  # noqa: E402
from .rpc import HTTPProvider  # noqa: E402


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 force_obj_to_text(make_request(method, params))
    return middleware
Esempio n. 21
0
    apply_formatters_to_dict,
    apply_formatter_to_array,
    apply_formatter_at_index,
    apply_one_of_formatters,
)

from .formatting import (
    construct_formatting_middleware,
)


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


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


is_false = partial(operator.is_, False)

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


# TODO: decide what inputs this allows.
TRANSACTION_PARAMS_FORMATTERS = {
    'value': integer_to_hex,
    'gas': integer_to_hex,
    'gasPrice': integer_to_hex,