diff --git a/newsfragments/3065.bugfix.rst b/newsfragments/3065.bugfix.rst new file mode 100644 index 0000000000..39b24b6ac3 --- /dev/null +++ b/newsfragments/3065.bugfix.rst @@ -0,0 +1 @@ +Fix return type for ``rpc_gas_price_strategy`` to ``int`` but also only convert the ``strategy_based_gas_price`` to ``hex`` if it is an ``int`` in the ``gas_price_strategy_middleware``. diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index e0c630a0d4..dae70c4215 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -688,6 +688,29 @@ def gas_price_strategy(w3: "Web3", txn: TxParams) -> Wei: assert txn["gasPrice"] == two_gwei_in_wei async_w3.eth.set_gas_price_strategy(None) # reset strategy + @pytest.mark.asyncio + async def test_gas_price_strategy_middleware_hex_value( + self, async_w3: "AsyncWeb3", async_unlocked_account_dual_type: ChecksumAddress + ) -> None: + txn_params: TxParams = { + "from": async_unlocked_account_dual_type, + "to": async_unlocked_account_dual_type, + "value": Wei(1), + "gas": 21000, + } + two_gwei_in_wei = async_w3.to_wei(2, "gwei") + + def gas_price_strategy(_w3: "Web3", _txn: TxParams) -> str: + return hex(two_gwei_in_wei) + + async_w3.eth.set_gas_price_strategy(gas_price_strategy) # type: ignore + + txn_hash = await async_w3.eth.send_transaction(txn_params) + txn = await async_w3.eth.get_transaction(txn_hash) + + assert txn["gasPrice"] == two_gwei_in_wei + async_w3.eth.set_gas_price_strategy(None) # reset strategy + @pytest.mark.asyncio @pytest.mark.parametrize( "max_fee", (1000000000, None), ids=["with_max_fee", "without_max_fee"] @@ -3199,6 +3222,28 @@ def gas_price_strategy(_w3: "Web3", _txn: TxParams) -> Wei: w3.eth.set_gas_price_strategy(None) # reset strategy + def test_gas_price_strategy_middleware_hex_value( + self, w3: "Web3", unlocked_account_dual_type: ChecksumAddress + ) -> None: + txn_params: TxParams = { + "from": unlocked_account_dual_type, + "to": unlocked_account_dual_type, + "value": Wei(1), + "gas": 21000, + } + two_gwei_in_wei = w3.to_wei(2, "gwei") + + def gas_price_strategy(_w3: "Web3", _txn: TxParams) -> str: + return hex(two_gwei_in_wei) + + w3.eth.set_gas_price_strategy(gas_price_strategy) # type: ignore + + txn_hash = w3.eth.send_transaction(txn_params) + txn = w3.eth.get_transaction(txn_hash) + + assert txn["gasPrice"] == two_gwei_in_wei + w3.eth.set_gas_price_strategy(None) # reset strategy + def test_eth_replace_transaction_legacy( self, w3: "Web3", unlocked_account_dual_type: ChecksumAddress ) -> None: diff --git a/web3/gas_strategies/rpc.py b/web3/gas_strategies/rpc.py index 23660ff3e2..22ffdb5073 100644 --- a/web3/gas_strategies/rpc.py +++ b/web3/gas_strategies/rpc.py @@ -5,9 +5,6 @@ from web3 import ( Web3, ) -from web3._utils.rpc_abi import ( - RPC, -) from web3.types import ( TxParams, Wei, @@ -20,4 +17,4 @@ def rpc_gas_price_strategy( """ A simple gas price strategy deriving it's value from the eth_gasPrice JSON-RPC call. """ - return w3.manager.request_blocking(RPC.eth_gasPrice, []) + return w3.eth.gas_price diff --git a/web3/middleware/gas_price_strategy.py b/web3/middleware/gas_price_strategy.py index 9abdfb6b28..1cd5a31f09 100644 --- a/web3/middleware/gas_price_strategy.py +++ b/web3/middleware/gas_price_strategy.py @@ -8,6 +8,9 @@ assoc, ) +from web3._utils.method_formatters import ( + to_hex_if_integer, +) from web3._utils.utility_methods import ( all_in_dict, any_in_dict, @@ -45,7 +48,9 @@ def validate_transaction_params( and "gasPrice" not in transaction and none_in_dict(DYNAMIC_FEE_TXN_PARAMS, transaction) ): - transaction = assoc(transaction, "gasPrice", hex(strategy_based_gas_price)) + transaction = assoc( + transaction, "gasPrice", to_hex_if_integer(strategy_based_gas_price) + ) # legacy and dynamic fee tx variables used: if "gasPrice" in transaction and any_in_dict(DYNAMIC_FEE_TXN_PARAMS, transaction):