Skip to content

Commit

Permalink
Handle typed and untyped strings (similar to other ABI values).
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Oct 24, 2024
1 parent bb1b7be commit ea72e30
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
25 changes: 25 additions & 0 deletions multiversx_sdk/abi/abi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def test_load_abi_with_counted_variadic():
def test_encode_endpoint_input_parameters_artificial_contract():
abi = Abi.load(testdata / "artificial.abi.json")

# All values untyped.
encoded_values = abi.encode_endpoint_input_parameters(
endpoint_name="yellow",
values=[[42, "hello", True]]
Expand All @@ -101,6 +102,28 @@ def test_encode_endpoint_input_parameters_artificial_contract():
assert encoded_values[1].hex() == "hello".encode().hex()
assert encoded_values[2].hex() == "01"

# Some values typed.
encoded_values = abi.encode_endpoint_input_parameters(
endpoint_name="red",
values=[
"hello",
StringValue("world"),
]
)

assert encoded_values == [b"hello", b"world"]

# All values typed.
encoded_values = abi.encode_endpoint_input_parameters(
endpoint_name="red",
values=[
StringValue("hello"),
StringValue("world"),
]
)

assert encoded_values == [b"hello", b"world"]


def test_decode_endpoint_output_parameters_artificial_contract():
abi = Abi.load(testdata / "artificial.abi.json")
Expand Down Expand Up @@ -236,6 +259,8 @@ def __init__(self, to: Address, egld_amount: int, endpoint_name: str, arguments:
]
)

assert encoded_values == expected_encoded_values

# All values typed.
encoded_values = abi.encode_endpoint_input_parameters(
endpoint_name="proposeBatch",
Expand Down
2 changes: 2 additions & 0 deletions multiversx_sdk/abi/string_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def set_payload(self, value: Any):
self.value = value.decode("utf-8")
elif isinstance(value, str):
self.value = value
elif isinstance(value, StringValue):
self.value = value.value
else:
raise ValueError(f"cannot set payload for string (should be either a string or bytes, but got: {type(value)})")

Expand Down
9 changes: 7 additions & 2 deletions multiversx_sdk/abi/string_value_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@


def test_set_payload_and_get_payload():
# Simple
# Simple (string)
value = StringValue()
value.set_payload("hello")
assert value.get_payload() == "hello"

# Simple
# Simple (bytes)
value = StringValue()
value.set_payload(b"hello")
assert value.get_payload() == "hello"

# Simple (StringValue)
value = StringValue()
value.set_payload(StringValue("hello"))
assert value.get_payload() == "hello"

# With errors
with pytest.raises(ValueError, match=re.escape("cannot set payload for string (should be either a string or bytes, but got: <class 'types.SimpleNamespace'>)")):
StringValue().set_payload(SimpleNamespace(a=1, b=2, c=3))
15 changes: 15 additions & 0 deletions multiversx_sdk/testutils/testdata/artificial.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@
"type": "OperationCompletionStatus"
}
]
},
{
"name": "red",
"mutability": "mutable",
"inputs": [
{
"name": "a",
"type": "utf-8 string"
},
{
"name": "b",
"type": "utf-8 string"
}
],
"outputs": []
}
],
"types": {
Expand Down

0 comments on commit ea72e30

Please sign in to comment.