From 2e69955e064343cb40140233a2cdec18080bc9b6 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Wed, 30 Oct 2024 16:48:03 -0500 Subject: [PATCH] perf: lazy load ape-ethereum --- src/ape_ethereum/__init__.py | 112 ++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 40 deletions(-) diff --git a/src/ape_ethereum/__init__.py b/src/ape_ethereum/__init__.py index 552427d10e..849efbcac1 100644 --- a/src/ape_ethereum/__init__.py +++ b/src/ape_ethereum/__init__.py @@ -1,49 +1,32 @@ from ape import plugins -from ape.api.networks import ForkedNetworkAPI, NetworkAPI, create_network_type - -from ._converters import WeiConversions -from .ecosystem import ( - NETWORKS, - BaseEthereumConfig, - Block, - Ethereum, - EthereumConfig, - ForkedNetworkConfig, - NetworkConfig, -) -from .provider import EthereumNodeProvider, Web3Provider, assert_web3_provider_uri_env_var_not_set -from .query import EthereumQueryProvider -from .trace import CallTrace, Trace, TransactionTrace -from .transactions import ( - AccessListTransaction, - BaseTransaction, - DynamicFeeTransaction, - Receipt, - SharedBlobReceipt, - SharedBlobTransaction, - StaticFeeTransaction, - TransactionStatusEnum, - TransactionType, -) @plugins.register(plugins.Config) def config_class(): + from ape_ethereum.ecosystem import EthereumConfig + return EthereumConfig @plugins.register(plugins.ConversionPlugin) def converters(): + from ape_ethereum._converters import WeiConversions + yield int, WeiConversions @plugins.register(plugins.EcosystemPlugin) def ecosystems(): + from ape_ethereum.ecosystem import Ethereum + yield Ethereum @plugins.register(plugins.NetworkPlugin) def networks(): + from ape.api.networks import ForkedNetworkAPI, NetworkAPI, create_network_type + from ape_ethereum.ecosystem import NETWORKS + for network_name, network_params in NETWORKS.items(): yield "ethereum", network_name, create_network_type(*network_params) yield "ethereum", f"{network_name}-fork", ForkedNetworkAPI @@ -54,29 +37,78 @@ def networks(): @plugins.register(plugins.QueryPlugin) def query_engines(): + from .query import EthereumQueryProvider + yield EthereumQueryProvider +def __getattr__(name): + if name in ( + "BaseEthereumConfig", + "Block", + "Ethereum", + "EthereumConfig", + "ForkedNetworkConfig", + "NetworkConfig", + ): + import ape_ethereum.ecosystem as ecosystem_module + + return getattr(ecosystem_module, name) + + elif name in ( + "EthereumNodeProvider", + "Web3Provider", + "assert_web3_provider_uri_env_var_not_set", + ): + import ape_ethereum.provider as provider_module + + return getattr(provider_module, name) + + elif name in ( + "AccessListTransaction", + "BaseTransaction", + "DynamicFeeTransaction", + "Receipt", + "SharedBlobReceipt", + "SharedBlobTransaction", + "StaticFeeTransaction", + "TransactionStatusEnum", + "TransactionType", + ): + import ape_ethereum.transactions as tx_module + + return getattr(tx_module, name) + + elif name in ("CallTrace", "Trace", "TransactionTrace"): + import ape_ethereum.trace as trace_module + + return getattr(trace_module, name) + + else: + raise AttributeError(name) + + __all__ = [ - "Ethereum", - "EthereumConfig", - "NetworkConfig", - "ForkedNetworkConfig", + "AccessListTransaction", + "assert_web3_provider_uri_env_var_not_set", "BaseEthereumConfig", + "BaseTransaction", "Block", - "assert_web3_provider_uri_env_var_not_set", - "Web3Provider", + "CallTrace", + "DynamicFeeTransaction", + "Ethereum", + "EthereumConfig", "EthereumNodeProvider", + "ForkedNetworkConfig", + "NetworkConfig", + "Receipt", + "SharedBlobReceipt", + "SharedBlobTransaction", + "StaticFeeTransaction", "Trace", - "TransactionTrace", - "CallTrace", "TransactionStatusEnum", + "TransactionTrace", "TransactionType", - "BaseTransaction", - "StaticFeeTransaction", - "AccessListTransaction", - "DynamicFeeTransaction", "SharedBlobTransaction", - "Receipt", - "SharedBlobReceipt", + "Web3Provider", ]