Skip to content

Commit

Permalink
Merge pull request #53 from lidofinance/blockscholes-async-web3
Browse files Browse the repository at this point in the history
Blockscholes async web3
  • Loading branch information
F4ever authored Nov 4, 2024
2 parents 0a672be + 11b66da commit 82dbad2
Show file tree
Hide file tree
Showing 14 changed files with 546 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
uses: JRubics/poetry-publish@v2.0
with:
pypi_token: ${{ secrets.PYPI_TOKEN }}
poetry_publish_options: "--dry-run"
poetry_publish_options: "--dry-run"
7 changes: 0 additions & 7 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,6 @@ max-line-length=100
# Maximum number of lines in a module.
max-module-lines=1000

# List of optional constructs for which whitespace checking is disabled. `dict-
# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}.
# `trailing-comma` allows a space between comma and closing bracket: (a, ).
# `empty-line` allows space-only lines.
no-space-check=trailing-comma,
dict-separator

# Allow the body of a class to be on the same line as the declaration if body
# contains single statement.
single-line-class-stmt=no
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,29 @@ last_block = w3.eth.get_block('latest')

### `MultiProvider`

This provider keeps track of the current endpoint and switches to the next one if an error occurs.
This provider keeps track of the current endpoint and switches to the next one if an error occurs.
It fails if no endpoints are available.

### `FallbackProvider`

This provider sends requests to the all endpoints in the sequence until response received or endpoints list exhausted.

### `AsyncMultiProvider` and `AsyncFallbackProvider`

These providers are async versions of `MultiProvider` and `FallbackProvider` respectively. They may
be used with instances of `AsyncWeb3`.

```py
from web3 import AsyncWeb3
from web3_multi_provider import AsyncMultiProvider
from web3_multi_provider import AsyncFallbackProvider

w3 = AsyncWeb3(AsyncMultiProvider([ # RPC endpoints list
'http://127.0.0.1:8000/',
'https://mainnet.infura.io/v3/...',
]))
```

## For developers

1. `poetry install` - to install deps
Expand Down
22 changes: 20 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ readme = "README.md"
include = [
"LICENSE",
]
packages = [
{include = "web3_multi_provider"},
{include = "web3_multi_provider/py.typed"},
]

[tool.poetry.dependencies]
python = ">=3.12,<4"
web3 = ">=7,<8"
pytest-asyncio = "^0.24.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.3.3"
Expand All @@ -25,3 +30,6 @@ pytest-mock = "^3.14.0"

[tool.isort]
profile = "black"

[tool.pytest.ini_options]
asyncio_default_fixture_loop_scope = "function"
44 changes: 44 additions & 0 deletions tests/mocked_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# pylint: disable=line-too-long
_MOCK_REQUEST_GET_RESULT = b'{"jsonrpc": "2.0", "id": 0, "result": {"baseFeePerGas": "0x1816eeb4af", "difficulty": "0x27aca623255aa9", "extraData": "0x486976656f6e2065752d68656176792d32", "gasLimit": "0x1c9c364", "gasUsed": "0x8454f8", "hash": "0x2420cd3a3f572ba42a881457c88c5c3f58cf44a46e7f25aea53d3a7313922694", "logsBloom": "0x5260400700da108048d93200854128614100802800081404698960db80801ec8464604e3940845a34200c3c2800a4971922881803c01a13902474008842a2518012005042016030f1a80920a703402e209a160240845ce0128a451c282e040c0be0401180a3a0608a355581942010aa06441180242406622780550b4460a02004c11890083047425054a21690dcc044450012c7389089a0a0c20674c419008840b790700804034120a000fc08c2394019087886200038c440c8124b090850d11404120a2818840537b410143518037f000006a0b063a2438148c25020023e606b81825ad000202011506060096a080810459c904a1000062108b191212013223", "miner": "0x1ad91ee08f21be3de0ba2ba6918e714da6b45836", "mixHash": "0x4889052c97da7a2386244f85d8061a0765e1c0f98a212c2eda929dc406713dbe", "nonce": "0xf078269bfcafc704", "number": "0xd1271e", "parentHash": "0x857a1b6ee3a8f2b837f9ae69f5a0b1b181903f5e23df0ed9d166776333e14ec8", "receiptsRoot": "0x1760ed19deceff14ce9cdfebd18ff257c3afc22359edb144d663cd6eb6523aba", "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", "size": "0xa1cc", "stateRoot": "0xa9d6469a301598d5de4f43f121c020da2814875c9154fffb72fb7a6edb8a88cd", "timestamp": "0x61a4733a", "totalDifficulty": "0x78150c58dfe2dc7fcb1", "transactions": [], "transactionsRoot": "0x46fdbd1a3ea670807040dd45f91ebe007c9521395d219150c2d8ac8b77055722", "uncles": []}}'


