diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index e0c630a0d4..f6597dc6f5 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,29 @@ def gas_price_strategy(_w3: "Web3", _txn: TxParams) -> Wei: w3.eth.set_gas_price_strategy(None) # reset strategy + @pytest.mark.asyncio + 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/middleware/gas_price_strategy.py b/web3/middleware/gas_price_strategy.py index 29c20dabc2..1cd5a31f09 100644 --- a/web3/middleware/gas_price_strategy.py +++ b/web3/middleware/gas_price_strategy.py @@ -7,10 +7,10 @@ from eth_utils.toolz import ( assoc, ) + from web3._utils.method_formatters import ( - to_integer_if_hex, + to_hex_if_integer, ) - from web3._utils.utility_methods import ( all_in_dict, any_in_dict, @@ -49,7 +49,7 @@ def validate_transaction_params( and none_in_dict(DYNAMIC_FEE_TXN_PARAMS, transaction) ): transaction = assoc( - transaction, "gasPrice", to_integer_if_hex(strategy_based_gas_price) + transaction, "gasPrice", to_hex_if_integer(strategy_based_gas_price) ) # legacy and dynamic fee tx variables used: