Skip to content

Commit

Permalink
Better readability within prepare_replacement_transaction code
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Aug 26, 2021
1 parent 9c2222f commit db0287e
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions web3/_utils/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,36 +206,42 @@ def assert_valid_transaction_params(transaction_params: TxParams) -> None:

def prepare_replacement_transaction(
web3: "Web3",
current_transaction: TxData,
new_transaction: TxParams,
original_transaction: TxData,
replacement_transaction: TxParams,
gas_multiplier: float = 1.125
) -> TxParams:
if current_transaction['blockHash'] is not None:
raise ValueError(f'Supplied transaction with hash {current_transaction["hash"]!r} '
if original_transaction['blockHash'] is not None:
raise ValueError(f'Supplied transaction with hash {original_transaction["hash"]!r} '
'has already been mined')
if 'nonce' in new_transaction and new_transaction['nonce'] != current_transaction['nonce']:
if 'nonce' in replacement_transaction and (
replacement_transaction['nonce'] != original_transaction['nonce']
):
raise ValueError('Supplied nonce in new_transaction must match the pending transaction')

if 'nonce' not in new_transaction:
new_transaction = assoc(new_transaction, 'nonce', current_transaction['nonce'])
if 'nonce' not in replacement_transaction:
replacement_transaction = assoc(
replacement_transaction, 'nonce', original_transaction['nonce']
)

if 'maxFeePerGas' in new_transaction or 'maxPriorityFeePerGas' in new_transaction:
if any(_ in replacement_transaction for _ in ('maxFeePerGas', 'maxPriorityFeePerGas')):
# for now, the client decides if a 1559 txn can replace the existing txn or not
pass

elif 'gasPrice' in new_transaction and current_transaction['gasPrice'] is not None:
if new_transaction['gasPrice'] <= current_transaction['gasPrice']:
elif 'gasPrice' in replacement_transaction and original_transaction['gasPrice'] is not None:
if replacement_transaction['gasPrice'] <= original_transaction['gasPrice']:
raise ValueError('Supplied gas price must exceed existing transaction gas price')

else:
generated_gas_price = web3.eth.generate_gas_price(new_transaction)
minimum_gas_price = int(math.ceil(current_transaction['gasPrice'] * gas_multiplier))
generated_gas_price = web3.eth.generate_gas_price(replacement_transaction)
minimum_gas_price = int(math.ceil(original_transaction['gasPrice'] * gas_multiplier))
if generated_gas_price and generated_gas_price > minimum_gas_price:
new_transaction = assoc(new_transaction, 'gasPrice', generated_gas_price)
replacement_transaction = assoc(
replacement_transaction, 'gasPrice', generated_gas_price
)
else:
new_transaction = assoc(new_transaction, 'gasPrice', minimum_gas_price)
replacement_transaction = assoc(replacement_transaction, 'gasPrice', minimum_gas_price)

return new_transaction
return replacement_transaction


def replace_transaction(
Expand Down

0 comments on commit db0287e

Please sign in to comment.