Skip to content

Commit 6fdb036

Browse files
committed
Only calculate the topic when it is asked for, while caching the result.
- Calculating the topic at ``__init__`` is not necessary. Instead, calculate it when the topic is asked for and set the internal ``_topic`` attribute only on the first call.
1 parent 695e883 commit 6fdb036

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

web3/contract/base_contract.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class BaseContractEvent:
165165
argument_types: Tuple[str, ...] = tuple()
166166
args: Any = None
167167
kwargs: Any = None
168+
_topic: HexStr = None
168169

169170
def __init__(self, *argument_names: str, abi: Optional[ABIEvent] = None) -> None:
170171
self.abi_element_identifier = type(self).__name__
@@ -175,7 +176,6 @@ def __init__(self, *argument_names: str, abi: Optional[ABIEvent] = None) -> None
175176
self.abi = abi
176177

177178
self.signature = abi_to_signature(self.abi)
178-
self.topic = encode_hex(keccak(text=self.signature))
179179

180180
if argument_names:
181181
self.argument_names = argument_names
@@ -189,6 +189,12 @@ def __repr__(self) -> str:
189189
return f"<Event {abi_to_signature(self.abi)}>"
190190
return f"<Event {get_abi_element_signature(self.abi_element_identifier)}>"
191191

192+
@property
193+
def topic(self) -> HexStr:
194+
if self._topic is None:
195+
self._topic = encode_hex(keccak(text=self.signature))
196+
return self._topic
197+
192198
@combomethod
193199
def _get_event_abi(cls) -> ABIEvent:
194200
if cls.abi:
@@ -205,7 +211,6 @@ def _get_event_abi(cls) -> ABIEvent:
205211

206212
def _set_event_info(self) -> None:
207213
self.abi = self._get_event_abi()
208-
self.topic = encode_hex(keccak(text=self.signature))
209214

210215
@combomethod
211216
def process_receipt(

0 commit comments

Comments
 (0)