Skip to content

Commit

Permalink
Merge pull request #1690 from novasamatech/fix/swipe-gov-proxy-chain-…
Browse files Browse the repository at this point in the history
…restrictions

Block swipe gov for unsupported chains when proxied is using
  • Loading branch information
antonijzelinskij authored Oct 15, 2024
2 parents 8a34eb8 + 9af72ee commit 127f873
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.novafoundation.nova.feature_governance_impl.domain.referendum.tinderg

import io.novafoundation.nova.common.domain.filterLoaded
import io.novafoundation.nova.common.utils.flowOfAll
import io.novafoundation.nova.common.validation.ValidationSystem
import io.novafoundation.nova.feature_account_api.domain.interfaces.AccountRepository
import io.novafoundation.nova.feature_governance_api.data.model.TinderGovBasketItem
import io.novafoundation.nova.feature_governance_api.data.model.VotingPower
Expand All @@ -17,6 +18,8 @@ import io.novafoundation.nova.feature_governance_impl.data.repository.tindergov.
import io.novafoundation.nova.feature_governance_impl.domain.referendum.details.call.ReferendumPreImageParser
import io.novafoundation.nova.feature_governance_impl.domain.referendum.list.ReferendaSharedComputation
import io.novafoundation.nova.feature_governance_impl.domain.referendum.list.filtering.ReferendaFilteringProvider
import io.novafoundation.nova.feature_governance_impl.domain.referendum.tindergov.validation.StartStakingLandingValidationSystem
import io.novafoundation.nova.feature_governance_impl.domain.referendum.tindergov.validation.startSwipeGovValidation
import io.novafoundation.nova.feature_wallet_api.domain.AssetUseCase
import io.novafoundation.nova.feature_wallet_api.domain.getCurrentAsset
import io.novafoundation.nova.feature_wallet_api.domain.model.Asset
Expand Down Expand Up @@ -54,6 +57,8 @@ interface TinderGovInteractor {
suspend fun getVotingPowerState(): VotingPowerState

suspend fun awaitAllItemsVoted(coroutineScope: CoroutineScope, basket: List<TinderGovBasketItem>)

fun startSwipeGovValidationSystem(): StartStakingLandingValidationSystem
}

class RealTinderGovInteractor(
Expand Down Expand Up @@ -130,6 +135,10 @@ class RealTinderGovInteractor(
}.first()
}

override fun startSwipeGovValidationSystem(): StartStakingLandingValidationSystem {
return ValidationSystem.startSwipeGovValidation()
}

