Skip to content

Commit f660e75

Browse files
committed
refactor: Add const for 1 btc in sats
1 parent 374f644 commit f660e75

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

app/src/main/java/to/bitkit/models/Currency.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.text.DecimalFormatSymbols
88
import java.util.Locale
99

1010
const val BITCOIN_SYMBOL = ""
11+
const val SATS_IN_BTC = 100_000_000
1112

1213
@Serializable
1314
data class FxRateResponse(
@@ -51,7 +52,7 @@ data class ConvertedAmount(
5152
val flag: String,
5253
val sats: Long,
5354
) {
54-
val btcValue: BigDecimal = BigDecimal(sats).divide(BigDecimal(100_000_000))
55+
val btcValue: BigDecimal = BigDecimal(sats).divide(BigDecimal(SATS_IN_BTC))
5556

5657
data class BitcoinDisplayComponents(
5758
val symbol: String,

app/src/main/java/to/bitkit/services/CurrencyService.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import to.bitkit.async.ServiceQueue
55
import to.bitkit.data.BlocktankHttpClient
66
import to.bitkit.models.ConvertedAmount
77
import to.bitkit.models.FxRate
8+
import to.bitkit.models.SATS_IN_BTC
89
import to.bitkit.ui.utils.formatCurrency
910
import to.bitkit.utils.AppError
1011
import java.math.BigDecimal
@@ -53,7 +54,7 @@ class CurrencyService @Inject constructor(
5354
}
5455

5556
fun convert(sats: Long, rate: FxRate): ConvertedAmount? {
56-
val btcAmount = BigDecimal(sats).divide(BigDecimal(100_000_000))
57+
val btcAmount = BigDecimal(sats).divide(BigDecimal(SATS_IN_BTC))
5758
val value: BigDecimal = btcAmount.multiply(BigDecimal.valueOf(rate.rate))
5859

5960
val formatted = value.formatCurrency() ?: return null
@@ -70,7 +71,7 @@ class CurrencyService @Inject constructor(
7071

7172
fun convertFiatToSats(fiatValue: BigDecimal, rate: FxRate): ULong {
7273
val btcAmount = fiatValue.divide(BigDecimal.valueOf(rate.rate), 8, RoundingMode.HALF_UP)
73-
val satsDecimal = btcAmount.multiply(BigDecimal(100_000_000))
74+
val satsDecimal = btcAmount.multiply(BigDecimal(SATS_IN_BTC))
7475

7576
val roundedNumber = satsDecimal.setScale(0, RoundingMode.HALF_UP)
7677

@@ -82,7 +83,7 @@ class CurrencyService @Inject constructor(
8283

8384
// Convert the fiat amount to BTC, then to sats
8485
val btc = fiatAmount / rate.rate
85-
val sats = (btc * 100_000_000).roundToLong()
86+
val sats = (btc * SATS_IN_BTC).roundToLong()
8687

8788
return sats
8889
}

app/src/main/java/to/bitkit/ui/components/NumberPadTextField.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import to.bitkit.ext.removeSpaces
2424
import to.bitkit.models.BITCOIN_SYMBOL
2525
import to.bitkit.models.BitcoinDisplayUnit
2626
import to.bitkit.models.PrimaryDisplay
27+
import to.bitkit.models.SATS_IN_BTC
2728
import to.bitkit.models.formatToModernDisplay
2829
import to.bitkit.ui.currencyViewModel
2930
import to.bitkit.ui.theme.AppThemeSurface
@@ -168,7 +169,7 @@ fun AmountInputHandler(
168169
LaunchedEffect(input) {
169170
val sats = when (primaryDisplay) {
170171
PrimaryDisplay.BITCOIN -> {
171-
if (displayUnit == BitcoinDisplayUnit.MODERN) input else (input.toLongOrDefault(0L) * 100_000_000).toString()
172+
if (displayUnit == BitcoinDisplayUnit.MODERN) input else (input.toLongOrDefault(0L) * SATS_IN_BTC).toString()
172173
}
173174

174175
PrimaryDisplay.FIAT -> {

app/src/main/java/to/bitkit/utils/Bip21Utils.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package to.bitkit.utils
22

3+
import to.bitkit.models.SATS_IN_BTC
4+
35
object Bip21Utils {
46

57
fun buildBip21Url(
@@ -37,8 +39,8 @@ object Bip21Utils {
3739
}
3840

3941
private fun formatBtcAmount(sats: ULong): String {
40-
val fullBtc = sats / 100_000_000uL
41-
val remainderSats = sats % 100_000_000uL
42+
val fullBtc = sats / SATS_IN_BTC.toULong()
43+
val remainderSats = sats % SATS_IN_BTC.toULong()
4244

4345
return if (remainderSats == 0uL) {
4446
fullBtc.toString()

0 commit comments

Comments
 (0)