From 1e186788685652ddce7806cb083487125d9ce041 Mon Sep 17 00:00:00 2001 From: Marc Garreau Date: Tue, 13 Sep 2022 15:25:12 -0600 Subject: [PATCH] snakecase toWei and fromWei --- docs/contracts.rst | 6 +- docs/examples.rst | 14 +- docs/gas_price.rst | 6 +- docs/overview.rst | 4 +- docs/web3.eth.account.rst | 4 +- docs/web3.eth.rst | 6 +- docs/web3.main.rst | 8 +- newsfragments/2647.breaking-change.rst | 1 + .../test_contract_caller_interface.py | 4 +- .../middleware/test_transaction_signing.py | 2 +- .../core/tools/pytest_ethereum/test_linker.py | 6 +- tests/ens/test_get_text.py | 8 +- tests/integration/test_ethereum_tester.py | 4 +- web3/_utils/module_testing/eth_module.py | 128 +++++++++--------- .../go_ethereum_personal_module.py | 2 +- web3/main.py | 4 +- 16 files changed, 104 insertions(+), 103 deletions(-) create mode 100644 newsfragments/2647.breaking-change.rst diff --git a/docs/contracts.rst b/docs/contracts.rst index 1055014093..a794b91bd3 100644 --- a/docs/contracts.rst +++ b/docs/contracts.rst @@ -646,7 +646,7 @@ Taking the following contract code as an example: >>> array_contract.functions.getBytes2Value().call() [b'b\x00'] - >>> array_contract.functions.setBytes2Value([b'a']).transact({'gas': 420000, 'gasPrice': Web3.toWei(1, 'gwei')}) + >>> array_contract.functions.setBytes2Value([b'a']).transact({'gas': 420000, 'gasPrice': Web3.to_wei(1, 'gwei')}) HexBytes('0xc5377ba25224bd763ceedc0ee455cc14fc57b23dbc6b6409f40a557a009ff5f4') >>> array_contract.functions.getBytes2Value().call() [b'a\x00'] @@ -1052,7 +1052,7 @@ Event Log Object alice, bob = w3.eth.accounts[0], w3.eth.accounts[1] assert alice == '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf', alice assert bob == '0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF', bob - tx_hash = my_token_contract.constructor(1000000).transact({'from': alice, 'gas': 899000, 'gasPrice': Web3.toWei(1, 'gwei')}) + tx_hash = my_token_contract.constructor(1000000).transact({'from': alice, 'gas': 899000, 'gasPrice': Web3.to_wei(1, 'gwei')}) assert tx_hash == HexBytes('0x49e3da72a95e4074a9eaea7b438c73ca154627d317e58abeae914e3769a15044'), tx_hash txn_receipt = w3.eth.wait_for_transaction_receipt(tx_hash) assert txn_receipt['contractAddress'] == '0xF2E246BB76DF876Cef8b38ae84130F4F55De395b', txn_receipt['contractAddress'] @@ -1061,7 +1061,7 @@ Event Log Object total_supply = contract.functions.totalSupply().call() decimals = 10 ** 18 assert total_supply == 1000000 * decimals, total_supply - tx_hash = contract.functions.transfer(alice, 10).transact({'gas': 899000, 'gasPrice': Web3.toWei(1, 'gwei')}) + tx_hash = contract.functions.transfer(alice, 10).transact({'gas': 899000, 'gasPrice': Web3.to_wei(1, 'gwei')}) tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash) .. doctest:: createFilter diff --git a/docs/examples.rst b/docs/examples.rst index 568dd2ff85..706d5f1c18 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -142,15 +142,15 @@ Web3 can help you convert between denominations. The following denominations ar +--------------+---------------------------------+ Picking up from the previous example, the largest account contained -3841357360894980500000001 wei. You can use the :meth:`~web3.fromWei` method +3841357360894980500000001 wei. You can use the :meth:`~web3.from_wei` method to convert that balance to ether (or another denomination). .. code-block:: python - >>> web3.fromWei(3841357360894980500000001, 'ether') + >>> web3.from_wei(3841357360894980500000001, 'ether') Decimal('3841357.360894980500000001') -To convert back to wei, you can use the inverse function, :meth:`~web3.toWei`. +To convert back to wei, you can use the inverse function, :meth:`~web3.to_wei`. Note that Python's default floating point precision is insufficient for this use case, so it's necessary to cast the value to a `Decimal `_ if it isn't already. @@ -158,7 +158,7 @@ use case, so it's necessary to cast the value to a .. code-block:: python >>> from decimal import Decimal - >>> web3.toWei(Decimal('3841357.360894980500000001'), 'ether') + >>> web3.to_wei(Decimal('3841357.360894980500000001'), 'ether') 3841357360894980500000001 Best practice: If you need to work with multiple currency denominations, default @@ -167,9 +167,9 @@ wei, then from wei to whatever you need. .. code-block:: python - >>> web3.toWei(Decimal('0.000000005'), 'ether') + >>> web3.to_wei(Decimal('0.000000005'), 'ether') 5000000000 - >>> web3.fromWei(5000000000, 'gwei') + >>> web3.from_wei(5000000000, 'gwei') Decimal('5') @@ -493,7 +493,7 @@ contract which conforms to this standard. alice, bob = w3.eth.accounts[0], w3.eth.accounts[1] assert alice == '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf', alice assert bob == '0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF', bob - tx_hash = factory.constructor(1000000).transact({'from': alice, 'gas': 899000, 'gasPrice': Web3.toWei(1, 'gwei')}) + tx_hash = factory.constructor(1000000).transact({'from': alice, 'gas': 899000, 'gasPrice': Web3.to_wei(1, 'gwei')}) assert tx_hash == HexBytes('0x49e3da72a95e4074a9eaea7b438c73ca154627d317e58abeae914e3769a15044'), tx_hash txn_receipt = w3.eth.wait_for_transaction_receipt(tx_hash) assert txn_receipt['contractAddress'] == '0xF2E246BB76DF876Cef8b38ae84130F4F55De395b', txn_receipt['contractAddress'] diff --git a/docs/gas_price.rst b/docs/gas_price.rst index 98df85112a..31804c9e93 100644 --- a/docs/gas_price.rst +++ b/docs/gas_price.rst @@ -48,10 +48,10 @@ returns a higher gas price when the value of the transaction is higher than from web3 import Web3 def value_based_gas_price_strategy(web3, transaction_params): - if transaction_params['value'] > Web3.toWei(1, 'ether'): - return Web3.toWei(20, 'gwei') + if transaction_params['value'] > Web3.to_wei(1, 'ether'): + return Web3.to_wei(20, 'gwei') else: - return Web3.toWei(5, 'gwei') + return Web3.to_wei(5, 'gwei') Selecting the gas price strategy -------------------------------- diff --git a/docs/overview.rst b/docs/overview.rst index 5fe3a8f321..a93bdd74f6 100644 --- a/docs/overview.rst +++ b/docs/overview.rst @@ -109,8 +109,8 @@ Address Helpers Currency Conversions -------------------- -- :meth:`Web3.fromWei() ` -- :meth:`Web3.toWei() ` +- :meth:`Web3.from_wei() ` +- :meth:`Web3.to_wei() ` Cryptographic Hashing diff --git a/docs/web3.eth.account.rst b/docs/web3.eth.account.rst index 97838117f0..033a42ce88 100644 --- a/docs/web3.eth.account.rst +++ b/docs/web3.eth.account.rst @@ -347,8 +347,8 @@ To sign a transaction locally that will invoke a smart contract: ... ).build_transaction({ ... 'chainId': 1, ... 'gas': 70000, - ... 'maxFeePerGas': w3.toWei('2', 'gwei'), - ... 'maxPriorityFeePerGas': w3.toWei('1', 'gwei'), + ... 'maxFeePerGas': w3.to_wei('2', 'gwei'), + ... 'maxPriorityFeePerGas': w3.to_wei('1', 'gwei'), ... 'nonce': nonce, ... }) diff --git a/docs/web3.eth.rst b/docs/web3.eth.rst index 92fd595ab1..9950b11e26 100644 --- a/docs/web3.eth.rst +++ b/docs/web3.eth.rst @@ -850,8 +850,8 @@ The following methods are available on the ``web3.eth`` namespace. 'from': web3.eth.coinbase, 'value': 12345, 'gas': 21000, - 'maxFeePerGas': web3.toWei(250, 'gwei'), - 'maxPriorityFeePerGas': web3.toWei(2, 'gwei'), + 'maxFeePerGas': web3.to_wei(250, 'gwei'), + 'maxPriorityFeePerGas': web3.to_wei(2, 'gwei'), }) HexBytes('0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331') @@ -862,7 +862,7 @@ The following methods are available on the ``web3.eth`` namespace. 'from': web3.eth.coinbase, 'value': 12345, 'gas': 21000, - 'gasPrice': web3.toWei(50, 'gwei'), + 'gasPrice': web3.to_wei(50, 'gwei'), }) HexBytes('0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331') diff --git a/docs/web3.main.rst b/docs/web3.main.rst index dc0022aa64..48a0951d9b 100644 --- a/docs/web3.main.rst +++ b/docs/web3.main.rst @@ -179,7 +179,7 @@ Encoding and Decoding Helpers Currency Conversions ~~~~~~~~~~~~~~~~~~~~~ -.. py:method:: Web3.toWei(value, currency) +.. py:method:: Web3.to_wei(value, currency) Returns the value in the denomination specified by the ``currency`` argument converted to wei. @@ -187,11 +187,11 @@ Currency Conversions .. code-block:: python - >>> Web3.toWei(1, 'ether') + >>> Web3.to_wei(1, 'ether') 1000000000000000000 -.. py:method:: Web3.fromWei(value, currency) +.. py:method:: Web3.from_wei(value, currency) Returns the value in wei converted to the given currency. The value is returned as a ``Decimal`` to ensure precision down to the wei. @@ -199,7 +199,7 @@ Currency Conversions .. code-block:: python - >>> Web3.fromWei(1000000000000000000, 'ether') + >>> Web3.from_wei(1000000000000000000, 'ether') Decimal('1') diff --git a/newsfragments/2647.breaking-change.rst b/newsfragments/2647.breaking-change.rst new file mode 100644 index 0000000000..2dbde2e400 --- /dev/null +++ b/newsfragments/2647.breaking-change.rst @@ -0,0 +1 @@ +Snakecase the ``toWei`` and ``fromWei`` methods diff --git a/tests/core/contracts/test_contract_caller_interface.py b/tests/core/contracts/test_contract_caller_interface.py index a38409a3bb..46c05b6e21 100644 --- a/tests/core/contracts/test_contract_caller_interface.py +++ b/tests/core/contracts/test_contract_caller_interface.py @@ -37,8 +37,8 @@ def transaction_dict(w3, address): return { "from": address, "gas": 210000, - "maxFeePerGas": w3.toWei(1, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(1, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), "value": 12345, } diff --git a/tests/core/middleware/test_transaction_signing.py b/tests/core/middleware/test_transaction_signing.py index e7b4e1e9ca..110aacb3d2 100644 --- a/tests/core/middleware/test_transaction_signing.py +++ b/tests/core/middleware/test_transaction_signing.py @@ -282,7 +282,7 @@ def test_gen_normalized_accounts_type_error(w3): @pytest.fixture() def fund_account(w3): # fund local account - tx_value = w3.toWei(10, "ether") + tx_value = w3.to_wei(10, "ether") for address in (ADDRESS_1, ADDRESS_2): w3.eth.send_transaction( {"to": address, "from": w3.eth.accounts[0], "gas": 21000, "value": tx_value} diff --git a/tests/core/tools/pytest_ethereum/test_linker.py b/tests/core/tools/pytest_ethereum/test_linker.py index 97514bc082..0f6ae679a4 100644 --- a/tests/core/tools/pytest_ethereum/test_linker.py +++ b/tests/core/tools/pytest_ethereum/test_linker.py @@ -70,13 +70,13 @@ def callback_fn(package): deploy( "Escrow", recipient, - transaction={"from": sender, "value": w3.toWei("1", "ether")}, + transaction={"from": sender, "value": w3.to_wei("1", "ether")}, ), run_python(callback_fn), ) escrow_deployer.register_strategy("Escrow", escrow_strategy) - assert w3.eth.get_balance(recipient) == w3.toWei("1000000", "ether") + assert w3.eth.get_balance(recipient) == w3.to_wei("1000000", "ether") linked_escrow_package = escrow_deployer.deploy("Escrow") escrow_instance = linked_escrow_package.deployments.get_instance("Escrow") assert escrow_instance.functions.sender().call() == sender - assert w3.eth.get_balance(recipient) == w3.toWei("1000001", "ether") + assert w3.eth.get_balance(recipient) == w3.to_wei("1000001", "ether") diff --git a/tests/ens/test_get_text.py b/tests/ens/test_get_text.py index d498fba8a2..3b7b6b1403 100644 --- a/tests/ens/test_get_text.py +++ b/tests/ens/test_get_text.py @@ -47,8 +47,8 @@ def test_set_text_pass_in_transaction_dict(ens): "avatar", "example.jpeg", transact={ - "maxFeePerGas": Web3.toWei(100, "gwei"), - "maxPriorityFeePerGas": Web3.toWei(100, "gwei"), + "maxFeePerGas": Web3.to_wei(100, "gwei"), + "maxPriorityFeePerGas": Web3.to_wei(100, "gwei"), }, ) assert ens.get_text("tester.eth", "url") == "http://example.com" @@ -120,8 +120,8 @@ async def async_test_set_text_pass_in_transaction_dict(async_ens): "avatar", "example.jpeg", transact={ - "maxFeePerGas": Web3.toWei(100, "gwei"), - "maxPriorityFeePerGas": Web3.toWei(100, "gwei"), + "maxFeePerGas": Web3.to_wei(100, "gwei"), + "maxPriorityFeePerGas": Web3.to_wei(100, "gwei"), }, ) assert await async_ens.get_text("tester.eth", "url") == "http://example.com" diff --git a/tests/integration/test_ethereum_tester.py b/tests/integration/test_ethereum_tester.py index cead9c4e55..1fa79b7bc0 100644 --- a/tests/integration/test_ethereum_tester.py +++ b/tests/integration/test_ethereum_tester.py @@ -209,7 +209,7 @@ def unlockable_account(w3, unlockable_account_pw): { "from": w3.eth.coinbase, "to": account, - "value": w3.toWei(10, "ether"), + "value": w3.to_wei(10, "ether"), "gas": 21000, } ) @@ -242,7 +242,7 @@ def funded_account_for_raw_txn(w3): { "from": w3.eth.coinbase, "to": account, - "value": w3.toWei(10, "ether"), + "value": w3.to_wei(10, "ether"), "gas": 21000, "gas_price": 1, } diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index e27974fe90..7afffba77f 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -143,8 +143,8 @@ async def test_eth_send_transaction( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "maxFeePerGas": async_w3.toWei(3, "gwei"), - "maxPriorityFeePerGas": async_w3.toWei(1, "gwei"), + "maxFeePerGas": async_w3.to_wei(3, "gwei"), + "maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"), } txn_hash = await async_w3.eth.send_transaction(txn_params) # type: ignore txn = await async_w3.eth.get_transaction(txn_hash) # type: ignore @@ -255,7 +255,7 @@ async def test_eth_send_transaction_no_priority_fee( async def test_eth_send_transaction_no_max_fee( self, async_w3: "Web3", unlocked_account_dual_type: ChecksumAddress ) -> None: - maxPriorityFeePerGas = async_w3.toWei(2, "gwei") + maxPriorityFeePerGas = async_w3.to_wei(2, "gwei") txn_params: TxParams = { "from": unlocked_account_dual_type, "to": unlocked_account_dual_type, @@ -303,8 +303,8 @@ async def test_validation_middleware_chain_id_mismatch( "to": unlocked_account_dual_type, "value": Wei(1), "gas": Wei(21000), - "maxFeePerGas": async_w3.toWei(2, "gwei"), - "maxPriorityFeePerGas": async_w3.toWei(1, "gwei"), + "maxFeePerGas": async_w3.to_wei(2, "gwei"), + "maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"), "chainId": wrong_chain_id, } with pytest.raises( @@ -364,7 +364,7 @@ async def test_gas_price_strategy_middleware( "value": Wei(1), "gas": 21000, } - two_gwei_in_wei = async_w3.toWei(2, "gwei") + two_gwei_in_wei = async_w3.to_wei(2, "gwei") def gas_price_strategy(w3: "Web3", txn: TxParams) -> Wei: return two_gwei_in_wei @@ -387,7 +387,7 @@ async def test_gas_price_from_strategy_bypassed_for_dynamic_fee_txn( unlocked_account_dual_type: ChecksumAddress, max_fee: Wei, ) -> None: - max_priority_fee = async_w3.toWei(1, "gwei") + max_priority_fee = async_w3.to_wei(1, "gwei") txn_params: TxParams = { "from": unlocked_account_dual_type, "to": unlocked_account_dual_type, @@ -399,7 +399,7 @@ async def test_gas_price_from_strategy_bypassed_for_dynamic_fee_txn( txn_params = assoc(txn_params, "maxFeePerGas", max_fee) def gas_price_strategy(w3: "Web3", txn: TxParams) -> Wei: - return async_w3.toWei(2, "gwei") + return async_w3.to_wei(2, "gwei") async_w3.eth.set_gas_price_strategy(gas_price_strategy) @@ -432,7 +432,7 @@ async def test_gas_price_from_strategy_bypassed_for_dynamic_fee_txn_no_tip( } def gas_price_strategy(_w3: "Web3", _txn: TxParams) -> Wei: - return async_w3.toWei(2, "gwei") + return async_w3.to_wei(2, "gwei") async_w3.eth.set_gas_price_strategy(gas_price_strategy) @@ -1073,8 +1073,8 @@ async def test_async_eth_get_transaction_receipt_unmined( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "maxFeePerGas": async_w3.toWei(3, "gwei"), - "maxPriorityFeePerGas": async_w3.toWei(1, "gwei"), + "maxFeePerGas": async_w3.to_wei(3, "gwei"), + "maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"), } ) with pytest.raises(TransactionNotFound): @@ -1137,8 +1137,8 @@ async def test_async_eth_wait_for_transaction_receipt_unmined( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "maxFeePerGas": async_w3.toWei(3, "gwei"), - "maxPriorityFeePerGas": async_w3.toWei(1, "gwei"), + "maxFeePerGas": async_w3.to_wei(3, "gwei"), + "maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"), } ) @@ -1854,8 +1854,8 @@ def test_eth_sign_transaction( "to": unlocked_account, "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(2, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(2, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), "nonce": Nonce(0), } result = w3.eth.sign_transaction(txn_params) @@ -1878,8 +1878,8 @@ def test_eth_sign_transaction_hex_fees( "to": unlocked_account, "value": Wei(1), "gas": 21000, - "maxFeePerGas": hex(w3.toWei(2, "gwei")), - "maxPriorityFeePerGas": hex(w3.toWei(1, "gwei")), + "maxFeePerGas": hex(w3.to_wei(2, "gwei")), + "maxPriorityFeePerGas": hex(w3.to_wei(1, "gwei")), "nonce": Nonce(0), } result = w3.eth.sign_transaction(txn_params) @@ -1903,8 +1903,8 @@ def test_eth_sign_transaction_ens_names( "to": "unlocked-account.eth", "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(2, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(2, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), "nonce": Nonce(0), } result = w3.eth.sign_transaction(txn_params) @@ -1929,8 +1929,8 @@ def test_eth_send_transaction_addr_checksum_required( "to": unlocked_account, "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(2, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(2, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), } with pytest.raises(InvalidAddress): @@ -1953,7 +1953,7 @@ def test_eth_send_transaction_legacy( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "gasPrice": w3.toWei( + "gasPrice": w3.to_wei( 1, "gwei" ), # post-london needs to be more than the base fee } @@ -1974,8 +1974,8 @@ def test_eth_send_transaction( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(3, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(3, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), } txn_hash = w3.eth.send_transaction(txn_params) txn = w3.eth.get_transaction(txn_hash) @@ -1999,8 +1999,8 @@ def test_eth_send_transaction_with_nonce( "value": Wei(1), "gas": 21000, # unique maxFeePerGas to ensure transaction hash different from other tests - "maxFeePerGas": w3.toWei(4.321, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(4.321, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), "nonce": w3.eth.get_transaction_count(unlocked_account), } txn_hash = w3.eth.send_transaction(txn_params) @@ -2107,7 +2107,7 @@ def test_eth_send_transaction_no_priority_fee( def test_eth_send_transaction_no_max_fee( self, w3: "Web3", unlocked_account_dual_type: ChecksumAddress ) -> None: - maxPriorityFeePerGas = w3.toWei(2, "gwei") + maxPriorityFeePerGas = w3.to_wei(2, "gwei") txn_params: TxParams = { "from": unlocked_account_dual_type, "to": unlocked_account_dual_type, @@ -2153,8 +2153,8 @@ def test_validation_middleware_chain_id_mismatch( "to": unlocked_account_dual_type, "value": Wei(1), "gas": Wei(21000), - "maxFeePerGas": w3.toWei(2, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(2, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), "chainId": wrong_chain_id, } with pytest.raises( @@ -2170,7 +2170,7 @@ def test_validation_middleware_chain_id_mismatch( def test_gas_price_from_strategy_bypassed_for_dynamic_fee_txn( self, w3: "Web3", unlocked_account_dual_type: ChecksumAddress, max_fee: Wei ) -> None: - max_priority_fee = w3.toWei(1, "gwei") + max_priority_fee = w3.to_wei(1, "gwei") txn_params: TxParams = { "from": unlocked_account_dual_type, "to": unlocked_account_dual_type, @@ -2182,7 +2182,7 @@ def test_gas_price_from_strategy_bypassed_for_dynamic_fee_txn( txn_params = assoc(txn_params, "maxFeePerGas", max_fee) def gas_price_strategy(_w3: "Web3", _txn: TxParams) -> Wei: - return w3.toWei(2, "gwei") + return w3.to_wei(2, "gwei") w3.eth.set_gas_price_strategy(gas_price_strategy) @@ -2214,7 +2214,7 @@ def test_gas_price_from_strategy_bypassed_for_dynamic_fee_txn_no_tip( } def gas_price_strategy(_w3: "Web3", _txn: TxParams) -> Wei: - return w3.toWei(2, "gwei") + return w3.to_wei(2, "gwei") w3.eth.set_gas_price_strategy(gas_price_strategy) @@ -2233,13 +2233,13 @@ def test_eth_replace_transaction_legacy( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "gasPrice": w3.toWei( + "gasPrice": w3.to_wei( 1, "gwei" ), # must be greater than base_fee post London } txn_hash = w3.eth.send_transaction(txn_params) - txn_params["gasPrice"] = w3.toWei(2, "gwei") + txn_params["gasPrice"] = w3.to_wei(2, "gwei") replace_txn_hash = w3.eth.replace_transaction(txn_hash, txn_params) replace_txn = w3.eth.get_transaction(replace_txn_hash) @@ -2256,8 +2256,8 @@ def test_eth_replace_transaction_legacy( def test_eth_replace_transaction( self, w3: "Web3", unlocked_account_dual_type: ChecksumAddress ) -> None: - two_gwei_in_wei = w3.toWei(2, "gwei") - three_gwei_in_wei = w3.toWei(3, "gwei") + two_gwei_in_wei = w3.to_wei(2, "gwei") + three_gwei_in_wei = w3.to_wei(3, "gwei") txn_params: TxParams = { "from": unlocked_account_dual_type, @@ -2265,7 +2265,7 @@ def test_eth_replace_transaction( "value": Wei(1), "gas": 21000, "maxFeePerGas": two_gwei_in_wei, - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), } txn_hash = w3.eth.send_transaction(txn_params) @@ -2294,12 +2294,12 @@ def test_eth_replace_transaction_underpriced( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(3, "gwei"), - "maxPriorityFeePerGas": w3.toWei(2, "gwei"), + "maxFeePerGas": w3.to_wei(3, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(2, "gwei"), } txn_hash = w3.eth.send_transaction(txn_params) - one_gwei_in_wei = w3.toWei(1, "gwei") + one_gwei_in_wei = w3.to_wei(1, "gwei") txn_params["maxFeePerGas"] = one_gwei_in_wei txn_params["maxPriorityFeePerGas"] = one_gwei_in_wei @@ -2314,8 +2314,8 @@ def test_eth_replace_transaction_non_existing_transaction( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(3, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(3, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), } with pytest.raises(TransactionNotFound): w3.eth.replace_transaction( @@ -2333,8 +2333,8 @@ def test_eth_replace_transaction_already_mined( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(2, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(2, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), } txn_hash = w3.eth.send_transaction(txn_params) try: @@ -2343,8 +2343,8 @@ def test_eth_replace_transaction_already_mined( finally: w3.geth.miner.stop() # type: ignore - txn_params["maxFeePerGas"] = w3.toWei(3, "gwei") - txn_params["maxPriorityFeePerGas"] = w3.toWei(2, "gwei") + txn_params["maxFeePerGas"] = w3.to_wei(3, "gwei") + txn_params["maxPriorityFeePerGas"] = w3.to_wei(2, "gwei") with pytest.raises(ValueError, match="Supplied transaction with hash"): w3.eth.replace_transaction(txn_hash, txn_params) @@ -2356,14 +2356,14 @@ def test_eth_replace_transaction_incorrect_nonce( "to": unlocked_account, "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(2, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(2, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), } txn_hash = w3.eth.send_transaction(txn_params) txn = w3.eth.get_transaction(txn_hash) - txn_params["maxFeePerGas"] = w3.toWei(3, "gwei") - txn_params["maxPriorityFeePerGas"] = w3.toWei(2, "gwei") + txn_params["maxFeePerGas"] = w3.to_wei(3, "gwei") + txn_params["maxPriorityFeePerGas"] = w3.to_wei(2, "gwei") txn_params["nonce"] = Nonce(txn["nonce"] + 1) with pytest.raises(ValueError): w3.eth.replace_transaction(txn_hash, txn_params) @@ -2376,18 +2376,18 @@ def test_eth_replace_transaction_gas_price_too_low( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "gasPrice": w3.toWei(2, "gwei"), + "gasPrice": w3.to_wei(2, "gwei"), } txn_hash = w3.eth.send_transaction(txn_params) - txn_params["gasPrice"] = w3.toWei(1, "gwei") + txn_params["gasPrice"] = w3.to_wei(1, "gwei") with pytest.raises(ValueError): w3.eth.replace_transaction(txn_hash, txn_params) def test_eth_replace_transaction_gas_price_defaulting_minimum( self, w3: "Web3", unlocked_account: ChecksumAddress ) -> None: - gas_price = w3.toWei(1, "gwei") + gas_price = w3.to_wei(1, "gwei") txn_params: TxParams = { "from": unlocked_account, @@ -2414,11 +2414,11 @@ def test_eth_replace_transaction_gas_price_defaulting_strategy_higher( "to": unlocked_account, "value": Wei(1), "gas": 21000, - "gasPrice": w3.toWei(1, "gwei"), + "gasPrice": w3.to_wei(1, "gwei"), } txn_hash = w3.eth.send_transaction(txn_params) - two_gwei_in_wei = w3.toWei(2, "gwei") + two_gwei_in_wei = w3.to_wei(2, "gwei") def higher_gas_price_strategy(w3: "Web3", txn: TxParams) -> Wei: return two_gwei_in_wei @@ -2436,7 +2436,7 @@ def higher_gas_price_strategy(w3: "Web3", txn: TxParams) -> Wei: def test_eth_replace_transaction_gas_price_defaulting_strategy_lower( self, w3: "Web3", unlocked_account: ChecksumAddress ) -> None: - gas_price = w3.toWei(2, "gwei") + gas_price = w3.to_wei(2, "gwei") txn_params: TxParams = { "from": unlocked_account, @@ -2448,7 +2448,7 @@ def test_eth_replace_transaction_gas_price_defaulting_strategy_lower( txn_hash = w3.eth.send_transaction(txn_params) def lower_gas_price_strategy(w3: "Web3", txn: TxParams) -> Wei: - return w3.toWei(1, "gwei") + return w3.to_wei(1, "gwei") w3.eth.set_gas_price_strategy(lower_gas_price_strategy) @@ -2467,7 +2467,7 @@ def test_eth_modify_transaction_legacy( "to": unlocked_account, "value": Wei(1), "gas": 21000, - "gasPrice": w3.toWei( + "gasPrice": w3.to_wei( 1, "gwei" ), # must be greater than base_fee post London } @@ -2496,8 +2496,8 @@ def test_eth_modify_transaction( "to": unlocked_account, "value": Wei(1), "gas": 21000, - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), - "maxFeePerGas": w3.toWei(2, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), + "maxFeePerGas": w3.to_wei(2, "gwei"), } txn_hash = w3.eth.send_transaction(txn_params) @@ -2993,8 +2993,8 @@ def test_eth_get_transaction_receipt_unmined( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(3, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(3, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), } ) with pytest.raises(TransactionNotFound): @@ -3050,8 +3050,8 @@ def test_eth_wait_for_transaction_receipt_unmined( "to": unlocked_account_dual_type, "value": Wei(1), "gas": 21000, - "maxFeePerGas": w3.toWei(3, "gwei"), - "maxPriorityFeePerGas": w3.toWei(1, "gwei"), + "maxFeePerGas": w3.to_wei(3, "gwei"), + "maxPriorityFeePerGas": w3.to_wei(1, "gwei"), } ) diff --git a/web3/_utils/module_testing/go_ethereum_personal_module.py b/web3/_utils/module_testing/go_ethereum_personal_module.py index 76b9566c97..58661e2b6f 100644 --- a/web3/_utils/module_testing/go_ethereum_personal_module.py +++ b/web3/_utils/module_testing/go_ethereum_personal_module.py @@ -113,7 +113,7 @@ def test_personal_send_transaction( "to": unlockable_account_dual_type, "gas": 21000, "value": Wei(1), - "gasPrice": w3.toWei(1, "gwei"), + "gasPrice": w3.to_wei(1, "gwei"), } txn_hash = w3.geth.personal.send_transaction(txn_params, unlockable_account_pw) assert txn_hash diff --git a/web3/main.py b/web3/main.py index 185f17a7a0..915e79f32b 100644 --- a/web3/main.py +++ b/web3/main.py @@ -205,12 +205,12 @@ def toJSON(obj: Dict[Any, Any]) -> str: # Currency Utility @staticmethod @wraps(to_wei) - def toWei(number: Union[int, float, str, decimal.Decimal], unit: str) -> Wei: + def to_wei(number: Union[int, float, str, decimal.Decimal], unit: str) -> Wei: return cast(Wei, to_wei(number, unit)) @staticmethod @wraps(from_wei) - def fromWei(number: int, unit: str) -> Union[int, decimal.Decimal]: + def from_wei(number: int, unit: str) -> Union[int, decimal.Decimal]: return from_wei(number, unit) # Address Utility