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

Accept list or string for filter_param address #1005

Merged
merged 1 commit into from
Aug 20, 2018
Merged
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
35 changes: 35 additions & 0 deletions tests/core/middleware/test_request_param_normalizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from web3 import Web3
from web3.middleware import ( # noqa: F401
construct_result_generator_middleware,
request_parameter_normalizer,
)
from web3.providers.base import (
BaseProvider,
)


@pytest.fixture
def w3_base():
return Web3(providers=[BaseProvider()], middlewares=[])


@pytest.fixture
def result_generator_middleware():
return construct_result_generator_middleware({
'eth_getLogs': lambda _, params: params,
})


@pytest.fixture
def w3(w3_base, result_generator_middleware):
w3_base.middleware_stack.add(result_generator_middleware)
w3_base.middleware_stack.add(request_parameter_normalizer)
return w3_base


def test_eth_getLogs_param_normalization(w3):
result = w3.eth.getLogs({
'from': 'latest', 'address': '0x1111111111111111111111111111111111111111'})
assert isinstance(result[0]['address'], list)
2 changes: 2 additions & 0 deletions web3/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
name_to_address_middleware,
normalize_errors_middleware,
pythonic_middleware,
request_parameter_normalizer,
validation_middleware,
)
from web3.providers import (
Expand Down Expand Up @@ -70,6 +71,7 @@ def default_middlewares(web3):
Leaving ens unspecified will prevent the middleware from resolving names.
'''
return [
(request_parameter_normalizer, 'request_param_normalizer'),
(gas_price_strategy_middleware, 'gas_price_strategy'),
(name_to_address_middleware(web3), 'name_to_address'),
(attrdict_middleware, 'attrdict'),
Expand Down
3 changes: 3 additions & 0 deletions web3/middleware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
from .normalize_errors import ( # noqa: F401
normalize_errors_middleware,
)
from .normalize_request_parameters import ( # noqa: F401
request_parameter_normalizer,
)
from .pythonic import ( # noqa: F401
pythonic_middleware,
)
Expand Down
25 changes: 25 additions & 0 deletions web3/middleware/normalize_request_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from eth_utils import (
is_string,
)

from web3.utils.formatters import (
apply_formatter_at_index,
apply_formatter_if,
apply_formatters_to_dict,
)

from .formatting import (
construct_formatting_middleware,
)

FILTER_PARAM_NORMALIZERS = apply_formatters_to_dict({
'address': apply_formatter_if(is_string, lambda x: [x])})

METHOD_NORMALIZERS = {
'eth_getLogs': apply_formatter_at_index(FILTER_PARAM_NORMALIZERS, 0),
'eth_newFilter': apply_formatter_at_index(FILTER_PARAM_NORMALIZERS, 0)
}

request_parameter_normalizer = construct_formatting_middleware(
request_formatters=METHOD_NORMALIZERS,
)
10 changes: 10 additions & 0 deletions web3/utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,16 @@ def test_eth_getLogs_without_logs(self, web3, block_with_txn_with_log):
result = web3.eth.getLogs(filter_params)
assert len(result) == 0

# Test with multiple `address`

# filter with other address
filter_params = {
"fromBlock": 0,
"address": [UNKNOWN_ADDRESS, UNKNOWN_ADDRESS],
}
result = web3.eth.getLogs(filter_params)
assert len(result) == 0

def test_eth_getLogs_with_logs(
self,
web3,
Expand Down
2 changes: 1 addition & 1 deletion web3/utils/rpc_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

FILTER_PARAMS_ABIS = {
'to': 'address',
'address': 'address',
'address': 'address[]',
}

TRACE_PARAMS_ABIS = {
Expand Down