Beispiel #1
0
def test_apply_formatters_to_sequence_curried(formatters, value, expected):
    list_formatter = apply_formatters_to_sequence(formatters)
    if isinstance(expected, type) and issubclass(expected, Exception):
        with pytest.raises(expected):
            list_formatter(value)
    else:
        assert list_formatter(value) == expected
def _sign_create_validator(transaction_dict, private_key):
    """
    Sign a create validator transaction
    See sign_staking_transaction for details
    """
    # preliminary steps
    if transaction_dict['directive'] != Directive.CreateValidator:
        raise TypeError('Only CreateValidator is supported by _sign_create_or_edit_validator')
    # first common step
    account, sanitized_transaction = _get_account_and_transaction(transaction_dict, private_key)
    # encode the stakeMsg
    description = [
            sanitized_transaction.pop('name'),
            sanitized_transaction.pop('identity'),
            sanitized_transaction.pop('website'),
            sanitized_transaction.pop('security-contact'),
            sanitized_transaction.pop('details'),
        ]
    commission = apply_formatter_to_array( hexstr_if_str(to_int),       # formatter
        [
            _convert_staking_percentage_to_number(sanitized_transaction.pop('rate')),
            _convert_staking_percentage_to_number(sanitized_transaction.pop('max-rate')),
            _convert_staking_percentage_to_number(sanitized_transaction.pop('max-change-rate')),
        ]
    )
    commission = [ [element] for element in commission ]
    bls_keys = apply_formatter_to_array( hexstr_if_str(to_bytes),       # formatter
        sanitized_transaction.pop('bls-public-keys')
    )
    sanitized_transaction['stakeMsg'] = \
        apply_formatters_to_sequence( [
            hexstr_if_str(to_bytes),        # address
            identity,                       # description
            identity,                       # commission rates
            hexstr_if_str(to_int),          # min self delegation (in ONE), decimals are silently dropped
            hexstr_if_str(to_int),          # max total delegation (in ONE), decimals are silently dropped
            identity,                       # bls public keys
            hexstr_if_str(to_int),          # amount (the Hexlify in the SDK drops the decimals, which is what we will do too)
        ], [
            convert_one_to_hex(sanitized_transaction.pop('validatorAddress')),
            description,
            commission,
            math.floor(sanitized_transaction.pop('min-self-delegation')),       # Decimal floors it correctly
            math.floor(sanitized_transaction.pop('max-total-delegation')),
            bls_keys,
            math.floor(sanitized_transaction.pop('amount')),
            ]
        )
    return _sign_transaction_generic(account, sanitized_transaction, CreateValidator)
def _sign_edit_validator(transaction_dict, private_key):
    """
    Sign an edit validator transaction
    See sign_staking_transaction for details
    """
    # preliminary steps
    if transaction_dict['directive'] != Directive.EditValidator:
        raise TypeError('Only EditValidator is supported by _sign_create_or_edit_validator')
    # first common step
    account, sanitized_transaction = _get_account_and_transaction(transaction_dict, private_key)
    # encode the stakeMsg
    description = [
            sanitized_transaction.pop('name'),
            sanitized_transaction.pop('identity'),
            sanitized_transaction.pop('website'),
            sanitized_transaction.pop('security-contact'),
            sanitized_transaction.pop('details'),
        ]
    sanitized_transaction['stakeMsg'] = \
        apply_formatters_to_sequence( [
            hexstr_if_str(to_bytes),        # address
            identity,                       # description
            identity,                       # new rate (it's in a list so can't do hexstr_if_str)
            hexstr_if_str(to_int),          # min self delegation (in ONE), decimals are silently dropped
            hexstr_if_str(to_int),          # max total delegation (in ONE), decimals are silently dropped
            hexstr_if_str(to_bytes),        # key to remove
            hexstr_if_str(to_bytes),        # key to add
        ], [
            convert_one_to_hex(sanitized_transaction.pop('validatorAddress')),
            description,
            [ _convert_staking_percentage_to_number(sanitized_transaction.pop('rate')) ],
            math.floor(sanitized_transaction.pop('min-self-delegation')),       # Decimal floors it correctly
            math.floor(sanitized_transaction.pop('max-total-delegation')),
            sanitized_transaction.pop('bls-key-to-remove'),
            sanitized_transaction.pop('bls-key-to-add')
            ]
        )
    return _sign_transaction_generic(account, sanitized_transaction, EditValidator)
def _sign_delegate_or_undelegate(transaction_dict, private_key, delegate):
    """
    Sign a delegate or undelegate transaction
    See sign_staking_transaction for details
    """
    # preliminary steps
    if transaction_dict['directive'] not in [ Directive.Delegate, Directive.Undelegate ]:
        raise TypeError('Only Delegate or Undelegate are supported by _sign_delegate_or_undelegate')
    # first common step
    account, sanitized_transaction = _get_account_and_transaction(transaction_dict, private_key)
    # encode the stakeMsg
    sanitized_transaction['stakeMsg'] = \
        apply_formatters_to_sequence( [
            hexstr_if_str(to_bytes),
            hexstr_if_str(to_bytes),
            hexstr_if_str(to_int)
        ], [
            convert_one_to_hex(sanitized_transaction.pop('delegatorAddress')),
            convert_one_to_hex(sanitized_transaction.pop('validatorAddress')),
            sanitized_transaction.pop('amount'),
            ]
        )
    return _sign_transaction_generic(account, sanitized_transaction, DelegateOrUndelegate)
