Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 58f04d5

Browse files
authored
Add support for event querying (#111)
* Add event filtering and docs * Add default to query options * Update version
1 parent b5e008c commit 58f04d5

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

docs/docs/contract-events.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,24 @@ def remove_all_listeners()
8181

8282
Remove all event listeners from this contract.
8383

84+
<a id="core.classes.contract_events.ContractEvents.get_events"></a>
85+
86+
#### get\_events
87+
88+
```python
89+
def get_events(
90+
event_name: str, options: EventQueryOptions = EventQueryOptions()
91+
) -> Tuple[AttributeDict]
92+
```
93+
94+
Query past events of a specific type on the contract.
95+
96+
**Arguments**:
97+
98+
- `event_name`: The name of the event to query.
99+
- `options`: The options to use when querying for events, including block range specifications and filters
100+
101+
**Returns**:
102+
103+
A list of events.
104+

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ packages = [
66
{include = "thirdweb"},
77
]
88
readme = "README.md"
9-
version = "2.0.18"
9+
version = "2.1.1"
1010

1111
[tool.poetry.dependencies]
1212
python = ">=3.7.1"

tests/test_events.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from thirdweb.contracts.nft_collection import NFTCollection
88
from thirdweb.core.sdk import ThirdwebSDK
99
from thirdweb.types.contracts.signature import PayloadToSign721
10-
from thirdweb.types.events import SignatureEvent, TxEvent
10+
from thirdweb.types.events import EventQueryOptions, SignatureEvent, TxEvent
1111
from thirdweb.types.nft import NFTMetadataInput
1212
from thirdweb.types.settings.metadata import NFTCollectionContractMetadata
1313

@@ -77,3 +77,14 @@ def listener(event: SignatureEvent):
7777
)
7878
)
7979
assert event_status is not None
80+
81+
82+
@pytest.mark.usefixtures("primary_account")
83+
def test_collection_events(nft_collection: NFTCollection, primary_account, secondary_account):
84+
nft_collection.mint(NFTMetadataInput(name="Minted"))
85+
events = nft_collection.events.get_events("TokensMinted", EventQueryOptions(filters={"mintedTo": primary_account.address}))
86+
assert len(events) == 1
87+
assert events[0].get("event") == "TokensMinted"
88+
89+
events = nft_collection.events.get_events("TokensMinted", EventQueryOptions(filters={"mintedTo": secondary_account.address}))
90+
assert len(events) == 0

thirdweb/core/classes/contract_events.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from typing import Any, Callable, Dict, Generic
1+
from typing import Any, Callable, Dict, Generic, Tuple
22
from thirdweb.constants.events import EventType
33
from thirdweb.core.classes.contract_wrapper import ContractWrapper
44
from thirdweb.types.contract import TContractABI
5-
from thirdweb.types.events import TxEvent
6-
7-
5+
from thirdweb.types.events import EventQueryOptions, TxEvent
6+
from web3.datastructures import AttributeDict
87
class ContractEvents(Generic[TContractABI]):
98
_contract_wrapper: ContractWrapper[TContractABI]
109

@@ -71,3 +70,16 @@ def remove_all_listeners(self):
7170
"""
7271

7372
self._contract_wrapper.remove_all_listeners()
73+
74+
def get_events(self, event_name: str, options: EventQueryOptions = EventQueryOptions()) -> Tuple[AttributeDict]:
75+
"""
76+
Query past events of a specific type on the contract.
77+
78+
:param event_name: The name of the event to query.
79+
:param options: The options to use when querying for events, including block range specifications and filters
80+
:return: A list of events.
81+
"""
82+
83+
events_interface = self._contract_wrapper.get_contract_interface().events[event_name]
84+
return events_interface.getLogs(options.filters, options.from_block, options.to_block)
85+

thirdweb/types/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from .sdk import *
2-
from .nft import *
3-
from .multiwrap import *
4-
from .marketplace import *
5-
from .settings import *
6-
from .auth import *
1+
from thirdweb.types.sdk import *
2+
from thirdweb.types.nft import *
3+
from thirdweb.types.multiwrap import *
4+
from thirdweb.types.marketplace import *
5+
from thirdweb.types.settings import *
6+
from thirdweb.types.auth import *
7+
from thirdweb.types.events import *

thirdweb/types/events.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from dataclasses import dataclass
2-
from typing import Union
3-
2+
import dataclasses
3+
from typing import Any, Dict, Optional, Union
44
from thirdweb.constants.events import EventStatus
5+
from web3.types import BlockIdentifier
56

67

78
@dataclass
@@ -15,3 +16,9 @@ class SignatureEvent:
1516
status: EventStatus
1617
message: str
1718
signature: Union[str, bytes]
19+
20+
@dataclass
21+
class EventQueryOptions:
22+
filters: Dict[str, Any] = dataclasses.field(default_factory=dict)
23+
from_block: Optional[BlockIdentifier] = "earliest"
24+
to_block: Optional[BlockIdentifier] = "latest"

0 commit comments

Comments
 (0)