|
| 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 | +) |
0 commit comments