def test_format_dict_error(): with pytest.raises(ValueError) as exc_info: apply_formatters_to_dict( {'myfield': int}, {'myfield': 'a'}, ) assert 'myfield' in str(exc_info.value)
def serializable_unsigned_transaction_from_dict(web3, transaction_dict): ''' if web3 is None, fill out transaction as much as possible without calling client ''' filled_transaction = pipe( transaction_dict, dict, fill_transaction_defaults(web3), chain_id_to_v, apply_formatters_to_dict(TRANSACTION_FORMATTERS), ) if 'v' in filled_transaction: serializer = Transaction else: serializer = UnsignedTransaction return serializer.from_dict(filled_transaction)
def serializable_unsigned_transaction_from_dict(web3, transaction_dict): ''' if web3 is None, fill out transaction as much as possible without calling client ''' filled_transaction = pipe( transaction_dict, dict, fill_transaction_defaults(web3), chain_id_to_v, apply_formatters_to_dict(TRANSACTION_FORMATTERS), ) if 'v' in filled_transaction: serializer = Transaction else: serializer = UnsignedTransaction return serializer.from_dict(filled_transaction)
def middleware(method, params): return force_obj_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), 'eth_getFilterChanges': apply_formatter_at_index(to_integer_if_hex, 0), 'eth_getFilterLogs': apply_formatter_at_index(to_integer_if_hex, 0), }, result_formatters={ # Eth 'eth_newFilter': apply_formatter_if(is_integer, hex), 'eth_protocolVersion': apply_formatter_if(is_integer, str), 'eth_getTransactionByHash': apply_formatters_to_dict(TRANSACTION_FORMATTERS), # Net 'net_version': apply_formatter_if(is_integer, str), }, ) return_none_result = static_result(None) ethtestrpc_exception_middleware = construct_exception_handler_middleware( method_handlers={ 'eth_getBlockByHash': (ValueError, static_result(None)), 'eth_getBlockByNumber': (ValueError, static_result(None)), }, )
'nonce': to_integer_if_hex, 'gas': to_integer_if_hex, 'gasPrice': to_integer_if_hex, 'value': to_integer_if_hex, 'from': to_checksum_address, 'publicKey': to_hexbytes(64), 'r': to_hexbytes(32, variable_length=True), 'raw': HexBytes, 's': to_hexbytes(32, variable_length=True), 'to': apply_formatter_if(is_address, to_checksum_address), 'hash': to_hexbytes(32), 'v': apply_formatter_if(is_not_null, to_integer_if_hex), 'standardV': apply_formatter_if(is_not_null, to_integer_if_hex), } transaction_formatter = apply_formatters_to_dict(TRANSACTION_FORMATTERS) WHISPER_LOG_FORMATTERS = { 'from': to_hexbytes(60), 'hash': to_hexbytes(32), 'payload': HexBytes, 'to': to_hexbytes(60), 'topics': apply_formatter_to_array(HexBytes), } whisper_log_formatter = apply_formatters_to_dict(WHISPER_LOG_FORMATTERS) LOG_ENTRY_FORMATTERS = { 'blockHash': apply_formatter_if(is_not_null, to_hexbytes(32)), 'blockNumber': apply_formatter_if(is_not_null, to_integer_if_hex), 'transactionIndex': apply_formatter_if(is_not_null, to_integer_if_hex),
block_key_remapper = apply_key_map(BLOCK_KEY_MAPPINGS) 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 = 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, } filter_params_formatter = apply_formatters_to_dict(FILTER_PARAMS_FORMATTERS)
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, } transaction_params_formatter = apply_formatters_to_dict(TRANSACTION_PARAMS_FORMATTERS) TRANSACTION_FORMATTERS = { 'to': apply_formatter_if(static_return(None), partial(operator.eq, b'')), } transaction_formatter = apply_formatters_to_dict(TRANSACTION_FORMATTERS) ethereum_tester_middleware = construct_formatting_middleware( request_formatters={ # Eth 'eth_getBlockByNumber': apply_formatters_to_args( apply_formatter_if(to_integer_if_hex, is_not_named_block),
ethtestrpc_middleware = construct_formatting_middleware( request_formatters={ 'eth_uninstallFilter': apply_formatter_at_index(to_integer_if_hex, 0), 'eth_getFilterChanges': apply_formatter_at_index(to_integer_if_hex, 0), 'eth_getFilterLogs': apply_formatter_at_index(to_integer_if_hex, 0), }, result_formatters={ # Eth 'eth_newFilter': apply_formatter_if(is_integer, hex), 'eth_protocolVersion': apply_formatter_if(is_integer, str), 'eth_getTransactionByHash': apply_formatters_to_dict(TRANSACTION_FORMATTERS), # Net 'net_version': apply_formatter_if(is_integer, str), }, ) return_none_result = static_result(None) ethtestrpc_exception_middleware = construct_exception_handler_middleware( method_handlers={ 'eth_getBlockByHash': (ValueError, static_result(None)), 'eth_getBlockByNumber': (ValueError, static_result(None)), }, )
'gas': to_integer_if_hex, 'gasPrice': to_integer_if_hex, 'value': to_integer_if_hex, 'from': to_checksum_address, 'publicKey': to_hexbytes(64), 'r': to_hexbytes(32, variable_length=True), 'raw': HexBytes, 's': to_hexbytes(32, variable_length=True), 'to': apply_formatter_if(is_address, to_checksum_address), 'hash': to_hexbytes(32), 'v': apply_formatter_if(is_not_null, to_integer_if_hex), 'standardV': apply_formatter_if(is_not_null, to_integer_if_hex), } transaction_formatter = apply_formatters_to_dict(TRANSACTION_FORMATTERS) WHISPER_LOG_FORMATTERS = { 'from': to_hexbytes(60), 'hash': to_hexbytes(32), 'payload': HexBytes, 'to': to_hexbytes(60), 'topics': apply_formatter_to_array(HexBytes), } whisper_log_formatter = apply_formatters_to_dict(WHISPER_LOG_FORMATTERS) LOG_ENTRY_FORMATTERS = {
'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 = 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, } filter_params_formatter = apply_formatters_to_dict(FILTER_PARAMS_FORMATTERS)
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, )