Skip to content
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

Web3 on pyodide #3088

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 9 additions & 18 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
#!/usr/bin/env python
from setuptools import (
find_packages,
setup,
)
from setuptools import find_packages, setup

extras_require = {
"tester": [
"eth-tester[py-evm]==v0.9.1-b.1",
"py-geth>=3.11.0",
],
"tester": ["eth-tester[py-evm]==v0.9.1-b.1", "py-geth>=3.11.0"],
"linter": [
"black>=22.1.0",
"flake8==3.8.3",
Expand All @@ -18,11 +12,7 @@
"types-requests>=2.26.1",
"types-protobuf==3.19.13",
],
"docs": [
"sphinx>=5.3.0",
"sphinx_rtd_theme>=1.0.0",
"towncrier>=21,<22",
],
"docs": ["sphinx>=5.3.0", "sphinx_rtd_theme>=1.0.0", "towncrier>=21,<22"],
"dev": [
"bumpversion",
"flaky>=3.7.0",
Expand All @@ -40,9 +30,7 @@
"when-changed>=0.3.0",
"build>=0.9.0",
],
"ipfs": [
"ipfshttpclient==0.8.0a2",
],
"ipfs": ["ipfshttpclient==0.8.0a2"],
}

extras_require["dev"] = (
Expand All @@ -68,7 +56,7 @@
url="https://github.com/ethereum/web3.py",
include_package_data=True,
install_requires=[
"aiohttp>=3.7.4.post0",
"aiohttp>=3.7.4.post0;sys_platform!='emscripten'",
"eth-abi>=4.0.0",
"eth-account>=0.8.0",
"eth-hash[pycryptodome]>=0.5.1",
Expand All @@ -81,8 +69,11 @@
"pywin32>=223;platform_system=='Windows'",
"requests>=2.16.0",
"typing-extensions>=4.0.1",
"websockets>=10.0.0",
"websockets>=10.0.0;sys_platform!='emscripten'",
"pyunormalize>=15.0.0",
# pyodide includes - unversioned to use the one from pyodide distribution
"micropip;sys_platform=='emscripten'",
"pyodide-http;sys_platform=='emscripten'",
],
python_requires=">=3.7.2",
extras_require=extras_require,
Expand Down
86 changes: 70 additions & 16 deletions web3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,76 @@
from eth_account import Account # noqa: E402,
import pkg_resources
import sys

from web3.main import (
AsyncWeb3,
Web3,
)
from web3.providers.async_rpc import ( # noqa: E402
AsyncHTTPProvider,
)
from web3.providers.eth_tester import ( # noqa: E402
EthereumTesterProvider,
)
from web3.providers.ipc import ( # noqa: E402
IPCProvider,
)
from web3.providers.rpc import ( # noqa: E402
HTTPProvider,
)
if sys.platform == "emscripten":
# pyodide has a built in patcher which makes the requests module work
from pyodide_http import patch_all

patch_all()
# asynchronous connections and websockets aren't supported on
# emscripten yet.
# We mock the aiohttp and websockets module so that things import okay
from micropip import add_mock_package

add_mock_package(
"aiohttp",
"1.0.0",
modules={
"aiohttp": """
class __NotImplemented:
def __init__(self,*args,**argv):
raise NotImplementedError(
"Async web3 functions aren't supported on pyodide yet"
)
class ClientSession(__NotImplemented):
pass
class ClientResponse(__NotImplemented):
pass
class ClientTimeout(__NotImplemented):
pass
"""
},
)
# mock websockets
add_mock_package(
"websockets",
"1.0.0",
modules={
"websockets": """
class __NotImplemented:
def __init__(self,*args,**argv):
raise NotImplementedError(
"Async web3 functions aren't supported on pyodide yet"
)
""",
"websockets.legacy": "",
"websockets.legacy.client": """
from websockets import __NotImplemented
class WebSocketClientProtocol(__NotImplemented):
pass
""",
"websockets.client": """
def connect(*args,**argv):
raise NotImplementedError(
"Websockets aren't supported on pyodide yet"
)
""",
"websockets.exceptions": """
from websockets import __NotImplemented
class WebSocketException(__NotImplemented):
pass
class ConnectionClosedOK(__NotImplemented):
pass
""",
},
)


from web3.main import AsyncWeb3, Web3 # noqa: E402
from web3.providers.async_rpc import AsyncHTTPProvider # noqa: E402
from web3.providers.eth_tester import EthereumTesterProvider # noqa: E402
from web3.providers.ipc import IPCProvider # noqa: E402
from web3.providers.rpc import HTTPProvider # noqa: E402
from web3.providers.websocket import ( # noqa: E402
WebsocketProvider,
WebsocketProviderV2,
Expand Down