-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request param normalization middleware
Along with wrapping address in a list in filter parameter
- Loading branch information
Showing
6 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters