Updated on 2026-08-14

This commit is contained in:
Tangem 2021-11-11 02:32:19 +03:00
parent 146bbc6767
commit 560bf8c98d
6 changed files with 71 additions and 40 deletions

View file

@ -1,19 +1,38 @@
package com.tangem.tap.domain.extensions
import com.tangem.blockchain.common.Blockchain
import com.tangem.tap.features.wallet.redux.Currency
import com.tangem.tap.network.moonpay.MoonpayStatus
/**
[REDACTED_AUTHOR]
*/
fun MoonpayStatus.buyIsAllowed(blockchain: Blockchain): Boolean {
if (blockchain == Blockchain.Unknown || blockchain == Blockchain.BSC) return false
fun MoonpayStatus.buyIsAllowed(currency: Currency): Boolean {
if (!isBuyAllowed) return false
return isBuyAllowed && availableToBuy.contains(blockchain.currency)
return when (currency) {
is Currency.Blockchain -> {
if (currency.blockchain == Blockchain.Unknown || currency.blockchain == Blockchain.BSC) {
false
} else {
availableToBuy.contains(currency.currencySymbol)
}
}
is Currency.Token -> false
}
}
fun MoonpayStatus.sellIsAllowed(blockchain: Blockchain): Boolean {
if (blockchain == Blockchain.Unknown || blockchain == Blockchain.BSC) return false
fun MoonpayStatus.sellIsAllowed(currency: Currency): Boolean {
if (!isSellAllowed) return false
return isSellAllowed && availableToSell.contains(blockchain.currency)
return when (currency) {
is Currency.Blockchain -> {
if (currency.blockchain == Blockchain.Unknown || currency.blockchain == Blockchain.BSC) {
false
} else {
availableToSell.contains(currency.currencySymbol)
}
}
is Currency.Token -> false
}
}

View file

@ -27,7 +27,7 @@ data class OnboardingNoteState(
get() = steps.indexOf(currentStep)
val isBuyAllowed: Boolean by ReadOnlyProperty<Any, Boolean> { thisRef, property ->
store.state.globalState.moonpayStatus?.buyIsAllowed(walletBalance.currency.blockchain) ?: false
store.state.globalState.moonpayStatus?.buyIsAllowed(walletBalance.currency) ?: false
}
}

View file

@ -57,7 +57,7 @@ data class TwinCardsState(
get() = currentStep == TwinCardsStep.CreateSecondWallet || currentStep == TwinCardsStep.CreateThirdWallet
val isBuyAllowed: Boolean by ReadOnlyProperty<Any, Boolean> { thisRef, property ->
store.state.globalState.moonpayStatus?.buyIsAllowed(walletBalance.currency.blockchain) ?: false
store.state.globalState.moonpayStatus?.buyIsAllowed(walletBalance.currency) ?: false
}
}

View file

@ -160,6 +160,14 @@ data class WalletState(
return updatedWallets + remainingWallets
}
fun updateTradeCryptoState(moonpayStatus: MoonpayStatus?, walletData: WalletData): WalletData {
return walletData.copy(tradeCryptoState = TradeCryptoState.from(moonpayStatus, walletData))
}
fun updateTradeCryptoState(moonpayStatus: MoonpayStatus?, walletDataList: List<WalletData>): List<WalletData> {
return walletDataList.map { it.copy(tradeCryptoState = TradeCryptoState.from(moonpayStatus, it)) }
}
fun addWalletManagers(newWalletManagers: List<WalletManager>): WalletState {
val updatedWalletManagers = this.walletManagers +
newWalletManagers.filterNot { this.blockchains.contains(it.wallet.blockchain) }
@ -220,9 +228,9 @@ data class TradeCryptoState(
companion object {
fun from(moonpayStatus: MoonpayStatus?, walletData: WalletData): TradeCryptoState {
val status = moonpayStatus ?: return walletData.tradeCryptoState
val blockchain = walletData.currency?.blockchain ?: return walletData.tradeCryptoState
val currency = walletData.currency
return TradeCryptoState(status.sellIsAllowed(blockchain), status.buyIsAllowed(blockchain))
return TradeCryptoState(status.sellIsAllowed(currency), status.buyIsAllowed(currency))
}
}
}

View file

@ -10,10 +10,7 @@ import com.tangem.tap.common.extensions.toFormattedFiatValue
import com.tangem.tap.domain.getFirstToken
import com.tangem.tap.features.wallet.models.removeUnknownTransactions
import com.tangem.tap.features.wallet.models.toPendingTransactions
import com.tangem.tap.features.wallet.redux.Currency
import com.tangem.tap.features.wallet.redux.ProgressState
import com.tangem.tap.features.wallet.redux.WalletMainButton
import com.tangem.tap.features.wallet.redux.WalletState
import com.tangem.tap.features.wallet.redux.*
import com.tangem.tap.features.wallet.ui.BalanceStatus
import com.tangem.tap.features.wallet.ui.BalanceWidgetData
import com.tangem.tap.features.wallet.ui.TokenData
@ -22,18 +19,18 @@ import java.math.RoundingMode
class OnWalletLoadedReducer {
fun reduce(wallet: Wallet, walletState: WalletState, topUpAllowed: Boolean? = null): WalletState {
fun reduce(wallet: Wallet, walletState: WalletState): WalletState {
return if (!walletState.isMultiwalletAllowed) {
onSingleWalletLoaded(wallet, walletState, topUpAllowed)
onSingleWalletLoaded(wallet, walletState)
} else {
onMultiWalletLoaded(wallet, walletState, topUpAllowed)
onMultiWalletLoaded(wallet, walletState)
}
}
private fun onMultiWalletLoaded(
wallet: Wallet, walletState: WalletState, topUpAllowed: Boolean? = null
): WalletState {
private fun onMultiWalletLoaded(wallet: Wallet, walletState: WalletState): WalletState {
val fiatCurrencySymbol = store.state.globalState.appCurrency
val moonpayStatus = store.state.globalState.moonpayStatus
val amount = wallet.amounts[AmountType.Coin]?.value
if (walletState.getWalletData(wallet.blockchain) == null) {
return walletState
@ -64,7 +61,8 @@ class OnWalletLoadedReducer {
),
pendingTransactions = pendingTransactions.removeUnknownTransactions(),
mainButton = WalletMainButton.SendButton(sendButtonEnabled),
currency = Currency.Blockchain(wallet.blockchain)
currency = Currency.Blockchain(wallet.blockchain),
tradeCryptoState = TradeCryptoState.from(moonpayStatus, walletData)
)
val tokens = wallet.getTokens().mapNotNull { token ->
@ -88,7 +86,9 @@ class OnWalletLoadedReducer {
fiatAmountFormatted = tokenFiatAmount?.toFormattedFiatValue(fiatCurrencySymbol)
),
pendingTransactions = tokenPendingTransactions.removeUnknownTransactions(),
mainButton = WalletMainButton.SendButton(sendButtonEnabled)
mainButton = WalletMainButton.SendButton(sendButtonEnabled),
tradeCryptoState = TradeCryptoState.from(moonpayStatus, tokenWalletData)
)
}
val newWallets = (tokens + newWalletData).mapNotNull { it }
@ -104,12 +104,12 @@ class OnWalletLoadedReducer {
)
}
private fun onSingleWalletLoaded(
wallet: Wallet, walletState: WalletState, topUpAllowed: Boolean? = null
): WalletState {
private fun onSingleWalletLoaded(wallet: Wallet, walletState: WalletState): WalletState {
if (wallet.blockchain != walletState.primaryBlockchain) return walletState
val fiatCurrencySymbol = store.state.globalState.appCurrency
val moonpayStatus = store.state.globalState.moonpayStatus
val token = wallet.getFirstToken()
val tokenData = if (token != null) {
val tokenAmount = wallet.getTokenAmount(token)
@ -155,7 +155,8 @@ class OnWalletLoadedReducer {
fiatAmount = fiatAmountRaw
),
pendingTransactions = pendingTransactions.removeUnknownTransactions(),
mainButton = WalletMainButton.SendButton(sendButtonEnabled)
mainButton = WalletMainButton.SendButton(sendButtonEnabled),
tradeCryptoState = TradeCryptoState.from(moonpayStatus, walletState.primaryWallet)
)
val wallets = walletData?.let { listOf(walletData) } ?: emptyList()
return walletState.copy(

View file

@ -16,6 +16,7 @@ import com.tangem.tap.domain.twins.TwinCardNumber
import com.tangem.tap.features.wallet.redux.*
import com.tangem.tap.features.wallet.ui.BalanceStatus
import com.tangem.tap.features.wallet.ui.BalanceWidgetData
import com.tangem.tap.store
import org.rekotlin.Action
import java.math.BigDecimal
import java.math.RoundingMode
@ -33,6 +34,7 @@ private fun internalReduce(action: Action, state: AppState): WalletState {
if (action !is WalletAction) return state.walletState
val moonpayStatus = store.state.globalState.moonpayStatus
var newState = state.walletState
when (action) {
@ -112,7 +114,7 @@ private fun internalReduce(action: Action, state: AppState): WalletState {
val walletManager = newState.getWalletManager(action.blockchain) ?: return newState
val blockchain = walletManager.wallet.blockchain
val currencies = listOf(Currency.Blockchain(blockchain)) +
walletManager.cardTokens.map { Currency.Token(it) }
walletManager.cardTokens.map { Currency.Token(it) }
val newWallets = newState.wallets.filter { currencies.contains(it.currency) }
.map { wallet ->
wallet.copy(
@ -126,7 +128,7 @@ private fun internalReduce(action: Action, state: AppState): WalletState {
)
}
val wallets = newState.replaceSomeWallets(newWallets)
newState = newState.copy(wallets = wallets)
newState = newState.copy(wallets = newState.updateTradeCryptoState(moonpayStatus, wallets))
}
}
is WalletAction.LoadWallet.Success -> newState =
@ -151,7 +153,7 @@ private fun internalReduce(action: Action, state: AppState): WalletState {
}
newState = newState.copy(
state = progressState,
wallets = wallets
wallets = newState.updateTradeCryptoState(moonpayStatus, wallets)
)
}
is WalletAction.LoadWallet.Failure -> {
@ -185,7 +187,8 @@ private fun internalReduce(action: Action, state: AppState): WalletState {
ProgressState.Done
}
newState = newState.copy(
state = progressState, wallets = wallets
state = progressState,
wallets = newState.updateTradeCryptoState(moonpayStatus, wallets)
)
}
is WalletAction.SetArtworkId -> {
@ -201,11 +204,11 @@ private fun internalReduce(action: Action, state: AppState): WalletState {
newState = setNewFiatRate(action.fiatRate, state.globalState.appCurrency, newState)
is WalletAction.LoadArtwork -> {
val artworkUrl = action.card.getArtworkUrl(action.artworkId)
?: when (state.twinCardsState.cardNumber) {
TwinCardNumber.First -> Artwork.TWIN_CARD_1
TwinCardNumber.Second -> Artwork.TWIN_CARD_2
else -> Artwork.DEFAULT_IMG_URL
}
?: when (state.twinCardsState.cardNumber) {
TwinCardNumber.First -> Artwork.TWIN_CARD_1
TwinCardNumber.Second -> Artwork.TWIN_CARD_2
else -> Artwork.DEFAULT_IMG_URL
}
newState = newState.copy(cardImage = Artwork(artworkId = artworkUrl))
}
is WalletAction.ShowDialog.SignedHashesMultiWalletDialog -> {
@ -254,7 +257,7 @@ fun createAddressList(wallet: Wallet?, walletAddresses: WalletAddresses? = null)
var indexOfSelectedWallet = 0
walletAddresses?.let {
val index =
listOfAddressData.indexOfFirst { it.address == walletAddresses.selectedAddress.address }
listOfAddressData.indexOfFirst { it.address == walletAddresses.selectedAddress.address }
if (index != -1) indexOfSelectedWallet = index
}
return WalletAddresses(listOfAddressData[indexOfSelectedWallet], listOfAddressData)
@ -265,10 +268,10 @@ fun Wallet.createAddressesData(): List<AddressData> {
// put a defaultAddress at the first place
addresses.forEach {
val addressData = AddressData(
it.value,
it.type,
getShareUri(it.value),
getExploreUrl(it.value)
it.value,
it.type,
getShareUri(it.value),
getExploreUrl(it.value)
)
if (it.type == blockchain.defaultAddressType()) {
listOfAddressData.add(0, addressData)