Skip to content

Commit

Permalink
implement the rest queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcgroul committed Dec 27, 2023
1 parent fcbc93f commit 30e7b89
Show file tree
Hide file tree
Showing 3 changed files with 453 additions and 53 deletions.
171 changes: 158 additions & 13 deletions ape_subsquid/archive.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import NotRequired, Optional, TypedDict
from typing import Literal, NotRequired, Optional, TypedDict, Union

from requests import Session

TraceType = Union[Literal["create"], Literal["call"], Literal["reward"], Literal["suicide"]]


class BlockFieldSelection(TypedDict, total=False):
number: bool
Expand All @@ -25,21 +27,124 @@ class BlockFieldSelection(TypedDict, total=False):
baseFeePerGas: bool


TxFieldSelection = TypedDict(
"TxFieldSelection",
{
"transactionIndex": bool,
"hash": bool,
"nonce": bool,
"from": bool,
"to": bool,
"input": bool,
"value": bool,
"gas": bool,
"gasPrice": bool,
"maxFeePerGas": bool,
"maxPriorityFeePerGas": bool,
"v": bool,
"r": bool,
"s": bool,
"yParity": bool,
"chainId": bool,
"contractAddress": bool,
"gasUsed": bool,
"cumulativeGasUsed": bool,
"effectiveGasPrice": bool,
"type": bool,
"status": bool,
},
total=False,
)


class LogFieldSelection(TypedDict, total=False):
logIndex: bool
transactionIndex: bool
transactionHash: bool
address: bool
data: bool
topics: bool


class TraceFieldSelection(TypedDict, total=False):
traceAddress: bool
subtraces: bool
transactionIndex: bool
type: bool
error: bool
revertReason: bool
createFrom: bool
createValue: bool
createGas: bool
createInit: bool
createResultGasUsed: bool
createResultCode: bool
createResultAddress: bool
callFrom: bool
callTo: bool
callValue: bool
callGas: bool
callInput: bool
callSighash: bool
callType: bool
callResultGasUsed: bool
callResultOutput: bool
suicideAddress: bool
suicideRefundAddress: bool
suicideBalance: bool
rewardAuthor: bool
rewardValue: bool
rewardType: bool


class FieldSelection(TypedDict, total=False):
block: BlockFieldSelection
# transaction: TxFieldSelection
# log: LogFieldSelection
# trace: TraceFieldSelection
transaction: TxFieldSelection
log: LogFieldSelection
trace: TraceFieldSelection


TxRequest = TypedDict(
"TxRequest",
{
"from": list[str],
"to": list[str],
"sighash": list[str],
"firstNonce": int,
"lastNonce": int,
"logs": bool,
"traces": bool,
"stateDiffs": bool,
},
total=False,
)


class LogRequest(TypedDict, total=False):
address: list[str]
topic0: list[str]
topic1: list[str]
topic2: list[str]
topic3: list[str]
transaction: bool
transactionTraces: bool
transactionLogs: bool


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


class Query(TypedDict):
fromBlock: int
toBlock: NotRequired[int]
includeAllBlocks: NotRequired[bool]
fields: NotRequired[FieldSelection]
transactions: NotRequired[list[dict]]
# logs: NotRequired[list[LogRequest]]
# traces: NotRequired[list[TraceRequest]]
transactions: NotRequired[list[TxRequest]]
logs: NotRequired[list[LogRequest]]
traces: NotRequired[list[TraceRequest]]


class BlockHeader(TypedDict):
Expand All @@ -65,15 +170,53 @@ class BlockHeader(TypedDict):


class Log(TypedDict):
pass


class Transaction(TypedDict):
pass
address: str
transactionIndex: int
transactionHash: str
logIndex: int
data: str
topics: list[str]


Transaction = TypedDict(
"Transaction",
{
"from": str,
"to": Optional[str],
"hash": str,
"gas": str,
"gasPrice": str,
"maxFeePerGas": Optional[str],
"maxPriorityFeePerGas": Optional[str],
"input": str,
"nonce": int,
"transactionIndex": int,
"value": str,
"yParity": Optional[int],
"chainId": Optional[int],
"gasUsed": str,
"cumulativeGasUsed": str,
"effectiveGasPrice": str,
"contractAddress": Optional[str],
"type": int,
"status": int,
"v": str,
"r": str,
"s": str,
},
)


class TraceCreateActionResult(TypedDict):
gasUsed: int
code: str
address: str


class Trace(TypedDict):
pass
type: TraceType
transactionIndex: int
result: NotRequired[TraceCreateActionResult]


class Block(TypedDict):
Expand All @@ -89,9 +232,11 @@ class Archive:
def get_worker(self, start_block: int) -> str:
url = f"https://v2.archive.subsquid.io/network/ethereum-mainnet/{start_block}/worker"
response = self._session.get(url)
response.raise_for_status()
return response.text

def query(self, query: Query) -> list[Block]:
worker_url = self.get_worker(query["fromBlock"])
response = self._session.post(worker_url, json=query)
response.raise_for_status()
return response.json()
Loading

0 comments on commit 30e7b89

Please sign in to comment.