Skip to content

Commit

Permalink
Add keys (indexed fields aka topics) to the EventType (abi v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus committed Nov 21, 2024
1 parent 2a84190 commit 8d76ee0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions starknet_py/abi/v2/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def _parse_event(self, event: EventDict) -> EventType:
types=self._parse_members(
cast(List[TypedParameterDict], members_), event["name"]
),
keys=self._parse_keys(cast(List[EventStructMemberDict], members_)),
)

TypedParam = TypeVar(
Expand All @@ -250,6 +251,9 @@ def _parse_members(
for name, param in members.items()
)

def _parse_keys(self, params: List[EventStructMemberDict]) -> List[str]:
return [param["name"] for param in params if param["kind"] == "key"]

def _parse_interface(self, interface: InterfaceDict) -> Abi.Interface:
return Abi.Interface(
name=interface["name"],
Expand Down
7 changes: 5 additions & 2 deletions starknet_py/abi/v2/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from marshmallow import Schema, fields
from marshmallow import Schema, fields, validate
from marshmallow_oneofschema.one_of_schema import OneOfSchema

from starknet_py.abi.v2.shape import (
Expand All @@ -9,6 +9,7 @@
FUNCTION_ENTRY,
IMPL_ENTRY,
INTERFACE_ENTRY,
KEY_KIND,
L1_HANDLER_ENTRY,
NESTED_KIND,
STRUCT_ENTRY,
Expand Down Expand Up @@ -51,7 +52,9 @@ class L1HandlerAbiEntrySchema(FunctionBaseSchema):


class EventStructMemberSchema(TypedParameterSchema):
kind = fields.Constant(DATA_KIND, data_key="kind", required=True)
kind = fields.String(
validate=validate.OneOf([KEY_KIND, DATA_KIND]), data_key="kind", required=True
)


class EventStructAbiEntrySchema(Schema):
Expand Down
3 changes: 2 additions & 1 deletion starknet_py/abi/v2/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
IMPL_ENTRY = "impl"
INTERFACE_ENTRY = "interface"

KEY_KIND = "key"
DATA_KIND = "data"
NESTED_KIND = "nested"

Expand Down Expand Up @@ -56,7 +57,7 @@ class EventBaseDict(TypedDict):


class EventStructMemberDict(TypedParameterDict):
kind: Literal["data"]
kind: Union[Literal["key"], Literal["data"]]


class EventStructDict(EventBaseDict):
Expand Down
1 change: 1 addition & 0 deletions starknet_py/cairo/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@ class EventType(CairoType):

name: str
types: OrderedDict[str, CairoType]
keys: List[str]
3 changes: 1 addition & 2 deletions starknet_py/tests/e2e/fixtures/abi_v2_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
pool_id_struct = StructType("PoolId", OrderedDict(value=UintType(256)))

pool_id_added_event: EventType = EventType(
"PoolIdAdded",
OrderedDict(pool_id=pool_id_struct),
"PoolIdAdded", OrderedDict(pool_id=pool_id_struct), []
)

abi_v2 = Abi(
Expand Down
1 change: 1 addition & 0 deletions starknet_py/tests/unit/abi/v2/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"TestEnum",
"TestOption",
"TokenBridge",
"l1_l2",
],
)
def test_abi_parse(contract_name):
Expand Down

0 comments on commit 8d76ee0

Please sign in to comment.