Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add nano-scale timestamp to fantom blocks #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())