Skip to content

Commit a9705e1

Browse files
committed
Support Ropsten on Infura
1 parent e3e7510 commit a9705e1

File tree

5 files changed

+69
-24
lines changed

5 files changed

+69
-24
lines changed

web3/auto/infura.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

web3/auto/infura/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from web3 import Web3
2+
from web3.providers.auto import (
3+
load_provider_from_uri,
4+
)
5+
6+
from .endpoints import (
7+
INFURA_MAINNET_DOMAIN,
8+
build_infura_url,
9+
)
10+
11+
_infura_url = build_infura_url(INFURA_MAINNET_DOMAIN)
12+
13+
w3 = Web3(load_provider_from_uri(_infura_url))

web3/auto/infura/endpoints.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import logging
2+
import os
3+
4+
from eth_utils import (
5+
ValidationError,
6+
)
7+
8+
INFURA_MAINNET_DOMAIN = 'mainnet.infura.io'
9+
INFURA_ROPSTEN_DOMAIN = 'ropsten.infura.io'
10+
11+
WEBSOCKET_SCHEME = 'wss'
12+
HTTP_SCHEME = 'https'
13+
14+
15+
def load_api_key():
16+
# at web3py v5, drop old variable name INFURA_API_KEY
17+
key = os.environ.get(
18+
'WEB3_INFURA_API_KEY',
19+
os.environ.get('INFURA_API_KEY', '')
20+
)
21+
if key == '':
22+
logging.getLogger('web3.auto.infura').warning(
23+
"No Infura API Key found. Add environment variable WEB3_INFURA_API_KEY to ensure "
24+
"continued API access. New keys are available at https://infura.io/register"
25+
)
26+
return key
27+
28+
29+
def build_infura_url(domain):
30+
scheme = os.environ.get('WEB3_INFURA_SCHEME', WEBSOCKET_SCHEME)
31+
32+
if scheme == WEBSOCKET_SCHEME:
33+
# websockets doesn't use the API key (yet?)
34+
return "%s://%s/ws" % (scheme, domain)
35+
elif scheme == HTTP_SCHEME:
36+
key = load_api_key()
37+
return "%s://%s/%s" % (scheme, domain, key)
38+
else:
39+
raise ValidationError("Cannot connect to Infura with scheme %r" % scheme)

web3/auto/infura/ropsten.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from web3 import Web3
2+
from web3.providers.auto import (
3+
load_provider_from_uri,
4+
)
5+
6+
from .endpoints import (
7+
INFURA_ROPSTEN_DOMAIN,
8+
build_infura_url,
9+
)
10+
11+
_infura_url = build_infura_url(INFURA_ROPSTEN_DOMAIN)
12+
13+
w3 = Web3(load_provider_from_uri(_infura_url))

web3/providers/auto.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def load_provider_from_environment():
2121
if not uri_string:
2222
return None
2323

24+
return load_provider_from_uri(uri_string)
25+
26+
27+
def load_provider_from_uri(uri_string):
2428
uri = urlparse(uri_string)
2529
if uri.scheme == 'file':
2630
return IPCProvider(uri.path)

0 commit comments

Comments
 (0)