From 9158bd4c98cff70511cd57a71e5a38027f624886 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Fri, 17 Mar 2023 13:55:49 -0700 Subject: [PATCH] Fix reject too low mining fees (< 12 blocks conf target) --- api/lightning/node.py | 4 ++-- api/logics.py | 36 ++++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/api/lightning/node.py b/api/lightning/node.py index 218ba388..af5471a4 100644 --- a/api/lightning/node.py +++ b/api/lightning/node.py @@ -77,9 +77,9 @@ class LNNode: def estimate_fee(cls, amount_sats, target_conf=2, min_confs=1): """Returns estimated fee for onchain payouts""" - # We assume segwit. Use robosats donation address as shortcut so there is no need of user inputs + # We assume segwit. Use hardcoded address as shortcut so there is no need of user inputs yet. request = lnrpc.EstimateFeeRequest( - AddrToAmount={"bc1q3cpp7ww92n6zp04hv40kd3eyy5avgughx6xqnx": amount_sats}, + AddrToAmount={"bc1qgxwaqe4m9mypd7ltww53yv3lyxhcfnhzzvy5j3": amount_sats}, target_conf=target_conf, min_confs=min_confs, spend_unconfirmed=False, diff --git a/api/logics.py b/api/logics.py index b128bccb..c54f30ee 100644 --- a/api/logics.py +++ b/api/logics.py @@ -566,9 +566,9 @@ class Logics: ): # Not enough onchain balance to commit for this swap. return False - suggested_mining_fee_rate = LNNode.estimate_fee(amount_sats=preliminary_amount)[ - "mining_fee_rate" - ] + suggested_mining_fee_rate = LNNode.estimate_fee( + amount_sats=preliminary_amount, target_conf=2 + )["mining_fee_rate"] # Hardcap mining fee suggested at 100 sats/vbyte if suggested_mining_fee_rate > 100: @@ -683,25 +683,29 @@ class Logics: "bad_request": "Only the buyer of this order can provide a payout address." } # not the right time to submit - if ( - not ( - order.taker_bond.status - == order.maker_bond.status - == LNPayment.Status.LOCKED - ) - and not order.status == Order.Status.FAI - ): - return False, {"bad_request": "You cannot submit an adress are not locked."} - # not a valid address (does not accept Taproot as of now) + if not ( + order.taker_bond.status + == order.maker_bond.status + == LNPayment.Status.LOCKED + ) or order.status not in [Order.Status.WFI, Order.Status.WF2]: + return False, {"bad_request": "You cannot submit an address now."} + # not a valid address valid, context = validate_onchain_address(address) if not valid: return False, context + num_satoshis = cls.payout_amount(order, user)[1]["invoice_amount"] if mining_fee_rate: # not a valid mining fee - if float(mining_fee_rate) < 2: + min_mining_fee_rate = LNNode.estimate_fee( + amount_sats=num_satoshis, target_conf=12 + )["mining_fee_rate"] + + min_mining_fee_rate = max(2, min_mining_fee_rate) + + if float(mining_fee_rate) < min_mining_fee_rate: return False, { - "bad_address": "The mining fee is too low, must be higher than 2 Sat/vbyte" + "bad_address": f"The mining fee is too low. Must be higher than {min_mining_fee_rate} Sat/vbyte" } elif float(mining_fee_rate) > 100: return False, { @@ -715,7 +719,7 @@ class Logics: tx = order.payout_tx tx.address = address tx.mining_fee_sats = int(tx.mining_fee_rate * 200) - tx.num_satoshis = cls.payout_amount(order, user)[1]["invoice_amount"] + tx.num_satoshis = num_satoshis tx.sent_satoshis = int( float(tx.num_satoshis) - float(tx.num_satoshis) * float(tx.swap_fee_rate) / 100