private fun TinderGovBasketItem.isItemNotAvailableToVote(
availableToVoteReferenda: Set<ReferendumId>,
asset: Asset
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.novafoundation.nova.feature_governance_impl.domain.referendum.tindergov.validation

import io.novafoundation.nova.common.validation.ValidationSystem
import io.novafoundation.nova.feature_account_api.domain.validation.hasChainAccount

typealias StartStakingLandingValidationSystem = ValidationSystem<StartSwipeGovValidationPayload, StartSwipeGovValidationFailure>

fun ValidationSystem.Companion.startSwipeGovValidation(): StartStakingLandingValidationSystem = ValidationSystem {
hasChainAccount(
chain = { it.chain },
metaAccount = { it.metaAccount },
error = StartSwipeGovValidationFailure::NoChainAccountFound
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.novafoundation.nova.feature_governance_impl.domain.referendum.tindergov.validation

import io.novafoundation.nova.feature_account_api.domain.model.MetaAccount
import io.novafoundation.nova.feature_account_api.domain.validation.NoChainAccountFoundError
import io.novafoundation.nova.runtime.multiNetwork.chain.model.Chain

sealed class StartSwipeGovValidationFailure {

class NoChainAccountFound(
override val chain: Chain,
override val account: MetaAccount,
override val addAccountState: NoChainAccountFoundError.AddAccountState
) : StartSwipeGovValidationFailure(), NoChainAccountFoundError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.novafoundation.nova.feature_governance_impl.domain.referendum.tindergov.validation

import io.novafoundation.nova.feature_account_api.domain.model.MetaAccount
import io.novafoundation.nova.runtime.multiNetwork.chain.model.Chain

class StartSwipeGovValidationPayload(
val chain: Chain,
val metaAccount: MetaAccount
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.recyclerview.widget.ConcatAdapter
import coil.ImageLoader
import io.novafoundation.nova.common.di.FeatureUtils
import io.novafoundation.nova.common.domain.dataOrNull
import io.novafoundation.nova.common.mixin.impl.observeValidations
import io.novafoundation.nova.feature_governance_api.di.GovernanceFeatureApi
import io.novafoundation.nova.feature_governance_impl.R
import io.novafoundation.nova.feature_governance_impl.di.GovernanceFeatureComponent
Expand Down Expand Up @@ -50,6 +51,8 @@ class ReferendaListFragment : BaseReferendaListFragment<ReferendaListViewModel>(

override fun subscribe(viewModel: ReferendaListViewModel) {
subscribeOnAssetClick(viewModel.assetSelectorMixin, imageLoader)
observeValidations(viewModel)

subscribeOnAssetChange(viewModel.assetSelectorMixin) {
referendaHeaderAdapter.setAsset(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import io.novafoundation.nova.common.domain.ExtendedLoadingState
import io.novafoundation.nova.common.domain.dataOrNull
import io.novafoundation.nova.common.list.toListWithHeaders
import io.novafoundation.nova.common.domain.mapLoading
import io.novafoundation.nova.common.mixin.api.Validatable
import io.novafoundation.nova.common.resources.ResourceManager
import io.novafoundation.nova.common.utils.LOG_TAG
import io.novafoundation.nova.common.utils.combineToPair
import io.novafoundation.nova.common.utils.firstLoaded
import io.novafoundation.nova.common.utils.formatting.format
import io.novafoundation.nova.common.utils.inBackground
import io.novafoundation.nova.common.utils.withItemScope
import io.novafoundation.nova.common.validation.ValidationExecutor
import io.novafoundation.nova.common.view.PlaceholderModel
import io.novafoundation.nova.core.updater.UpdateSystem
import io.novafoundation.nova.feature_account_api.domain.interfaces.SelectedAccountUseCase
Expand All @@ -34,10 +36,13 @@ import io.novafoundation.nova.feature_governance_impl.presentation.GovernanceRou
import io.novafoundation.nova.feature_governance_impl.presentation.referenda.common.ReferendumFormatter
import io.novafoundation.nova.feature_governance_impl.presentation.referenda.common.list.ReferendaListStateModel
import io.novafoundation.nova.feature_governance_api.presentation.referenda.details.ReferendumDetailsPayload
import io.novafoundation.nova.feature_governance_impl.domain.referendum.tindergov.TinderGovInteractor
import io.novafoundation.nova.feature_governance_impl.domain.referendum.tindergov.validation.StartSwipeGovValidationPayload
import io.novafoundation.nova.feature_governance_impl.presentation.referenda.list.model.ReferendaGroupModel
import io.novafoundation.nova.feature_governance_impl.presentation.referenda.list.model.ReferendumModel
import io.novafoundation.nova.feature_governance_impl.presentation.referenda.list.model.TinderGovBannerModel
import io.novafoundation.nova.feature_governance_impl.presentation.referenda.list.model.toReferendumDetailsPrefilledData
import io.novafoundation.nova.feature_governance_impl.presentation.referenda.list.validation.handleStartSwipeGovValidationFailure
import io.novafoundation.nova.feature_governance_impl.presentation.view.GovernanceLocksModel
import io.novafoundation.nova.feature_wallet_api.domain.model.Asset
import io.novafoundation.nova.feature_wallet_api.presentation.mixin.assetSelector.AssetSelectorFactory
Expand All @@ -64,8 +69,13 @@ class ReferendaListViewModel(
private val governanceRouter: GovernanceRouter,
private val referendumFormatter: ReferendumFormatter,
private val governanceDAppsInteractor: GovernanceDAppsInteractor,
private val referendaSummaryInteractor: ReferendaSummaryInteractor
) : BaseViewModel(), WithAssetSelector {
private val referendaSummaryInteractor: ReferendaSummaryInteractor,
private val tinderGovInteractor: TinderGovInteractor,
private val selectedMetaAccountUseCase: SelectedAccountUseCase,
private val validationExecutor: ValidationExecutor,
) : BaseViewModel(),
WithAssetSelector,
Validatable by validationExecutor {

override val assetSelectorMixin = assetSelectorFactory.create(
scope = this,
Expand Down Expand Up @@ -148,8 +158,25 @@ class ReferendaListViewModel(
governanceRouter.openReferendum(payload)
}

fun openTinderGovCards() {
governanceRouter.openTinderGovCards()
fun openTinderGovCards() = launch {
val payload = StartSwipeGovValidationPayload(
chain = selectedAssetSharedState.chain(),
metaAccount = selectedMetaAccountUseCase.getSelectedMetaAccount()
)

validationExecutor.requireValid(
validationSystem = tinderGovInteractor.startSwipeGovValidationSystem(),
payload = payload,
validationFailureTransformerCustom = { validationFailure, _ ->
handleStartSwipeGovValidationFailure(
resourceManager,
validationFailure,
governanceRouter
)
}
) {
governanceRouter.openTinderGovCards()
}
}

private fun mapLocksOverviewToUi(locksOverview: GovernanceLocksOverview?, asset: Asset): GovernanceLocksModel? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import dagger.multibindings.IntoMap
import io.novafoundation.nova.common.di.viewmodel.ViewModelKey
import io.novafoundation.nova.common.di.viewmodel.ViewModelModule
import io.novafoundation.nova.common.resources.ResourceManager
import io.novafoundation.nova.common.validation.ValidationExecutor
import io.novafoundation.nova.core.updater.UpdateSystem
import io.novafoundation.nova.feature_account_api.domain.interfaces.SelectedAccountUseCase
import io.novafoundation.nova.feature_governance_api.domain.referendum.list.ReferendaListInteractor
import io.novafoundation.nova.feature_governance_impl.domain.summary.ReferendaSummaryInteractor
import io.novafoundation.nova.feature_governance_impl.data.GovernanceSharedState
import io.novafoundation.nova.feature_governance_impl.domain.dapp.GovernanceDAppsInteractor
import io.novafoundation.nova.feature_governance_impl.domain.filters.ReferendaFiltersInteractor
import io.novafoundation.nova.feature_governance_impl.domain.referendum.tindergov.TinderGovInteractor
import io.novafoundation.nova.feature_governance_impl.presentation.GovernanceRouter
import io.novafoundation.nova.feature_governance_impl.presentation.referenda.common.ReferendumFormatter
import io.novafoundation.nova.feature_governance_impl.presentation.referenda.list.ReferendaListViewModel
Expand All @@ -38,7 +40,10 @@ class ReferendaListModule {
governanceRouter: GovernanceRouter,
referendumFormatter: ReferendumFormatter,
governanceDAppsInteractor: GovernanceDAppsInteractor,
summaryInteractor: ReferendaSummaryInteractor
summaryInteractor: ReferendaSummaryInteractor,
tinderGovInteractor: TinderGovInteractor,
selectedMetaAccountUseCase: SelectedAccountUseCase,
validationExecutor: ValidationExecutor,
): ViewModel {
return ReferendaListViewModel(
assetSelectorFactory = assetSelectorFactory,
Expand All @@ -51,7 +56,10 @@ class ReferendaListModule {
governanceRouter = governanceRouter,
referendumFormatter = referendumFormatter,
governanceDAppsInteractor = governanceDAppsInteractor,
referendaSummaryInteractor = summaryInteractor
referendaSummaryInteractor = summaryInteractor,
tinderGovInteractor = tinderGovInteractor,
selectedMetaAccountUseCase = selectedMetaAccountUseCase,
validationExecutor = validationExecutor
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.novafoundation.nova.feature_governance_impl.presentation.referenda.list.validation

import io.novafoundation.nova.common.resources.ResourceManager
import io.novafoundation.nova.common.validation.TransformedFailure
import io.novafoundation.nova.common.validation.ValidationStatus
import io.novafoundation.nova.feature_account_api.domain.validation.handleChainAccountNotFound
import io.novafoundation.nova.feature_governance_impl.R
import io.novafoundation.nova.feature_governance_impl.domain.referendum.tindergov.validation.StartSwipeGovValidationFailure
import io.novafoundation.nova.feature_governance_impl.presentation.GovernanceRouter

fun handleStartSwipeGovValidationFailure(
resourceManager: ResourceManager,
validationStatus: ValidationStatus.NotValid<StartSwipeGovValidationFailure>,
router: GovernanceRouter
): TransformedFailure {
return when (val reason = validationStatus.reason) {
is StartSwipeGovValidationFailure.NoChainAccountFound -> handleChainAccountNotFound(
failure = reason,
addAccountDescriptionRes = R.string.common_network_not_supported,
resourceManager = resourceManager,
goToWalletDetails = { router.openWalletDetails(reason.account.id) }
)
}
}

0 comments on commit 127f873

Please sign in to comment.