Skip to content

Commit

Permalink
Add tests for only converting to hex if gasPrice is int
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Jul 26, 2023
1 parent b3b318d commit ddb9ae5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
46 changes: 46 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,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:
Expand Down
6 changes: 3 additions & 3 deletions web3/middleware/gas_price_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ddb9ae5

Please sign in to comment.