Skip to content

Commit

Permalink
perf: use better filtering for contract_creation_query
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcgroul committed Jan 11, 2024
1 parent f53db9f commit 9aaa291
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
1 change: 1 addition & 0 deletions ape_subsquid/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class LogRequest(TypedDict, total=False):

class TraceRequest(TypedDict, total=False):
type: list[TraceType]
createResultAddress: list[str]
transaction: bool
transactionLogs: bool

Expand Down
41 changes: 22 additions & 19 deletions ape_subsquid/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def perform_account_transaction_query(
@perform_query.register
def perform_contract_creation_query(self, query: ContractCreationQuery) -> Iterator[ReceiptAPI]:
network = get_network(self)
contract = query.contract.lower()
q: Query = {
"fromBlock": query.start_block,
"toBlock": query.stop_block,
Expand All @@ -127,30 +128,32 @@ def perform_contract_creation_query(self, query: ContractCreationQuery) -> Itera
"createResultAddress": True,
},
},
"traces": [{"type": ["create"], "transaction": True, "transactionLogs": True}],
"traces": [
{"createResultAddress": [contract], "transaction": True, "transactionLogs": True}
],
}

for data in archive_ingest(self._archive, network, q):
for block in data:
for trace in block["traces"]:
if "result" in trace:
if trace["result"]["address"] == query.contract:
block_number = block["header"]["number"]
block_hash = HexBytes(block["header"]["hash"])
tx = (
tx
for tx in block["transactions"]
if tx["transactionIndex"] == trace["transactionIndex"]
).__next__()
logs = [
map_log(log, block_number, block_hash)
for log in block["logs"]
if log["transactionIndex"] == tx["transactionIndex"]
]
receipt_data = map_receipt(tx, block_number, block_hash, logs)

yield self.provider.network.ecosystem.decode_receipt(receipt_data)
return
assert trace["result"]["address"] == contract

block_number = block["header"]["number"]
block_hash = HexBytes(block["header"]["hash"])
tx = (
tx
for tx in block["transactions"]
if tx["transactionIndex"] == trace["transactionIndex"]
).__next__()
logs = [
map_log(log, block_number, block_hash)
for log in block["logs"]
if log["transactionIndex"] == tx["transactionIndex"]
]
receipt_data = map_receipt(tx, block_number, block_hash, logs)

yield self.provider.network.ecosystem.decode_receipt(receipt_data)
return

@perform_query.register
def perform_contract_event_query(self, query: ContractEventQuery) -> Iterator[ContractLog]:
Expand Down

0 comments on commit 9aaa291

Please sign in to comment.