-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
69 additions
and
24 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters