Skip to content

Commit ae9cdac

Browse files
committed
Remove unnecessary await for generate_gas_price() and move to BaseEth
1 parent 5a1496b commit ae9cdac

File tree

4 files changed

+15
-26
lines changed

4 files changed

+15
-26
lines changed

newsfragments/2735.performance.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove unnecessary ``await`` for ``generate_gas_price()`` method as it does not need to be awaited. Move this method to ``BaseEth`` to be used directly by both ``Eth`` and ``AsyncEth`` modules.

web3/_utils/async_transactions.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,24 @@ async def _estimate_gas(w3: "Web3", tx: TxParams) -> Awaitable[int]:
5151
return await w3.eth.estimate_gas(tx) # type: ignore
5252

5353

54-
async def _gas_price(w3: "Web3", tx: TxParams) -> Awaitable[Optional[Wei]]:
55-
return await w3.eth.generate_gas_price(tx) # type: ignore
56-
57-
58-
async def _max_fee_per_gas(w3: "Web3", tx: TxParams) -> Awaitable[Wei]:
54+
async def _max_fee_per_gas(w3: "Web3", _tx: TxParams) -> Awaitable[Wei]:
5955
block = await w3.eth.get_block("latest") # type: ignore
6056
return await w3.eth.max_priority_fee + (2 * block["baseFeePerGas"]) # type: ignore
6157

6258

63-
async def _max_priority_fee_gas(w3: "Web3", tx: TxParams) -> Awaitable[Wei]:
59+
async def _max_priority_fee_gas(w3: "Web3", _tx: TxParams) -> Awaitable[Wei]:
6460
return await w3.eth.max_priority_fee # type: ignore
6561

6662

67-
async def _chain_id(w3: "Web3", tx: TxParams) -> Awaitable[int]:
63+
async def _chain_id(w3: "Web3", _tx: TxParams) -> Awaitable[int]:
6864
return await w3.eth.chain_id # type: ignore
6965

7066

7167
TRANSACTION_DEFAULTS = {
7268
"value": 0,
7369
"data": b"",
7470
"gas": _estimate_gas,
75-
"gasPrice": _gas_price,
71+
"gasPrice": lambda w3, tx: w3.eth.generate_gas_price(tx),
7672
"maxFeePerGas": _max_fee_per_gas,
7773
"maxPriorityFeePerGas": _max_priority_fee_gas,
7874
"chainId": _chain_id,
@@ -112,9 +108,8 @@ async def fill_transaction_defaults(w3: "Web3", transaction: TxParams) -> TxPara
112108
"""
113109
if w3 is None, fill as much as possible while offline
114110
"""
115-
strategy_based_gas_price = await w3.eth.generate_gas_price( # type: ignore
116-
transaction
117-
)
111+
strategy_based_gas_price = w3.eth.generate_gas_price(transaction)
112+
118113
is_dynamic_fee_transaction = strategy_based_gas_price is None and (
119114
"gasPrice" not in transaction # default to dynamic fee transaction
120115
or any_in_dict(DYNAMIC_FEE_TXN_PARAMS, transaction)
@@ -138,7 +133,12 @@ async def fill_transaction_defaults(w3: "Web3", transaction: TxParams) -> TxPara
138133
raise ValueError(
139134
f"You must specify a '{key}' value in the transaction"
140135
)
141-
default_val = await default_getter(w3, transaction)
136+
if key == "gasPrice":
137+
# `generate_gas_price()` is on the `BaseEth` class and does not
138+
# need to be awaited
139+
default_val = default_getter(w3, transaction)
140+
else:
141+
default_val = await default_getter(w3, transaction)
142142
else:
143143
default_val = default_getter
144144

web3/eth.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def send_transaction_munger(self, transaction: TxParams) -> Tuple[TxParams]:
177177
mungers=[default_root_munger],
178178
)
179179

180-
def _generate_gas_price(
180+
def generate_gas_price(
181181
self, transaction_params: Optional[TxParams] = None
182182
) -> Optional[Wei]:
183183
if self.gasPriceStrategy:
@@ -488,11 +488,6 @@ async def get_raw_transaction_by_block(
488488
block_identifier, index
489489
)
490490

491-
async def generate_gas_price(
492-
self, transaction_params: Optional[TxParams] = None
493-
) -> Optional[Wei]:
494-
return self._generate_gas_price(transaction_params)
495-
496491
async def estimate_gas(
497492
self, transaction: TxParams, block_identifier: Optional[BlockIdentifier] = None
498493
) -> int:
@@ -971,8 +966,3 @@ def filter_munger(
971966
RPC.eth_getWork,
972967
is_property=True,
973968
)
974-
975-
def generate_gas_price(
976-
self, transaction_params: Optional[TxParams] = None
977-
) -> Optional[Wei]:
978-
return self._generate_gas_price(transaction_params)

web3/middleware/gas_price_strategy.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ async def async_gas_price_strategy_middleware(
109109
async def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
110110
if method == "eth_sendTransaction":
111111
transaction = params[0]
112-
generated_gas_price = await w3.eth.generate_gas_price( # type: ignore
113-
transaction
114-
)
112+
generated_gas_price = w3.eth.generate_gas_price(transaction)
115113
latest_block = await w3.eth.get_block("latest") # type: ignore
116114
transaction = validate_transaction_params(
117115
transaction, latest_block, generated_gas_price

0 commit comments

Comments
 (0)