Browse Source

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

The MMGen Project 7 months ago
parent
commit
91a2504e62
3 changed files with 24 additions and 12 deletions
  1. 1 1
      mmgen/proto/btc/tx/new.py
  2. 22 10
      mmgen/proto/eth/tx/new.py
  3. 1 1
      mmgen/tx/bump.py

+ 1 - 1
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'
 	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)'
 	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
 		return None
 
 
 	def process_data_output_arg(self, arg):
 	def process_data_output_arg(self, arg):

+ 22 - 10
mmgen/proto/eth/tx/new.py

@@ -35,9 +35,6 @@ class New(Base, TxBase.New):
 
 
 		super().__init__(*args, **kwargs)
 		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:
 		if self.cfg.contract_data:
 			m = "'--contract-data' option may not be used with token transaction"
 			m = "'--contract-data' option may not be used with token transaction"
 			assert 'Token' not in self.name, m
 			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.usr_contract_data = bytes.fromhex(fp.read().strip())
 			self.disable_fee_check = True
 			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):
 	async def get_nonce(self):
 		return ETHNonce(int(
 		return ETHNonce(int(
@@ -220,6 +224,14 @@ class TokenNew(TokenBase, New):
 	desc = 'transaction'
 	desc = 'transaction'
 	fee_is_approximate = True
 	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
 	@property
 	def total_gas(self):
 	def total_gas(self):
 		return self.gas + (self.router_gas if self.is_swap else 0)
 		return self.gas + (self.router_gas if self.is_swap else 0)

+ 1 - 1
mmgen/tx/bump.py

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