Skip to content

Commit

Permalink
fix: add nano-scale timestamp to fantom blocks
Browse files Browse the repository at this point in the history
fixes: #4
  • Loading branch information
fubuloubu committed Mar 19, 2022
1 parent 3eb98c7 commit 40cbd3c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions ape_fantom/ecosystem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from typing import Dict

from ape.api.config import PluginConfig
from ape.api.networks import LOCAL_NETWORK_NAME
from ape.api.providers import BlockAPI
from ape_ethereum.ecosystem import Block as EthereumBlock
from ape_ethereum.ecosystem import Ethereum, NetworkConfig
from pydantic import validator

NETWORKS = {
# chain_id, network_id
Expand All @@ -16,7 +21,26 @@ class FantomConfig(PluginConfig):
default_network: str = LOCAL_NETWORK_NAME


class Block(EthereumBlock):
timestamp_nano: int

@validator("timestamp_nano", pre=True)
def validate_nano_timestamp(cls, value):
if value and not (
isinstance(value, str)
and value.startswith("0x")
and set(value[2:]) < set("0123456789abcdef")
):
raise ValueError(f"Hash `{value}` is not a valid hexstr.")

return int(value, 16)


class Fantom(Ethereum):
@property
def config(self) -> FantomConfig: # type: ignore
return self.config_manager.get_config("fantom") # type: ignore

def decode_block(self, data: Dict) -> BlockAPI:
block = super().decode_block(data)
return Block(timestamp_nano=data.get("timestampNano", 0), **block.dict())

0 comments on commit 40cbd3c

Please sign in to comment.