Skip to content

Commit 7895d92

Browse files
committed
fix: correct lnurl msat to sat min rounding
1 parent f0f8274 commit 7895d92

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

app/src/main/java/to/bitkit/ext/Lnurl.kt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@ package to.bitkit.ext
33
import com.synonym.bitkitcore.LnurlPayData
44
import com.synonym.bitkitcore.LnurlWithdrawData
55

6+
private const val MSATS_PER_SAT: ULong = 1000u
7+
8+
/**
9+
* LNURL amounts are expressed in millisatoshis (msat).
10+
*
11+
* When converting a minimum bound to whole sats we must round up:
12+
* `minSendable = 100500 msat` means the minimum payable amount is `101 sat` (not `100 sat`).
13+
*/
14+
private fun msatsToSatsCeil(msats: ULong): ULong {
15+
val quotient = msats / MSATS_PER_SAT
16+
val remainder = msats % MSATS_PER_SAT
17+
return when (remainder) {
18+
0uL -> quotient
19+
else -> quotient + 1uL
20+
}
21+
}
22+
623
fun LnurlPayData.commentAllowed(): Boolean = commentAllowed?.let { it > 0u } == true
7-
fun LnurlPayData.maxSendableSat(): ULong = maxSendable / 1000u
8-
fun LnurlPayData.minSendableSat(): ULong = minSendable / 1000u
24+
fun LnurlPayData.maxSendableSat(): ULong = maxSendable / MSATS_PER_SAT
25+
fun LnurlPayData.minSendableSat(): ULong = msatsToSatsCeil(minSendable)
926

10-
fun LnurlWithdrawData.minWithdrawableSat(): ULong = (minWithdrawable ?: 0u) / 1000u
11-
fun LnurlWithdrawData.maxWithdrawableSat(): ULong = maxWithdrawable / 1000u
27+
fun LnurlWithdrawData.minWithdrawableSat(): ULong = msatsToSatsCeil(minWithdrawable ?: 0u)
28+
fun LnurlWithdrawData.maxWithdrawableSat(): ULong = maxWithdrawable / MSATS_PER_SAT

0 commit comments

Comments
 (0)