@@ -27,6 +27,7 @@ import org.lightningdevkit.ldknode.ChannelDetails
2727import to.bitkit.R
2828import to.bitkit.data.CacheStore
2929import to.bitkit.data.SettingsStore
30+ import to.bitkit.env.Env
3031import to.bitkit.models.TransactionSpeed
3132import to.bitkit.repositories.BlocktankRepo
3233import to.bitkit.repositories.CurrencyRepo
@@ -71,6 +72,7 @@ class TransferViewModel @Inject constructor(
7172 val transferEffects = MutableSharedFlow <TransferEffect >()
7273 fun setTransferEffect (effect : TransferEffect ) = viewModelScope.launch { transferEffects.emit(effect) }
7374 var retryTimes = 0
75+ var maxLspFee = 0uL
7476
7577 // region Spending
7678
@@ -119,10 +121,23 @@ class TransferViewModel @Inject constructor(
119121 )
120122 return
121123 }
124+ viewModelScope.launch {
125+ _spendingUiState .update { it.copy(isLoading = true ) }
122126
123- _spendingUiState .update { it.copy(isLoading = true ) }
127+ val minAmount = getMinAmountToPurchase()
128+ if (_spendingUiState .value.satsAmount < minAmount) {
129+ setTransferEffect(
130+ TransferEffect .ToastError (
131+ title = context.getString(R .string.lightning__spending_amount__error_min__title),
132+ description = context.getString(
133+ R .string.lightning__spending_amount__error_min__description
134+ ).replace(" {amount}" , " $minAmount " ),
135+ )
136+ )
137+ _spendingUiState .update { it.copy(overrideSats = minAmount, isLoading = false ) }
138+ return @launch
139+ }
124140
125- viewModelScope.launch {
126141 blocktankRepo.createOrder(_spendingUiState .value.satsAmount.toULong())
127142 .onSuccess { order ->
128143 onOrderCreated(order)
@@ -228,6 +243,11 @@ class TransferViewModel @Inject constructor(
228243 }
229244 }
230245
246+ private suspend fun getMinAmountToPurchase (): Long {
247+ val fee = lightningRepo.calculateTotalFee(_spendingUiState .value.satsAmount.toULong()).getOrNull() ? : 0u
248+ return max((fee + maxLspFee).toLong(), Env .TransactionDefaults .dustLimit.toLong())
249+ }
250+
231251 private fun onOrderCreated (order : IBtOrder ) {
232252 _spendingUiState .update { it.copy(order = order, isAdvanced = false , defaultOrder = null ) }
233253 setTransferEffect(TransferEffect .OnOrderCreated )
@@ -246,7 +266,7 @@ class TransferViewModel @Inject constructor(
246266 receivingBalanceSats = _transferValues .value.maxLspBalance
247267 ).onSuccess { estimate ->
248268 retryTimes = 0
249- val maxLspFee = estimate.feeSat
269+ maxLspFee = estimate.feeSat
250270
251271 // Calculate the available balance to send after LSP fee
252272 val balanceAfterLspFee = availableAmount - maxLspFee
0 commit comments