From 5135b8dbdd516ccd6c609632d538673071f6765c Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 4 Mar 2025 09:51:05 +0000 Subject: [PATCH] tx.new_swap: deduct estimated fee from quote for one-output TXs --- mmgen/swap/proto/thorchain/midgard.py | 15 ++++++++++++--- mmgen/tx/new.py | 4 +++- mmgen/tx/new_swap.py | 4 ++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/mmgen/swap/proto/thorchain/midgard.py b/mmgen/swap/proto/thorchain/midgard.py index ead3ead0..12c12eb1 100755 --- a/mmgen/swap/proto/thorchain/midgard.py +++ b/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} diff --git a/mmgen/tx/new.py b/mmgen/tx/new.py index a147a791..72898ed2 100755 --- a/mmgen/tx/new.py +++ b/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, diff --git a/mmgen/tx/new_swap.py b/mmgen/tx/new_swap.py index 55501f0e..d39f10eb 100755 --- a/mmgen/tx/new_swap.py +++ b/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':