Skip to content

Commit

Permalink
perf: faster plugin load (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 8, 2024
1 parent 4d5e2d3 commit bca2d8a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 42 deletions.
50 changes: 17 additions & 33 deletions ape_alchemy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
from ape import plugins

from .provider import Alchemy

NETWORKS = {
"ethereum": [
"mainnet",
"sepolia",
],
"arbitrum": [
"mainnet",
"sepolia",
],
"base": [
"mainnet",
"sepolia",
],
"fantom": [
"opera",
"testnet",
],
"optimism": [
"mainnet",
"sepolia",
],
"polygon": [
"mainnet",
"amoy",
],
"polygon-zkevm": [
"mainnet",
"cardona",
],
}


@plugins.register(plugins.ProviderPlugin)
def providers():
from ._utils import NETWORKS
from .provider import Alchemy

for ecosystem_name in NETWORKS:
for network_name in NETWORKS[ecosystem_name]:
yield ecosystem_name, network_name, Alchemy


def __getattr__(name: str):
if name == "NETWORKS":
from ._utils import NETWORKS

return NETWORKS

elif name == "Alchemy":
from .provider import Alchemy

return Alchemy

raise AttributeError(name)
30 changes: 30 additions & 0 deletions ape_alchemy/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
NETWORKS = {
"ethereum": [
"mainnet",
"sepolia",
],
"arbitrum": [
"mainnet",
"sepolia",
],
"base": [
"mainnet",
"sepolia",
],
"fantom": [
"opera",
"testnet",
],
"optimism": [
"mainnet",
"sepolia",
],
"polygon": [
"mainnet",
"amoy",
],
"polygon-zkevm": [
"mainnet",
"cardona",
],
}
13 changes: 8 additions & 5 deletions ape_alchemy/provider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from collections.abc import Iterable
from typing import Any, Optional
from typing import TYPE_CHECKING, Any, Optional

from ape.api import ReceiptAPI, TraceAPI, TransactionAPI, UpstreamProvider
from ape.exceptions import (
Expand All @@ -10,9 +10,7 @@
VirtualMachineError,
)
from ape.logging import logger
from ape.types import BlockID
from ape_ethereum.provider import Web3Provider
from ape_ethereum.transactions import AccessList
from eth_pydantic_types import HexBytes
from eth_typing import HexStr
from requests import HTTPError
Expand All @@ -28,6 +26,11 @@
from .exceptions import AlchemyFeatureNotAvailable, AlchemyProviderError, MissingProjectKeyError
from .trace import AlchemyTransactionTrace

if TYPE_CHECKING:
from ape.types import BlockID
from ape_ethereum.transactions import AccessList


# The user must either set one of these or an ENV VAR of the pattern:
# WEB3_<ECOSYSTEM>_<NETWORK>_PROJECT_ID or WEB3_<ECOSYSTEM>_<NETWORK>_API_KEY
DEFAULT_ENVIRONMENT_VARIABLE_NAMES = ("WEB3_ALCHEMY_PROJECT_ID", "WEB3_ALCHEMY_API_KEY")
Expand Down Expand Up @@ -205,8 +208,8 @@ def get_virtual_machine_error(self, exception: Exception, **kwargs) -> VirtualMa
return VirtualMachineError(message=message, txn=txn)

def create_access_list(
self, transaction: TransactionAPI, block_id: Optional[BlockID] = None
) -> list[AccessList]:
self, transaction: TransactionAPI, block_id: Optional["BlockID"] = None
) -> list["AccessList"]:
if self.network.ecosystem.name == "polygon-zkevm":
# The error is only 400 with no info otherwise.
raise APINotImplementedError()
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[flake8]
max-line-length = 100
ignore = E704,W503,PYD002
ignore = E704,W503,PYD002,TC003,TC006
exclude =
venv*
docs
build
type-checking-pydantic-enabled = True
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
from typing import TYPE_CHECKING

import ape
import pytest
from requests import HTTPError, Response
from web3 import Web3

from ape_alchemy.provider import Alchemy
if TYPE_CHECKING:
from ape_alchemy.provider import Alchemy

FEATURE_NOT_AVAILABLE_BECAUSE_OF_TIER_RESPONSE = (
"trace_transaction is not available on the Free tier - "
Expand Down Expand Up @@ -91,5 +93,5 @@ def feature_not_available_http_error(mocker, request):


@pytest.fixture
def alchemy_provider(networks) -> Alchemy:
def alchemy_provider(networks) -> "Alchemy":
return networks.ethereum.sepolia.get_provider("alchemy")
2 changes: 1 addition & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ape.exceptions import APINotImplementedError
from ape.utils import ZERO_ADDRESS

from ape_alchemy import NETWORKS
from ape_alchemy._utils import NETWORKS
from ape_alchemy.provider import NETWORKS_SUPPORTING_WEBSOCKETS, Alchemy


Expand Down

0 comments on commit bca2d8a

Please sign in to comment.