Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,28 @@ Then set the environment variable ``WEB3_INFURA_PROJECT_ID`` with your Project I
>>> w3.isConnected()
True

Nodesmith
~~~~~~~~~

To connect to Nodesmith's mainnet or various testnet endpoints, first register
for a free API Key at https://nodesmith.io.

Then set the environment variable ``NODESMITH_API_KEY`` with your API key::

$ export NODESMITH_API_KEY=YourApiKey

In your code, import the appropriate module for the network you want to connect to
with the format ``web3.auto.nodesmith.<network_name>``. For example, to connect to the
`Goerli <https://goerli.net/>`_ test network:

.. code-block:: python

>>> from web3.auto.nodesmith.goerli import w3

# Get the network's id, 5 for Goerli
>>> w3.net.version
'5'

Geth dev Proof of Authority
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 2 additions & 2 deletions docs/web3.eth.account.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Local Node
When you run ``geth`` or ``parity`` on your machine, you are running a local node.

Hosted Node
A hosted node is controlled by someone else. When you connect to Infura, you are
connected to a hosted node.
A hosted node is controlled by someone else. When you connect to Infura or Nodesmith,
you are connected to a hosted node.

Local vs Hosted Keys
---------------------------------
Expand Down
43 changes: 43 additions & 0 deletions tests/core/providers/test_auto_provider.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import importlib
import logging
import os
import pytest

from eth_utils import (
ValidationError,
)

from web3.auto import (
nodesmith,
)
from web3.auto.nodesmith import (
goerli,
kovan,
mainnet,
rinkeby,
ropsten,
)
from web3.exceptions import (
InfuraKeyNotFound,
)
Expand Down Expand Up @@ -122,3 +133,35 @@ def test_web3_auto_infura_raises_error_with_nonexistent_scheme(monkeypatch):
error_msg = "Cannot connect to Infura with scheme 'not-a-scheme'"
with pytest.raises(ValidationError, match=error_msg):
importlib.reload(infura)


def test_web3_auto_nodesmith_missing_key(monkeypatch, caplog):
importlib.reload(nodesmith)
assert len(caplog.record_tuples) == 1
logger, level, msg = caplog.record_tuples[0]
assert 'NODESMITH_API_KEY' in msg
assert level == logging.ERROR


@pytest.mark.parametrize(
'network', [
(goerli, 'goerli'),
(kovan, 'kovan'),
(mainnet, 'mainnet'),
(rinkeby, 'rinkeby'),
(ropsten, 'ropsten')
])
def test_web3_auto_nodesmith_different_networks(monkeypatch, network):

network_module = network[0]
network_name = network[1]
API_KEY = 'ns_python'
monkeypatch.setenv('NODESMITH_API_KEY', API_KEY)
expected_url = 'https://ethereum.api.nodesmith.io/v1/%s/jsonrpc?apiKey=%s' % \
(network_name, API_KEY)

importlib.reload(network_module)

w3 = network_module.w3
assert isinstance(w3.provider, HTTPProvider)
assert getattr(w3.provider, 'endpoint_uri') == expected_url
1 change: 1 addition & 0 deletions web3/auto/infura/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
INFURA_ROPSTEN_DOMAIN = 'ropsten.infura.io'
INFURA_RINKEBY_DOMAIN = 'rinkeby.infura.io'
INFURA_KOVAN_DOMAIN = 'kovan.infura.io'
INFURA_GOERLI_DOMAIN = 'goerli.infura.io'

WEBSOCKET_SCHEME = 'wss'
HTTP_SCHEME = 'https'
Expand Down
13 changes: 13 additions & 0 deletions web3/auto/infura/goerli.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_GOERLI_DOMAIN,
build_infura_url,
)

_infura_url = build_infura_url(INFURA_GOERLI_DOMAIN)

w3 = Web3(load_provider_from_uri(_infura_url))
13 changes: 13 additions & 0 deletions web3/auto/nodesmith/__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 (
build_nodesmith_url,
)

# By default, connect to the mainnet endpoint
_nodesmith_url = build_nodesmith_url("mainnet")

w3 = Web3(load_provider_from_uri(_nodesmith_url))
20 changes: 20 additions & 0 deletions web3/auto/nodesmith/endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging
import os

NODESMITH_URL_FORMAT = 'https://ethereum.api.nodesmith.io/v1/%s/jsonrpc?apiKey=%s'


def load_api_key():
key = os.environ.get('NODESMITH_API_KEY', '')
if key == '':
logging.getLogger('web3.auto.nodesmith').error(
"No Nodesmith API key found. Add environment variable NODESMITH_API_KEY with "
"your key. Get a free API key at https://nodesmith.io"
)
return key


def build_nodesmith_url(network):
key = load_api_key()
url = NODESMITH_URL_FORMAT % (network, key)
return url
12 changes: 12 additions & 0 deletions web3/auto/nodesmith/goerli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from web3 import Web3
from web3.providers.auto import (
load_provider_from_uri,
)

from .endpoints import (
build_nodesmith_url,
)

_nodesmith_url = build_nodesmith_url("goerli")

w3 = Web3(load_provider_from_uri(_nodesmith_url))
12 changes: 12 additions & 0 deletions web3/auto/nodesmith/kovan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from web3 import Web3
from web3.providers.auto import (
load_provider_from_uri,
)

from .endpoints import (
build_nodesmith_url,
)

_nodesmith_url = build_nodesmith_url("kovan")

w3 = Web3(load_provider_from_uri(_nodesmith_url))
12 changes: 12 additions & 0 deletions web3/auto/nodesmith/mainnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from web3 import Web3
from web3.providers.auto import (
load_provider_from_uri,
)

from .endpoints import (
build_nodesmith_url,
)

_nodesmith_url = build_nodesmith_url("mainnet")

w3 = Web3(load_provider_from_uri(_nodesmith_url))
16 changes: 16 additions & 0 deletions web3/auto/nodesmith/rinkeby.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from web3 import Web3
from web3.middleware import (
geth_poa_middleware,
)
from web3.providers.auto import (
load_provider_from_uri,
)

from .endpoints import (
build_nodesmith_url,
)

_nodesmith_url = build_nodesmith_url("rinkeby")

w3 = Web3(load_provider_from_uri(_nodesmith_url))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
12 changes: 12 additions & 0 deletions web3/auto/nodesmith/ropsten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from web3 import Web3
from web3.providers.auto import (
load_provider_from_uri,
)

from .endpoints import (
build_nodesmith_url,
)

_nodesmith_url = build_nodesmith_url("ropsten")

w3 = Web3(load_provider_from_uri(_nodesmith_url))