Skip to content

Commit b78bbfe

Browse files
committed
feat: reuse fetched fee rates
1 parent 432241c commit b78bbfe

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package to.bitkit.repositories
22

33
import com.google.firebase.messaging.FirebaseMessaging
4+
import com.synonym.bitkitcore.FeeRates
45
import com.synonym.bitkitcore.LightningInvoice
56
import com.synonym.bitkitcore.Scanner
67
import com.synonym.bitkitcore.createWithdrawCallbackUrl
@@ -579,10 +580,11 @@ class LightningRepo @Inject constructor(
579580
address: Address? = null,
580581
speed: TransactionSpeed? = null,
581582
utxosToSpend: List<SpendableUtxo>? = null,
583+
feeRates: FeeRates? = null,
582584
): Result<ULong> = withContext(bgDispatcher) {
583585
return@withContext try {
584586
val transactionSpeed = speed ?: settingsStore.data.first().defaultTransactionSpeed
585-
val satsPerVByte = getFeeRateForSpeed(transactionSpeed).getOrThrow().toUInt()
587+
val satsPerVByte = getFeeRateForSpeed(transactionSpeed, feeRates).getOrThrow().toUInt()
586588

587589
val addressOrDefault = address ?: cacheStore.data.first().onchainAddress
588590

@@ -600,9 +602,12 @@ class LightningRepo @Inject constructor(
600602
}
601603
}
602604

603-
suspend fun getFeeRateForSpeed(speed: TransactionSpeed): Result<ULong> = withContext(bgDispatcher) {
605+
suspend fun getFeeRateForSpeed(
606+
speed: TransactionSpeed,
607+
feeRates: FeeRates? = null,
608+
): Result<ULong> = withContext(bgDispatcher) {
604609
return@withContext runCatching {
605-
val fees = coreService.blocktank.getFees().getOrThrow()
610+
val fees = feeRates ?: coreService.blocktank.getFees().getOrThrow()
606611
val satsPerVByte = fees.getSatsPerVByteFor(speed)
607612
satsPerVByte.toULong()
608613
}.onFailure { e ->

app/src/main/java/to/bitkit/ui/screens/wallets/send/SendFeeViewModel.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ class SendFeeViewModel @Inject constructor(
7878
.map { speed ->
7979
async {
8080
val rate = FeeRate.fromSpeed(speed)
81-
val fee = getFeeSatsForSpeed(speed, feeRates)
81+
val fee = if (feeRates?.getSatsPerVByteFor(speed)?.toLong() != 0L) {
82+
getFeeForSpeed(speed, feeRates)
83+
} else {
84+
0
85+
}
8286
rate to fee
8387
}
8488
}.awaitAll()
@@ -87,13 +91,12 @@ class SendFeeViewModel @Inject constructor(
8791
}
8892
}
8993

90-
private suspend fun getFeeSatsForSpeed(speed: TransactionSpeed, feeRates: FeeRates?): Long {
91-
if (feeRates?.getSatsPerVByteFor(speed)?.toLong() == 0L) return 0L
92-
94+
private suspend fun getFeeForSpeed(speed: TransactionSpeed, feeRates: FeeRates?): Long {
9395
return lightningRepo.calculateTotalFee(
9496
amountSats = sendUiState.amount,
9597
utxosToSpend = sendUiState.selectedUtxos,
9698
speed = speed,
99+
feeRates = feeRates,
97100
).getOrDefault(0u).toLong()
98101
}
99102

0 commit comments

Comments
 (0)