Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rpc gas strategy #3065

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3065.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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``.
45 changes: 45 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions web3/gas_strategies/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
from web3 import (
Web3,
)
from web3._utils.rpc_abi import (
RPC,
)
from web3.types import (
TxParams,
Wei,
Expand All @@ -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
7 changes: 6 additions & 1 deletion web3/middleware/gas_price_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down