Skip to content

Commit 71801f0

Browse files
refactor: moving scratch slots and app logs to TransactionBase (#10)
* refactor: moving scratch slots and app logs to TransactionBase * build: updated to latest puyapy * refactor: minor refactors * refactor: refactor txn group initialization * refactor: add additional gtxn properties from 2.0 stubs * refactor: consolidate log and scratch space implementation. logs can be mocked on the underlying application, or for specific transactions active txn can append logs * refactor: defer some imports to reduce circular imports * chore: add some TODO's for future work * test: tweak test for mocking abi_call, by ensuring mocked function is in algopy namespace * feat: implement gaid op * fix: added mock implementations for new algopy functions, add util for raising consistent mockable method errors * chore: add TODO for asset property --------- Co-authored-by: Daniel McGregor <daniel.mcgregor@makerx.com.au>
1 parent 6df2567 commit 71801f0

File tree

30 files changed

+421
-389
lines changed

30 files changed

+421
-389
lines changed

examples/scratch_storage/test_contract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_arc4_contract(context: AlgopyTestContext) -> None:
2222

2323
# Assert
2424
assert result
25-
scratch_space = context.txn.get_scratch_space(context.txn.last_active)
25+
scratch_space = context.txn.last_group.get_scratch_space()
2626
assert scratch_space[1] == 5
2727
assert scratch_space[2] == b"Hello World"
2828

examples/zk_whitelist/test_contract.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@ def test_add_address_to_whitelist(
3636
)
3737
)
3838

39-
# TODO: allow callables for side effects
40-
dummy_verifier_app = context.any.application()
39+
dummy_verifier_app = context.any.application(logs=[arc4_prefix(b"\x80")])
4140
context.set_template_var("VERIFIER_APP_ID", dummy_verifier_app.id)
42-
context.txn.add_app_logs(
43-
app_id=dummy_verifier_app.id,
44-
logs=arc4_prefix(b"\x80"),
45-
)
4641

4742
# Act
4843
result = contract.add_address_to_whitelist(address, proof)
@@ -64,12 +59,8 @@ def test_add_address_to_whitelist_invalid_proof(
6459
*[algopy.arc4.Byte(0) for _ in range(32)]
6560
)
6661
)
67-
dummy_verifier_app = context.any.application()
62+
dummy_verifier_app = context.any.application(logs=[arc4_prefix(b"")])
6863
context.set_template_var("VERIFIER_APP_ID", dummy_verifier_app.id)
69-
context.txn.add_app_logs(
70-
app_id=dummy_verifier_app.id,
71-
logs=arc4_prefix(b""),
72-
)
7364

7465
# Act
7566
result = contract.add_address_to_whitelist(address, proof)

pyproject.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies = [
2929
"pynacl>=1.4.0,<2",
3030
"ecdsa>=0.17.0",
3131
"coincurve>=19.0.1",
32-
"algorand-python>=1.2"
32+
"algorand-python>=2.0"
3333
]
3434

