Skip to content

Commit

Permalink
refactor: rename Archive to SubsquidGateway
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcgroul committed Feb 12, 2024
1 parent c1ac39f commit fb14959
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions ape_subsquid/archive.py → ape_subsquid/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class Block(TypedDict, total=False):
T = TypeVar("T")


class Archive:
class SubsquidGateway:
_session = Session()
_retry_schedule = [5, 10, 20, 30, 60]

Expand Down Expand Up @@ -274,7 +274,7 @@ def _retry(self, request: Callable[..., T], *args, **kwargs) -> T:
if self._is_retryable_error(e) and retries < len(self._retry_schedule):
pause = self._retry_schedule[retries]
retries += 1
logger.warning(f"Archive request failed, will retry in {pause} secs")
logger.warning(f"Gateway request failed, will retry in {pause} secs")
sleep(pause)
else:
self._raise_error(e)
Expand Down
2 changes: 1 addition & 1 deletion ape_subsquid/mappings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from hexbytes import HexBytes

from ape_subsquid.archive import BlockHeader, Log, Transaction
from ape_subsquid.gateway import BlockHeader, Log, Transaction
from ape_subsquid.utils import hex_to_int


Expand Down
26 changes: 13 additions & 13 deletions ape_subsquid/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
from ape.utils import singledispatchmethod
from hexbytes import HexBytes

from ape_subsquid.archive import (
Archive,
from ape_subsquid.exceptions import DataRangeIsNotAvailable
from ape_subsquid.gateway import (
Block,
BlockFieldSelection,
LogFieldSelection,
Query,
SubsquidGateway,
TxFieldSelection,
)
from ape_subsquid.exceptions import DataRangeIsNotAvailable
from ape_subsquid.mappings import map_header, map_log, map_receipt
from ape_subsquid.networks import get_network


class SubsquidQueryEngine(QueryAPI):
_archive = Archive()
_gateway = SubsquidGateway()

@singledispatchmethod
def estimate_query(self, query: QueryType) -> Optional[int]: # type: ignore[override]
Expand Down Expand Up @@ -70,7 +70,7 @@ def perform_block_query(self, query: BlockQuery) -> Iterator[BlockAPI]:
"transactions": [{}],
}

for data in archive_ingest(self._archive, network, q):
for data in gateway_ingest(self._gateway, network, q):
for block in data:
header_data = map_header(block["header"], block["transactions"])
yield self.provider.network.ecosystem.decode_block(header_data)
Expand All @@ -96,7 +96,7 @@ def perform_account_transaction_query(
],
}

for data in archive_ingest(self._archive, network, q):
for data in gateway_ingest(self._gateway, network, q):
for block in data:
for tx in block["transactions"]:
assert tx["nonce"] >= query.start_nonce
Expand Down Expand Up @@ -136,7 +136,7 @@ def perform_contract_creation_query(self, query: ContractCreationQuery) -> Itera
],
}

for data in archive_ingest(self._archive, network, q):
for data in gateway_ingest(self._gateway, network, q):
for block in data:
for trace in block["traces"]:
assert trace["result"]["address"] == contract
Expand Down Expand Up @@ -173,7 +173,7 @@ def perform_contract_event_query(self, query: ContractEventQuery) -> Iterator[Co
"logs": [{"address": address}],
}

for data in archive_ingest(self._archive, network, q):
for data in gateway_ingest(self._gateway, network, q):
for block in data:
block_number = block["header"]["number"]
block_hash = HexBytes(block["header"]["hash"])
Expand All @@ -192,17 +192,17 @@ def all_fields(cls: Type[T]) -> T:
return cast(T, fields)


def ensure_range_is_available(archive: Archive, network: str, query: Query):
height = archive.get_height(network)
def ensure_range_is_available(gateway: SubsquidGateway, network: str, query: Query):
height = gateway.get_height(network)
if query["toBlock"] > height:
range = (query["fromBlock"], query["toBlock"])
raise DataRangeIsNotAvailable(range, height)


def archive_ingest(archive: Archive, network: str, query: Query) -> Iterator[list[Block]]:
ensure_range_is_available(archive, network, query)
def gateway_ingest(gateway: SubsquidGateway, network: str, query: Query) -> Iterator[list[Block]]:
ensure_range_is_available(gateway, network, query)
while True:
data = archive.query(network, query)
data = gateway.query(network, query)
yield data

last_block = data[-1]["header"]["number"]
Expand Down

0 comments on commit fb14959

Please sign in to comment.