Skip to content

Commit

Permalink
Merge pull request #124 from moonstream-to/add-moonstream-types
Browse files Browse the repository at this point in the history
Add block_hash.
  • Loading branch information
Andrei-Dolgolev authored Jun 17, 2024
2 parents 9f9f772 + 40b96ce commit 0a0054e
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 4 deletions.
8 changes: 7 additions & 1 deletion moonworm/crawler/ethereum_state_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,28 @@ class Web3StateProvider(EthereumStateProvider):

def __init__(self, w3: Web3):
self.w3 = w3
self.metrics = {
"web3_get_block_calls": 0,
"web3_get_transaction_receipt_calls": 0,
}

self.blocks_cache = {}

def get_transaction_reciept(self, transaction_hash: str) -> Dict[str, Any]:
self.metrics["web3_get_transaction_receipt_calls"] += 1
return self.w3.eth.get_transaction_receipt(transaction_hash)

def get_last_block_number(self) -> int:
return self.w3.eth.block_number

def _get_block(self, block_number: int) -> Dict[str, Any]:
self.metrics["web3_get_block_calls"] += 1
if block_number in self.blocks_cache:
return self.blocks_cache[block_number]
block = self.w3.eth.getBlock(block_number, full_transactions=True)

# clear cache if it grows too large
if len(self.blocks_cache) > 50:
if len(self.blocks_cache) > 200:
self.blocks_cache = {}

self.blocks_cache[block_number] = block
Expand Down
2 changes: 2 additions & 0 deletions moonworm/crawler/function_call_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

@dataclass
class ContractFunctionCall:
block_hash: str
block_number: int
block_timestamp: int
transaction_hash: str
Expand Down Expand Up @@ -166,6 +167,7 @@ def process_transaction(self, transaction: Dict[str, Any]):
)

function_call = ContractFunctionCall(
block_hash=transaction["blockHash"],
block_number=transaction["blockNumber"],
block_timestamp=self.ethereum_state_provider.get_block_timestamp(
transaction["blockNumber"]
Expand Down
2 changes: 2 additions & 0 deletions moonworm/crawler/log_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _fetch_events_chunk(
"event": Event name,
"args": dictionary of event arguments,
"address": contract address,
"blockHash": block hash,
"blockNumber": block number,
"transactionHash": transaction hash,
"logIndex": log index
Expand Down Expand Up @@ -115,6 +116,7 @@ def _fetch_events_chunk(
"event": raw_event["event"],
"args": json.loads(Web3.toJSON(utfy_dict(dict(raw_event["args"])))),
"address": raw_event["address"],
"blockHash": raw_event["blockHash"],
"blockNumber": raw_event["blockNumber"],
"transactionHash": raw_event["transactionHash"].hex(),
"logIndex": raw_event["logIndex"],
Expand Down
1 change: 1 addition & 0 deletions moonworm/crawler/moonstream_ethereum_state_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def _transform_to_w3_tx(
tx_raw: tx_raw_types,
) -> Dict[str, Any]:
tx = {
"blockHash": tx_raw.block_hash,
"blockNumber": tx_raw.block_number,
"from": tx_raw.from_address,
"gas": tx_raw.gas,
Expand Down
2 changes: 1 addition & 1 deletion moonworm/crawler/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
EthereumBlock,
EthereumLabel,
)
from moonstreamdb.networks import MODELS, Network, tx_raw_types
from moonstreamtypes.networks import MODELS, Network, tx_raw_types, MODELS_V3

except ImportError:
print("this feature requires moonstreamdb which is not installed")
Expand Down
2 changes: 1 addition & 1 deletion moonworm/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MOONWORM_VERSION = "0.9.0"
MOONWORM_VERSION = "0.9.1"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
extras_require={
"dev": ["isort", "mypy", "wheel", "web3>=5.27.0"],
"moonstream": ["moonstreamdb>=0.3.3"],
"moonstream": ["moonstreamdb>=0.3.3", "moonstream-types>=0.0.3"],
"distribute": ["setuptools", "twine", "wheel"],
},
description="moonworm: Generate a command line interface to any Ethereum smart contract",
Expand Down

0 comments on commit 0a0054e

Please sign in to comment.