)
from client_sdk_python.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:
    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 #3
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 .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
from client_sdk_python.packages.eth_utils import (
    is_string, )

from client_sdk_python.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 = {
    'platon_getLogs': apply_formatter_at_index(FILTER_PARAM_NORMALIZERS, 0),
    'platon_newFilter': apply_formatter_at_index(FILTER_PARAM_NORMALIZERS, 0)
}

request_parameter_normalizer = construct_formatting_middleware(
    request_formatters=METHOD_NORMALIZERS, )
 '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,
    curry,
    identity,
    partial,
    pipe,
)


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',