Skip to content

Commit

Permalink
Fix 0x hash prefix missing in LazyTimestampContainer - looks like liv…
Browse files Browse the repository at this point in the history
…e RPC nodes

  where returning JSON-RPC responses differently formatted
  • Loading branch information
miohtama committed Jul 17, 2023
1 parent 39c7e39 commit cb79dae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.21.7

- Add `block_identifier` parameteter to `estimate_buy_received_amount() / estimate_sell_received_amount()`,
so we can ask historical prices and also track the price information per block
- Fix `0x` hash prefix missing in `LazyTimestampContainer` - looks like live RPC nodes
where returning JSON-RPC responses differently formatted

# 0.21.6

- Add `HotWallet.sign_bound_call_with_new_nonce`
Expand Down
14 changes: 14 additions & 0 deletions eth_defi/event_reader/lazy_timestamp_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
See :py:func:`extract_timestamps_json_rpc_lazy`
"""
import logging
from hexbytes import HexBytes

from eth_defi.event_reader.conversion import convert_jsonrpc_value_to_int
Expand All @@ -10,6 +11,9 @@
from web3.types import BlockIdentifier


logger = logging.getLogger(__name__)


class OutOfSpecifiedRangeRead(Exception):
"""We tried to read a block outside out original given range."""

Expand Down Expand Up @@ -44,13 +48,23 @@ def __init__(self, web3: Web3, start_block: int, end_block: int):
self.cache_by_block_number = {}

def update_block_hash(self, block_identifier: BlockIdentifier) -> int:
"""Internal function to get block timestamp from JSON-RPC and store it in the cache."""
# Skip web3.py stack of slow result formatters

# TODO: Later tune down log level when successfully run in the production
logger.info("update_block_hash(%s)", block_identifier)

if type(block_identifier) == int:
assert block_identifier > 0
result = self.web3.manager.request_blocking("eth_getBlockByNumber", (block_identifier, False))
else:
if isinstance(block_identifier, HexBytes):
block_identifier = block_identifier.hex()

# Make sure there is always 0x prefix for hashes
if not block_identifier.startswith("0x"):
block_identifier = "0x" + block_identifier

result = self.web3.manager.request_blocking("eth_getBlockByHash", (block_identifier, False))

# Note to self: block_number = 0 for the genesis block on Anvil
Expand Down

0 comments on commit cb79dae

Please sign in to comment.