Skip to content

Commit

Permalink
MOB-348 Show Squid routing error on deposit/withdrawal (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang authored Mar 14, 2024
1 parent 6a07df2 commit cf3d37a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package exchange.dydx.trading.feature.receipt.validation
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import exchange.dydx.abacus.output.input.ErrorType
import exchange.dydx.abacus.output.input.TransferInput
import exchange.dydx.abacus.output.input.ValidationError
import exchange.dydx.abacus.protocols.LocalizerProtocol
import exchange.dydx.dydxstatemanager.AbacusStateManagerProtocol
import exchange.dydx.dydxstatemanager.localizedString
import exchange.dydx.trading.common.DydxViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import javax.inject.Inject

@HiltViewModel
Expand All @@ -20,30 +21,33 @@ class DydxValidationViewModel @Inject constructor(
) : ViewModel(), DydxViewModel {

val state: Flow<DydxValidationView.ViewState?> =
abacusStateManager.state.validationErrors
.map {
createViewState(it)
}
combine(
abacusStateManager.state.validationErrors,
abacusStateManager.state.transferInput,
) { validationErrors, transferInput ->
createViewState(validationErrors, transferInput)
}
.distinctUntilChanged()

private fun createViewState(
validationErrors: List<ValidationError>?,
transferInput: TransferInput?,
): DydxValidationView.ViewState {
val transferError = transferInput?.errorMessage ?: transferInput?.errors
val firstBlockingError = validationErrors?.firstOrNull { it.type == ErrorType.error }
val firstWarning = validationErrors?.firstOrNull { it.type == ErrorType.warning }
return DydxValidationView.ViewState(
localizer = localizer,
state = when {
firstBlockingError != null -> DydxValidationView.State.Error
firstWarning != null -> DydxValidationView.State.Warning
transferError?.isNotEmpty() == true -> DydxValidationView.State.Error
else -> DydxValidationView.State.None
},
message = when {
firstBlockingError != null -> firstBlockingError.resources.text?.localizedString(
localizer,
)

firstBlockingError != null -> firstBlockingError.resources.text?.localizedString(localizer)
firstWarning != null -> firstWarning.resources.text?.localizedString(localizer)
transferError?.isNotEmpty() == true -> transferError
else -> null
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,29 @@ class DydxTransferDepositViewModel @Inject constructor(
.distinctUntilChanged()

init {
abacusStateManager.state.transferInput
.map { it?.depositOptions?.chains?.toList() }
.distinctUntilChanged()
.onEach { chains ->
selectedChainFlow.value = chains?.firstOrNull()
combine(
abacusStateManager.state.transferInput
.map { it?.depositOptions?.chains?.toList() }
.distinctUntilChanged(),
abacusStateManager.state.transferInput.map { it?.chain }.distinctUntilChanged(),
) { chains, selected ->
chains?.firstOrNull { it.type == selected } ?: chains?.firstOrNull()
}
.onEach { chain ->
selectedChainFlow.value = chain
}
.launchIn(viewModelScope)

abacusStateManager.state.transferInput
.map { it?.depositOptions?.assets?.toList() }
.distinctUntilChanged()
.onEach { tokens ->
selectedTokenFlow.value = tokens?.firstOrNull()
combine(
abacusStateManager.state.transferInput
.map { it?.depositOptions?.assets?.toList() }
.distinctUntilChanged(),
abacusStateManager.state.transferInput.map { it?.token }.distinctUntilChanged(),
) { tokens, selected ->
tokens?.firstOrNull { it.type == selected } ?: tokens?.firstOrNull()
}
.onEach { token ->
selectedTokenFlow.value = token
}
.launchIn(viewModelScope)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,26 @@ class DydxTransferWithdrawalViewModel @Inject constructor(
.distinctUntilChanged()

init {
abacusStateManager.state.transferInput
.map { it?.withdrawalOptions?.chains?.toList() }
.distinctUntilChanged()
.onEach { chains ->
selectedChainFlow.value = chains?.firstOrNull()
}
combine(
abacusStateManager.state.transferInput
.map { it?.withdrawalOptions?.chains?.toList() }
.distinctUntilChanged(),
abacusStateManager.state.transferInput.map { it?.chain }.distinctUntilChanged(),
) { chains, selected ->
chains?.firstOrNull { it.type == selected } ?: chains?.firstOrNull()
}
.onEach { selectedChainFlow.value = it }
.launchIn(viewModelScope)

abacusStateManager.state.transferInput
.map { it?.withdrawalOptions?.assets?.toList() }
.distinctUntilChanged()
.onEach { tokens ->
selectedTokenFlow.value = tokens?.firstOrNull()
}
combine(
abacusStateManager.state.transferInput
.map { it?.withdrawalOptions?.assets?.toList() }
.distinctUntilChanged(),
abacusStateManager.state.transferInput.map { it?.token }.distinctUntilChanged(),
) { tokens, selected ->
tokens?.firstOrNull { it.type == selected } ?: tokens?.firstOrNull()
}
.onEach { selectedTokenFlow.value = it }
.launchIn(viewModelScope)
}

Expand Down

0 comments on commit cf3d37a

Please sign in to comment.