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

set maxFeePerGas if not provided #2055

Merged
merged 2 commits into from
Jun 30, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/2055.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set a default maxFeePerGas value consistent with Geth
7 changes: 3 additions & 4 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,12 +942,13 @@ def test_1559_no_priority_fee(
def test_1559_no_max_fee(
self, web3: "Web3", unlocked_account_dual_type: ChecksumAddress
) -> None:
maxPriorityFeePerGas = web3.toWei(2, 'gwei')
txn_params: TxParams = {
'from': unlocked_account_dual_type,
'to': unlocked_account_dual_type,
'value': Wei(1),
'gas': Wei(21000),
'maxPriorityFeePerGas': Wei(2 * 10**9),
'maxPriorityFeePerGas': maxPriorityFeePerGas,
}
txn_hash = web3.eth.send_transaction(txn_params)
txn = web3.eth.get_transaction(txn_hash)
Expand All @@ -958,9 +959,7 @@ def test_1559_no_max_fee(
assert txn['gas'] == 21000

block = web3.eth.get_block('latest')
# TODO: what if base_fee < tip?
assert txn['maxFeePerGas'] >= block['baseFeePerGas']
# assert txn['maxFeePerGas'] == base_fee * 2
assert txn['maxFeePerGas'] == maxPriorityFeePerGas + 2 * block['baseFeePerGas']

def test_1559_max_fee_less_than_tip(
self, web3: "Web3", unlocked_account_dual_type: ChecksumAddress
Expand Down
18 changes: 7 additions & 11 deletions web3/middleware/gas_price_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from web3.types import (
RPCEndpoint,
RPCResponse,
Wei,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -44,18 +43,15 @@ def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
transaction["maxPriorityFeePerGas"], 16
):
raise InvalidTransaction("maxFeePerGas must be >= maxPriorityFeePerGas")
# 1559 - no feecap:
# 1559 - no max fee:
elif 'maxFeePerGas' not in transaction and 'maxPriorityFeePerGas' in transaction:
block = web3.eth.get_block('latest')
base_fee = block['baseFeePerGas']
tip = Wei(int(transaction['maxPriorityFeePerGas'], 16))
if base_fee < tip:
# TODO: throw or just set feecap to max prio?
base_fee = tip
# raise InvalidTransaction('maxFeePerGas must be >= maxPriorityFeePerGas')
transaction = assoc(transaction, 'maxFeePerGas', hex(base_fee * 2))
latest_block = web3.eth.get_block('latest')
base_fee = latest_block['baseFeePerGas']
priority_fee = int(transaction['maxPriorityFeePerGas'], 16)
max_fee_per_gas = priority_fee + 2 * base_fee
transaction = assoc(transaction, 'maxFeePerGas', hex(max_fee_per_gas))
return make_request(method, [transaction])
# 1559 - no tip:
# 1559 - no priority fee:
elif 'maxFeePerGas' in transaction and 'maxPriorityFeePerGas' not in transaction:
raise InvalidTransaction(
"maxPriorityFeePerGas must be defined in a 1559 transaction."
Expand Down