File tree Expand file tree Collapse file tree 5 files changed +69
-24
lines changed Expand file tree Collapse file tree 5 files changed +69
-24
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change @@ -21,6 +21,10 @@ def load_provider_from_environment():
21
21
if not uri_string :
22
22
return None
23
23
24
+ return load_provider_from_uri (uri_string )
25
+
26
+
27
+ def load_provider_from_uri (uri_string ):
24
28
uri = urlparse (uri_string )
25
29
if uri .scheme == 'file' :
26
30
return IPCProvider (uri .path )
You can’t perform that action at this time.
0 commit comments