Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle typed and untyped strings (similar to other ABI values) #119

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading