From c7cfaaa217f3115f950419ba8a95f311b0f4a737 Mon Sep 17 00:00:00 2001 From: Keri Date: Thu, 28 Jan 2021 14:51:53 -0700 Subject: [PATCH] Add get_transaction_by_block, deprecate getTransactionByBlock --- docs/overview.rst | 2 +- docs/web3.eth.rst | 10 ++++++--- newsfragments/1859.feature.rst | 1 + web3/_utils/module_testing/eth_module.py | 26 ++++++++++++++++++++++-- web3/eth.py | 5 ++++- 5 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 newsfragments/1859.feature.rst diff --git a/docs/overview.rst b/docs/overview.rst index 5f70cd75d1..36113a3d9f 100644 --- a/docs/overview.rst +++ b/docs/overview.rst @@ -147,7 +147,7 @@ API - :meth:`web3.eth.get_proof() ` - :meth:`web3.eth.get_storage_at() ` - :meth:`web3.eth.get_transaction() ` -- :meth:`web3.eth.getTransactionByBlock() ` +- :meth:`web3.eth.get_transaction_by_block() ` - :meth:`web3.eth.getTransactionCount() ` - :meth:`web3.eth.getUncleByBlock() ` - :meth:`web3.eth.getUncleCount() ` diff --git a/docs/web3.eth.rst b/docs/web3.eth.rst index 3dd209ac95..e58c94b2a8 100644 --- a/docs/web3.eth.rst +++ b/docs/web3.eth.rst @@ -557,7 +557,7 @@ The following methods are available on the ``web3.eth`` namespace. .. note:: This method is deprecated in EIP 1474. -.. py:method:: Eth.getTransactionByBlock(block_identifier, transaction_index) +.. py:method:: Eth.get_transaction_by_block(block_identifier, transaction_index) * Delegates to ``eth_getTransactionByBlockNumberAndIndex`` or ``eth_getTransactionByBlockHashAndIndex`` RPC Methods @@ -571,7 +571,7 @@ The following methods are available on the ``web3.eth`` namespace. .. code-block:: python - >>> web3.eth.getTransactionFromBlock(46147, 0) + >>> web3.eth.get_transaction_by_block(46147, 0) AttributeDict({ 'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd', 'blockNumber': 46147, @@ -585,7 +585,7 @@ The following methods are available on the ``web3.eth`` namespace. 'transactionIndex': 0, 'value': 31337, }) - >>> web3.eth.getTransactionFromBlock('0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd', 0) + >>> web3.eth.get_transaction_by_block('0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd', 0) AttributeDict({ 'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd', 'blockNumber': 46147, @@ -600,6 +600,10 @@ The following methods are available on the ``web3.eth`` namespace. 'value': 31337, }) +.. py:method:: Eth.getTransactionByBlock(block_identifier, transaction_index) + + .. warning:: Deprecated: This method is deprecated in favor of + :attr:`~web3.eth.Eth.get_transaction_by_block` .. py:method:: Eth.waitForTransactionReceipt(transaction_hash, timeout=120, poll_latency=0.1) diff --git a/newsfragments/1859.feature.rst b/newsfragments/1859.feature.rst new file mode 100644 index 0000000000..4ee4075585 --- /dev/null +++ b/newsfragments/1859.feature.rst @@ -0,0 +1 @@ +Add ``eth.get_transaction_by_block``, deprecate ``eth.getTransactionByBlock`` diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index 8955c5c22c..52da873ca7 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -1006,14 +1006,36 @@ def test_eth_getTransactionByHash_contract_creation( def test_eth_getTransactionByBlockHashAndIndex( self, web3: "Web3", block_with_txn: BlockData, mined_txn_hash: HexStr ) -> None: - transaction = web3.eth.getTransactionByBlock(block_with_txn['hash'], 0) + transaction = web3.eth.get_transaction_by_block(block_with_txn['hash'], 0) + assert is_dict(transaction) + assert transaction['hash'] == HexBytes(mined_txn_hash) + + def test_eth_getTransactionByBlockHashAndIndex_deprecated( + self, web3: "Web3", block_with_txn: BlockData, mined_txn_hash: HexStr + ) -> None: + with pytest.warns( + DeprecationWarning, + match='getTransactionByBlock is deprecated in favor of get_transaction_by_block' + ): + transaction = web3.eth.getTransactionByBlock(block_with_txn['hash'], 0) assert is_dict(transaction) assert transaction['hash'] == HexBytes(mined_txn_hash) def test_eth_getTransactionByBlockNumberAndIndex( self, web3: "Web3", block_with_txn: BlockData, mined_txn_hash: HexStr ) -> None: - transaction = web3.eth.getTransactionByBlock(block_with_txn['number'], 0) + transaction = web3.eth.get_transaction_by_block(block_with_txn['number'], 0) + assert is_dict(transaction) + assert transaction['hash'] == HexBytes(mined_txn_hash) + + def test_eth_getTransactionByBlockNumberAndIndex_deprecated( + self, web3: "Web3", block_with_txn: BlockData, mined_txn_hash: HexStr + ) -> None: + with pytest.warns( + DeprecationWarning, + match='getTransactionByBlock is deprecated in favor of get_transaction_by_block' + ): + transaction = web3.eth.getTransactionByBlock(block_with_txn['number'], 0) assert is_dict(transaction) assert transaction['hash'] == HexBytes(mined_txn_hash) diff --git a/web3/eth.py b/web3/eth.py index 4f4b394c1a..05f3377ad1 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -405,7 +405,7 @@ def getTransactionFromBlock( """ raise DeprecationWarning("This method has been deprecated as of EIP 1474.") - getTransactionByBlock: Method[Callable[[BlockIdentifier, int], TxData]] = Method( + get_transaction_by_block: 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, @@ -656,3 +656,6 @@ def setGasPriceStrategy(self, gas_price_strategy: GasPriceStrategy) -> None: getCode = DeprecatedMethod(get_code, 'getCode', 'get_code') getProof = DeprecatedMethod(get_proof, 'getProof', 'get_proof') getTransaction = DeprecatedMethod(get_transaction, 'getTransaction', 'get_transaction') + getTransactionByBlock = DeprecatedMethod(get_transaction_by_block, + 'getTransactionByBlock', + 'get_transaction_by_block')