Browse Source

swaps: fix normal amount formulas

In a normal (forward) swap (onchain->offchain):
send_amount = receive_amount * (1 + service_percentage) + normal_fee ,
and vice versa:
receive_amount = (send_amount + normal_fee) / (1 + service_percentage) ,
i.e., the service fee is charged on the received offchain amount.
master
bitromortac 5 years ago
parent
commit
64ecf8539a
No known key found for this signature in database
GPG Key ID: 1965063FC13BEBE2
  1. 20
      electrum/submarine_swaps.py

20
electrum/submarine_swaps.py

@ -281,7 +281,7 @@ class SwapManager(Logger):
if locktime - self.network.get_local_height() >= 144: if locktime - self.network.get_local_height() >= 144:
raise Exception("fswap check failed: locktime too far in future") raise Exception("fswap check failed: locktime too far in future")
# create funding tx # create funding tx
funding_output = PartialTxOutput.from_address_and_value(lockup_address, expected_onchain_amount) funding_output = PartialTxOutput.from_address_and_value(lockup_address, onchain_amount)
if tx is None: if tx is None:
tx = self.wallet.create_transaction(outputs=[funding_output], rbf=False, password=password) tx = self.wallet.create_transaction(outputs=[funding_output], rbf=False, password=password)
else: else:
@ -310,7 +310,7 @@ class SwapManager(Logger):
await self.network.broadcast_transaction(tx) await self.network.broadcast_transaction(tx)
return tx.txid() return tx.txid()
async def reverse_swap(self, amount_sat: int, expected_amount: int) -> bool: async def reverse_swap(self, lightning_amount: int, expected_onchain_amount: int) -> bool:
"""send on Lightning, receive on-chain """send on Lightning, receive on-chain
- User generates preimage, RHASH. Sends RHASH to server. - User generates preimage, RHASH. Sends RHASH to server.
@ -330,7 +330,7 @@ class SwapManager(Logger):
"type": "reversesubmarine", "type": "reversesubmarine",
"pairId": "BTC/BTC", "pairId": "BTC/BTC",
"orderSide": "buy", "orderSide": "buy",
"invoiceAmount": amount_sat, "invoiceAmount": lightning_amount,
"preimageHash": preimage_hash.hex(), "preimageHash": preimage_hash.hex(),
"claimPublicKey": pubkey.hex() "claimPublicKey": pubkey.hex()
} }
@ -361,9 +361,9 @@ class SwapManager(Logger):
if locktime != int.from_bytes(parsed_script[10][1], byteorder='little'): if locktime != int.from_bytes(parsed_script[10][1], byteorder='little'):
raise Exception("rswap check failed: inconsistent locktime and script") raise Exception("rswap check failed: inconsistent locktime and script")
# check that the onchain amount is what we expected # check that the onchain amount is what we expected
if onchain_amount < expected_amount: if onchain_amount < expected_onchain_amount:
raise Exception(f"rswap check failed: onchain_amount is less than what we expected: " raise Exception(f"rswap check failed: onchain_amount is less than what we expected: "
f"{onchain_amount} < {expected_amount}") f"{onchain_amount} < {expected_onchain_amount}")
# verify that we will have enough time to get our tx confirmed # verify that we will have enough time to get our tx confirmed
if locktime - self.network.get_local_height() <= 60: if locktime - self.network.get_local_height() <= 60:
raise Exception("rswap check failed: locktime too close") raise Exception("rswap check failed: locktime too close")
@ -379,9 +379,9 @@ class SwapManager(Logger):
prepay_hash = fee_lnaddr.paymenthash prepay_hash = fee_lnaddr.paymenthash
else: else:
prepay_hash = None prepay_hash = None
if int(invoice_amount) != amount_sat: if int(invoice_amount) != lightning_amount:
raise Exception(f"rswap check failed: invoice_amount ({invoice_amount}) " raise Exception(f"rswap check failed: invoice_amount ({invoice_amount}) "
f"not what we requested ({amount_sat})") f"not what we requested ({lightning_amount})")
# save swap data to wallet file # save swap data to wallet file
swap = SwapData( swap = SwapData(
redeem_script = redeem_script, redeem_script = redeem_script,
@ -391,7 +391,7 @@ class SwapManager(Logger):
prepay_hash = prepay_hash, prepay_hash = prepay_hash,
lockup_address = lockup_address, lockup_address = lockup_address,
onchain_amount = onchain_amount, onchain_amount = onchain_amount,
lightning_amount = amount_sat, lightning_amount = lightning_amount,
is_reverse = True, is_reverse = True,
is_redeemed = False, is_redeemed = False,
funding_txid = None, funding_txid = None,
@ -443,7 +443,7 @@ class SwapManager(Logger):
return return
else: else:
x -= self.normal_fee x -= self.normal_fee
x = int(x * (100 - self.percentage) / 100) x = int(x / ((100 + self.percentage) / 100))
if not self.check_invoice_amount(x): if not self.check_invoice_amount(x):
return return
return x return x
@ -461,7 +461,7 @@ class SwapManager(Logger):
else: else:
if not self.check_invoice_amount(x): if not self.check_invoice_amount(x):
return return
x = int(x * 100 / (100 - self.percentage)) + 1 x = int(x * 100 / (100 + self.percentage)) + 1
x += self.normal_fee x += self.normal_fee
return x return x

Loading…
Cancel
Save