Skip to content

Commit

Permalink
feat: add support for .http_uri and .ws_uri in ProviderAPI v0.6.1…
Browse files Browse the repository at this point in the history
…9 [APE-1381] (#65)

Co-authored-by: Juliya Smith <jules@apeworx.io>
  • Loading branch information
fubuloubu and antazoey authored Oct 5, 2023
1 parent 249c34d commit d7fbbf6
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 39 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v4.4.0
hooks:
- id: check-yaml

Expand All @@ -10,24 +10,24 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.9.1
hooks:
- id: black
name: black

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 6.1.0
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies: [types-PyYAML, types-requests, types-setuptools, pydantic]

- repo: https://github.com/executablebooks/mdformat
rev: 0.7.14
rev: 0.7.17
hooks:
- id: mdformat
additional_dependencies: [mdformat-gfm, mdformat-frontmatter]
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ A pull request represents the start of a discussion, and doesn't necessarily nee
If you are opening a work-in-progress pull request to verify that it passes CI tests, please consider
[marking it as a draft](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests#draft-pull-requests).

Join the Ethereum Python [Discord](https://discord.gg/PcEJ54yX) if you have any questions.
Join the ApeWorX [Discord](https://discord.gg/apeworx) if you have any questions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ To connect to Infura from a Python script, use the `networks` top-level manager:
from ape import networks

with networks.parse_network_choice("ethereum:mainnet:infura") as provider:
...
# Also, access the websocket URI:
print(provider.ws_uri)
```
11 changes: 11 additions & 0 deletions ape_infura/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ def uri(self) -> str:
self.network_uris[(ecosystem_name, network_name)] = network_uri
return network_uri

@property
def http_uri(self) -> str:
# NOTE: Overriding `Web3Provider.http_uri` implementation
return self.uri

@property
def ws_uri(self) -> str:
# NOTE: Overriding `Web3Provider.ws_uri` implementation
# Remove `http` in default URI w/ `ws`, also infura adds `/ws` to URI
return "ws" + self.uri[4:].replace("v3", "ws/v3")

@property
def connection_str(self) -> str:
return self.uri
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=51.1.1", "wheel", "setuptools_scm[toml]>=5.0"]
requires = ["setuptools>=51.1.1", "wheel", "setuptools_scm[toml]>=5.0,<8"]

[tool.mypy]
exclude = "build/"
Expand Down
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
"ape-optimism",
"ape-polygon",
"ape-linea",
"websocket-client", # Used for web socket integration testing
],
"lint": [
"black>=23.3.0,<24", # auto-formatter and linter
"mypy>=0.991,<1", # Static type analyzer
"flake8>=6.0.0,<7", # Style linter
"black>=23.9.1,<24", # auto-formatter and linter
"mypy>=1.5.1,<2", # Static type analyzer
"flake8>=6.1.0,<7", # Style linter
"isort>=5.10.1,<6", # Import sorting linter
"types-setuptools", # Needed due to mypy typeshed
"mdformat>=0.7.16", # Auto-formatter for markdown
"mdformat>=0.7.17", # Auto-formatter for markdown
"mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown
"mdformat-frontmatter>=0.4.1", # Needed for frontmatters-style headers in issue templates
],
Expand Down Expand Up @@ -68,7 +69,7 @@
url="https://github.com/ApeWorX/ape-infura",
include_package_data=True,
install_requires=[
"eth-ape>=0.6.5,<0.7",
"eth-ape>=0.6.19,<0.7",
],
python_requires=">=3.8,<4",
extras_require=extras_require,
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ape
import pytest

from ape_infura import NETWORKS


@pytest.fixture
def accounts():
Expand All @@ -15,3 +17,11 @@ def Contract():
@pytest.fixture
def networks():
return ape.networks


@pytest.fixture(params=[(name, net) for name, values in NETWORKS.items() for net in values])
def provider(networks, request):
ecosystem_cls = networks.get_ecosystem(request.param[0])
network_cls = ecosystem_cls.get_network(request.param[1])
with network_cls.use_provider("infura") as provider:
yield provider
48 changes: 22 additions & 26 deletions tests/test_provider.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import pytest
from ape import networks
import websocket # type: ignore
from ape.utils import ZERO_ADDRESS

from ape_infura.provider import Infura


@pytest.mark.parametrize(
"ecosystem,network",
[
("ethereum", "mainnet"),
("ethereum", "goerli"),
("ethereum", "sepolia"),
("arbitrum", "mainnet"),
("arbitrum", "goerli"),
("optimism", "mainnet"),
("optimism", "goerli"),
("polygon", "mainnet"),
("polygon", "mumbai"),
("linea", "mainnet"),
("linea", "goerli"),
],
)
def test_infura(ecosystem, network):
ecosystem_cls = networks.get_ecosystem(ecosystem)
network_cls = ecosystem_cls.get_network(network)
with network_cls.use_provider("infura") as provider:
assert isinstance(provider, Infura)
assert provider.get_balance(ZERO_ADDRESS) > 0
assert provider.get_block(0)
ecosystem_uri = "" if ecosystem == "ethereum" else f"{ecosystem}-"
assert f"https://{ecosystem_uri}{network}.infura.io/v3/" in provider.uri
def test_infura_http(provider):
ecosystem = provider.network.ecosystem.name
network = provider.network.name
assert isinstance(provider, Infura)
assert provider.http_uri.startswith("https")
assert provider.get_balance(ZERO_ADDRESS) > 0
assert provider.get_block(0)
ecosystem_uri = "" if ecosystem == "ethereum" else f"{ecosystem}-"
assert f"https://{ecosystem_uri}{network}.infura.io/v3/" in provider.uri


def test_infura_ws(provider):
assert provider.ws_uri.startswith("wss")

try:
ws = websocket.WebSocket()
ws.connect(provider.ws_uri)
ws.close()

except Exception as err:
pytest.fail(f"Websocket URI not accessible. Reason: {err}")

0 comments on commit d7fbbf6

Please sign in to comment.