-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto web3 #344
Auto web3 #344
Changes from 3 commits
facd755
52e9151
51d5abb
7726190
49e0d10
12c23d8
26de091
daed896
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from web3.auto import ipc | ||
from web3.auto import http | ||
|
||
w3 = ipc.ipc() | ||
|
||
if not w3: | ||
w3 = http.http() | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from web3 import ( | ||
HTTPProvider, | ||
Web3 | ||
) | ||
|
||
|
||
def http(): | ||
|
||
w3 = Web3(HTTPProvider('http://localhost:8545')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's nice to be able to configure this type of thing using environment variables. This can be done something like the following. w3 = Web3(HTTPProvider(
os.environ.get('WEB3_HTTP_PROVIDER_URI', 'http://localhost:8545')
)) This allows for this functionality to be used beyond just |
||
if w3.isConnected(): | ||
return w3 | ||
else: | ||
return False | ||
|
||
|
||
w3 = http() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking that it may not be good to instantiate this here since it's done in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This supports the API from the issue:
Which would give you an autocreated http connection even if an IPC one exists. I suppose we could revisit whether that's necessary. I don't have a strong use case for it, it just seemed like a good idea in the abstract. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this comment should address the double-instantiation issue: https://github.com/pipermerriam/web3.py/pull/344#discussion_r143795348 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback. I will work on the recommendations. ^.^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My two cents on the issue of whether to use I don't think people will be running three different ethereum clients, maybe me to test this implementation. They will likely be running a single client. I might be wrong on this but it is usually testrpc or parity or geth. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm happy with going that direction: either you're using a simple, single-client setup and can use |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from os import path | ||
|
||
from web3 import ( | ||
IPCProvider, | ||
Web3 | ||
) | ||
|
||
|
||
def ipc(): | ||
|
||
home = path.expanduser('~') | ||
|
||
# The following 2 lines are default paths | ||
parity_path = home + '/.local/share/io.parity.ethereum/jsonrpc.ipc' | ||
geth_path = home + '/.ethereum/geth.ipc' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't look like this will cover the major operating systems, for geth. Take a look at how the IPC module does it: https://github.com/pipermerriam/web3.py/blob/master/web3/providers/ipc.py#L38-L65 In fact, it might be better to just initialize Nice that it looks for the parity path! Can you add in support for parity on other OS's, too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be supportive of just adding the parity paths to the Order of operations I would like to see on that is:
|
||
|
||
w3 = Web3(IPCProvider(parity_path)) | ||
if w3.isConnected(): | ||
return w3 | ||
|
||
w3 = Web3(IPCProvider(geth_path)) | ||
if w3.isConnected(): | ||
return w3 | ||
|
||
return False | ||
|
||
|
||
w3 = ipc() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Maybe remove this instantiation since it's done independently in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, this is opening more connections than it needs to:
You could get around both of those issues by switching to a dynamic import and using the w3 already defined, looking something like:
It could be nice to give these modules a consistent interface, something like
ipc.connect()
andhttp.connect()
. Not critical, though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the
ipc.connect()
andhttp.connect()
, makes it clearer.