3535
[project.urls]
@@ -52,7 +52,7 @@ type = "virtual"
5252
path = ".venv"
5353
python = "3.12"
5454
dependencies = [
55-
"puyapy>=2.2.0",
55+
"puyapy>=3.0",
5656
"pytest>=7.4",
5757
"pytest-mock>=3.10.0",
5858
"pytest-xdist[psutil]>=3.3",
@@ -243,6 +243,11 @@ unfixable = [
243243
"F841", # don't delete unused local variables automatically
244244
]
245245
[tool.ruff.lint.per-file-ignores]
246+
"src/algopy/**" = [
247+
"PLC0414", # allow explicit alias
248+
"F401", # allow unused imports in module with stubs
249+
"F403" # allow * imports in module with stubs
250+
]
246251
"src/**" = [
247252
"PT", # no pytest rules
248253
]

src/algopy/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from algopy_testing._compiled import (
2+
CompiledContract,
3+
CompiledLogicSig,
4+
compile_contract,
5+
compile_logicsig,
6+
)
17
from algopy_testing.decorators.subroutine import subroutine
28
from algopy_testing.enums import OnCompleteAction, TransactionType
39
from algopy_testing.models.account import Account
@@ -23,6 +29,8 @@
2329
"BigUInt",
2430
"Bytes",
2531
"BytesBacked",
32+
"CompiledContract",
33+
"CompiledLogicSig",
2634
"Contract",
2735
"Global",
2836
"GlobalState",
@@ -37,6 +45,8 @@
3745
"Txn",
3846
"UInt64",
3947
"arc4",
48+
"compile_contract",
49+
"compile_logicsig",
4050
"ensure_budget",
4151
"gtxn",
4252
"itxn",

src/algopy/arc4.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
from algopy_testing.arc4 import * # noqa: F403
1+
from algopy_testing.arc4 import *
2+
from algopy_testing.decorators.arc4 import (
3+
abimethod as abimethod,
4+
baremethod as baremethod,
5+
)

src/algopy/gtxn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from algopy_testing.gtxn import * # noqa: F403
1+
from algopy_testing.gtxn import *

src/algopy/itxn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from algopy_testing.itxn import * # noqa: F403
1+
from algopy_testing.itxn import *

src/algopy/op.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from algopy_testing.op import * # noqa: F403
1+
from algopy_testing.op import *

src/algopy_testing/_compiled.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from __future__ import annotations
2+
3+
# ruff: noqa: ARG001, PLR0913
4+
import dataclasses
5+
import typing
6+
7+
from algopy_testing.utils import raise_mocked_function_error
8+
9+
if typing.TYPE_CHECKING:
10+
from collections.abc import Mapping
11+
12+
import algopy
13+
14+
15+
@dataclasses.dataclass
16+
class CompiledContract:
17+
approval_program: tuple[algopy.Bytes, algopy.Bytes]
18+
clear_state_program: tuple[algopy.Bytes, algopy.Bytes]
19+
extra_program_pages: algopy.UInt64
20+
global_uints: algopy.UInt64
21+
global_bytes: algopy.UInt64
22+
local_uints: algopy.UInt64
23+
local_bytes: algopy.UInt64
24+
25+
26+
@dataclasses.dataclass
27+
class CompiledLogicSig:
28+
account: algopy.Account
29+
30+
31+
def compile_contract(
32+
contract: type[algopy.Contract],
33+
/,
34+
*,
35+
extra_program_pages: algopy.UInt64 | int = 0,
36+
global_uints: algopy.UInt64 | int = 0,
37+
global_bytes: algopy.UInt64 | int = 0,
38+
local_uints: algopy.UInt64 | int = 0,
39+
local_bytes: algopy.UInt64 | int = 0,
40+
template_vars: Mapping[str, object] | None = None,
41+
template_vars_prefix: str = "",
42+
) -> CompiledContract:
43+
raise_mocked_function_error("compile_contract")
44+
45+
46+
def compile_logicsig(
47+
logicsig: algopy.LogicSig,
48+
/,
49+
*,
50+
template_vars: Mapping[str, object] | None = None,
51+
template_vars_prefix: str = "",
52+
) -> CompiledLogicSig:
53+
raise_mocked_function_error("compile_logicsig")

src/algopy_testing/_context_helpers/ledger_context.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
import typing
44
from collections import defaultdict
55

6-
from algopy_testing.models.account import AccountContextData, AccountFields, get_empty_account
76
from algopy_testing.utils import assert_address_is_valid, get_default_global_fields
87

98
if typing.TYPE_CHECKING:
109
import algopy
1110

11+
from algopy_testing.models.account import AccountFields
1212
from algopy_testing.models.application import ApplicationContextData, ApplicationFields
1313
from algopy_testing.models.asset import AssetFields
1414
from algopy_testing.op.global_values import GlobalFields
1515

1616

1717
class LedgerContext:
1818
def __init__(self) -> None:
19+
from algopy_testing.models.account import AccountContextData, get_empty_account
20+
1921
self.account_data = defaultdict[str, AccountContextData](get_empty_account)
2022
self.application_data: dict[int, ApplicationContextData] = {}
2123
self.asset_data: dict[int, AssetFields] = {}

0 commit comments

Comments
 (0)