moonstream/crawlers/mooncrawl/mooncrawl/moonworm_crawler/function_call_crawler.py

101 wiersze
3.2 KiB
Python
Czysty Zwykły widok Historia

2021-12-14 15:39:04 +00:00
import logging
2022-08-09 16:29:19 +00:00
from typing import List
2021-12-14 15:39:04 +00:00
2022-08-10 16:55:49 +00:00
from moonstreamdb.blockchain import AvailableBlockchainType
2024-01-31 10:09:04 +00:00
from moonstreamdb.networks import Network # type: ignore
2021-12-16 19:46:44 +00:00
from moonworm.crawler.function_call_crawler import ( # type: ignore
2021-12-14 15:39:04 +00:00
ContractFunctionCall,
FunctionCallCrawler,
)
2021-12-16 19:46:44 +00:00
from moonworm.crawler.moonstream_ethereum_state_provider import ( # type: ignore
2021-12-14 15:39:04 +00:00
MoonstreamEthereumStateProvider,
)
2022-09-21 15:38:05 +00:00
from moonworm.watch import MockState # type: ignore
2021-12-14 15:39:04 +00:00
from sqlalchemy.orm import Session
from web3 import Web3
2021-12-16 19:43:32 +00:00
from .crawler import FunctionCallCrawlJob, _generate_reporter_callback
2021-12-14 15:39:04 +00:00
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def _crawl_functions(
blockchain_type: AvailableBlockchainType,
ethereum_state_provider: MoonstreamEthereumStateProvider,
jobs: List[FunctionCallCrawlJob],
from_block: int,
to_block: int,
) -> List[ContractFunctionCall]:
shared_state = MockState()
crawled_functions = []
for job in jobs:
function_call_crawler = FunctionCallCrawler(
shared_state,
ethereum_state_provider,
job.contract_abi,
[job.contract_address],
on_decode_error=_generate_reporter_callback(
"function_call", blockchain_type
),
)
function_call_crawler.crawl(
from_block,
to_block,
)
crawled_functions = shared_state.state
return crawled_functions
def function_call_crawler(
db_session: Session,
blockchain_type: AvailableBlockchainType,
web3: Web3,
jobs: List[FunctionCallCrawlJob],
start_block: int,
end_block: int,
batch_size: int,
):
2022-06-22 19:01:06 +00:00
if blockchain_type == AvailableBlockchainType.ETHEREUM:
network = Network.ethereum
elif blockchain_type == AvailableBlockchainType.POLYGON:
network = Network.polygon
2022-08-10 16:55:49 +00:00
elif blockchain_type == AvailableBlockchainType.MUMBAI:
network = Network.mumbai
elif blockchain_type == AvailableBlockchainType.XDAI:
network = Network.xdai
elif blockchain_type == AvailableBlockchainType.WYRM:
network = Network.wyrm
2023-07-12 12:31:53 +00:00
elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA_TESTNET:
network = Network.zksync_era_testnet
2023-08-29 03:41:47 +00:00
elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA:
network = Network.zksync_era
2024-01-31 10:09:04 +00:00
elif blockchain_type == AvailableBlockchainType.ARBITRUM_NOVA:
network = Network.arbitrum_nova
2024-02-20 12:57:29 +00:00
elif blockchain_type == AvailableBlockchainType.ARBITRUM_SEPOLIA:
network = Network.arbitrum_sepolia
2024-02-21 01:27:06 +00:00
elif blockchain_type == AvailableBlockchainType.XAI:
network = Network.xai
2024-03-13 22:09:54 +00:00
elif blockchain_type == AvailableBlockchainType.XAI_TESTNET:
network = Network.xai_testnet
2022-06-22 19:01:06 +00:00
else:
raise ValueError(f"Unknown blockchain type: {blockchain_type}")
2021-12-14 15:39:04 +00:00
ethereum_state_provider = MoonstreamEthereumStateProvider(
web3,
network,
db_session,
)
for i in range(start_block, end_block + 1, batch_size):
logger.info(f"Crawling from block {i} to {i + batch_size - 1}")
crawled_functions = _crawl_functions(
blockchain_type,
ethereum_state_provider,
jobs,
i,
min(i + batch_size - 1, end_block),
)