Browse Source

tx.new_swap: deduct estimated fee from quote for one-output TXs

The MMGen Project 1 week ago
parent
commit
5135b8dbdd
3 changed files with 17 additions and 6 deletions
  1. 12 3
      mmgen/swap/proto/thorchain/midgard.py
  2. 3 1
      mmgen/tx/new.py
  3. 2 2
      mmgen/tx/new_swap.py

+ 12 - 3
mmgen/swap/proto/thorchain/midgard.py

@@ -62,8 +62,8 @@ class Midgard:
 			from ....util import pp_fmt, die
 			die(2, pp_fmt(self.data))
 
-	def format_quote(self):
-		from ....util import make_timestr
+	def format_quote(self, *, deduct_est_fee=False):
+		from ....util import make_timestr, ymsg
 		from ....util2 import format_elapsed_hr
 		from ....color import blue, cyan, pink, orange
 		from . import name
@@ -75,6 +75,15 @@ class Midgard:
 		in_amt = self.in_amt
 		out_amt = tx.recv_proto.coin_amt(int(d['expected_amount_out']), from_unit='satoshi')
 
+		_amount_in_label = 'Amount in:'
+		if deduct_est_fee:
+			if d['gas_rate_units'] == 'satsperbyte':
+				in_amt -= tx.feespec2abs(d['recommended_gas_rate'] + 's', tx.estimate_size())
+				out_amt *= (in_amt / self.in_amt)
+				_amount_in_label = 'Amount in (estimated):'
+			else:
+				ymsg('Warning: unknown gas unit ‘{}’, cannot estimate fee'.format(d['gas_rate_units']))
+
 		min_in_amt = tx.send_proto.coin_amt(int(d['recommended_min_amount_in']), from_unit='satoshi')
 		gas_unit = {
 			'satsperbyte': 'sat/byte',
@@ -92,7 +101,7 @@ class Midgard:
   Direction:                     {orange(f'{in_coin} => {out_coin}')}
   Vault address:                 {cyan(d['inbound_address'])}
   Quote expires:                 {pink(elapsed_disp)} [{make_timestr(d['expiry'])}]
-  Amount in:                     {in_amt.hl()} {in_coin}
+  {_amount_in_label:<22}         {in_amt.hl()} {in_coin}
   Expected amount out:           {out_amt.hl()} {out_coin}
   Rate:                          {(out_amt / in_amt).hl()} {out_coin}/{in_coin}
   Reverse rate:                  {(in_amt / out_amt).hl()} {in_coin}/{out_coin}

+ 3 - 1
mmgen/tx/new.py

@@ -474,7 +474,9 @@ class New(Base):
 				continue
 			fee_hint = None
 			if self.is_swap:
-				fee_hint = self.update_vault_output(self.vault_output.amt or self.sum_inputs())
+				fee_hint = self.update_vault_output(
+					self.vault_output.amt or self.sum_inputs(),
+					deduct_est_fee = self.vault_output == self.chg_output)
 			if funds_left := await self.get_fee(
 					self.cfg.fee or fee_hint,
 					outputs_sum,

+ 2 - 2
mmgen/tx/new_swap.py

@@ -24,7 +24,7 @@ class NewSwap(New):
 		self.swap_proto_mod = importlib.import_module(f'mmgen.swap.proto.{self.swap_proto}')
 		New.__init__(self, *args, **kwargs)
 
-	def update_vault_output(self, amt):
+	def update_vault_output(self, amt, *, deduct_est_fee=False):
 		sp = self.swap_proto_mod
 		c = sp.rpc_client(self, amt)
 
@@ -34,7 +34,7 @@ class NewSwap(New):
 			self.cfg._util.qmsg(f'Retrieving data from {c.rpc.host}...')
 			c.get_quote()
 			self.cfg._util.qmsg('OK')
-			msg(c.format_quote())
+			msg(c.format_quote(deduct_est_fee=deduct_est_fee))
 			ch = get_char('Press ‘r’ to refresh quote, any other key to continue: ')
 			msg('')
 			if ch not in 'Rr':