Skip to content

Commit af0b488

Browse files
authored
Merge pull request #589 from synonymdev/feat/sweep-funds
feat: sweep funds from unsupported address types
2 parents a80515d + 2342045 commit af0b488

19 files changed

Lines changed: 1860 additions & 71 deletions

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package to.bitkit.models
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.ui.res.stringResource
5+
import com.synonym.bitkitcore.FeeRates
56
import kotlinx.serialization.KSerializer
67
import kotlinx.serialization.Serializable
78
import kotlinx.serialization.descriptors.PrimitiveKind
@@ -25,6 +26,13 @@ sealed class TransactionSpeed {
2526
is Custom -> "custom_$satsPerVByte"
2627
}
2728

29+
fun getFeeRate(feeRates: FeeRates): UInt = when (this) {
30+
is Fast -> feeRates.fast
31+
is Medium -> feeRates.mid
32+
is Slow -> feeRates.slow
33+
is Custom -> satsPerVByte
34+
}
35+
2836
companion object {
2937
fun default(): TransactionSpeed = Medium
3038

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package to.bitkit.repositories
2+
3+
import com.synonym.bitkitcore.FeeRates
4+
import com.synonym.bitkitcore.broadcastSweepTransaction
5+
import com.synonym.bitkitcore.checkSweepableBalances
6+
import com.synonym.bitkitcore.prepareSweepTransaction
7+
import kotlinx.coroutines.CoroutineDispatcher
8+
import kotlinx.coroutines.withContext
9+
import to.bitkit.async.ServiceQueue
10+
import to.bitkit.data.keychain.Keychain
11+
import to.bitkit.di.BgDispatcher
12+
import to.bitkit.env.Env
13+
import to.bitkit.models.toCoreNetwork
14+
import to.bitkit.services.CoreService
15+
import to.bitkit.utils.Logger
16+
import to.bitkit.utils.ServiceError
17+
import to.bitkit.viewmodels.SweepResult
18+
import to.bitkit.viewmodels.SweepTransactionPreview
19+
import to.bitkit.viewmodels.SweepableBalances
20+
import javax.inject.Inject
21+
import javax.inject.Singleton
22+
import com.synonym.bitkitcore.SweepResult as BitkitCoreSweepResult
23+
import com.synonym.bitkitcore.SweepTransactionPreview as BitkitCoreSweepTransactionPreview
24+
import com.synonym.bitkitcore.SweepableBalances as BitkitCoreSweepableBalances
25+
26+
@Singleton
27+
class SweepRepo @Inject constructor(
28+
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
29+
private val keychain: Keychain,
30+
private val coreService: CoreService,
31+
) {
32+
suspend fun checkSweepableBalances(): Result<SweepableBalances> = withContext(bgDispatcher) {
33+
runCatching {
34+
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)
35+
?: throw ServiceError.MnemonicNotFound
36+
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
37+
38+
Logger.debug("Checking sweepable balances...", context = TAG)
39+
40+
val balances = ServiceQueue.CORE.background {
41+
checkSweepableBalances(
42+
mnemonicPhrase = mnemonic,
43+
network = Env.network.toCoreNetwork(),
44+
bip39Passphrase = passphrase,
45+
electrumUrl = Env.electrumServerUrl,
46+
)
47+
}
48+
49+
balances.toSweepableBalances()
50+
}
51+
}
52+
53+
suspend fun prepareSweepTransaction(
54+
destinationAddress: String,
55+
feeRateSatsPerVbyte: UInt,
56+
): Result<SweepTransactionPreview> = withContext(bgDispatcher) {
57+
runCatching {
58+
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)
59+
?: throw ServiceError.MnemonicNotFound
60+
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
61+
62+
Logger.debug("Preparing sweep transaction...", context = TAG)
63+
64+
val preview = ServiceQueue.CORE.background {
65+
prepareSweepTransaction(
66+
mnemonicPhrase = mnemonic,
67+
network = Env.network.toCoreNetwork(),
68+
bip39Passphrase = passphrase,
69+
electrumUrl = Env.electrumServerUrl,
70+
destinationAddress = destinationAddress,
71+
feeRateSatsPerVbyte = feeRateSatsPerVbyte,
72+
)
73+
}
74+
75+
preview.toSweepTransactionPreview()
76+
}
77+
}
78+
79+
suspend fun broadcastSweepTransaction(psbt: String): Result<SweepResult> = withContext(bgDispatcher) {
80+
runCatching {
81+
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)
82+
?: throw ServiceError.MnemonicNotFound
83+
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
84+
85+
Logger.debug("Broadcasting sweep transaction...", context = TAG)
86+
87+
val result = ServiceQueue.CORE.background {
88+
broadcastSweepTransaction(
89+
psbt = psbt,
90+
mnemonicPhrase = mnemonic,
91+
network = Env.network.toCoreNetwork(),
92+
bip39Passphrase = passphrase,
93+
electrumUrl = Env.electrumServerUrl,
94+
)
95+
}
96+
97+
result.toSweepResult()
98+
}
99+
}
100+
101+
suspend fun getFeeRates(): Result<FeeRates> = coreService.blocktank.getFees()
102+
103+
suspend fun hasSweepableFunds(): Result<Boolean> = checkSweepableBalances().map { balances ->
104+
val hasFunds = balances.totalBalance > 0u
105+
if (hasFunds) {
106+
Logger.info("Found ${balances.totalBalance} sats to sweep", context = TAG)
107+
} else {
108+
Logger.debug("No sweepable funds found", context = TAG)
109+
}
110+
hasFunds
111+
}
112+
113+
companion object {
114+
private const val TAG = "SweepRepo"
115+
}
116+
}
117+
118+
private fun BitkitCoreSweepableBalances.toSweepableBalances() = SweepableBalances(
119+
legacyBalance = legacyBalance,
120+
legacyUtxosCount = legacyUtxosCount,
121+
p2shBalance = p2shBalance,
122+
p2shUtxosCount = p2shUtxosCount,
123+
taprootBalance = taprootBalance,
124+
taprootUtxosCount = taprootUtxosCount,
125+
)
126+
127+
private fun BitkitCoreSweepTransactionPreview.toSweepTransactionPreview() = SweepTransactionPreview(
128+
psbt = psbt,
129+
estimatedFee = estimatedFee,
130+
amountAfterFees = amountAfterFees,
131+
estimatedVsize = estimatedVsize,
132+
)
133+
134+
private fun BitkitCoreSweepResult.toSweepResult() = SweepResult(
135+
txid = txid,
136+
amountSwept = amountSwept,
137+
)

