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

Eth to method 5 #1746

Merged
merged 4 commits into from
Sep 18, 2020
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
1 change: 1 addition & 0 deletions newsfragments/1746.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eth_getTransactionByBlock, eth_getTransactionReceipt, and eth_signTransaction now use Method class
16 changes: 14 additions & 2 deletions web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
RPC.eth_getStorageAt: apply_formatter_at_index(block_number_formatter, 2),
RPC.eth_getTransactionByBlockNumberAndIndex: compose(
apply_formatter_at_index(block_number_formatter, 0),
apply_formatter_at_index(integer_to_hex, 1),
apply_formatter_at_index(to_hex_if_integer, 1),
),
RPC.eth_getTransactionCount: apply_formatter_at_index(block_number_formatter, 1),
RPC.eth_getUncleCountByBlockNumber: apply_formatter_at_index(block_number_formatter, 0),
Expand Down Expand Up @@ -499,8 +499,8 @@ def get_request_formatters(
) -> Dict[str, Callable[..., Any]]:
request_formatter_maps = (
METHOD_NORMALIZERS,
PYTHONIC_REQUEST_FORMATTERS,
ABI_REQUEST_FORMATTERS,
PYTHONIC_REQUEST_FORMATTERS,
)
formatters = combine_formatters(request_formatter_maps, method_name)
return compose(*formatters)
Expand All @@ -526,6 +526,15 @@ def raise_transaction_not_found(params: Tuple[_Hash32]) -> NoReturn:
raise TransactionNotFound(f"Transaction with hash: {transaction_hash} not found.")


def raise_transaction_not_found_with_index(params: Tuple[BlockIdentifier, int]) -> NoReturn:
block_identifier = params[0]
transaction_index = to_integer_if_hex(params[1])
raise TransactionNotFound(
f"Transaction index: {transaction_index} "
f"on block id: {block_identifier} not found."
)


NULL_RESULT_FORMATTERS: Dict[RPCEndpoint, Callable[..., Any]] = {
RPC.eth_getBlockByHash: raise_block_not_found,
RPC.eth_getBlockByNumber: raise_block_not_found,
Expand All @@ -536,6 +545,9 @@ def raise_transaction_not_found(params: Tuple[_Hash32]) -> NoReturn:
RPC.eth_getUncleByBlockHashAndIndex: raise_block_not_found_for_uncle_at_index,
RPC.eth_getUncleByBlockNumberAndIndex: raise_block_not_found_for_uncle_at_index,
RPC.eth_getTransactionByHash: raise_transaction_not_found,
RPC.eth_getTransactionByBlockHashAndIndex: raise_transaction_not_found_with_index,
RPC.eth_getTransactionByBlockNumberAndIndex: raise_transaction_not_found_with_index,
RPC.eth_getTransactionReceipt: raise_transaction_not_found,
}


Expand Down
49 changes: 15 additions & 34 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
)
from web3.exceptions import (
TimeExhausted,
TransactionNotFound,
)
from web3.iban import (
Iban,
Expand Down Expand Up @@ -321,29 +320,14 @@ def getTransactionFromBlock(
"""
raise DeprecationWarning("This method has been deprecated as of EIP 1474.")

def getTransactionByBlock(
self, block_identifier: BlockIdentifier, transaction_index: int
) -> TxData:
"""
`eth_getTransactionByBlockHashAndIndex`
`eth_getTransactionByBlockNumberAndIndex`
"""
method = select_method_for_block_identifier(
block_identifier,
getTransactionByBlock: Method[Callable[[BlockIdentifier, int], TxData]] = Method(
method_choice_depends_on_args=select_method_for_block_identifier(
if_predefined=RPC.eth_getTransactionByBlockNumberAndIndex,
if_hash=RPC.eth_getTransactionByBlockHashAndIndex,
if_number=RPC.eth_getTransactionByBlockNumberAndIndex,
)
result = self.web3.manager.request_blocking(
method,
[block_identifier, transaction_index],
)
if result is None:
raise TransactionNotFound(
f"Transaction index: {transaction_index} "
f"on block id: {block_identifier} not found."
)
return result
),
mungers=[default_root_munger]
)

def waitForTransactionReceipt(
self, transaction_hash: _Hash32, timeout: int=120, poll_latency: float=0.1
Expand All @@ -358,14 +342,10 @@ def waitForTransactionReceipt(
)
)

def getTransactionReceipt(self, transaction_hash: _Hash32) -> TxReceipt:
result = self.web3.manager.request_blocking(
RPC.eth_getTransactionReceipt,
[transaction_hash],
)
if result is None:
raise TransactionNotFound(f"Transaction with hash: {transaction_hash} not found.")
return result
getTransactionReceipt: Method[Callable[[_Hash32], TxReceipt]] = Method(
RPC.eth_getTransactionReceipt,
mungers=[default_root_munger]
)

getTransactionCount: Method[Callable[..., Nonce]] = Method(
RPC.eth_getTransactionCount,
Expand All @@ -387,7 +367,7 @@ def modifyTransaction(
new_transaction = merge(current_transaction_params, transaction_params)
return replace_transaction(self.web3, current_transaction, new_transaction)

def sendTransaction(self, transaction: TxParams) -> HexBytes:
def send_transaction_munger(self, transaction: TxParams) -> Tuple[TxParams]:
# TODO: move to middleware
if 'from' not in transaction and is_checksum_address(self.defaultAccount):
transaction = assoc(transaction, 'from', self.defaultAccount)
Expand All @@ -399,11 +379,12 @@ def sendTransaction(self, transaction: TxParams) -> HexBytes:
'gas',
get_buffered_gas_estimate(self.web3, transaction),
)
return (transaction,)

return self.web3.manager.request_blocking(
RPC.eth_sendTransaction,
[transaction],
)
sendTransaction: Method[Callable[[TxParams], HexBytes]] = Method(
RPC.eth_sendTransaction,
mungers=[send_transaction_munger]
)

sendRawTransaction: Method[Callable[[Union[HexStr, bytes]], HexBytes]] = Method(
RPC.eth_sendRawTransaction,
Expand Down
4 changes: 2 additions & 2 deletions web3/providers/eth_tester/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
fill_default_from,
fill_default_gas,
)
return make_request(method, [filled_transaction] + params[1:])
return make_request(method, [filled_transaction] + list(params)[1:])
elif method in (
'eth_estimateGas',
'eth_sendTransaction',
Expand All @@ -336,7 +336,7 @@ def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
params[0],
fill_default_from,
)
return make_request(method, [filled_transaction] + params[1:])
return make_request(method, [filled_transaction] + list(params)[1:])
else:
return make_request(method, params)
return middleware