Beispiel #5
0
    'maxFeePerGas': to_hex_if_integer,
    'maxPriorityFeePerGas': to_hex_if_integer,
}

transaction_request_formatter = apply_formatters_to_dict(
    TRANSACTION_REQUEST_FORMATTERS)
transaction_param_formatter = compose(
    remove_key_if('to', lambda txn: txn['to'] in {'', b'', None}),
    remove_key_if('gasPrice', lambda txn: txn['gasPrice'] in {'', b'', None}),
    transaction_request_formatter,
)

call_without_override: Callable[[Tuple[TxParams, BlockIdentifier]],
                                Tuple[Dict[str, Any], int]]
call_without_override = apply_formatters_to_sequence([
    transaction_param_formatter,
    to_hex_if_integer,
])
call_with_override: Callable[
    [Tuple[TxParams, BlockIdentifier,
           CallOverrideParams]], Tuple[Dict[str, Any], int, Dict[str, Any]], ]
call_with_override = apply_formatters_to_sequence([
    transaction_param_formatter,
    to_hex_if_integer,
    lambda x: x,
])

estimate_gas_without_block_id: Callable[[Dict[str, Any]], Dict[str, Any]]
estimate_gas_without_block_id = apply_formatter_at_index(
    transaction_param_formatter, 0)
estimate_gas_with_block_id: Callable[[Tuple[Dict[str, Any], Union[str, int]]],
                                     Tuple[Dict[str, Any], int]]
Beispiel #6
0
filter_result_formatter = apply_one_of_formatters((
    (is_array_of_dicts, apply_list_to_array_formatter(log_entry_formatter)),
    (is_array_of_strings, apply_list_to_array_formatter(to_hexbytes(32))),
))

transaction_param_formatter = compose(
    remove_key_if('to', lambda txn: txn['to'] in {'', b'', None}), )

estimate_gas_without_block_id: Callable[[Dict[str, Any]], Dict[str, Any]]
estimate_gas_without_block_id = apply_formatter_at_index(
    transaction_param_formatter, 0)
estimate_gas_with_block_id: Callable[[Tuple[Dict[str, Any], Union[str, int]]],
                                     Tuple[Dict[str, Any], int]]
estimate_gas_with_block_id = apply_formatters_to_sequence([
    transaction_param_formatter,
    block_number_formatter,
])

SIGNED_TX_FORMATTER = {
    'raw': HexBytes,
    'tx': transaction_formatter,
}

signed_tx_formatter = apply_formatters_to_dict(SIGNED_TX_FORMATTER)

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

GETH_WALLET_FORMATTER = {'address': to_checksum_address}

geth_wallet_formatter = apply_formatters_to_dict(GETH_WALLET_FORMATTER)
Beispiel #7
0
    to_hex,
)

from eth_utils.toolz import curried

environment_formatter = apply_formatters_to_dict({
    "currentCoinbase": to_checksum_address,
    "previousHash": encode_hex,
    "currentNumber": to_hex,
    "currentDifficulty": to_hex,
    "currentGasLimit": to_hex,
    "currentTimestamp": to_hex,
})

storage_item_formatter: Callable[[List[Any]], List[Any]]
storage_item_formatter = apply_formatters_to_sequence([to_hex, to_hex])
storage_formatter = curried.itemmap(storage_item_formatter)

account_state_formatter = apply_formatters_to_dict({
    "balance":
    to_hex,
    "nonce":
    to_hex,
    "code":
    encode_hex,
    "storage":
    storage_formatter,
})

state_item_formatter: Callable[[List[Any]], List[Any]]
state_item_formatter = apply_formatters_to_sequence(
Beispiel #8
0
))


transaction_param_formatter = compose(
    remove_key_if('to', lambda txn: txn['to'] in {'', b'', None}),
)


estimate_gas_without_block_id: Callable[[Dict[str, Any]], Dict[str, Any]]
estimate_gas_without_block_id = apply_formatter_at_index(transaction_param_formatter, 0)
estimate_gas_with_block_id: Callable[
    [Tuple[Dict[str, Any], Union[str, int]]],
    Tuple[Dict[str, Any], int]
]
estimate_gas_with_block_id = apply_formatters_to_sequence([
    transaction_param_formatter,
    to_hex_if_integer,
])

SIGNED_TX_FORMATTER = {
    'raw': HexBytes,
    'tx': transaction_formatter,
}

signed_tx_formatter = apply_formatters_to_dict(SIGNED_TX_FORMATTER)

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


GETH_WALLET_FORMATTER = {
    (apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings),
))

TRANSACTION_PARAM_FORMATTERS = {
    'chainId': apply_formatter_if(is_integer, str),
}

transaction_param_formatter = compose(
    remove_key_if('to', lambda txn: txn['to'] in {'', b'', None}),
    apply_formatters_to_dict(TRANSACTION_PARAM_FORMATTERS),
)

estimate_gas_without_block_id = apply_formatter_at_index(
    transaction_param_formatter, 0)
estimate_gas_with_block_id = apply_formatters_to_sequence([
    transaction_param_formatter,
    block_number_formatter,
])

min_gas_price_formatter = apply_formatters_to_sequence([
    to_integer_if_hex,
    to_integer_if_hex,
])

BLOCK_CREATION_PARAMETERS_FORMATTER = {
    'block_number': to_integer_if_hex,
    'parent_hash': HexBytes,
    'nonce': to_integer_if_hex,
    'receive_transactions': apply_formatter_to_array(HexBytes),
    'reward_bundle': HexBytes,
}