Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-arbitrum-sepolia
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu authored Nov 3, 2023
2 parents 0336825 + e334d52 commit 7a97ff0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 11 additions & 2 deletions ape_infura/provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Dict, Tuple
from typing import Dict, Optional, Tuple

from ape.api import UpstreamProvider, Web3Provider
from ape.exceptions import ContractLogicError, ProviderError, VirtualMachineError
Expand All @@ -9,6 +9,12 @@
from web3.middleware import geth_poa_middleware

_ENVIRONMENT_VARIABLE_NAMES = ("WEB3_INFURA_PROJECT_ID", "WEB3_INFURA_API_KEY")
# NOTE: https://docs.infura.io/learn/websockets#supported-networks
_WEBSOCKET_CAPABLE_ECOSYSTEMS = {
"ethereum",
"polygon",
"linea",
}


class InfuraProviderError(ProviderError):
Expand Down Expand Up @@ -54,8 +60,11 @@ def http_uri(self) -> str:
return self.uri

@property
def ws_uri(self) -> str:
def ws_uri(self) -> Optional[str]:
# NOTE: Overriding `Web3Provider.ws_uri` implementation
if self.network.ecosystem.name not in _WEBSOCKET_CAPABLE_ECOSYSTEMS:
return None

# Remove `http` in default URI w/ `ws`, also infura adds `/ws` to URI
return "ws" + self.uri[4:].replace("v3", "ws/v3")

Expand Down
7 changes: 6 additions & 1 deletion tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import websocket # type: ignore
from ape.utils import ZERO_ADDRESS

from ape_infura.provider import Infura
from ape_infura.provider import _WEBSOCKET_CAPABLE_ECOSYSTEMS, Infura


def test_infura_http(provider):
Expand All @@ -17,6 +17,11 @@ def test_infura_http(provider):


def test_infura_ws(provider):
ecosystem = provider.network.ecosystem.name
if ecosystem not in _WEBSOCKET_CAPABLE_ECOSYSTEMS:
assert provider.ws_uri is None
return

assert provider.ws_uri.startswith("wss")

try:
Expand Down

0 comments on commit 7a97ff0

Please sign in to comment.