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

ABI Strings #278

Merged
merged 8 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions pyteal/ast/abi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .string import String, Address, StringTypeSpec, AddressTypeSpec
from .type import TypeSpec, BaseType, ComputedValue
from .bool import BoolTypeSpec, Bool
from .uint import (
Expand Down Expand Up @@ -32,6 +33,10 @@
from .method_return import MethodReturn

__all__ = [
"String",
"StringTypeSpec",
"Address",
"AddressTypeSpec",
"TypeSpec",
"BaseType",
"ComputedValue",
Expand Down
55 changes: 55 additions & 0 deletions pyteal/ast/abi/string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from .array_static import StaticArray, StaticArrayTypeSpec
from .array_dynamic import DynamicArray, DynamicArrayTypeSpec
from .uint import ByteTypeSpec

address_length = 32


class AddressTypeSpec(StaticArrayTypeSpec):
def __init__(self) -> None:
super().__init__(ByteTypeSpec, address_length)
barnjamin marked this conversation as resolved.
Show resolved Hide resolved

def new_instance(self) -> "Address":
return Address()

def __str__(self) -> str:
return "address"


AddressTypeSpec.__module__ = "pyteal"


class Address(StaticArray):
def __init__(self) -> None:
super().__init__(AddressTypeSpec(), address_length)

def type_spec(self) -> AddressTypeSpec:
return AddressTypeSpec()


Address.__module__ = "pyteal"


class StringTypeSpec(DynamicArrayTypeSpec):
def __init__(self) -> None:
super().__init__(ByteTypeSpec)
barnjamin marked this conversation as resolved.
Show resolved Hide resolved

def new_instance(self) -> "String":
return String()

def __str__(self) -> str:
return "string"


StringTypeSpec.__module__ = "pyteal"


class String(DynamicArray):
def __init__(self) -> None:
super().__init__(StringTypeSpec())

def type_spec(self) -> StringTypeSpec:
return StringTypeSpec()


String.__module__ = "pyteal"
11 changes: 11 additions & 0 deletions pyteal/ast/abi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def type_spec_from_annotation(annotation: Any) -> TypeSpec:
Tuple4,
Tuple5,
)
from .string import String, Address, StringTypeSpec, AddressTypeSpec

origin = get_origin(annotation)
if origin is None:
Expand Down Expand Up @@ -144,6 +145,16 @@ def type_spec_from_annotation(annotation: Any) -> TypeSpec:
raise TypeError("Uint64 expects 0 type arguments. Got: {}".format(args))
return Uint64TypeSpec()

if origin is String:
if len(args) != 0:
raise TypeError("String expects 0 arguments. Got: {}".format(args))
return StringTypeSpec()

if origin is Address:
if len(args) != 0:
raise TypeError("Address expects 0 arguments. Got: {}".format(args))
return AddressTypeSpec()

if origin is DynamicArray:
if len(args) != 1:
raise TypeError(
Expand Down