def mocked_request_get(
endpoint_uri,
data,
*args,
**kwargs,
):
if "http://127.0.0.1:9000" in endpoint_uri:
return _MOCK_REQUEST_GET_RESULT
raise ConnectionError("Mocked connection error.")


async def mocked_async_request_get(
endpoint_uri,
data,
*args,
**kwargs,
):
return mocked_request_get(endpoint_uri, data, *args, **kwargs)


# pylint: disable=line-too-long
_MOCK_REQUEST_POA_RESULT = b'{"jsonrpc": "2.0", "id": 0, "result": {"baseFeePerGas": "0x1816eeb4af", "difficulty": "0x27aca623255aa9", "extraData": "0x00000000000000000000000051396620476f65726c6920417574686f72697479a8766f851c7ae7b3be68b8766225f28c8a0daf86bcdcdc7cb6a2cadec54bd393506e7d2088192110067a6d6280b13a2430d6b44dd2dbbe93d190ddce4309b83500", "gasLimit": "0x1c9c364", "gasUsed": "0x8454f8", "hash": "0x2420cd3a3f572ba42a881457c88c5c3f58cf44a46e7f25aea53d3a7313922694", "logsBloom": "0x5260400700da108048d93200854128614100802800081404698960db80801ec8464604e3940845a34200c3c2800a4971922881803c01a13902474008842a2518012005042016030f1a80920a703402e209a160240845ce0128a451c282e040c0be0401180a3a0608a355581942010aa06441180242406622780550b4460a02004c11890083047425054a21690dcc044450012c7389089a0a0c20674c419008840b790700804034120a000fc08c2394019087886200038c440c8124b090850d11404120a2818840537b410143518037f000006a0b063a2438148c25020023e606b81825ad000202011506060096a080810459c904a1000062108b191212013223", "miner": "0x1ad91ee08f21be3de0ba2ba6918e714da6b45836", "mixHash": "0x4889052c97da7a2386244f85d8061a0765e1c0f98a212c2eda929dc406713dbe", "nonce": "0xf078269bfcafc704", "number": "0xd1271e", "parentHash": "0x857a1b6ee3a8f2b837f9ae69f5a0b1b181903f5e23df0ed9d166776333e14ec8", "receiptsRoot": "0x1760ed19deceff14ce9cdfebd18ff257c3afc22359edb144d663cd6eb6523aba", "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", "size": "0xa1cc", "stateRoot": "0xa9d6469a301598d5de4f43f121c020da2814875c9154fffb72fb7a6edb8a88cd", "timestamp": "0x61a4733a", "totalDifficulty": "0x78150c58dfe2dc7fcb1", "transactions": [], "transactionsRoot": "0x46fdbd1a3ea670807040dd45f91ebe007c9521395d219150c2d8ac8b77055722", "uncles": []}}'


def mocked_request_poa(
endpoint_uri,
data,
*args,
**kwargs,
):
return _MOCK_REQUEST_POA_RESULT


async def mocked_async_request_poa(
endpoint_uri,
data,
*args,
**kwargs,
):
return mocked_request_poa(endpoint_uri, data, *args, **kwargs)
Loading

0 comments on commit 82dbad2

Please sign in to comment.