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
9 changes: 6 additions & 3 deletions pyteal/ast/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def add_method_handler(
overriding_name: str = None,
method_config: MethodConfig = None,
description: str = None,
) -> None:
) -> ABIReturnSubroutine:
if not isinstance(method_call, ABIReturnSubroutine):
raise TealInputError(
"for adding method handler, must be ABIReturnSubroutine"
Expand Down Expand Up @@ -576,6 +576,7 @@ def add_method_handler(
self.clear_state_ast.add_method_to_ast(
method_signature, method_clear_state_cond, method_call
)
return method_call

def method(
self,
Expand Down Expand Up @@ -607,7 +608,7 @@ def method(
# - None
# - CallConfig.Never
# both cases evaluate to False in if statement.
def wrap(_func):
def wrap(_func) -> ABIReturnSubroutine:
wrapped_subroutine = ABIReturnSubroutine(_func)
call_configs: MethodConfig
if (
Expand Down Expand Up @@ -639,7 +640,9 @@ def none_to_never(x: None | CallConfig):
update_application=_update_app,
delete_application=_delete_app,
)
self.add_method_handler(wrapped_subroutine, name, call_configs, description)
return self.add_method_handler(
wrapped_subroutine, name, call_configs, description
)

if not func:
return wrap
Expand Down
34 changes: 24 additions & 10 deletions pyteal/compiler/compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2203,39 +2203,44 @@ def add(
) -> pt.Expr:
return output.set(a.get() + b.get())

router.add_method_handler(add)
meth = router.add_method_handler(add)
assert meth.method_signature() == "add(uint64,uint64)uint64"

@pt.ABIReturnSubroutine
def sub(
a: pt.abi.Uint64, b: pt.abi.Uint64, *, output: pt.abi.Uint64
) -> pt.Expr:
return output.set(a.get() - b.get())

router.add_method_handler(sub)
meth = router.add_method_handler(sub)
assert meth.method_signature() == "sub(uint64,uint64)uint64"

@pt.ABIReturnSubroutine
def mul(
a: pt.abi.Uint64, b: pt.abi.Uint64, *, output: pt.abi.Uint64
) -> pt.Expr:
return output.set(a.get() * b.get())

router.add_method_handler(mul)
meth = router.add_method_handler(mul)
assert meth.method_signature() == "mul(uint64,uint64)uint64"

@pt.ABIReturnSubroutine
def div(
a: pt.abi.Uint64, b: pt.abi.Uint64, *, output: pt.abi.Uint64
) -> pt.Expr:
return output.set(a.get() / b.get())

router.add_method_handler(div)
meth = router.add_method_handler(div)
assert meth.method_signature() == "div(uint64,uint64)uint64"

@pt.ABIReturnSubroutine
def mod(
a: pt.abi.Uint64, b: pt.abi.Uint64, *, output: pt.abi.Uint64
) -> pt.Expr:
return output.set(a.get() % b.get())

router.add_method_handler(mod)
meth = router.add_method_handler(mod)
assert meth.method_signature() == "mod(uint64,uint64)uint64"

@pt.ABIReturnSubroutine
def all_laid_to_args(
Expand Down Expand Up @@ -2277,26 +2282,31 @@ def all_laid_to_args(
+ _p.get()
)

router.add_method_handler(all_laid_to_args)
meth = router.add_method_handler(all_laid_to_args)
assert (
meth.method_signature()
== "all_laid_to_args(uint64,uint64,uint64,uint64,uint64,uint64,uint64,uint64,uint64,uint64,uint64,uint64,uint64,uint64,uint64,uint64)uint64"
)

@pt.ABIReturnSubroutine
def empty_return_subroutine() -> pt.Expr:
return pt.Log(pt.Bytes("appear in both approval and clear state"))

router.add_method_handler(
meth = router.add_method_handler(
empty_return_subroutine,
method_config=pt.MethodConfig(
no_op=pt.CallConfig.CALL,
opt_in=pt.CallConfig.ALL,
clear_state=pt.CallConfig.CALL,
),
)
assert meth.method_signature() == "empty_return_subroutine()void"

@pt.ABIReturnSubroutine
def log_1(*, output: pt.abi.Uint64) -> pt.Expr:
return output.set(1)

router.add_method_handler(
meth = router.add_method_handler(
log_1,
method_config=pt.MethodConfig(
no_op=pt.CallConfig.CALL,
Expand All @@ -2305,13 +2315,16 @@ def log_1(*, output: pt.abi.Uint64) -> pt.Expr:
),
)

assert meth.method_signature() == "log_1()uint64"

@pt.ABIReturnSubroutine
def log_creation(*, output: pt.abi.String) -> pt.Expr:
return output.set("logging creation")

router.add_method_handler(
meth = router.add_method_handler(
log_creation, method_config=pt.MethodConfig(no_op=pt.CallConfig.CREATE)
)
assert meth.method_signature() == "log_creation()string"

@pt.ABIReturnSubroutine
def approve_if_odd(condition_encoding: pt.abi.Uint32) -> pt.Expr:
Expand All @@ -2321,12 +2334,13 @@ def approve_if_odd(condition_encoding: pt.abi.Uint32) -> pt.Expr:
.Else(pt.Reject())
)

router.add_method_handler(
meth = router.add_method_handler(
approve_if_odd,
method_config=pt.MethodConfig(
no_op=pt.CallConfig.NEVER, clear_state=pt.CallConfig.CALL
),
)
assert meth.method_signature() == "approve_if_odd(uint32)void"

on_completion_actions = pt.BareCallActions(
opt_in=pt.OnCompleteAction.call_only(pt.Log(pt.Bytes("optin call"))),
Expand Down