|
73 | 73 | from ape_ethereum.trace import CallTrace, TraceApproach, TransactionTrace |
74 | 74 | from ape_ethereum.transactions import AccessList, AccessListTransaction, TransactionStatusEnum |
75 | 75 |
|
| 76 | +WEB3_PROVIDER_URI_ENV_VAR_NAME = "WEB3_PROVIDER_URI" |
| 77 | + |
76 | 78 | if TYPE_CHECKING: |
77 | 79 | from ethpm_types import EventABI |
78 | 80 |
|
@@ -105,34 +107,6 @@ def _sanitize_web3_url(msg: str) -> str: |
105 | 107 | return f"{prefix} URI: {sanitized_url} {' '.join(rest[1:])}" |
106 | 108 |
|
107 | 109 |
|
108 | | -WEB3_PROVIDER_URI_ENV_VAR_NAME = "WEB3_PROVIDER_URI" |
109 | | - |
110 | | - |
111 | | -def assert_web3_provider_uri_env_var_not_set(): |
112 | | - """ |
113 | | - Environment variable $WEB3_PROVIDER_URI causes problems |
114 | | - when used with Ape (ignores Ape's networks). Use |
115 | | - this validator to eliminate the concern. |
116 | | -
|
117 | | - Raises: |
118 | | - :class:`~ape.exceptions.ProviderError`: If environment variable |
119 | | - WEB3_PROVIDER_URI exists in ``os.environ``. |
120 | | - """ |
121 | | - if WEB3_PROVIDER_URI_ENV_VAR_NAME not in os.environ: |
122 | | - return |
123 | | - |
124 | | - # NOTE: This was the source of confusion for user when they noticed |
125 | | - # Ape would only connect to RPC URL set by an environment variable |
126 | | - # named $WEB3_PROVIDER_URI instead of whatever network they were telling Ape. |
127 | | - raise ProviderError( |
128 | | - "Ape does not support Web3.py's environment variable " |
129 | | - f"${WEB3_PROVIDER_URI_ENV_VAR_NAME}. If you are using this environment " |
130 | | - "variable name incidentally, please use a different name. If you are " |
131 | | - "trying to set the network in Web3.py, please use Ape's `ape-config.yaml` " |
132 | | - "or `--network` option instead." |
133 | | - ) |
134 | | - |
135 | | - |
136 | 110 | def _post_send_transaction(tx: TransactionAPI, receipt: ReceiptAPI): |
137 | 111 | """Execute post-transaction ops""" |
138 | 112 |
|
@@ -171,8 +145,6 @@ class Web3Provider(ProviderAPI, ABC): |
171 | 145 | _transaction_trace_cache: dict[str, TransactionTrace] = {} |
172 | 146 |
|
173 | 147 | def __new__(cls, *args, **kwargs): |
174 | | - assert_web3_provider_uri_env_var_not_set() |
175 | | - |
176 | 148 | # Post-connection ops |
177 | 149 | def post_connect_hook(connect): |
178 | 150 | @wraps(connect) |
@@ -384,7 +356,20 @@ def _default_http_uri(self) -> Optional[str]: |
384 | 356 | # Use a default localhost value. |
385 | 357 | return DEFAULT_HTTP_URI |
386 | 358 |
|
387 | | - elif rpc := self._get_random_rpc(): |
| 359 | + elif env_var := os.getenv(WEB3_PROVIDER_URI_ENV_VAR_NAME): |
| 360 | + # Default Web3 environment variable support that works with web3 out-the-box. |
| 361 | + # Eliminates need for random RPCs and allows usage of this feature. **MUST BE** |
| 362 | + # for the same chain the user is trying to connect to, which requires a bit of a hack. |
| 363 | + # NOTE: We should be able to assume NOT dev here which means chain IDs are hardcoded. |
| 364 | + tmp_w3 = Web3(HTTPProvider(endpoint_uri=env_var)) |
| 365 | + if self.network.chain_id == tmp_w3.eth.chain_id: |
| 366 | + return env_var |
| 367 | + |
| 368 | + # NOTE: We **must** remove the environment variable here or else Ape won't work. |
| 369 | + os.environ.pop(WEB3_PROVIDER_URI_ENV_VAR_NAME, None) |
| 370 | + # Next, drop down and try to use a random RPc from evmchains. |
| 371 | + |
| 372 | + if rpc := self._get_random_rpc(): |
388 | 373 | # This works when the network is in `evmchains`. |
389 | 374 | return rpc |
390 | 375 |
|
|
0 commit comments