@@ -42,7 +42,6 @@ import to.bitkit.repositories.WalletRepo
4242import to.bitkit.ui.shared.toast.ToastEventBus
4343import to.bitkit.utils.Logger
4444import javax.inject.Inject
45- import kotlin.math.max
4645import kotlin.math.min
4746import kotlin.math.roundToLong
4847import kotlin.time.Duration.Companion.minutes
@@ -88,7 +87,8 @@ class TransferViewModel @Inject constructor(
8887 // region Spending
8988
9089 fun onConfirmAmount (satsAmount : Long ) {
91- if (_transferValues .value.maxLspBalance == 0uL ) {
90+ val values = blocktankRepo.calculateLiquidityOptions(satsAmount.toULong()).getOrNull()
91+ if (values == null || values.maxLspBalanceSat == 0uL ) {
9292 setTransferEffect(
9393 TransferEffect .ToastError (
9494 title = context.getString(R .string.lightning__spending_amount__error_max__title),
@@ -99,28 +99,20 @@ class TransferViewModel @Inject constructor(
9999 )
100100 return
101101 }
102+
103+ val lspBalance = maxOf(values.defaultLspBalanceSat, values.minLspBalanceSat)
104+
102105 viewModelScope.launch {
103106 _spendingUiState .update { it.copy(isLoading = true ) }
104107
105- val minAmount = getMinAmountToPurchase(satsAmount)
106- if (satsAmount < minAmount) {
107- setTransferEffect(
108- TransferEffect .ToastError (
109- title = context.getString(R .string.lightning__spending_amount__error_min__title),
110- description = context.getString(
111- R .string.lightning__spending_amount__error_min__description
112- ).replace(" {amount}" , " $minAmount " ),
113- )
114- )
115- _spendingUiState .update { it.copy(isLoading = false ) }
116- return @launch
117- }
118-
119108 withTimeoutOrNull(1 .minutes) {
120109 isNodeRunning.first { it }
121110 }
122111
123- blocktankRepo.createOrder(satsAmount.toULong())
112+ blocktankRepo.createOrder(
113+ spendingBalanceSats = satsAmount.toULong(),
114+ receivingBalanceSats = lspBalance,
115+ )
124116 .onSuccess { order ->
125117 settingsStore.update { it.copy(lightningSetupStep = 0 ) }
126118 onOrderCreated(order)
@@ -269,11 +261,6 @@ class TransferViewModel @Inject constructor(
269261 }
270262 }
271263
272- private suspend fun getMinAmountToPurchase (satsAmount : Long = 0L): Long {
273- val fee = lightningRepo.calculateTotalFee(satsAmount.toULong()).getOrNull() ? : 0u
274- return max((fee + maxLspFee).toLong(), Env .TransactionDefaults .dustLimit.toLong())
275- }
276-
277264 private suspend fun onOrderCreated (order : IBtOrder ) {
278265 settingsStore.update { it.copy(lightningSetupStep = 0 ) }
279266 _spendingUiState .update { it.copy(order = order, isAdvanced = false , defaultOrder = null ) }
@@ -355,7 +342,7 @@ class TransferViewModel @Inject constructor(
355342 // region Balance Calc
356343
357344 fun updateTransferValues (clientBalanceSat : ULong ) {
358- val options = blocktankRepo.calculateLiquidityOptions(clientBalanceSat)
345+ val options = blocktankRepo.calculateLiquidityOptions(clientBalanceSat).getOrNull()
359346 _transferValues .value = if (options != null ) {
360347 TransferValues (
361348 defaultLspBalance = options.defaultLspBalanceSat,
0 commit comments