Skip to content

Commit

Permalink
chore: Update pyteal to 0.22 (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
StylishTriangles authored Jan 27, 2023
1 parent 86cd807 commit 5befaf8
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 155 deletions.
39 changes: 29 additions & 10 deletions beaker/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ def __init__(
all_deletes = []
all_opt_ins = []
all_close_outs = []
all_clear_states = []

self.hints: dict[str, MethodHints] = {}
self.bare_externals: dict[str, OnCompleteAction] = {}
self.bare_clear_state: Optional[HandlerFunc] = None
self.methods: dict[str, tuple[ABIReturnSubroutine, Optional[MethodConfig]]] = {}
self.precompiles: dict[str, AppPrecompile | LSigPrecompile] = {}

Expand Down Expand Up @@ -189,6 +189,14 @@ def __init__(

self.bare_externals[oc] = action

# Clear state
elif handler_config.clear_state is not None:
if self.bare_clear_state is not None:
raise BareOverwriteError("clear_state")
if handler_config.referenced_self:
handler_config.clear_state.subroutine.implementation = bound_attr
self.bare_clear_state = static_attr

# ABI externals
elif handler_config.method_spec is not None and not handler_config.internal:
# Create the ABIReturnSubroutine from the static attr
Expand All @@ -210,8 +218,6 @@ def __init__(
all_deletes.append(static_attr)
if handler_config.is_opt_in():
all_opt_ins.append(static_attr)
if handler_config.is_clear_state():
all_clear_states.append(static_attr)
if handler_config.is_close_out():
all_close_outs.append(static_attr)

Expand All @@ -229,13 +235,20 @@ def __init__(
handler_config.subroutine(static_attr),
)

self.on_create = all_creates.pop() if len(all_creates) == 1 else None
self.on_update = all_updates.pop() if len(all_updates) == 1 else None
self.on_delete = all_deletes.pop() if len(all_deletes) == 1 else None
self.on_opt_in = all_opt_ins.pop() if len(all_opt_ins) == 1 else None
self.on_close_out = all_close_outs.pop() if len(all_close_outs) == 1 else None
self.on_clear_state = (
all_clear_states.pop() if len(all_clear_states) == 1 else None
self.on_create: Optional[HandlerFunc] = (
all_creates.pop() if len(all_creates) == 1 else None
)
self.on_update: Optional[HandlerFunc] = (
all_updates.pop() if len(all_updates) == 1 else None
)
self.on_delete: Optional[HandlerFunc] = (
all_deletes.pop() if len(all_deletes) == 1 else None
)
self.on_opt_in: Optional[HandlerFunc] = (
all_opt_ins.pop() if len(all_opt_ins) == 1 else None
)
self.on_close_out: Optional[HandlerFunc] = (
all_close_outs.pop() if len(all_close_outs) == 1 else None
)

self.acct_state = AccountState(acct_vals)
Expand Down Expand Up @@ -264,10 +277,16 @@ def compile(self, client: Optional[AlgodClient] = None) -> tuple[str, str]:
for precompile in self.precompiles.values():
precompile.compile(client) # type: ignore

cs_ast = (
get_handler_config(self.bare_clear_state).clear_state
if self.bare_clear_state
else None
)
self.router = Router(
name=self.__class__.__name__,
bare_calls=BareCallActions(**self.bare_externals),
descr=self.__doc__,
clear_state=cs_ast,
)

# Add method externals
Expand Down
33 changes: 10 additions & 23 deletions beaker/client/application_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,31 +324,18 @@ def clear_state(
sender = self.get_sender(sender, signer)

atc = AtomicTransactionComposer()
if self.app.on_clear_state is not None:
self.add_method_call(
atc,
self.app.on_clear_state,
on_complete=transaction.OnComplete.ClearStateOC,
sender=sender,
suggested_params=sp,
index=self.app_id,
app_args=args,
atc.add_transaction(
TransactionWithSigner(
txn=transaction.ApplicationClearStateTxn(
sender=sender,
sp=sp,
index=self.app_id,
app_args=args,
**kwargs,
),
signer=signer,
**kwargs,
)
else:
atc.add_transaction(
TransactionWithSigner(
txn=transaction.ApplicationClearStateTxn(
sender=sender,
sp=sp,
index=self.app_id,
app_args=args,
**kwargs,
),
signer=signer,
)
)
)

clear_state_result = atc.execute(self.client, 4)

Expand Down
42 changes: 15 additions & 27 deletions beaker/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class HandlerConfig:
read_only: bool = field(kw_only=True, default=False)
internal: bool = field(kw_only=True, default=False)

clear_state: Optional[SubroutineFnWrapper] = field(kw_only=True, default=None)

def hints(self) -> "MethodHints":
mh: dict[str, Any] = {"read_only": self.read_only}

Expand Down Expand Up @@ -185,10 +187,7 @@ def is_opt_in(self) -> bool:
)

def is_clear_state(self) -> bool:
return (
self.method_config is not None
and self.method_config.clear_state != CallConfig.NEVER
)
return self.clear_state is not None

def is_close_out(self) -> bool:
return (
Expand Down Expand Up @@ -530,7 +529,6 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
def bare_external(
no_op: CallConfig | None = None,
opt_in: CallConfig | None = None,
clear_state: CallConfig | None = None,
delete_application: CallConfig | None = None,
update_application: CallConfig | None = None,
close_out: CallConfig | None = None,
Expand All @@ -540,7 +538,6 @@ def bare_external(
Args:
no_op: CallConfig to handle a `NoOp`
opt_in: CallConfig to handle an `OptIn`
clear_state: CallConfig to handle a `ClearState`
delete_application: CallConfig to handle a `DeleteApplication`
update_application: CallConfig to handle a `UpdateApplication`
close_out: CallConfig to handle a `CloseOut`
Expand Down Expand Up @@ -574,9 +571,6 @@ def _impl(fun: HandlerFunc) -> HandlerFunc:
close_out=OnCompleteAction(action=fn, call_config=close_out)
if close_out is not None
else OnCompleteAction.never(),
clear_state=OnCompleteAction(action=fn, call_config=clear_state)
if clear_state is not None
else OnCompleteAction.never(),
)

set_handler_config(fun, bare_method=bca)
Expand Down Expand Up @@ -789,42 +783,36 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:


@overload
def clear_state(
fn: HandlerFunc, /, *, authorize: SubroutineFnWrapper | None = None
) -> HandlerFunc:
def clear_state(fn: HandlerFunc, /) -> HandlerFunc:
...


@overload
def clear_state(*, authorize: SubroutineFnWrapper | None = None) -> DecoratorFunc:
def clear_state() -> DecoratorFunc:
...


def clear_state(
fn: HandlerFunc | None = None, /, *, authorize: SubroutineFnWrapper | None = None
) -> HandlerFunc | DecoratorFunc:
def clear_state(fn: HandlerFunc | None = None, /) -> HandlerFunc | DecoratorFunc:
"""set method to be handled by an application call with it'ws
:code:`OnComplete` set to :code:`ClearState` call
Args:
fn: The method to be wrapped.
authorize: a subroutine with input of ``Txn.sender()`` and output uint64
interpreted as allowed if the output>0.
Returns:
The original method with changes made to its signature and
attributes set in it's :code:`__handler_config__`
"""

def _impl(fn: HandlerFunc) -> HandlerFunc:
if is_bare(fn):
if authorize is not None:
fn = _authorize(authorize)(fn)
return bare_external(clear_state=CallConfig.CALL)(fn)
def _impl(fun: HandlerFunc) -> HandlerFunc:
if is_bare(fun):
fun = _remove_self(fun)
fn = Subroutine(TealType.none)(fun)
set_handler_config(fun, clear_state=fn)
return fun
else:
return external(
method_config=MethodConfig(clear_state=CallConfig.CALL),
authorize=authorize,
)(fn)
raise ValueError(
"clear_state cannot be expected to receive any arguments during runtime"
)

if fn is None:
return _impl
Expand Down
2 changes: 1 addition & 1 deletion examples/amm/amm.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

# WARNING: This code is provided for example only. Do NOT deploy to mainnet.

pragma(compiler_version="^0.21.0")
pragma(compiler_version="^0.22.0")


def commented_assert(conditions: list[tuple[Expr, str]]) -> list[Expr]:
Expand Down
Loading

0 comments on commit 5befaf8

Please sign in to comment.