lnworker/swaps: add '_sat' suffix to arg names and force kwargs
This commit is contained in:
@@ -1124,7 +1124,11 @@ class Commands:
|
|||||||
else:
|
else:
|
||||||
lightning_amount_sat = satoshis(lightning_amount)
|
lightning_amount_sat = satoshis(lightning_amount)
|
||||||
onchain_amount_sat = satoshis(onchain_amount)
|
onchain_amount_sat = satoshis(onchain_amount)
|
||||||
txid = await wallet.lnworker.swap_manager.normal_swap(lightning_amount_sat, onchain_amount_sat, password)
|
txid = await wallet.lnworker.swap_manager.normal_swap(
|
||||||
|
lightning_amount_sat=lightning_amount_sat,
|
||||||
|
expected_onchain_amount_sat=onchain_amount_sat,
|
||||||
|
password=password,
|
||||||
|
)
|
||||||
return {
|
return {
|
||||||
'txid': txid,
|
'txid': txid,
|
||||||
'lightning_amount': format_satoshis(lightning_amount_sat),
|
'lightning_amount': format_satoshis(lightning_amount_sat),
|
||||||
@@ -1149,7 +1153,10 @@ class Commands:
|
|||||||
else:
|
else:
|
||||||
lightning_amount_sat = satoshis(lightning_amount)
|
lightning_amount_sat = satoshis(lightning_amount)
|
||||||
onchain_amount_sat = satoshis(onchain_amount)
|
onchain_amount_sat = satoshis(onchain_amount)
|
||||||
success = await wallet.lnworker.swap_manager.reverse_swap(lightning_amount_sat, onchain_amount_sat)
|
success = await wallet.lnworker.swap_manager.reverse_swap(
|
||||||
|
lightning_amount_sat=lightning_amount_sat,
|
||||||
|
expected_onchain_amount_sat=onchain_amount_sat,
|
||||||
|
)
|
||||||
return {
|
return {
|
||||||
'success': success,
|
'success': success,
|
||||||
'lightning_amount': format_satoshis(lightning_amount_sat),
|
'lightning_amount': format_satoshis(lightning_amount_sat),
|
||||||
|
|||||||
@@ -757,7 +757,11 @@ class SwapDialog(Factory.Popup):
|
|||||||
return
|
return
|
||||||
loop = self.app.network.asyncio_loop
|
loop = self.app.network.asyncio_loop
|
||||||
coro = self.swap_manager.normal_swap(
|
coro = self.swap_manager.normal_swap(
|
||||||
lightning_amount, onchain_amount, password, tx=tx)
|
lightning_amount_sat=lightning_amount,
|
||||||
|
expected_onchain_amount_sat=onchain_amount,
|
||||||
|
password=password,
|
||||||
|
tx=tx,
|
||||||
|
)
|
||||||
asyncio.run_coroutine_threadsafe(coro, loop)
|
asyncio.run_coroutine_threadsafe(coro, loop)
|
||||||
|
|
||||||
def do_reverse_swap(self, lightning_amount, onchain_amount, password):
|
def do_reverse_swap(self, lightning_amount, onchain_amount, password):
|
||||||
@@ -765,7 +769,9 @@ class SwapDialog(Factory.Popup):
|
|||||||
return
|
return
|
||||||
loop = self.app.network.asyncio_loop
|
loop = self.app.network.asyncio_loop
|
||||||
coro = self.swap_manager.reverse_swap(
|
coro = self.swap_manager.reverse_swap(
|
||||||
lightning_amount, onchain_amount + self.swap_manager.get_claim_fee())
|
lightning_amount_sat=lightning_amount,
|
||||||
|
expected_onchain_amount_sat=onchain_amount + self.swap_manager.get_claim_fee(),
|
||||||
|
)
|
||||||
asyncio.run_coroutine_threadsafe(coro, loop)
|
asyncio.run_coroutine_threadsafe(coro, loop)
|
||||||
|
|
||||||
def on_ok(self):
|
def on_ok(self):
|
||||||
|
|||||||
@@ -218,7 +218,10 @@ class SwapDialog(WindowModalDialog):
|
|||||||
onchain_amount = self.recv_amount_e.get_amount()
|
onchain_amount = self.recv_amount_e.get_amount()
|
||||||
if lightning_amount is None or onchain_amount is None:
|
if lightning_amount is None or onchain_amount is None:
|
||||||
return
|
return
|
||||||
coro = self.swap_manager.reverse_swap(lightning_amount, onchain_amount + self.swap_manager.get_claim_fee())
|
coro = self.swap_manager.reverse_swap(
|
||||||
|
lightning_amount_sat=lightning_amount,
|
||||||
|
expected_onchain_amount_sat=onchain_amount + self.swap_manager.get_claim_fee(),
|
||||||
|
)
|
||||||
self.window.run_coroutine_from_thread(coro)
|
self.window.run_coroutine_from_thread(coro)
|
||||||
else:
|
else:
|
||||||
lightning_amount = self.recv_amount_e.get_amount()
|
lightning_amount = self.recv_amount_e.get_amount()
|
||||||
@@ -269,7 +272,12 @@ class SwapDialog(WindowModalDialog):
|
|||||||
def do_normal_swap(self, lightning_amount, onchain_amount, password):
|
def do_normal_swap(self, lightning_amount, onchain_amount, password):
|
||||||
tx = self.tx
|
tx = self.tx
|
||||||
assert tx
|
assert tx
|
||||||
coro = self.swap_manager.normal_swap(lightning_amount, onchain_amount, password, tx=tx)
|
coro = self.swap_manager.normal_swap(
|
||||||
|
lightning_amount_sat=lightning_amount,
|
||||||
|
expected_onchain_amount_sat=onchain_amount,
|
||||||
|
password=password,
|
||||||
|
tx=tx,
|
||||||
|
)
|
||||||
self.window.run_coroutine_from_thread(coro)
|
self.window.run_coroutine_from_thread(coro)
|
||||||
|
|
||||||
def get_description(self):
|
def get_description(self):
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ class FeeUpdate(StoredObject):
|
|||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
class ChannelConstraints(StoredObject):
|
class ChannelConstraints(StoredObject):
|
||||||
capacity = attr.ib(type=int)
|
capacity = attr.ib(type=int) # in sat
|
||||||
is_initiator = attr.ib(type=bool) # note: sometimes also called "funder"
|
is_initiator = attr.ib(type=bool) # note: sometimes also called "funder"
|
||||||
funding_txn_minimum_depth = attr.ib(type=int)
|
funding_txn_minimum_depth = attr.ib(type=int)
|
||||||
|
|
||||||
|
|||||||
@@ -1217,7 +1217,7 @@ class LNWallet(LNWorker):
|
|||||||
raise Exception(_("add invoice timed out"))
|
raise Exception(_("add invoice timed out"))
|
||||||
|
|
||||||
@log_exceptions
|
@log_exceptions
|
||||||
async def create_invoice(self, amount_msat: Optional[int], message, expiry: int):
|
async def create_invoice(self, *, amount_msat: Optional[int], message, expiry: int):
|
||||||
timestamp = int(time.time())
|
timestamp = int(time.time())
|
||||||
routing_hints = await self._calc_routing_hints_for_invoice(amount_msat)
|
routing_hints = await self._calc_routing_hints_for_invoice(amount_msat)
|
||||||
if not routing_hints:
|
if not routing_hints:
|
||||||
@@ -1245,7 +1245,11 @@ class LNWallet(LNWorker):
|
|||||||
|
|
||||||
async def _add_request_coro(self, amount_sat: Optional[int], message, expiry: int) -> str:
|
async def _add_request_coro(self, amount_sat: Optional[int], message, expiry: int) -> str:
|
||||||
amount_msat = amount_sat * 1000 if amount_sat is not None else None
|
amount_msat = amount_sat * 1000 if amount_sat is not None else None
|
||||||
lnaddr, invoice = await self.create_invoice(amount_msat, message, expiry)
|
lnaddr, invoice = await self.create_invoice(
|
||||||
|
amount_msat=amount_msat,
|
||||||
|
message=message,
|
||||||
|
expiry=expiry,
|
||||||
|
)
|
||||||
key = bh2u(lnaddr.paymenthash)
|
key = bh2u(lnaddr.paymenthash)
|
||||||
req = LNInvoice.from_bech32(invoice)
|
req = LNInvoice.from_bech32(invoice)
|
||||||
self.wallet.add_payment_request(req)
|
self.wallet.add_payment_request(req)
|
||||||
|
|||||||
@@ -225,8 +225,14 @@ class SwapManager(Logger):
|
|||||||
callback = lambda: self._claim_swap(swap)
|
callback = lambda: self._claim_swap(swap)
|
||||||
self.lnwatcher.add_callback(swap.lockup_address, callback)
|
self.lnwatcher.add_callback(swap.lockup_address, callback)
|
||||||
|
|
||||||
async def normal_swap(self, lightning_amount: int, expected_onchain_amount: int,
|
async def normal_swap(
|
||||||
password, *, tx: PartialTransaction = None) -> str:
|
self,
|
||||||
|
*,
|
||||||
|
lightning_amount_sat: int,
|
||||||
|
expected_onchain_amount_sat: int,
|
||||||
|
password,
|
||||||
|
tx: PartialTransaction = None,
|
||||||
|
) -> str:
|
||||||
"""send on-chain BTC, receive on Lightning
|
"""send on-chain BTC, receive on Lightning
|
||||||
|
|
||||||
- User generates an LN invoice with RHASH, and knows preimage.
|
- User generates an LN invoice with RHASH, and knows preimage.
|
||||||
@@ -238,7 +244,11 @@ class SwapManager(Logger):
|
|||||||
assert self.lnwatcher
|
assert self.lnwatcher
|
||||||
privkey = os.urandom(32)
|
privkey = os.urandom(32)
|
||||||
pubkey = ECPrivkey(privkey).get_public_key_bytes(compressed=True)
|
pubkey = ECPrivkey(privkey).get_public_key_bytes(compressed=True)
|
||||||
lnaddr, invoice = await self.lnworker.create_invoice(lightning_amount*1000, 'swap', expiry=3600*24)
|
lnaddr, invoice = await self.lnworker.create_invoice(
|
||||||
|
amount_msat=lightning_amount_sat * 1000,
|
||||||
|
message='swap',
|
||||||
|
expiry=3600 * 24,
|
||||||
|
)
|
||||||
payment_hash = lnaddr.paymenthash
|
payment_hash = lnaddr.paymenthash
|
||||||
preimage = self.lnworker.get_preimage(payment_hash)
|
preimage = self.lnworker.get_preimage(payment_hash)
|
||||||
request_data = {
|
request_data = {
|
||||||
@@ -274,9 +284,9 @@ class SwapManager(Logger):
|
|||||||
if locktime != int.from_bytes(parsed_script[6][1], byteorder='little'):
|
if locktime != int.from_bytes(parsed_script[6][1], byteorder='little'):
|
||||||
raise Exception("fswap check failed: inconsistent locktime and script")
|
raise Exception("fswap check failed: inconsistent locktime and script")
|
||||||
# check that onchain_amount is not more than what we estimated
|
# check that onchain_amount is not more than what we estimated
|
||||||
if onchain_amount > expected_onchain_amount:
|
if onchain_amount > expected_onchain_amount_sat:
|
||||||
raise Exception(f"fswap check failed: onchain_amount is more than what we estimated: "
|
raise Exception(f"fswap check failed: onchain_amount is more than what we estimated: "
|
||||||
f"{onchain_amount} > {expected_onchain_amount}")
|
f"{onchain_amount} > {expected_onchain_amount_sat}")
|
||||||
# verify that they are not locking up funds for more than a day
|
# verify that they are not locking up funds for more than a day
|
||||||
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")
|
||||||
@@ -285,7 +295,7 @@ class SwapManager(Logger):
|
|||||||
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:
|
||||||
dummy_output = PartialTxOutput.from_address_and_value(ln_dummy_address(), expected_onchain_amount)
|
dummy_output = PartialTxOutput.from_address_and_value(ln_dummy_address(), expected_onchain_amount_sat)
|
||||||
tx.outputs().remove(dummy_output)
|
tx.outputs().remove(dummy_output)
|
||||||
tx.add_outputs([funding_output])
|
tx.add_outputs([funding_output])
|
||||||
tx.set_rbf(False)
|
tx.set_rbf(False)
|
||||||
@@ -298,8 +308,8 @@ class SwapManager(Logger):
|
|||||||
preimage = preimage,
|
preimage = preimage,
|
||||||
prepay_hash = None,
|
prepay_hash = None,
|
||||||
lockup_address = lockup_address,
|
lockup_address = lockup_address,
|
||||||
onchain_amount = expected_onchain_amount,
|
onchain_amount = expected_onchain_amount_sat,
|
||||||
lightning_amount = lightning_amount,
|
lightning_amount = lightning_amount_sat,
|
||||||
is_reverse = False,
|
is_reverse = False,
|
||||||
is_redeemed = False,
|
is_redeemed = False,
|
||||||
funding_txid = tx.txid(),
|
funding_txid = tx.txid(),
|
||||||
@@ -310,7 +320,12 @@ 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, lightning_amount: int, expected_onchain_amount: int) -> bool:
|
async def reverse_swap(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
lightning_amount_sat: int,
|
||||||
|
expected_onchain_amount_sat: 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 +345,7 @@ class SwapManager(Logger):
|
|||||||
"type": "reversesubmarine",
|
"type": "reversesubmarine",
|
||||||
"pairId": "BTC/BTC",
|
"pairId": "BTC/BTC",
|
||||||
"orderSide": "buy",
|
"orderSide": "buy",
|
||||||
"invoiceAmount": lightning_amount,
|
"invoiceAmount": lightning_amount_sat,
|
||||||
"preimageHash": preimage_hash.hex(),
|
"preimageHash": preimage_hash.hex(),
|
||||||
"claimPublicKey": pubkey.hex()
|
"claimPublicKey": pubkey.hex()
|
||||||
}
|
}
|
||||||
@@ -361,9 +376,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_onchain_amount:
|
if onchain_amount < expected_onchain_amount_sat:
|
||||||
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_onchain_amount}")
|
f"{onchain_amount} < {expected_onchain_amount_sat}")
|
||||||
# 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 +394,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) != lightning_amount:
|
if int(invoice_amount) != lightning_amount_sat:
|
||||||
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 ({lightning_amount})")
|
f"not what we requested ({lightning_amount_sat})")
|
||||||
# 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 +406,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 = lightning_amount,
|
lightning_amount = lightning_amount_sat,
|
||||||
is_reverse = True,
|
is_reverse = True,
|
||||||
is_redeemed = False,
|
is_redeemed = False,
|
||||||
funding_txid = None,
|
funding_txid = None,
|
||||||
|
|||||||
Reference in New Issue
Block a user