app/src/main/java/to/bitkit/ui/ContentView.kt

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ import to.bitkit.ui.settings.advanced.AddressViewerScreen
134134
import to.bitkit.ui.settings.advanced.CoinSelectPreferenceScreen
135135
import to.bitkit.ui.settings.advanced.ElectrumConfigScreen
136136
import to.bitkit.ui.settings.advanced.RgsServerScreen
137+
import to.bitkit.ui.settings.advanced.sweep.SweepConfirmScreen
138+
import to.bitkit.ui.settings.advanced.sweep.SweepFeeCustomScreen
139+
import to.bitkit.ui.settings.advanced.sweep.SweepFeeRateScreen
140+
import to.bitkit.ui.settings.advanced.sweep.SweepSettingsScreen
141+
import to.bitkit.ui.settings.advanced.sweep.SweepSuccessScreen
137142
import to.bitkit.ui.settings.appStatus.AppStatusScreen
138143
import to.bitkit.ui.settings.backgroundPayments.BackgroundPaymentsIntroScreen
139144
import to.bitkit.ui.settings.backgroundPayments.BackgroundPaymentsSettings
@@ -169,6 +174,7 @@ import to.bitkit.ui.sheets.LnurlAuthSheet
169174
import to.bitkit.ui.sheets.PinSheet
170175
import to.bitkit.ui.sheets.QuickPayIntroSheet
171176
import to.bitkit.ui.sheets.SendSheet
177+
import to.bitkit.ui.sheets.SweepPromptSheet
172178
import to.bitkit.ui.sheets.UpdateSheet
173179
import to.bitkit.ui.theme.TRANSITION_SHEET_MS
174180
import to.bitkit.ui.utils.AutoReadClipboardHandler
@@ -185,6 +191,7 @@ import to.bitkit.viewmodels.CurrencyViewModel
185191
import to.bitkit.viewmodels.MainScreenEffect
186192
import to.bitkit.viewmodels.RestoreState
187193
import to.bitkit.viewmodels.SettingsViewModel
194+
import to.bitkit.viewmodels.SweepViewModel
188195
import to.bitkit.viewmodels.TransferViewModel
189196
import to.bitkit.viewmodels.WalletViewModel
190197

