Skip to content

Commit

Permalink
Support Ropsten on Infura
Browse files Browse the repository at this point in the history
  • Loading branch information
carver committed Oct 24, 2018
1 parent e3e7510 commit a9705e1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 24 deletions.
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))
4 changes: 4 additions & 0 deletions web3/providers/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def load_provider_from_environment():
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)
Expand Down

0 comments on commit a9705e1

Please sign in to comment.