Skip to content

Commit 3b95140

Browse files
committed
make UInt64 backed enums subtypes of UInt64
1 parent 75cc43d commit 3b95140

File tree

5 files changed

+86
-16
lines changed

5 files changed

+86
-16
lines changed

examples/box/__init__.py

Whitespace-only changes.

examples/box/contract.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from algopy import ARC4Contract, Box, OnCompleteAction, TransactionType, arc4, op
2+
3+
4+
class BoxContract(ARC4Contract):
5+
6+
def __init__(self) -> None:
7+
self.oca = Box(OnCompleteAction)
8+
self.txn = Box(TransactionType)
9+
10+
@arc4.abimethod()
11+
def store_enums(self) -> None:
12+
self.oca.value = OnCompleteAction.OptIn
13+
self.txn.value = TransactionType.ApplicationCall
14+
15+
@arc4.abimethod()
16+
def read_enums(self) -> tuple[OnCompleteAction, TransactionType]:
17+
assert op.Box.get(b"oca")[0] == op.itob(self.oca.value)
18+
assert op.Box.get(b"txn")[0] == op.itob(self.txn.value)
19+
20+
return self.oca.value, self.txn.value

examples/box/test_contract.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from collections.abc import Generator
2+
3+
import pytest
4+
from algopy import op
5+
from algopy_testing import AlgopyTestContext, algopy_testing_context
6+
7+
from .contract import BoxContract
8+
9+
10+
@pytest.fixture()
11+
def context() -> Generator[AlgopyTestContext, None, None]:
12+
with algopy_testing_context() as ctx:
13+
yield ctx
14+
15+
16+
def test_enums(context: AlgopyTestContext) -> None:
17+
# Arrange
18+
contract = BoxContract()
19+
20+
# Act
21+
contract.store_enums()
22+
oca, txn = contract.read_enums()
23+
24+
# Assert
25+
assert context.get_box(b"oca") == op.itob(oca)
26+
assert context.get_box(b"txn") == op.itob(txn)

src/algopy_testing/enums.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
1-
from enum import Enum, IntEnum, StrEnum
1+
from __future__ import annotations
22

3+
from enum import Enum, StrEnum
34

4-
class OnCompleteAction(Enum):
5-
NoOp = 0
6-
OptIn = 1
7-
CloseOut = 2
8-
ClearState = 3
9-
UpdateApplication = 4
10-
DeleteApplication = 5
5+
from algopy_testing.primitives import UInt64
116

127

13-
class TransactionType(IntEnum):
14-
Payment = 0
15-
KeyRegistration = 1
16-
AssetConfig = 2
17-
AssetTransfer = 3
18-
AssetFreeze = 4
19-
ApplicationCall = 5
8+
class OnCompleteAction(UInt64):
9+
NoOp: OnCompleteAction
10+
OptIn: OnCompleteAction
11+
CloseOut: OnCompleteAction
12+
ClearState: OnCompleteAction
13+
UpdateApplication: OnCompleteAction
14+
DeleteApplication: OnCompleteAction
15+
16+
17+
OnCompleteAction.NoOp = OnCompleteAction(0)
18+
OnCompleteAction.OptIn = OnCompleteAction(1)
19+
OnCompleteAction.CloseOut = OnCompleteAction(2)
20+
OnCompleteAction.ClearState = OnCompleteAction(3)
21+
OnCompleteAction.UpdateApplication = OnCompleteAction(4)
22+
OnCompleteAction.DeleteApplication = OnCompleteAction(5)
23+
24+
25+
class TransactionType(UInt64):
26+
Payment: TransactionType
27+
KeyRegistration: TransactionType
28+
AssetConfig: TransactionType
29+
AssetTransfer: TransactionType
30+
AssetFreeze: TransactionType
31+
ApplicationCall: TransactionType
32+
33+
34+
TransactionType.Payment = TransactionType(0)
35+
TransactionType.KeyRegistration = TransactionType(1)
36+
TransactionType.AssetConfig = TransactionType(2)
37+
TransactionType.AssetTransfer = TransactionType(3)
38+
TransactionType.AssetFreeze = TransactionType(4)
39+
TransactionType.ApplicationCall = TransactionType(5)
2040

2141

2242
class ECDSA(Enum):

src/algopy_testing/models/box.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def _full_key(self, key: _TKey) -> algopy.Bytes:
450450
return self.key_prefix + _cast_to_bytes(key)
451451

452452

453-
def _cast_to_value_type(t: type[_TValue], value: bytes) -> _TValue:
453+
def _cast_to_value_type(t: type[_TValue], value: bytes) -> _TValue: # noqa: PLR0911
454454
"""
455455
assuming _TValue to be one of the followings:
456456
- bool,
@@ -474,6 +474,10 @@ def _cast_to_value_type(t: type[_TValue], value: bytes) -> _TValue:
474474
return algopy.Bytes(value) # type: ignore[return-value]
475475
elif t is algopy.UInt64:
476476
return algopy.op.btoi(value) # type: ignore[return-value]
477+
elif t is algopy.OnCompleteAction:
478+
return algopy.OnCompleteAction(algopy.op.btoi(value).value) # type: ignore[return-value]
479+
elif t is algopy.TransactionType:
480+
return algopy.TransactionType(algopy.op.btoi(value).value) # type: ignore[return-value]
477481
elif t is algopy.Asset:
478482
asset_id = algopy.op.btoi(value)
479483
return context.get_asset(asset_id) # type: ignore[return-value]

0 commit comments

Comments
 (0)