Skip to content

Commit

Permalink
refactor: remove scoped_lsig_args
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-makerx committed Aug 9, 2024
1 parent 47b9067 commit adb4e22
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 40 deletions.
5 changes: 2 additions & 3 deletions examples/htlc_logicsig/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def context() -> Generator[AlgopyTestContext, None, None]:

def test_seller_receives_payment(context: AlgopyTestContext) -> None:
# Arrange
with context.txn.enter_txn_group( # noqa: SIM117
with context.txn.enter_txn_group(
[
context.any.txn.payment(
fee=algopy.UInt64(500),
Expand All @@ -31,8 +31,7 @@ def test_seller_receives_payment(context: AlgopyTestContext) -> None:
],
):
# Act
with context.scoped_lsig_args([algopy.Bytes(b"secret")]):
result = context.execute_logicsig(hashed_time_locked_lsig)
result = context.execute_logicsig(hashed_time_locked_lsig, algopy.Bytes(b"secret"))

# Assert
assert result is True
48 changes: 11 additions & 37 deletions src/algopy_testing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import typing
from collections import defaultdict
from contextlib import contextmanager

import algosdk

Expand All @@ -21,7 +20,7 @@
)

if typing.TYPE_CHECKING:
from collections.abc import Generator, Sequence
from collections.abc import Sequence

import algopy

Expand Down Expand Up @@ -60,9 +59,7 @@ def __init__(
self._scratch_spaces = defaultdict[
algopy_testing.gtxn.TransactionBase, list[algopy.Bytes | algopy.UInt64]
](get_new_scratch_space)
# TODO: remove and pass args directly via execute_logicsig
self._active_lsig_args: Sequence[algopy.Bytes] = []

self._active_lsig_args: Sequence[algopy.Bytes] = ()
self._ledger_context = LedgerContext()
self._txn_context = TransactionContext()
self._value_generator = AlgopyValueGenerator(self)
Expand Down Expand Up @@ -212,44 +209,21 @@ def get_application_logs(self, app_id: algopy.UInt64 | int) -> list[bytes]:

return self._application_logs[app_id]

@contextmanager
def scoped_lsig_args(
self, lsig_args: Sequence[algopy.Bytes] | None = None
) -> Generator[None, None, None]:
"""Temporarily set the active logic signature arguments within a
context.
This context manager allows you to set logic signature arguments
for the duration of a specific block of code. When the context
is exited, the previous arguments are restored.
:param lsig_args: The logic signature arguments to set. If None,
an empty list will be used.
:type lsig_args: Sequence[algopy.Bytes] | None
:yield: None
:rtype: Generator[None, None, None]
"""
last_lsig_args = self._active_lsig_args
self._active_lsig_args = lsig_args or []
try:
yield
finally:
self._active_lsig_args = last_lsig_args

def execute_logicsig(
self,
lsig: algopy.LogicSig,
) -> bool | algopy.UInt64:
"""Execute a logic signature.
This method executes the given logic signature.
def execute_logicsig(self, lsig: algopy.LogicSig, *args: algopy.Bytes) -> bool | algopy.UInt64:
"""Execute a logic signature using provided args.
:param lsig: The logic signature to execute.
:type lsig: algopy.LogicSig
:param args: The logic signature arguments to use
:type args: algopy.Bytes
:return: The result of executing the logic signature function.
:rtype: bool | algopy.UInt64
"""
return lsig.func()
self._active_lsig_args = args
try:
return lsig.func()
finally:
self._active_lsig_args = ()

def clear_transaction_context(self) -> None:
"""Clear the transaction context."""
Expand Down

0 comments on commit adb4e22

Please sign in to comment.