From 125c6aedc618a008f07069f09ddc3430002af0df Mon Sep 17 00:00:00 2001 From: Immanuel Ifere Date: Thu, 21 Jan 2021 20:15:08 +0300 Subject: [PATCH] Move eth_getTransactionCount to snake case --- docs/contracts.rst | 2 +- docs/examples.rst | 2 +- docs/overview.rst | 2 +- docs/web3.eth.account.rst | 2 +- docs/web3.eth.rst | 12 +++++++--- newsfragments/1844.feature.rst | 1 + .../contracts/test_contract_constructor.py | 6 ++--- tests/ens/test_setup_address.py | 10 ++++----- tests/ens/test_setup_name.py | 4 ++-- web3/_utils/module_testing/eth_module.py | 22 +++++++++++++------ web3/_utils/transactions.py | 2 +- web3/eth.py | 7 +++++- 12 files changed, 46 insertions(+), 26 deletions(-) create mode 100644 newsfragments/1844.feature.rst diff --git a/docs/contracts.rst b/docs/contracts.rst index b7d61bfe19..d88ff56c21 100644 --- a/docs/contracts.rst +++ b/docs/contracts.rst @@ -879,7 +879,7 @@ Methods .. code-block:: python - >>> math_contract.functions.increment(5).buildTransaction({'nonce': web3.eth.getTransactionCount('0xF5...')}) + >>> math_contract.functions.increment(5).buildTransaction({'nonce': web3.eth.get_transaction_count('0xF5...')}) Returns a transaction dictionary. This transaction dictionary can then be sent using :meth:`~web3.eth.Eth.sendTransaction`. diff --git a/docs/examples.rst b/docs/examples.rst index 73481a6f3d..69ac2f8973 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -648,7 +648,7 @@ Just remember that you have to sign all transactions locally, as infura does not transaction = contract.functions.function_Name(params).buildTransaction() transaction.update({ 'gas' : appropriate_gas_amount }) - transaction.update({ 'nonce' : w3.eth.getTransactionCount('Your_Wallet_Address') }) + transaction.update({ 'nonce' : w3.eth.get_transaction_count('Your_Wallet_Address') }) signed_tx = w3.eth.account.signTransaction(transaction, private_key) P.S : the two updates are done to the transaction dictionary, since a raw transaction might not contain gas & nonce amounts, so you have to add them manually. diff --git a/docs/overview.rst b/docs/overview.rst index 003c293262..72056f9bdf 100644 --- a/docs/overview.rst +++ b/docs/overview.rst @@ -148,7 +148,7 @@ API - :meth:`web3.eth.get_storage_at() ` - :meth:`web3.eth.get_transaction() ` - :meth:`web3.eth.get_transaction_by_block() ` -- :meth:`web3.eth.getTransactionCount() ` +- :meth:`web3.eth.get_transaction_count() ` - :meth:`web3.eth.get_uncle_by_block() ` - :meth:`web3.eth.get_uncle_count() ` diff --git a/docs/web3.eth.account.rst b/docs/web3.eth.account.rst index 001248b1df..2cf6dc15f3 100644 --- a/docs/web3.eth.account.rst +++ b/docs/web3.eth.account.rst @@ -282,7 +282,7 @@ To sign a transaction locally that will invoke a smart contract: >>> unicorns = w3.eth.contract(address="0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359", abi=EIP20_ABI) - >>> nonce = w3.eth.getTransactionCount('0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E') # doctest: +SKIP + >>> nonce = w3.eth.get_transaction_count('0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E') # doctest: +SKIP # Build a transaction that invokes this contract's function, called transfer >>> unicorn_txn = unicorns.functions.transfer( diff --git a/docs/web3.eth.rst b/docs/web3.eth.rst index 7a3bdae213..6302b4891a 100644 --- a/docs/web3.eth.rst +++ b/docs/web3.eth.rst @@ -676,7 +676,7 @@ The following methods are available on the ``web3.eth`` namespace. }) -.. py:method:: Eth.getTransactionCount(account, block_identifier=web3.eth.default_block) +.. py:method:: Eth.get_transaction_count(account, block_identifier=web3.eth.default_block) * Delegates to ``eth_getTransactionCount`` RPC Method @@ -687,10 +687,16 @@ The following methods are available on the ``web3.eth`` namespace. .. code-block:: python - >>> web3.eth.getTransactionCount('0xd3CdA913deB6f67967B99D67aCDFa1712C293601') + >>> web3.eth.get_transaction_count('0xd3CdA913deB6f67967B99D67aCDFa1712C293601') 340 +.. py:method:: Eth.getTransactionCount(account, block_identifier=web3.eth.default_block) + + .. warning:: Deprecated: This method is deprecated in favor of + :meth:`~web3.eth.Eth.get_transaction_count()` + + .. py:method:: Eth.sendTransaction(transaction) * Delegates to ``eth_sendTransaction`` RPC Method @@ -739,7 +745,7 @@ The following methods are available on the ``web3.eth`` namespace. .. code-block:: python >>> signed_txn = w3.eth.signTransaction(dict( - nonce=w3.eth.getTransactionCount(w3.eth.coinbase), + nonce=w3.eth.get_transaction_count(w3.eth.coinbase), gasPrice=w3.eth.gas_price, gas=100000, to='0xd3CdA913deB6f67967B99D67aCDFa1712C293601', diff --git a/newsfragments/1844.feature.rst b/newsfragments/1844.feature.rst new file mode 100644 index 0000000000..7bfdd28e3e --- /dev/null +++ b/newsfragments/1844.feature.rst @@ -0,0 +1 @@ +Added ``get_transaction_count``, and deprecated ``getTransactionCount`` \ No newline at end of file diff --git a/tests/core/contracts/test_contract_constructor.py b/tests/core/contracts/test_contract_constructor.py index ed6270909f..d506ea4f03 100644 --- a/tests/core/contracts/test_contract_constructor.py +++ b/tests/core/contracts/test_contract_constructor.py @@ -185,7 +185,7 @@ def test_contract_constructor_build_transaction_no_constructor( {'from': address_conversion_func(web3.eth.accounts[0])} ) txn = web3.eth.get_transaction(txn_hash) - nonce = web3.eth.getTransactionCount(web3.eth.coinbase) + nonce = web3.eth.get_transaction_count(web3.eth.coinbase) unsent_txn = MathContract.constructor().buildTransaction({'nonce': nonce}) assert txn['data'] == unsent_txn['data'] @@ -203,7 +203,7 @@ def test_contract_constructor_build_transaction_with_constructor_without_argumen {'from': address_conversion_func(web3.eth.accounts[0])} ) txn = web3.eth.get_transaction(txn_hash) - nonce = web3.eth.getTransactionCount(web3.eth.coinbase) + nonce = web3.eth.get_transaction_count(web3.eth.coinbase) unsent_txn = MathContract.constructor().buildTransaction({'nonce': nonce}) assert txn['data'] == unsent_txn['data'] @@ -233,7 +233,7 @@ def test_contract_constructor_build_transaction_with_constructor_with_argument( {'from': address_conversion_func(web3.eth.accounts[0])} ) txn = web3.eth.get_transaction(txn_hash) - nonce = web3.eth.getTransactionCount(web3.eth.coinbase) + nonce = web3.eth.get_transaction_count(web3.eth.coinbase) unsent_txn = WithConstructorArgumentsContract.constructor( *constructor_args, **constructor_kwargs).buildTransaction({'nonce': nonce}) assert txn['data'] == unsent_txn['data'] diff --git a/tests/ens/test_setup_address.py b/tests/ens/test_setup_address.py index 236c25cbf5..fb5df1d309 100644 --- a/tests/ens/test_setup_address.py +++ b/tests/ens/test_setup_address.py @@ -101,7 +101,7 @@ def test_set_address_equivalence(ens, name, equivalent, TEST_ADDRESS): @pytest.mark.parametrize( 'set_address', [ - # since the test uses getTransactionCount, + # since the test uses get_transaction_count, # using a same address converted to bytes and hex will error with same count, # use two different addresses of each type (hex, bytes) "0x000000000000000000000000000000000000dEaD", @@ -115,11 +115,11 @@ def test_set_address_noop(ens, set_address): eth = ens.web3.eth owner = ens.owner('tester.eth') ens.setup_address('noop.tester.eth', set_address) - starting_transactions = eth.getTransactionCount(owner) + starting_transactions = eth.get_transaction_count(owner) # do not issue transaction if address is already set ens.setup_address('noop.tester.eth', set_address) - assert eth.getTransactionCount(owner) == starting_transactions + assert eth.get_transaction_count(owner) == starting_transactions def test_set_address_unauthorized(ens, TEST_ADDRESS): @@ -153,7 +153,7 @@ def test_set_resolver_leave_default(ens, TEST_ADDRESS): owner = ens.owner('tester.eth') ens.setup_address('leave-default-resolver.tester.eth', TEST_ADDRESS) eth = ens.web3.eth - num_transactions = eth.getTransactionCount(owner) + num_transactions = eth.get_transaction_count(owner) ens.setup_address( 'leave-default-resolver.tester.eth', @@ -162,4 +162,4 @@ def test_set_resolver_leave_default(ens, TEST_ADDRESS): # should skip setting the owner and setting the default resolver, and only # set the name in the default resolver to point to the new address - assert eth.getTransactionCount(owner) == num_transactions + 1 + assert eth.get_transaction_count(owner) == num_transactions + 1 diff --git a/tests/ens/test_setup_name.py b/tests/ens/test_setup_name.py index 2d8fbc76fa..a550ebfb64 100644 --- a/tests/ens/test_setup_name.py +++ b/tests/ens/test_setup_name.py @@ -119,14 +119,14 @@ def test_setup_reverse_dict_unmodified(ens): # setup owner = ens.owner('tester.eth') eth = ens.web3.eth - start_count = eth.getTransactionCount(owner) + start_count = eth.get_transaction_count(owner) address = ens.web3.eth.accounts[3] transact = {} ens.setup_name('tester.eth', address, transact=transact) # even though a transaction was issued, the dict argument was not modified - assert eth.getTransactionCount(owner) > start_count + assert eth.get_transaction_count(owner) > start_count assert transact == {} # teardown diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index 21c4316da2..af20402dc9 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -220,25 +220,33 @@ def test_eth_get_storage_at_invalid_address(self, web3: "Web3") -> None: with pytest.raises(InvalidAddress): web3.eth.get_storage_at(ChecksumAddress(HexAddress(HexStr(coinbase.lower()))), 0) - def test_eth_getTransactionCount( + def test_eth_get_transaction_count( self, web3: "Web3", unlocked_account_dual_type: ChecksumAddress ) -> None: - transaction_count = web3.eth.getTransactionCount(unlocked_account_dual_type) + transaction_count = web3.eth.get_transaction_count(unlocked_account_dual_type) assert is_integer(transaction_count) assert transaction_count >= 0 - def test_eth_getTransactionCount_ens_name( + def test_eth_getTransactionCount_deprecated( + self, web3: "Web3", unlocked_account_dual_type: ChecksumAddress + ) -> None: + with pytest.warns(DeprecationWarning): + transaction_count = web3.eth.getTransactionCount(unlocked_account_dual_type) + assert is_integer(transaction_count) + assert transaction_count >= 0 + + def test_eth_get_transaction_count_ens_name( self, web3: "Web3", unlocked_account_dual_type: ChecksumAddress ) -> None: with ens_addresses(web3, {'unlocked-acct-dual-type.eth': unlocked_account_dual_type}): - transaction_count = web3.eth.getTransactionCount('unlocked-acct-dual-type.eth') + transaction_count = web3.eth.get_transaction_count('unlocked-acct-dual-type.eth') assert is_integer(transaction_count) assert transaction_count >= 0 - def test_eth_getTransactionCount_invalid_address(self, web3: "Web3") -> None: + def test_eth_get_transaction_count_invalid_address(self, web3: "Web3") -> None: coinbase = web3.eth.coinbase with pytest.raises(InvalidAddress): - web3.eth.getTransactionCount(ChecksumAddress(HexAddress(HexStr(coinbase.lower())))) + web3.eth.get_transaction_count(ChecksumAddress(HexAddress(HexStr(coinbase.lower())))) def test_eth_getBlockTransactionCountByHash_empty_block( self, web3: "Web3", empty_block: BlockData @@ -597,7 +605,7 @@ def test_eth_sendTransaction_with_nonce( 'gas': Wei(21000), # Increased gas price to ensure transaction hash different from other tests 'gasPrice': Wei(web3.eth.gas_price * 3), - 'nonce': web3.eth.getTransactionCount(unlocked_account), + 'nonce': web3.eth.get_transaction_count(unlocked_account), } txn_hash = web3.eth.sendTransaction(txn_params) txn = web3.eth.get_transaction(txn_hash) diff --git a/web3/_utils/transactions.py b/web3/_utils/transactions.py index 5106f44f60..86b626ad19 100644 --- a/web3/_utils/transactions.py +++ b/web3/_utils/transactions.py @@ -67,7 +67,7 @@ def fill_nonce(web3: "Web3", transaction: TxParams) -> TxParams: return assoc( transaction, 'nonce', - web3.eth.getTransactionCount( + web3.eth.get_transaction_count( cast(ChecksumAddress, transaction['from']), block_identifier='pending')) else: diff --git a/web3/eth.py b/web3/eth.py index 8d267eae24..9988aa54b7 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -432,7 +432,9 @@ def waitForTransactionReceipt( mungers=[default_root_munger] ) - getTransactionCount: Method[Callable[..., Nonce]] = Method( + get_transaction_count: Method[Callable[..., Nonce]] = Method( + + RPC.eth_getTransactionCount, mungers=[block_id_munger], ) @@ -659,5 +661,8 @@ def setGasPriceStrategy(self, gas_price_strategy: GasPriceStrategy) -> None: getTransactionByBlock = DeprecatedMethod(get_transaction_by_block, 'getTransactionByBlock', 'get_transaction_by_block') + getTransactionCount = DeprecatedMethod(get_transaction_count, + 'getTransactionCount', + 'get_transaction_count') getUncleByBlock = DeprecatedMethod(get_uncle_by_block, 'getUncleByBlock', 'get_uncle_by_block') getUncleCount = DeprecatedMethod(get_uncle_count, 'getUncleCount', 'get_uncle_count')