Skip to content

Commit

Permalink
set key and key prefix from contract field name
Browse files Browse the repository at this point in the history
  • Loading branch information
boblat committed Jul 19, 2024
1 parent 7aa6f73 commit 038aa92
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 191 deletions.
12 changes: 5 additions & 7 deletions examples/proof_of_attendance/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def __init__(self) -> None:
self.max_attendees = algopy.UInt64(30)
self.asset_url = algopy.String("ipfs://QmW5vERkgeJJtSY1YQdcWU6gsHCZCyLFtM1oT9uyy2WGm8")
self.total_attendees = algopy.UInt64(0)
self.box_map = algopy.BoxMap(algopy.Bytes, algopy.UInt64)

@algopy.arc4.abimethod(create="require")
def init(self, max_attendees: algopy.UInt64) -> None:
Expand Down Expand Up @@ -57,11 +58,10 @@ def confirm_attendance_with_box_map(self) -> None:
minted_asset = self._mint_poa(algopy.Txn.sender)
self.total_attendees += 1

box_map = algopy.BoxMap(type(algopy.Txn.sender.bytes), algopy.UInt64)
has_claimed = algopy.Txn.sender.bytes in box_map
has_claimed = algopy.Txn.sender.bytes in self.box_map
assert not has_claimed, "Already claimed POA"

box_map[algopy.Txn.sender.bytes] = minted_asset.id
self.box_map[algopy.Txn.sender.bytes] = minted_asset.id

@algopy.arc4.abimethod(readonly=True)
def get_poa_id(self) -> algopy.UInt64:
Expand All @@ -85,8 +85,7 @@ def get_poa_id_with_box_ref(self) -> algopy.UInt64:

@algopy.arc4.abimethod(readonly=True)
def get_poa_id_with_box_map(self) -> algopy.UInt64:
box_map = algopy.BoxMap(type(algopy.Txn.sender.bytes), algopy.UInt64)
poa_id, exists = box_map.maybe(algopy.Txn.sender.bytes)
poa_id, exists = self.box_map.maybe(algopy.Txn.sender.bytes)
assert exists, "POA not found"
return poa_id

Expand Down Expand Up @@ -151,8 +150,7 @@ def claim_poa_with_box_ref(self, opt_in_txn: algopy.gtxn.AssetTransferTransactio

@algopy.arc4.abimethod()
def claim_poa_with_box_map(self, opt_in_txn: algopy.gtxn.AssetTransferTransaction) -> None:
box_map = algopy.BoxMap(type(algopy.Txn.sender.bytes), algopy.UInt64)
poa_id, exists = box_map.maybe(algopy.Txn.sender.bytes)
poa_id, exists = self.box_map.maybe(algopy.Txn.sender.bytes)
assert exists, "POA not found, attendance validation failed!"
assert opt_in_txn.xfer_asset.id == poa_id, "POA ID mismatch"
assert opt_in_txn.fee == algopy.UInt64(0), "We got you covered for free!"
Expand Down
31 changes: 18 additions & 13 deletions examples/proof_of_attendance/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ def test_init(context: AlgopyTestContext) -> None:


@pytest.mark.parametrize(
"confirm_attendance",
("confirm_attendance", "key_prefix"),
[
"confirm_attendance",
"confirm_attendance_with_box",
"confirm_attendance_with_box_ref",
"confirm_attendance_with_box_map",
("confirm_attendance", b""),
("confirm_attendance_with_box", b""),
("confirm_attendance_with_box_ref", b""),
("confirm_attendance_with_box_map", b"box_map"),
],
)
def test_confirm_attendance(
context: AlgopyTestContext,
confirm_attendance: str,
key_prefix: bytes,
) -> None:
# Arrange
contract = ProofOfAttendance()
Expand All @@ -49,19 +50,23 @@ def test_confirm_attendance(
confirm()

# Assert
assert context.get_box(context.default_creator.bytes) == algopy.op.itob(1)
assert context.get_box(key_prefix + context.default_creator.bytes) == algopy.op.itob(1)


@pytest.mark.parametrize(
"claim_poa",
("claim_poa", "key_prefix"),
[
"claim_poa",
"claim_poa_with_box",
"claim_poa_with_box_ref",
"claim_poa_with_box_map",
("claim_poa", b""),
("claim_poa_with_box", b""),
("claim_poa_with_box_ref", b""),
("claim_poa_with_box_map", b"box_map"),
],
)
def test_claim_poa(context: AlgopyTestContext, claim_poa: str) -> None:
def test_claim_poa(
context: AlgopyTestContext,
claim_poa: str,
key_prefix: bytes,
) -> None:
# Arrange
contract = ProofOfAttendance()
dummy_poa = context.any_asset()
Expand All @@ -74,7 +79,7 @@ def test_claim_poa(context: AlgopyTestContext, claim_poa: str) -> None:
fee=algopy.UInt64(0),
asset_amount=algopy.UInt64(0),
)
context.set_box(context.default_creator.bytes, algopy.op.itob(dummy_poa.id))
context.set_box(key_prefix + context.default_creator.bytes, algopy.op.itob(dummy_poa.id))

# Act
claim = getattr(contract, claim_poa)
Expand Down
Loading

0 comments on commit 038aa92

Please sign in to comment.