prepare-pay-onchain: add option for drain (#464)

* prepare-pay-onchain: add option for drain in req

* Fix clippy

* ChainSwapStateHandler: gracefully handle building both drain and non-drain lockups

* Send Chain swap: use standard feerate when estimating lockup tx fee

* UDL: move new drain field above the last PreparePayOnchainRequest optional field

* UDL: move new drain field optional

* prepare-pay-onchain: treat normal payment as drain if receiver amount is high enough

If the receiver amount is as high as it would be in case of drain, treat the current prepare-pay-onchain as drain, even if the drain flag is not set.

* build_drain_tx: add optional amount validation

* Add PayOnchainAmount enum to cover amount types (drain, receiver)

* Add ability to find max_receiver_amount_sat for non-drain sends

* Revert "Add ability to find max_receiver_amount_sat for non-drain sends"

This reverts commit 60ee1c768021810f72bc64a8ada69d35b638185e.

* prepare_pay_onchain: treat drain and non-drain cases separately

If the non-drain case is chosen with a receiver_amount equivalent to what drain would have calculated, it results in an error. For drain, the caller has to explicitly choose PayOnchainAmount::Drain.

* CLI: send-onchain-payment accepts optional amount

* CLI: add docs for send-onchain-payment drain arg

* SDK: expand docs for prepare_pay_onchain

* Re-generate RN bindings

* Re-generate flutter bindings
This commit is contained in:
ok300
2024-09-11 15:52:56 +00:00
committed by GitHub
parent d42e37ce1e
commit e8cd66f81f
17 changed files with 749 additions and 72 deletions

View File

@@ -1330,13 +1330,13 @@ fun asPreparePayOnchainRequest(preparePayOnchainRequest: ReadableMap): PreparePa
if (!validateMandatoryFields(
preparePayOnchainRequest,
arrayOf(
"receiverAmountSat",
"amount",
),
)
) {
return null
}
val receiverAmountSat = preparePayOnchainRequest.getDouble("receiverAmountSat").toULong()
val amount = preparePayOnchainRequest.getMap("amount")?.let { asPayOnchainAmount(it) }!!
val satPerVbyte =
if (hasNonNullKey(
preparePayOnchainRequest,
@@ -1347,12 +1347,12 @@ fun asPreparePayOnchainRequest(preparePayOnchainRequest: ReadableMap): PreparePa
} else {
null
}
return PreparePayOnchainRequest(receiverAmountSat, satPerVbyte)
return PreparePayOnchainRequest(amount, satPerVbyte)
}
fun readableMapOf(preparePayOnchainRequest: PreparePayOnchainRequest): ReadableMap =
readableMapOf(
"receiverAmountSat" to preparePayOnchainRequest.receiverAmountSat,
"amount" to readableMapOf(preparePayOnchainRequest.amount),
"satPerVbyte" to preparePayOnchainRequest.satPerVbyte,
)
@@ -2485,6 +2485,44 @@ fun asNetworkList(arr: ReadableArray): List<Network> {
return list
}
fun asPayOnchainAmount(payOnchainAmount: ReadableMap): PayOnchainAmount? {
val type = payOnchainAmount.getString("type")
if (type == "receiver") {
val amountSat = payOnchainAmount.getDouble("amountSat").toULong()
return PayOnchainAmount.Receiver(amountSat)
}
if (type == "drain") {
return PayOnchainAmount.Drain
}
return null
}
fun readableMapOf(payOnchainAmount: PayOnchainAmount): ReadableMap? {
val map = Arguments.createMap()
when (payOnchainAmount) {
is PayOnchainAmount.Receiver -> {
pushToMap(map, "type", "receiver")
pushToMap(map, "amountSat", payOnchainAmount.amountSat)
}
is PayOnchainAmount.Drain -> {
pushToMap(map, "type", "drain")
}
}
return map
}
fun asPayOnchainAmountList(arr: ReadableArray): List<PayOnchainAmount> {
val list = ArrayList<PayOnchainAmount>()
for (value in arr.toList()) {
when (value) {
is ReadableMap -> list.add(asPayOnchainAmount(value)!!)
else -> throw SdkException.Generic(errUnexpectedType(value))
}
}
return list
}
fun asPaymentDetails(paymentDetails: ReadableMap): PaymentDetails? {
val type = paymentDetails.getString("type")