def test_register_and_use_callables():
    registry.register('null', encode_null, decode_null)

    try:
        assert encode_single('null', None) == NULL_ENCODING
        assert decode_single('null', NULL_ENCODING) is None

        encoded_tuple = encode_single('(int,null)', (1, None))

        assert encoded_tuple == b'\x00' * 31 + b'\x01' + NULL_ENCODING
        assert decode_single('(int,null)', encoded_tuple) == (1, None)
    finally:
        registry.unregister('null')
def test_register_and_use_callables():
    registry.register('null', encode_null, decode_null)

    try:
        assert encode_single('null', None) == NULL_ENCODING
        assert decode_single('null', NULL_ENCODING) is None

        encoded_tuple = encode_single('(int,null)', (1, None))

        assert encoded_tuple == b'\x00' * 31 + b'\x01' + NULL_ENCODING
        assert decode_single('(int,null)', encoded_tuple) == (1, None)
    finally:
        registry.unregister('null')
def test_register_and_use_coder_classes():
    registry.register(
        lambda x: x.startswith('null'),
        EncodeNull,
        DecodeNull,
        label='null',
    )

    try:
        assert encode_single('null2', None) == NULL_ENCODING * 2
        assert decode_single('null2', NULL_ENCODING * 2) is None

        encoded_tuple = encode_single('(int,null2)', (1, None))

        assert encoded_tuple == b'\x00' * 31 + b'\x01' + NULL_ENCODING * 2
        assert decode_single('(int,null2)', encoded_tuple) == (1, None)
    finally:
        registry.unregister('null')
def test_register_and_use_coder_classes():
    registry.register(
        lambda x: x.startswith('null'),
        EncodeNull,
        DecodeNull,
        label='null',
    )

    try:
        assert encode_single('null2', None) == NULL_ENCODING * 2
        assert decode_single('null2', NULL_ENCODING * 2) is None

        encoded_tuple = encode_single('(int,null2)', (1, None))

        assert encoded_tuple == b'\x00' * 31 + b'\x01' + NULL_ENCODING * 2
        assert decode_single('(int,null2)', encoded_tuple) == (1, None)
    finally:
        registry.unregister('null')
Beispiel #5
0
def test_encode_single_fixed_length(_type, value, expected):
    actual = encode_single(_type, value)
    assert actual == expected
Beispiel #6
0
    def _get_logs_in_block_range(
        self,
        address: Union[AddressType, List[AddressType]],
        abi: Union[List[EventABI], EventABI],
        start_block: Optional[int] = None,
        stop_block: Optional[int] = None,
        block_page_size: Optional[int] = None,
        event_parameters: Optional[Dict] = None,
    ):
        start_block = start_block or 0
        abis = abi if isinstance(abi, (list, tuple)) else [abi]
        block_page_size = block_page_size or 100
        stop_block = start_block + block_page_size if stop_block is None else stop_block
        event_parameters = event_parameters or {}
        for abi in abis:
            if not isinstance(address, (list, tuple)):
                address = [address]

            addresses = [
                self.conversion_manager.convert(a, AddressType)
                for a in address
            ]
            log_filter: Dict = {
                "address": addresses,
                "fromBlock": start_block,
                "toBlock": stop_block,
                "topics": [],
            }

            if "topics" not in event_parameters:
                event_signature_hash = add_0x_prefix(
                    HexStr(keccak(text=abi.selector).hex()))
                log_filter["topics"] = [event_signature_hash]
                search_topics = []
                abi_types = []
                topics = LogInputABICollection(abi, [
                    abi_input for abi_input in abi.inputs if abi_input.indexed
                ])

                for name, arg in event_parameters.items():
                    if hasattr(arg, "address"):
                        arg = self.conversion_manager.convert(arg, AddressType)

                    abi_type = None
                    for argument in topics.values:
                        if argument.name == name:
                            abi_type = argument.type

                    if not abi_type:
                        raise DecodingError(
                            f"'{name}' is not an indexed topic for event '{abi.name}'."
                        )

                    search_topics.append(arg)
                    abi_types.append(abi_type)

                encoded_topic_data = [
                    encode_single(topic_type, topic_data).hex()  # type: ignore
                    for topic_type, topic_data in zip(topics.types,
                                                      search_topics)
                ]
                log_filter["topics"].extend(encoded_topic_data)
            else:
                log_filter["topics"] = event_parameters.pop("topics")

            log_result = [
                dict(log) for log in self.web3.eth.get_logs(log_filter)
            ]  # type: ignore
            yield from self.network.ecosystem.decode_logs(abi, log_result)
Beispiel #7
0
def test_encode_single(typ, python_value, expected):
    actual = encode_single(typ, python_value)
    assert actual == expected
def test_encode_single_dynamic_length(_type, value, expected):
    actual = encode_single(_type, value)
    assert actual == expected
def test_encode_single(typ, python_value, abi_encoding, _):
    actual = encode_single(typ, python_value)
    assert actual == abi_encoding