Skip to content

Commit

Permalink
Merge pull request #1124 from carver/infura-ropsten-v4
Browse files Browse the repository at this point in the history
Support Ropsten on Infura (v4 backport)
  • Loading branch information
carver authored Oct 25, 2018
2 parents e3e7510 + e59d8b8 commit 621583a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 36 deletions.
37 changes: 26 additions & 11 deletions tests/core/providers/test_auto_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,57 @@ def test_load_provider_from_env(monkeypatch, uri, expected_type, expected_attrs)
assert getattr(provider, attr) == val


def test_web3_auto_infura_empty_key(monkeypatch, caplog):
monkeypatch.setenv('INFURA_API_KEY', '')
@pytest.mark.parametrize('environ_name', ['INFURA_API_KEY', 'WEB3_INFURA_API_KEY'])
def test_web3_auto_infura_empty_key(monkeypatch, caplog, environ_name):
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https')
monkeypatch.setenv(environ_name, '')

importlib.reload(infura)
assert len(caplog.record_tuples) == 1
logger, level, msg = caplog.record_tuples[0]
assert 'INFURA_API_KEY' in msg
assert 'WEB3_INFURA_API_KEY' in msg
assert level == logging.WARNING

w3 = infura.w3
assert isinstance(w3.providers[0], HTTPProvider)
assert getattr(w3.providers[0], 'endpoint_uri') == infura.INFURA_MAINNET_BASE_URL
assert getattr(w3.providers[0], 'endpoint_uri') == 'https://mainnet.infura.io/'


def test_web3_auto_infura_missing_key(monkeypatch, caplog):
monkeypatch.delenv('INFURA_API_KEY', raising=False)
@pytest.mark.parametrize('environ_name', ['INFURA_API_KEY', 'WEB3_INFURA_API_KEY'])
def test_web3_auto_infura_deleted_key(monkeypatch, caplog, environ_name):
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https')
monkeypatch.delenv(environ_name, raising=False)

importlib.reload(infura)
assert len(caplog.record_tuples) == 1
logger, level, msg = caplog.record_tuples[0]
assert 'INFURA_API_KEY' in msg
assert 'WEB3_INFURA_API_KEY' in msg
assert level == logging.WARNING

w3 = infura.w3
assert isinstance(w3.providers[0], HTTPProvider)
assert getattr(w3.providers[0], 'endpoint_uri') == infura.INFURA_MAINNET_BASE_URL
assert getattr(w3.providers[0], 'endpoint_uri') == 'https://mainnet.infura.io/'


def test_web3_auto_infura(monkeypatch, caplog):
@pytest.mark.parametrize('environ_name', ['INFURA_API_KEY', 'WEB3_INFURA_API_KEY'])
def test_web3_auto_infura(monkeypatch, caplog, environ_name):
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https')
API_KEY = 'aoeuhtns'
monkeypatch.setenv('INFURA_API_KEY', API_KEY)
expected_url = '%s/%s' % (infura.INFURA_MAINNET_BASE_URL, API_KEY)
monkeypatch.setenv(environ_name, API_KEY)
expected_url = 'https://%s/%s' % (infura.INFURA_MAINNET_DOMAIN, API_KEY)

importlib.reload(infura)
assert len(caplog.record_tuples) == 0

w3 = infura.w3
assert isinstance(w3.providers[0], HTTPProvider)
assert getattr(w3.providers[0], 'endpoint_uri') == expected_url


def test_web3_auto_infura_websocket_default(caplog):
importlib.reload(infura)
assert len(caplog.record_tuples) == 0

w3 = infura.w3
assert isinstance(w3.providers[0], WebsocketProvider)
assert getattr(w3.providers[0], 'endpoint_uri') == 'wss://mainnet.infura.io/ws'
24 changes: 0 additions & 24 deletions web3/auto/infura.py

This file was deleted.

13 changes: 13 additions & 0 deletions web3/auto/infura/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from web3 import Web3
from web3.providers.auto import (
load_provider_from_uri,
)

from .endpoints import (
INFURA_MAINNET_DOMAIN,
build_infura_url,
)

_infura_url = build_infura_url(INFURA_MAINNET_DOMAIN)

w3 = Web3(load_provider_from_uri(_infura_url))
39 changes: 39 additions & 0 deletions web3/auto/infura/endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging
import os

from eth_utils import (
ValidationError,
)

INFURA_MAINNET_DOMAIN = 'mainnet.infura.io'
INFURA_ROPSTEN_DOMAIN = 'ropsten.infura.io'

WEBSOCKET_SCHEME = 'wss'
HTTP_SCHEME = 'https'


def load_api_key():
# at web3py v5, drop old variable name INFURA_API_KEY
key = os.environ.get(
'WEB3_INFURA_API_KEY',
os.environ.get('INFURA_API_KEY', '')
)
if key == '':
logging.getLogger('web3.auto.infura').warning(
"No Infura API Key found. Add environment variable WEB3_INFURA_API_KEY to ensure "
"continued API access. New keys are available at https://infura.io/register"
)
return key


def build_infura_url(domain):
scheme = os.environ.get('WEB3_INFURA_SCHEME', WEBSOCKET_SCHEME)

if scheme == WEBSOCKET_SCHEME:
# websockets doesn't use the API key (yet?)
return "%s://%s/ws" % (scheme, domain)
elif scheme == HTTP_SCHEME:
key = load_api_key()
return "%s://%s/%s" % (scheme, domain, key)
else:
raise ValidationError("Cannot connect to Infura with scheme %r" % scheme)
13 changes: 13 additions & 0 deletions web3/auto/infura/ropsten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from web3 import Web3
from web3.providers.auto import (
load_provider_from_uri,
)

from .endpoints import (
INFURA_ROPSTEN_DOMAIN,
build_infura_url,
)

_infura_url = build_infura_url(INFURA_ROPSTEN_DOMAIN)

w3 = Web3(load_provider_from_uri(_infura_url))
7 changes: 6 additions & 1 deletion web3/providers/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@
)

HTTP_SCHEMES = {'http', 'https'}
WS_SCHEMES = {'ws', 'wss'}


def load_provider_from_environment():
uri_string = os.environ.get('WEB3_PROVIDER_URI', '')
if not uri_string:
return None

return load_provider_from_uri(uri_string)


def load_provider_from_uri(uri_string):
uri = urlparse(uri_string)
if uri.scheme == 'file':
return IPCProvider(uri.path)
elif uri.scheme in HTTP_SCHEMES:
return HTTPProvider(uri_string)
elif uri.scheme == 'ws':
elif uri.scheme in WS_SCHEMES:
return WebsocketProvider(uri_string)
else:
raise NotImplementedError(
Expand Down

0 comments on commit 621583a

Please sign in to comment.