swaps: revise send/recv amount calculation
- document SwapManager._get_recv_amount and SwapManager._get_send_amount
- change calculations so that they match the boltz-backend
- note that in the reverse swap case, the server does not care about the on-chain claim tx the client
needs to pay for. This introduced some implicit hacks and inconsistencies in the code in the past,
it is still a bit ugly but at least this is now explicit.
- SwapManager._get_recv_amount and SwapManager._get_send_amount are now proper inverses of each other
-----
Here are some code snippets to play around with in Qt console.
For the forward swap case:
```
from electrum import ecc; lnworker = wallet.lnworker; sm = lnworker.swap_manager
invoice = network.run_from_another_thread(lnworker.create_invoice(amount_msat=3000000*1000, message="swap", expiry=86400))[1]; request_data = {"type": "submarine", "pairId": "BTC/BTC", "orderSide": "sell", "invoice": invoice, "refundPublicKey": ecc.GENERATOR.get_public_key_bytes().hex()}
network.send_http_on_proxy('post', sm.api_url + '/createswap', json=request_data, timeout=30)
sm.get_send_amount(3000000, is_reverse=False)
sm.get_recv_amount(3026730, is_reverse=False)
```
For the reverse swap case:
```
from electrum import ecc; import os; lnworker = wallet.lnworker; sm = lnworker.swap_manager
request_data = {"type": "reversesubmarine", "pairId": "BTC/BTC", "orderSide": "buy", "invoiceAmount": 3000000, "preimageHash": os.urandom(32).hex(), "claimPublicKey": ecc.GENERATOR.get_public_key_bytes().hex()}
network.send_http_on_proxy('post', sm.api_url + '/createswap', json=request_data, timeout=30)
sm.get_recv_amount(3000000, is_reverse=True)
sm.get_send_amount(2974443, is_reverse=True)
```
This commit is contained in:
@@ -145,7 +145,7 @@ class SwapDialog(WindowModalDialog):
|
||||
return
|
||||
self.send_amount_e.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet())
|
||||
send_amount = self.send_amount_e.get_amount()
|
||||
recv_amount = self.swap_manager.get_recv_amount(send_amount, self.is_reverse)
|
||||
recv_amount = self.swap_manager.get_recv_amount(send_amount, is_reverse=self.is_reverse)
|
||||
if self.is_reverse and send_amount and send_amount > self.lnworker.num_sats_can_send():
|
||||
# cannot send this much on lightning
|
||||
recv_amount = None
|
||||
@@ -166,7 +166,7 @@ class SwapDialog(WindowModalDialog):
|
||||
return
|
||||
self.recv_amount_e.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet())
|
||||
recv_amount = self.recv_amount_e.get_amount()
|
||||
send_amount = self.swap_manager.get_send_amount(recv_amount, self.is_reverse)
|
||||
send_amount = self.swap_manager.get_send_amount(recv_amount, is_reverse=self.is_reverse)
|
||||
if self.is_reverse and send_amount and send_amount > self.lnworker.num_sats_can_send():
|
||||
send_amount = None
|
||||
self.send_amount_e.follows = True
|
||||
|
||||
Reference in New Issue
Block a user