Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOB-348 Show Squid routing error on deposit/withdrawal #28

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading