proto.eth.tx.new: improve gas selection logic for set_gas()

This commit is contained in:
The MMGen Project 2025-05-08 15:22:07 +00:00
commit 91a2504e62
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 24 additions and 12 deletions

View file

@ -24,7 +24,7 @@ class New(Base, TxNew):
no_chg_msg = 'Warning: Change address will be deleted as transaction produces no change'
msg_insufficient_funds = 'Selected outputs insufficient to fund this transaction ({} {} needed)'
async def set_gas(self, *, to_addr=None):
async def set_gas(self, *, to_addr=None, force=False):
return None
def process_data_output_arg(self, arg):

View file

@ -35,9 +35,6 @@ class New(Base, TxBase.New):
super().__init__(*args, **kwargs)
if self.is_token and self.is_swap:
self.router_gas = int(self.cfg.router_gas or self.dfl_router_gas)
if self.cfg.contract_data:
m = "'--contract-data' option may not be used with token transaction"
assert 'Token' not in self.name, m
@ -45,13 +42,20 @@ class New(Base, TxBase.New):
self.usr_contract_data = bytes.fromhex(fp.read().strip())
self.disable_fee_check = True
async def set_gas(self, *, to_addr=None):
if to_addr or not hasattr(self, 'gas'):
auto_gas = self.cfg.gas in ('auto', None)
self.gas = (
self.dfl_gas if self.cfg.gas == 'fallback' or (auto_gas and not self.is_token) else
(await self.get_gas_estimateGas(to_addr=to_addr)) if auto_gas else
int(self.cfg.gas))
async def get_gas_estimateGas(self, *, to_addr):
return self.dfl_gas
async def set_gas(self, *, to_addr=None, force=False):
if force or to_addr or not hasattr(self, 'gas'):
if is_int(self.cfg.gas):
self.gas = int(self.cfg.gas)
elif self.cfg.gas == 'fallback':
self.gas = self.dfl_gas
elif self.is_bump and not self.rpc.daemon.id == 'reth':
self.gas = self.txobj['startGas']
else:
assert self.cfg.gas in ('auto', None), f'{self.cfg.gas}: invalid value for cfg.gas'
self.gas = await self.get_gas_estimateGas(to_addr=to_addr)
async def get_nonce(self):
return ETHNonce(int(
@ -220,6 +224,14 @@ class TokenNew(TokenBase, New):
desc = 'transaction'
fee_is_approximate = True
async def set_gas(self, *, to_addr=None, force=False):
await super().set_gas(to_addr=to_addr, force=force)
if self.is_swap and (force or not hasattr(self, 'router_gas')):
self.router_gas = (
int(self.cfg.router_gas) if self.cfg.router_gas else
self.txobj['router_gas'] if self.txobj else
self.dfl_router_gas)
@property
def total_gas(self):
return self.gas + (self.router_gas if self.is_swap else 0)

View file

@ -72,7 +72,7 @@ class Bump(Completed, NewSwap):
output_idx = self.choose_output()
await self.set_gas()
await self.set_gas(force=True)
if not silent:
msg('Minimum fee for new transaction: {} {} ({} {})'.format(