Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly support note prefix as bytes or string #15

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TransactionFilter(TypedDict):
receiver: NotRequired[str | list[str]]
"""Filter to transactions being received by the specified address(es)."""

note_prefix: NotRequired[str]
note_prefix: NotRequired[str | bytes]
"""Filter to transactions with a note having the given prefix."""

app_id: NotRequired[int | list[int]]
Expand Down
2 changes: 1 addition & 1 deletion src/algokit_subscriber/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def poll_once(self) -> TransactionSubscriptionResult:
matched_transactions = [
t
for t in poll_result["subscribed_transactions"]
if filter_name in t.get("filters_matched", [])
if filter_name in (t.get("filters_matched") or [])
]
mapped_transactions = (
mapper(matched_transactions) if mapper else matched_transactions
Expand Down
24 changes: 17 additions & 7 deletions src/algokit_subscriber/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def extract_arc28_events(
return emitted_events if emitted_events else []


def indexer_pre_filter(
def indexer_pre_filter( # noqa: C901
subscription: TransactionFilter, min_round: int, max_round: int
) -> dict[str, Any]:
"""
Expand All @@ -212,9 +212,10 @@ def indexer_pre_filter(
args["txn_type"] = subscription["type"]

if subscription.get("note_prefix"):
args["note_prefix"] = base64.b64encode(
subscription["note_prefix"].encode()
).decode()
if isinstance(subscription["note_prefix"], bytes):
args["note_prefix"] = base64.b64encode(subscription["note_prefix"])
elif isinstance(subscription["note_prefix"], str):
args["note_prefix"] = subscription["note_prefix"].encode()

if subscription.get("app_id") and isinstance(subscription["app_id"], int):
args["application_id"] = int(subscription["app_id"])
Expand Down Expand Up @@ -294,9 +295,18 @@ def filter_transaction(t: TransactionResult) -> bool: # noqa: C901, PLR0912
result = result and t["tx-type"] in subscription["type"]

if subscription.get("note_prefix"):
result = result and t.get("note", "").startswith(
subscription["note_prefix"]
)
if isinstance(subscription["note_prefix"], bytes):
note = t.get("note", b"")
result = (
result
and len(note) >= len(subscription["note_prefix"])
and note[: len(subscription["note_prefix"])]
== subscription["note_prefix"]
)
else:
result = result and t.get("note", "").startswith(
subscription["note_prefix"]
)

if subscription.get("app_id"):
if isinstance(subscription["app_id"], int):
Expand Down
2 changes: 1 addition & 1 deletion src/algokit_subscriber/types/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class TransactionFilter(TypedDict):
receiver: NotRequired[str | list[str]]
"""Filter to transactions being received by the specified address(es)."""

note_prefix: NotRequired[str]
note_prefix: NotRequired[str | bytes]
"""Filter to transactions with a note having the given prefix."""

app_id: NotRequired[int | list[int]]
Expand Down
Loading