Skip to content

Commit

Permalink
feat: add support for fantom networks (#32)
Browse files Browse the repository at this point in the history
* feat: add support for fantom networks

* refactor: support more than just mainnet forks in config

* fix: don't assume ethereum mainnet for connection to upstream

* docs: add a warning about deprecated key

* refactor: degrade error about genesis block mismatch to warning
  • Loading branch information
fubuloubu authored Mar 19, 2022
1 parent 3a26479 commit 1db175b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
2 changes: 2 additions & 0 deletions ape_hardhat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def config_class():
def providers():
yield "ethereum", LOCAL_NETWORK_NAME, HardhatProvider
yield "ethereum", "mainnet-fork", HardhatMainnetForkProvider
yield "fantom", LOCAL_NETWORK_NAME, HardhatProvider
yield "fantom", "opera-fork", HardhatMainnetForkProvider


__all__ = [
Expand Down
48 changes: 37 additions & 11 deletions ape_hardhat/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shutil
from pathlib import Path
from subprocess import PIPE, call
from typing import Any, List, Optional, Union, cast
from typing import Any, Dict, List, Optional, Union, cast

from ape._compat import Literal
from ape.api import (
Expand Down Expand Up @@ -106,7 +106,11 @@ class HardhatNetworkConfig(PluginConfig):

# For setting the values in --fork and --fork-block-number command arguments.
# Used only in HardhatMainnetForkProvider.
mainnet_fork: Optional[HardhatForkConfig] = None
# Mapping of ecosystem_name => network_name => HardhatForkConfig
fork: Dict[str, Dict[str, HardhatForkConfig]] = {}

class Config:
extra = "allow"


def _call(*args):
Expand Down Expand Up @@ -341,23 +345,43 @@ class HardhatMainnetForkProvider(HardhatProvider):
A Hardhat provider that uses ``--fork``, like:
``npx hardhat node --fork <upstream-provider-url>``.
Set the ``upstream_provider`` in the ``hardhat.mainnet_fork`` config
Set the ``upstream_provider`` in the ``hardhat.fork`` config
section of your ``ape-config.yaml` file to specify which provider
to use as your archive node.
"""

@property
def _upstream_network_name(self) -> str:
return self.network.name.replace("-fork", "")

@property
def _fork_config(self) -> HardhatForkConfig:
config = cast(HardhatNetworkConfig, self.config)
return config.mainnet_fork or HardhatForkConfig()

# NOTE: Only for backwards compatibility
if "mainnet_fork" in config.dict():
logger.warning(
"Use of key `mainnet_fork` in `hardhat` config is deprecated. "
"Please use the `fork` key, with `ecosystem` and `network` subkeys."
)
return HardhatForkConfig.parse_obj(config.dict().get("mainnet_fork"))

ecosystem_name = self.network.ecosystem.name
if ecosystem_name not in config.fork:
return HardhatForkConfig() # Just use default

network_name = self._upstream_network_name
if network_name not in config.fork[ecosystem_name]:
return HardhatForkConfig() # Just use default

return config.fork[ecosystem_name][network_name]

@cached_property
def _upstream_provider(self) -> ProviderAPI:
# NOTE: if 'upstream_provider_name' is 'None', this gets the default mainnet provider.
mainnet = self.network.ecosystem.mainnet
upstream_network = self.network.ecosystem.networks[self._upstream_network_name]
upstream_provider_name = self._fork_config.upstream_provider
upstream_provider = mainnet.get_provider(provider_name=upstream_provider_name)
return upstream_provider
# NOTE: if 'upstream_provider_name' is 'None', this gets the default mainnet provider.
return upstream_network.get_provider(provider_name=upstream_provider_name)

def connect(self):
super().connect()
Expand All @@ -367,10 +391,12 @@ def connect(self):
upstream_genesis_block_hash = self._upstream_provider.get_block(0).hash
self._upstream_provider.disconnect()
if self.get_block(0).hash != upstream_genesis_block_hash:
self.disconnect()
raise HardhatProviderError(
f"Upstream network is not {self.network.name.replace('-fork', '')}"
# self.disconnect()
logger.warning(
"Upstream network has mismatching genesis block. "
"This could be an issue with hardhat."
)
# raise HardhatProviderError(f"Upstream network is not {self._upstream_network_name}")

def build_command(self) -> List[str]:
if not isinstance(self._upstream_provider, UpstreamProvider):
Expand Down

0 comments on commit 1db175b

Please sign in to comment.