@@ -333,7 +340,10 @@ fun ContentView(
333340
return
334341
} else if (restoreState is RestoreState.Completed) {
335342
WalletRestoreSuccessView(
336-
onContinue = { walletViewModel.onRestoreContinue() },
343+
onContinue = {
344+
walletViewModel.onRestoreContinue()
345+
appViewModel.checkForSweepableFunds()
346+
},
337347
)
338348
return
339349
}
@@ -397,6 +407,13 @@ fun ContentView(
397407
is Sheet.Backup -> BackupSheet(sheet, onDismiss = { appViewModel.hideSheet() })
398408
is Sheet.LnurlAuth -> LnurlAuthSheet(sheet, appViewModel)
399409
Sheet.ForceTransfer -> ForceTransferSheet(appViewModel, transferViewModel)
410+
Sheet.SweepPrompt -> SweepPromptSheet(
411+
onSweep = {
412+
appViewModel.hideSheet()
413+
navController.navigate(Routes.SweepNav)
414+
},
415+
onCancel = { appViewModel.hideSheet() },
416+
)
400417
is Sheet.Gift -> GiftSheet(sheet, appViewModel)
401418
is Sheet.TimedSheet -> {
402419
when (sheet.type) {
@@ -996,6 +1013,34 @@ private fun NavGraphBuilder.advancedSettings(navController: NavHostController) {
9961013
composableWithDefaultTransitions<Routes.AddressViewer> {
9971014
AddressViewerScreen(navController)
9981015
}
1016+
navigationWithDefaultTransitions<Routes.SweepNav>(
1017+
startDestination = Routes.Sweep,
1018+
) {
1019+
composableWithDefaultTransitions<Routes.Sweep> {
1020+
val parentEntry = remember(it) { navController.getBackStackEntry(Routes.SweepNav) }
1021+
val viewModel = hiltViewModel<SweepViewModel>(parentEntry)
1022+
SweepSettingsScreen(navController, viewModel)
1023+
}
1024+
composableWithDefaultTransitions<Routes.SweepConfirm> {
1025+
val parentEntry = remember(it) { navController.getBackStackEntry(Routes.SweepNav) }
1026+
val viewModel = hiltViewModel<SweepViewModel>(parentEntry)
1027+
SweepConfirmScreen(navController, viewModel)
1028+
}
1029+
composableWithDefaultTransitions<Routes.SweepFeeRate> {
1030+
val parentEntry = remember(it) { navController.getBackStackEntry(Routes.SweepNav) }
1031+
val viewModel = hiltViewModel<SweepViewModel>(parentEntry)
1032+
SweepFeeRateScreen(navController, viewModel)
1033+
}
1034+
composableWithDefaultTransitions<Routes.SweepFeeCustom> {
1035+
val parentEntry = remember(it) { navController.getBackStackEntry(Routes.SweepNav) }
1036+
val viewModel = hiltViewModel<SweepViewModel>(parentEntry)
1037+
SweepFeeCustomScreen(navController, viewModel)
1038+
}
1039+
composableWithDefaultTransitions<Routes.SweepSuccess> {
1040+
val route = it.toRoute<Routes.SweepSuccess>()
1041+
SweepSuccessScreen(navController, amountSats = route.amountSats)
1042+
}
1043+
}
9991044
composableWithDefaultTransitions<Routes.NodeInfo> {
10001045
NodeInfoScreen(navController)
10011046
}
@@ -1689,6 +1734,24 @@ sealed interface Routes {
16891734
@Serializable
16901735
data object AddressViewer : Routes
16911736

1737+
@Serializable
1738+
data object SweepNav : Routes
1739+
1740+
@Serializable
1741+
data object Sweep : Routes
1742+
1743+
@Serializable
1744+
data object SweepConfirm : Routes
1745+
1746+
@Serializable
1747+
data object SweepFeeRate : Routes
1748+
1749+
@Serializable
1750+
data object SweepFeeCustom : Routes
1751+
1752+
@Serializable
1753+
data class SweepSuccess(val amountSats: Long) : Routes
1754+
16921755
@Serializable
16931756
data object AboutSettings : Routes
16941757

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ sealed interface Sheet {
4545
data class LnurlAuth(val domain: String, val lnurl: String, val k1: String) : Sheet
4646
data object ForceTransfer : Sheet
4747
data class Gift(val code: String, val amount: ULong) : Sheet
48+
data object SweepPrompt : Sheet
4849

4950
data class TimedSheet(val type: TimedSheetType) : Sheet
5051
}

app/src/main/java/to/bitkit/ui/onboarding/InitializingWalletView.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import androidx.compose.runtime.remember
2626
import androidx.compose.ui.Alignment
2727
import androidx.compose.ui.Modifier
2828
import androidx.compose.ui.draw.rotate
29+
import androidx.compose.ui.keepScreenOn
2930
import androidx.compose.ui.layout.ContentScale
3031
import androidx.compose.ui.res.painterResource
3132
import androidx.compose.ui.res.stringResource
@@ -54,7 +55,9 @@ fun InitializingWalletView(
5455
isRestoring: Boolean = false,
5556
) {
5657
BoxWithConstraints(
57-
modifier = Modifier.fillMaxSize(),
58+
modifier = Modifier
59+
.fillMaxSize()
60+
.keepScreenOn(),
5861
contentAlignment = Alignment.Center,
5962
) {
6063
val percentage = remember { Animatable(0f) }

0 commit comments

Comments
 (0)