Skip to content

Commit

Permalink
feat: add scv.to_void and scv.from_void. (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Jan 9, 2024
1 parent f0b7c01 commit 6ab765e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Release History
==============

### Pending
#### Update
- feat: add `scv.to_void` and `scv.from_void`. ([#863](https://github.com/StellarCN/py-stellar-base/pull/863))

### Version 9.1.3

Expand Down
22 changes: 22 additions & 0 deletions stellar_sdk/scval.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"from_address",
"to_bool",
"from_bool",
"to_void",
"from_void",
"to_bytes",
"from_bytes",
"to_duration",
Expand Down Expand Up @@ -95,6 +97,26 @@ def from_bool(sc_val: stellar_xdr.SCVal) -> bool:
return sc_val.b


def to_void() -> stellar_xdr.SCVal:
"""Creates a new :class:`stellar_sdk.xdr.SCVal` XDR object of type :class:`stellar_sdk.xdr.SCValType.SCV_VOID`.
:return: A new :class:`stellar_sdk.xdr.SCVal` XDR object of type :class:`stellar_sdk.xdr.SCValType.SCV_VOID`.
"""
return stellar_xdr.SCVal(stellar_xdr.SCValType.SCV_VOID)


def from_void(sc_val: stellar_xdr.SCVal) -> None:
"""Creates a None value from a :class:`stellar_sdk.xdr.SCVal` XDR object.
:param sc_val: The :class:`stellar_sdk.xdr.SCVal` XDR object to convert.
:return: None.
:raises: :exc:`ValueError` if ``sc_val`` is not of type :class:`stellar_sdk.xdr.SCValType.SCV_VOID`.
"""
if sc_val.type != stellar_xdr.SCValType.SCV_VOID:
raise ValueError(f"Invalid sc_val type, must be SCV_VOID, got {sc_val.type}")
return None


def to_bytes(data: bytes) -> stellar_xdr.SCVal:
"""Creates a new :class:`stellar_sdk.xdr.SCVal` XDR object from a bytes value.
Expand Down
7 changes: 7 additions & 0 deletions tests/test_scval.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def test_bool():
assert from_bool(scval) is True


def test_void():
scval = to_void()
expected_scval = xdr.SCVal(stellar_xdr.SCValType.SCV_VOID)
assert scval == expected_scval
assert from_void(scval) is None


def test_bytes():
v = b"hello"
scval = to_bytes(v)
Expand Down

0 comments on commit 6ab765e

Please sign in to comment.