Skip to content
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
2 changes: 2 additions & 0 deletions pyteal/ast/abi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
algosdk_from_annotation,
algosdk_from_type_spec,
make,
size_of,
type_spec_from_annotation,
)

Expand Down Expand Up @@ -86,6 +87,7 @@
"MethodReturn",
"type_spec_from_annotation",
"make",
"size_of",
"algosdk_from_annotation",
"algosdk_from_type_spec",
]
10 changes: 10 additions & 0 deletions pyteal/ast/abi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ def type_spec_from_annotation(annotation: Any) -> TypeSpec:
T = TypeVar("T", bound=BaseType)


def size_of(t: type[T]) -> int:
"""Get the size in bytes of an ABI type. Must be a static type"""

ts = type_spec_from_annotation(t)
if ts.is_dynamic():
raise TealInputError("Cannot get size of dynamic type")

return ts.byte_length_static()


def make(t: type[T]) -> T:
"""Create a new instance of an ABI type. The type to create is determined by the input argument,
which must be a fully-specified type's class. Fully-specified means that every generic argument
Expand Down
15 changes: 15 additions & 0 deletions pyteal/ast/abi/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
int_literal_from_annotation,
type_spec_from_annotation,
)
from pyteal.errors import TealInputError

options = pt.CompileOptions(version=5)

Expand Down Expand Up @@ -317,6 +318,20 @@ def test_make():
assert type(actual) is abi.Tuple


def test_size_of():
values = [
(abi.Uint8, 1),
(abi.Address, 32),
(abi.StaticArray[abi.Uint16, Literal[10]], 2 * 10),
]

for (t, s) in values:
assert abi.size_of(t) == s

with pytest.raises(TealInputError):
abi.size_of(abi.String)


def test_abi_type_translation():
test_cases = [
# Test for byte/bool/address/strings
Expand Down