Skip to content

Commit e334d52

Browse files
authored
fix: limit to websocket capable ecosystems [APE-1492] (#67)
* fix: limit to websocket capable ecosystems * test: fix websocket test
1 parent d7fbbf6 commit e334d52

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

ape_infura/provider.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import Dict, Tuple
2+
from typing import Dict, Optional, Tuple
33

44
from ape.api import UpstreamProvider, Web3Provider
55
from ape.exceptions import ContractLogicError, ProviderError, VirtualMachineError
@@ -9,6 +9,12 @@
99
from web3.middleware import geth_poa_middleware
1010

1111
_ENVIRONMENT_VARIABLE_NAMES = ("WEB3_INFURA_PROJECT_ID", "WEB3_INFURA_API_KEY")
12+
# NOTE: https://docs.infura.io/learn/websockets#supported-networks
13+
_WEBSOCKET_CAPABLE_ECOSYSTEMS = {
14+
"ethereum",
15+
"polygon",
16+
"linea",
17+
}
1218

1319

1420
class InfuraProviderError(ProviderError):
@@ -54,8 +60,11 @@ def http_uri(self) -> str:
5460
return self.uri
5561

5662
@property
57-
def ws_uri(self) -> str:
63+
def ws_uri(self) -> Optional[str]:
5864
# NOTE: Overriding `Web3Provider.ws_uri` implementation
65+
if self.network.ecosystem.name not in _WEBSOCKET_CAPABLE_ECOSYSTEMS:
66+
return None
67+
5968
# Remove `http` in default URI w/ `ws`, also infura adds `/ws` to URI
6069
return "ws" + self.uri[4:].replace("v3", "ws/v3")
6170

tests/test_provider.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import websocket # type: ignore
33
from ape.utils import ZERO_ADDRESS
44

5-
from ape_infura.provider import Infura
5+
from ape_infura.provider import _WEBSOCKET_CAPABLE_ECOSYSTEMS, Infura
66

77

88
def test_infura_http(provider):
@@ -17,6 +17,11 @@ def test_infura_http(provider):
1717

1818

1919
def test_infura_ws(provider):
20+
ecosystem = provider.network.ecosystem.name
21+
if ecosystem not in _WEBSOCKET_CAPABLE_ECOSYSTEMS:
22+
assert provider.ws_uri is None
23+
return
24+
2025
assert provider.ws_uri.startswith("wss")
2126

2227
try:

0 commit comments

Comments
 (0)