@@ -21,6 +21,7 @@ import jp.co.soramitsu.common.utils.applyFiatRate
2121import jp.co.soramitsu.common.utils.format
2222import jp.co.soramitsu.common.utils.formatAsCurrency
2323import jp.co.soramitsu.common.utils.orZero
24+ import jp.co.soramitsu.common.validation.AmountTooLowToStakeException
2425import jp.co.soramitsu.common.validation.StakeInsufficientBalanceException
2526import jp.co.soramitsu.feature_staking_impl.R
2627import jp.co.soramitsu.runtime.multiNetwork.chain.model.Chain
@@ -39,6 +40,7 @@ import kotlinx.coroutines.flow.combine
3940import kotlinx.coroutines.flow.flow
4041import kotlinx.coroutines.flow.map
4142import kotlinx.coroutines.flow.stateIn
43+ import kotlinx.coroutines.launch
4244
4345@HiltViewModel
4446class SetupStakingPoolViewModel @Inject constructor(
@@ -144,35 +146,40 @@ class SetupStakingPoolViewModel @Inject constructor(
144146 val setupFlow = stakingPoolSharedStateProvider.requireJoinState
145147 val amount = enteredAmountFlow.value.toBigDecimalOrNull().orZero()
146148
147- isValid(amount).fold({
148- stakingPoolSharedStateProvider.joinFlowState.set(setupFlow.copy(amount = amount))
149- router.openSelectPool()
150- }, { throwable ->
151- val message =
152- throwable.localizedMessage ? : throwable.message ? : resourceManager.getString(R .string.common_undefined_error_message)
153- val errorAlertViewState = (throwable as ? ValidationException )?.let { (title, message) ->
154- AlertViewState (
155- title = title,
149+ viewModelScope.launch {
150+ isValid(amount).fold({
151+ stakingPoolSharedStateProvider.joinFlowState.set(setupFlow.copy(amount = amount))
152+ router.openSelectPool()
153+ }, { throwable ->
154+ val message =
155+ throwable.localizedMessage ? : throwable.message ? : resourceManager.getString(R .string.common_undefined_error_message)
156+ val errorAlertViewState = (throwable as ? ValidationException )?.let { (title, message) ->
157+ AlertViewState (
158+ title = title,
159+ message = message,
160+ buttonText = resourceManager.getString(R .string.common_got_it),
161+ iconRes = R .drawable.ic_status_warning_16
162+ )
163+ } ? : AlertViewState (
164+ title = resourceManager.getString(R .string.common_error_general_title),
156165 message = message,
157166 buttonText = resourceManager.getString(R .string.common_got_it),
158167 iconRes = R .drawable.ic_status_warning_16
159168 )
160- } ? : AlertViewState (
161- title = resourceManager.getString(R .string.common_error_general_title),
162- message = message,
163- buttonText = resourceManager.getString(R .string.common_got_it),
164- iconRes = R .drawable.ic_status_warning_16
165- )
166- router.openAlert(errorAlertViewState)
167- })
169+ router.openAlert(errorAlertViewState)
170+ })
171+ }
168172 }
169173
170- private fun isValid (amount : BigDecimal ): Result <Any > {
174+ private suspend fun isValid (amount : BigDecimal ): Result <Any > {
171175 val amountInPlanks = asset.token.planksFromAmount(amount)
172176 val transferableInPlanks = asset.token.planksFromAmount(asset.transferable)
177+ val minToJoinInPlanks = stakingPoolInteractor.getMinToJoinPool(chain.id)
178+ val minToJoinFormatted = asset.token.amountFromPlanks(minToJoinInPlanks).formatTokenAmount(asset.token.configuration)
173179
174180 return when {
175181 amountInPlanks >= transferableInPlanks -> Result .failure(StakeInsufficientBalanceException (resourceManager))
182+ amountInPlanks < minToJoinInPlanks -> Result .failure(AmountTooLowToStakeException (resourceManager, minToJoinFormatted))
176183 else -> Result .success(Unit )
177184 }
178185 }
0 commit comments