Skip to content

Commit

Permalink
Merge branch 'develop' of github_p2p:p2p-org/key-app-android into fea…
Browse files Browse the repository at this point in the history
…ture/ETH-951-display-full-sending-amount-in-button
  • Loading branch information
eduardmaximovich committed Feb 13, 2024
2 parents 508edd0 + b2d545d commit 66c8ee2
Show file tree
Hide file tree
Showing 41 changed files with 439 additions and 397 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.p2p.core.common.TextContainer
import org.p2p.core.common.di.AppScope
import org.p2p.core.model.CurrencyMode
import org.p2p.core.token.Token
import org.p2p.core.utils.SOL_DECIMALS
import org.p2p.core.utils.asNegativeUsdTransaction
import org.p2p.core.utils.asUsd
import org.p2p.core.utils.formatTokenWithSymbol
Expand Down Expand Up @@ -300,7 +301,7 @@ class BridgeSendPresenter(
}
is CurrencyMode.Token -> {
val tokenSymbol = mode.symbol
amountPlusTotalFee.formatTokenWithSymbol(tokenSymbol)
amountPlusTotalFee.formatTokenWithSymbol(tokenSymbol, SOL_DECIMALS)
}
else -> {
emptyString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.onCompletion
import org.p2p.wallet.R
import org.p2p.wallet.common.feature_toggles.toggles.remote.SendViaLinkFeatureToggle
import org.p2p.wallet.intercom.IntercomDeeplinkManager
import org.p2p.wallet.notification.NotificationType
import org.p2p.wallet.root.RootActivity
Expand All @@ -25,7 +26,8 @@ class AppDeeplinksManager(
private val context: Context,
private val swapDeeplinkHandler: SwapDeeplinkHandler,
private val referralDeeplinkHandler: ReferralDeeplinkHandler,
private val intercomDeeplinkManager: IntercomDeeplinkManager
private val intercomDeeplinkManager: IntercomDeeplinkManager,
private val sendViaLinkFeatureToggle: SendViaLinkFeatureToggle
) {
companion object {
const val NOTIFICATION_TYPE = "eventType"
Expand Down Expand Up @@ -114,6 +116,8 @@ class AppDeeplinksManager(
* or keyapp://t/... if came from website
*/
private fun isTransferDeeplink(data: Uri): Boolean {
if (!sendViaLinkFeatureToggle.isFeatureEnabled) return false

val transferHostMain = context.getString(R.string.transfer_app_host)
val transferSchemeMain = "https"
val transferHostAlternative = "t"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.google.android.material.snackbar.Snackbar
import org.koin.android.ext.android.inject
import org.p2p.core.crypto.Base58String
import org.p2p.core.glide.GlideManager
import org.p2p.core.utils.DEFAULT_DECIMAL
import org.p2p.core.utils.SOL_DECIMALS
import org.p2p.core.utils.formatFiat
import org.p2p.core.utils.formatToken
import org.p2p.core.utils.lessThenMinValue
Expand Down Expand Up @@ -324,7 +324,7 @@ class HistoryTransactionDetailsBottomSheetFragment :

val highlightedText = "($formattedUsdAmount)"
val commonText = buildString {
append(feeAmount.totalInTokens.formatToken(feeAmount.tokensDecimals ?: DEFAULT_DECIMAL))
append(feeAmount.totalInTokens.formatToken(feeAmount.tokensDecimals ?: SOL_DECIMALS))
appendWhitespace()
append(feeAmount.tokenSymbol.orEmpty())
appendWhitespace()
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/org/p2p/wallet/jupiter/JupiterSwapModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import org.p2p.wallet.jupiter.statemanager.token_selector.SwapInitialTokenSelect
import org.p2p.wallet.jupiter.statemanager.token_selector.SwapInitialTokensData
import org.p2p.wallet.jupiter.statemanager.validator.SwapMinimumSolAmountValidator
import org.p2p.wallet.jupiter.statemanager.validator.SwapValidator
import org.p2p.wallet.jupiter.ui.info.SwapInfoMapper
import org.p2p.wallet.jupiter.ui.main.JupiterSwapContract
import org.p2p.wallet.jupiter.ui.main.JupiterSwapPresenter
import org.p2p.wallet.jupiter.ui.main.SwapShareDeeplinkBuilder
Expand Down Expand Up @@ -230,7 +229,6 @@ object JupiterSwapModule : InjectionModule {
}

factoryOf(::SwapCommonSettingsMapper)
factoryOf(::SwapInfoMapper)
factoryOf(::SwapSelectRoutesMapper)
factoryOf(::SwapEmptySettingsMapper)
factoryOf(::SwapLoadingSettingsMapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.p2p.core.crypto.Base58String
data class JupiterSwapRouteV6(
val inAmountInLamports: BigInteger,
val outAmountInLamports: BigInteger,
val inTokenMint: Base58String,
val outTokenMint: Base58String,
val priceImpactPercent: BigDecimal,
@IntRange(from = 0, to = 10000)
val slippageBps: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ class JupiterSwapRoutesRepositoryV6Mapper(
return JupiterSwapRouteV6(
inAmountInLamports = response.inAmount.toBigInteger(),
outAmountInLamports = response.outAmount.toBigInteger(),
inTokenMint = response.inputMint.toBase58Instance(),
outTokenMint = response.outputMint.toBase58Instance(),
priceImpactPercent = response.priceImpactPct.toBigDecimal(),
slippageBps = response.slippageBps,
otherAmountThreshold = response.otherAmountThreshold,
swapMode = response.swapMode,
originalRoute = gson.toJsonObject(response),
routePlans = plans,
fees = fees
fees = fees,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class SwapRateTickerManager(

private suspend fun findJupiterTokenRate(to: SwapTokenModel.JupiterToken): BigDecimal? {
val tokenData = userLocalRepository.findTokenData(to.mintAddress.base58Value) ?: return null
val cachedPrice = tokenServiceRepository.findTokenPriceByAddress(
val cachedPrice = tokenServiceRepository.getTokenPriceByAddress(
tokenAddress = tokenData.mintAddress,
networkChain = TokenServiceNetwork.SOLANA
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ import android.os.Bundle
import android.view.View
import com.hannesdorfmann.adapterdelegates4.AdapterDelegate
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
import org.koin.android.ext.android.get
import org.koin.android.ext.android.inject
import org.koin.core.parameter.parametersOf
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.p2p.core.dispatchers.CoroutineDispatchers
import org.p2p.uikit.components.finance_block.MainCellModel
import org.p2p.uikit.components.finance_block.baseCellDelegate
import org.p2p.uikit.components.info_block.InfoBlockCellModel
import org.p2p.uikit.model.AnyCellItem
Expand All @@ -31,14 +28,9 @@ import org.p2p.wallet.common.adapter.CommonAnyCellAdapter
import org.p2p.wallet.common.ui.bottomsheet.BaseBottomSheet
import org.p2p.wallet.databinding.DialogSwapInfoBinding
import org.p2p.wallet.databinding.ItemSwapInfoBannerBinding
import org.p2p.wallet.jupiter.interactor.SwapTokensInteractor
import org.p2p.wallet.jupiter.interactor.model.SwapTokenModel
import org.p2p.wallet.jupiter.repository.model.JupiterSwapRoutePlanV6
import org.p2p.wallet.jupiter.repository.tokens.JupiterSwapTokensRepository
import org.p2p.wallet.jupiter.statemanager.SwapState
import org.p2p.wallet.jupiter.statemanager.SwapStateManager
import org.p2p.wallet.jupiter.statemanager.SwapStateManagerHolder
import org.p2p.wallet.jupiter.ui.main.SwapRateLoaderState
import org.p2p.wallet.utils.args
import org.p2p.wallet.utils.viewbinding.viewBinding
import org.p2p.wallet.utils.withArgs
Expand All @@ -55,8 +47,6 @@ enum class SwapInfoType {
TOKEN_2022_TRANSFER
}

private typealias LoadRateBox = Triple<JupiterSwapRoutePlanV6, MainCellModel, SwapRateLoaderState>

private fun swapInfoBannerDelegate(): AdapterDelegate<List<AnyCellItem>> =
adapterDelegateViewBinding<SwapInfoBannerCellModel, AnyCellItem, ItemSwapInfoBannerBinding>(
viewBinding = { inflater, parent -> ItemSwapInfoBannerBinding.inflate(inflater, parent, false) }
Expand Down Expand Up @@ -96,13 +86,12 @@ class SwapFeeInfoBottomSheet : BaseBottomSheet(R.layout.dialog_swap_info) {
private val swapInfoType: SwapInfoType by args(ARG_INFO_TYPE_KEY)

private val managerHolder: SwapStateManagerHolder by inject()
private val mapper: SwapInfoMapper by inject()
private val interactor: SwapTokensInteractor by inject {
parametersOf(stateManagerKey)
private val mapper = SwapInfoMapper()
private val liquidityFeeMapper: SwapInfoLiquidityFeeMapper by lazy {
SwapInfoLiquidityFeeMapper(swapTokensRepository = get(), tokenServiceRepository = get())
}

private val swapTokensRepository: JupiterSwapTokensRepository by inject()
private val coroutineDispatchers: CoroutineDispatchers by inject()
private val dispatchers: CoroutineDispatchers by inject()
private val stateManager: SwapStateManager
get() = managerHolder.get(stateManagerKey)

Expand Down Expand Up @@ -144,82 +133,39 @@ class SwapFeeInfoBottomSheet : BaseBottomSheet(R.layout.dialog_swap_info) {
setExpanded(true)
}

@OptIn(ExperimentalCoroutinesApi::class)
private fun observeFeatureState() {
stateManager.observe()
.flatMapLatest { handleFeatureState(it) }
.flowOn(coroutineDispatchers.io)
.onEach { adapter.items = it }
.flatMapLatest(transform = ::mapLiquidityCellsFlow)
.flowOn(dispatchers.io)
.onEach(adapter::setItems)
.flowWithLifecycle(lifecycle)
.launchIn(lifecycleScope)
}

private suspend fun handleFeatureState(state: SwapState): Flow<List<AnyCellItem>> {
private fun mapLiquidityCellsFlow(state: SwapState): Flow<List<AnyCellItem>> {
return when (state) {
SwapState.InitialLoading,
is SwapState.LoadingRoutes,
is SwapState.TokenANotZero,
is SwapState.TokenAZero -> {
flowOf(mapper.mapEmptyLiquidityFee())
flowOf(liquidityFeeMapper.mapNoRouteLoaded())
}
is SwapState.SwapException -> {
handleFeatureState(state.previousFeatureState)
mapLiquidityCellsFlow(state.previousFeatureState)
}
is SwapState.RoutesLoaded,
is SwapState.LoadingTransaction,
is SwapState.SwapLoaded -> {
val route = when (state) {
is SwapState.RoutesLoaded -> state.route
is SwapState.SwapLoaded -> state.route
else -> return flowOf(mapper.mapEmptyLiquidityFee())
}
flow {
val rateLoaderList = mutableListOf<Flow<LoadRateBox>>()
val feeCells = route.routePlans.map { plan ->
val loadingCell = mapper.getLiquidityFeeCell(plan)
rateLoaderList += getRateLoaderFlow(plan, loadingCell)
loadingCell
}
var fullUiList = mapper.mapEmptyLiquidityFee().plus(feeCells)
emit(fullUiList)

rateLoaderList.merge()
.collect {
val routePlan = it.first
val loadingCell = it.second
val rateLoaderState = it.third
val indexOf = fullUiList.indexOf(loadingCell)
if (indexOf >= 0) {
val newCell = mapper.updateLiquidityFee(
marketInfo = routePlan,
oldCell = loadingCell,
state = rateLoaderState
)
val newList = fullUiList.toMutableList().apply { set(indexOf, newCell) }
fullUiList = newList
emit(newList)
}
}
}
}
is SwapState.LoadingTransaction -> {
flow {
val fullUiList = mapper.mapLoadingLiquidityFee(state.route)
emit(fullUiList)
}
}
}
}
is SwapState.LoadingTransaction -> state.route
else -> null
} ?: return flowOf(liquidityFeeMapper.mapNoRouteLoaded())

private suspend fun getRateLoaderFlow(
routePlan: JupiterSwapRoutePlanV6,
loadingCell: MainCellModel,
): Flow<LoadRateBox> {
val lpToken = swapTokensRepository.findTokenByMint(routePlan.feeMint)
val loadingCellFlow = flowOf(routePlan to loadingCell)
val rateLoaderFlow = lpToken
?.let { stateManager.getTokenRate(SwapTokenModel.JupiterToken(it)) }
?: flowOf(SwapRateLoaderState.Error)
return loadingCellFlow.combine(rateLoaderFlow) { a, b ->
Triple(a.first, a.second, b)
liquidityFeeMapper.mapLiquidityFees(route)
}
}
}

Expand Down
Loading

0 comments on commit 66c8ee2

Please sign in to comment.