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