diff --git a/multiversx_sdk/abi/array_value.py b/multiversx_sdk/abi/array_value.py index 7da4570d..54f2b1ac 100644 --- a/multiversx_sdk/abi/array_value.py +++ b/multiversx_sdk/abi/array_value.py @@ -2,8 +2,7 @@ from typing import Any, Callable, List, Optional from multiversx_sdk.abi.interface import ISingleValue -from multiversx_sdk.abi.shared import (convert_native_value_to_list, - decode_length, encode_length) +from multiversx_sdk.abi.shared import convert_native_value_to_list class ArrayValue: @@ -20,17 +19,14 @@ def __init__(self, self.item_creator = item_creator def encode_nested(self, writer: io.BytesIO): - encode_length(writer, len(self.items)) self._encode_list_items(writer) def encode_top_level(self, writer: io.BytesIO): self._encode_list_items(writer) def decode_nested(self, reader: io.BytesIO): - length = decode_length(reader) - self.items = [] - for _ in range(length): + for _ in range(self.length): self._decode_list_item(reader) def decode_top_level(self, data: bytes): diff --git a/multiversx_sdk/abi/array_value_test.py b/multiversx_sdk/abi/array_value_test.py index 144e28c7..76a2e33b 100644 --- a/multiversx_sdk/abi/array_value_test.py +++ b/multiversx_sdk/abi/array_value_test.py @@ -4,8 +4,9 @@ from multiversx_sdk.abi.array_value import ArrayValue from multiversx_sdk.abi.biguint_value import BigUIntValue +from multiversx_sdk.abi.codec import Codec from multiversx_sdk.abi.fields import Field -from multiversx_sdk.abi.small_int_values import U32Value +from multiversx_sdk.abi.small_int_values import U16Value, U32Value from multiversx_sdk.abi.struct_value import StructValue @@ -69,3 +70,39 @@ def test_set_payload_and_get_payload(): with pytest.raises(ValueError, match="cannot convert native value to list, because of: 'int' object is not iterable"): value = ArrayValue(length=1, item_creator=lambda: U32Value()) value.set_payload(42) + + +def test_encode_top_level(): + codec = Codec() + array_value = ArrayValue(length=3, items=[U16Value(1), U16Value(2), U16Value(3)]) + + encoded = codec.encode_top_level(array_value) + assert encoded.hex() == "000100020003" + + +def test_encode_nested(): + codec = Codec() + array_value = ArrayValue(length=3, items=[U16Value(1), U16Value(2), U16Value(3)]) + + encoded = codec.encode_nested(array_value) + assert encoded.hex() == "000100020003" + + +def test_decode_top_level(): + codec = Codec() + data = bytes.fromhex("000100020003") + + destinattion = ArrayValue(length=3, item_creator=lambda: U16Value()) + codec.decode_top_level(data, destinattion) + + assert destinattion.items == [U16Value(1), U16Value(2), U16Value(3)] + + +def test_decode_nested(): + codec = Codec() + data = bytes.fromhex("000100020003") + + destinattion = ArrayValue(length=3, item_creator=lambda: U16Value()) + codec.decode_nested(data, destinattion) + + assert destinattion.items == [U16Value(1), U16Value(2), U16Value(3)]