From 91a2504e625f8dd16d321583c6ab58bdb41f58f4 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 8 May 2025 15:22:07 +0000 Subject: [PATCH] proto.eth.tx.new: improve gas selection logic for `set_gas()` --- mmgen/proto/btc/tx/new.py | 2 +- mmgen/proto/eth/tx/new.py | 32 ++++++++++++++++++++++---------- mmgen/tx/bump.py | 2 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/mmgen/proto/btc/tx/new.py b/mmgen/proto/btc/tx/new.py index 0ff310d7..1d502fef 100755 --- a/mmgen/proto/btc/tx/new.py +++ b/mmgen/proto/btc/tx/new.py @@ -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): diff --git a/mmgen/proto/eth/tx/new.py b/mmgen/proto/eth/tx/new.py index 219712f7..64eaba9d 100755 --- a/mmgen/proto/eth/tx/new.py +++ b/mmgen/proto/eth/tx/new.py @@ -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) diff --git a/mmgen/tx/bump.py b/mmgen/tx/bump.py index 368b23fc..c347f0c0 100755 --- a/mmgen/tx/bump.py +++ b/mmgen/tx/bump.py @@ -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(