Skip to content

Commit

Permalink
Merge pull request #2209 from p2p-org/release/v2.12.0
Browse files Browse the repository at this point in the history
Release/2.12.0 to develop
  • Loading branch information
gslevinkov authored Feb 28, 2024
2 parents ddf90ad + 2ab28a5 commit f914f6a
Show file tree
Hide file tree
Showing 35 changed files with 195 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.p2p.core.utils.formatTokenWithSymbol
import org.p2p.core.utils.isConnectionError
import org.p2p.core.utils.isZero
import org.p2p.core.utils.orZero
import org.p2p.core.utils.scaleShort
import org.p2p.core.utils.scaleTwo
import org.p2p.core.utils.toBigDecimalOrZero
import org.p2p.uikit.utils.text.TextViewCellModel
import org.p2p.wallet.R
Expand Down Expand Up @@ -242,7 +242,7 @@ class BridgeSendPresenter(
if (calculationMode.getCurrencyMode() is CurrencyMode.Fiat.Usd) {
switchCurrencyMode()
}
val newTextValue = inputAmount.scaleShort().toPlainString()
val newTextValue = inputAmount.scaleTwo().toPlainString()
updateInputValue(newTextValue, forced = true)
calculationMode.updateInputAmount(newTextValue)
disableInputs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.p2p.core.utils.asNegativeUsdTransaction
import org.p2p.core.utils.asPositiveUsdTransaction
import org.p2p.core.utils.asUsdTransaction
import org.p2p.core.utils.formatTokenWithSymbol
import org.p2p.core.utils.scaleShortOrFirstNotZero
import org.p2p.core.utils.scaleTwoOrFirstNotZero
import org.p2p.wallet.R
import org.p2p.wallet.history.model.HistoryTransaction
import org.p2p.wallet.transaction.model.HistoryTransactionStatus
Expand Down Expand Up @@ -210,7 +210,7 @@ sealed class RpcHistoryTransaction(

fun getFormattedFiatValue(): String? {
return amount.totalInUsd
?.scaleShortOrFirstNotZero()
?.scaleTwoOrFirstNotZero()
?.asUsdTransaction(getSymbol(isSend))
}

Expand Down Expand Up @@ -263,7 +263,7 @@ sealed class RpcHistoryTransaction(

fun getFormattedFiatValue(): String? {
return amount.totalInUsd
?.scaleShortOrFirstNotZero()
?.scaleTwoOrFirstNotZero()
?.asPositiveUsdTransaction()
}

Expand Down Expand Up @@ -305,7 +305,7 @@ sealed class RpcHistoryTransaction(

fun getValue(): String? {
return amount.totalInUsd
?.scaleShortOrFirstNotZero()
?.scaleTwoOrFirstNotZero()
?.asUsdTransaction(getSymbol(isStake))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.threeten.bp.ZonedDateTime
import org.p2p.core.utils.asNegativeUsdTransaction
import org.p2p.core.utils.asPositiveUsdTransaction
import org.p2p.core.utils.formatToken
import org.p2p.core.utils.scaleMedium
import org.p2p.core.utils.scaleSix
import org.p2p.core.utils.toBigDecimalOrZero
import org.p2p.uikit.utils.recycler.RoundedItem
import org.p2p.wallet.bridge.model.BridgeBundle
Expand Down Expand Up @@ -92,7 +92,7 @@ sealed interface HistoryItem {
val tokenSymbol = sendDetails.amount.symbol

return if (scaleMedium) {
"${totalAmount.scaleMedium().formatToken()} $tokenSymbol"
"${totalAmount.scaleSix().formatToken()} $tokenSymbol"
} else {
"${totalAmount.formatToken()} $tokenSymbol"
}
Expand All @@ -117,7 +117,7 @@ sealed interface HistoryItem {
val tokenSymbol = bundle.resultAmount.symbol

return if (scaleMedium) {
"${totalAmount.scaleMedium().formatToken()} $tokenSymbol"
"${totalAmount.scaleSix().formatToken()} $tokenSymbol"
} else {
"${totalAmount.formatToken()} $tokenSymbol"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ class MainFragmentDeeplinkHandler(
* Handles buy token for fiat deeplink
*/
private fun handleBuyDeeplink(data: DeeplinkData) {
val cryptoToken = data.args["to"]
val cryptoTokenMint = data.args["to"]
val fiatToken = data.args["from"]
val fiatAmount = data.args["amount"]

if (!cryptoToken.isNullOrBlank() && !fiatToken.isNullOrBlank()) {
if (!cryptoTokenMint.isNullOrBlank() && !fiatToken.isNullOrBlank()) {
coroutineScope.launch {
val token = userInteractor.getSingleTokenForBuy(listOf(cryptoToken))
val token = userInteractor.getSingleTokenForBuy(listOf(cryptoTokenMint))
if (token != null) {
screenNavigator?.navigateToBuyScreen(token, fiatToken, fiatAmount)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import org.p2p.wallet.jupiter.ui.main.JupiterSwapFragment
import org.p2p.wallet.settings.ui.settings.SettingsFragment
import org.p2p.wallet.tokenservice.TokenServiceCoordinator
import org.p2p.wallet.tokenservice.UserTokensState
import org.p2p.wallet.updates.SocketUpdatesManager

class MainContainerPresenter(
private val deeplinksManager: AppDeeplinksManager,
private val connectionManager: ConnectionManager,
private val tokenServiceCoordinator: TokenServiceCoordinator,
private val metadataInteractor: MetadataInteractor,
private val walletStrigaInteractor: WalletStrigaInteractor,
private val socketUpdatesManager: SocketUpdatesManager,
private val balanceMapper: WalletBalanceMapper,
private val mainScreenAnalytics: MainScreenAnalytics,
) : BasePresenter<MainContainerContract.View>(), MainContainerContract.Presenter {
Expand Down Expand Up @@ -102,7 +104,14 @@ class MainContainerPresenter(
private fun observeInternetState() {
connectionManager.connectionStatus
.onEach { isConnected ->
if (!isConnected) view?.showUiKitSnackBar(messageResId = R.string.error_no_internet_message)
if (!isConnected) {
view?.showUiKitSnackBar(messageResId = R.string.error_no_internet_message)
socketUpdatesManager.stop()
} else {
if (!socketUpdatesManager.isStarted()) {
socketUpdatesManager.restart()
}
}
}
.launchIn(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.p2p.core.token.Token
import org.p2p.core.token.TokenVisibility
import org.p2p.core.token.filterTokensForWalletScreen
import org.p2p.core.utils.isMoreThan
import org.p2p.core.utils.scaleShort
import org.p2p.core.utils.scaleTwo
import org.p2p.uikit.model.AnyCellItem
import org.p2p.wallet.R
import org.p2p.wallet.auth.interactor.UsernameInteractor
Expand Down Expand Up @@ -199,7 +199,7 @@ class MyCryptoPresenter(
.mapNotNull(Token.Active::totalInUsd)
.filter { it.isMoreThan(MINIMAL_DUST_FOR_BALANCE) }
.fold(BigDecimal.ZERO, BigDecimal::add)
.scaleShort()
.scaleTwo()
}

private fun handleEmptyAccount() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,59 @@
package org.p2p.wallet.home.ui.select.bottomsheet

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.setFragmentResult
import androidx.recyclerview.widget.LinearLayoutManager
import android.os.Bundle
import android.view.View
import org.koin.android.ext.android.inject
import org.p2p.core.token.Token
import org.p2p.uikit.utils.attachAdapter
import org.p2p.wallet.common.analytics.interactor.ScreensAnalyticsInteractor
import org.p2p.wallet.common.ui.bottomsheet.BaseRecyclerDoneBottomSheet
import org.p2p.wallet.common.ui.recycler.adapter.DividerItemDecorator
import org.p2p.core.token.Token
import org.p2p.wallet.home.ui.select.NewSelectTokenAdapter
import org.p2p.wallet.moonpay.analytics.BuyAnalytics
import org.p2p.wallet.utils.args
import org.p2p.wallet.utils.unsafeLazy
import org.p2p.wallet.utils.withArgs

private const val ARG_ALL_TOKENS = "ARG_ALL_TOKENS"
private const val ARG_TOKENS_TO_BUY = "ARG_ALL_TOKENS"
private const val ARG_SELECTED_TOKEN = "ARG_SELECTED_TOKEN"

class NewSelectTokenBottomSheet : BaseRecyclerDoneBottomSheet() {
class BuySelectTokenBottomSheet : BaseRecyclerDoneBottomSheet() {

companion object {
fun show(
fm: FragmentManager,
title: String,
tokens: List<Token>,
tokensToBuy: List<Token>,
preselectedToken: Token? = null,
requestKey: String,
resultKey: String
) = NewSelectTokenBottomSheet().withArgs(
) = BuySelectTokenBottomSheet().withArgs(
ARG_TITLE to title,
ARG_ALL_TOKENS to tokens,
ARG_TOKENS_TO_BUY to tokensToBuy,
ARG_SELECTED_TOKEN to preselectedToken,
ARG_REQUEST_KEY to requestKey,
ARG_RESULT_KEY to resultKey
).show(fm, NewSelectTokenBottomSheet::javaClass.name)
).show(fm, BuySelectTokenBottomSheet::javaClass.name)
}

private val tokens: List<Token> by args(ARG_ALL_TOKENS)
private val tokensToBuy: List<Token> by args(ARG_TOKENS_TO_BUY)
private val preselectedToken: Token? by args(ARG_SELECTED_TOKEN)

private val buyAnalytics: BuyAnalytics by inject()
private val analyticsInteractor: ScreensAnalyticsInteractor by inject()

private val tokenAdapter: NewSelectTokenAdapter by unsafeLazy {
NewSelectTokenAdapter(preselectedItem = preselectedToken, onItemClicked = {
setFragmentResult(requestKey, bundleOf(resultKey to it))
dismissAllowingStateLoss()
})
NewSelectTokenAdapter(
preselectedItem = preselectedToken,
onItemClicked = {
setFragmentResult(requestKey, bundleOf(resultKey to it))
dismissAllowingStateLoss()
}
)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -61,7 +64,7 @@ class NewSelectTokenBottomSheet : BaseRecyclerDoneBottomSheet() {
DividerItemDecorator(requireContext())
)
recyclerView.attachAdapter(tokenAdapter)
tokenAdapter.setItems(tokens)
tokenAdapter.setItems(tokensToBuy)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sealed interface JupiterSwapTokensResult {
* https://github.com/orca-so/whirlpools/blob/e06505fb1e41508295eca116ca676c8f498398d2/programs/whirlpool/src/manager/whirlpool_manager.rs#L13
* happens randomly error occurs when sending transaction,
* but it's not our fault, it depends on the node state
* https://discord.com/channels/767761912167006292/1106227799448101004
*/
class InvalidTimestampRpcError(override val cause: ServerException) : Throwable(cause.message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.p2p.wallet.jupiter.repository.model
import java.math.BigDecimal
import java.math.BigInteger
import org.p2p.core.crypto.Base58String
import org.p2p.core.utils.scaleShortOrFirstNotZero
import org.p2p.core.utils.scaleTwoOrFirstNotZero

@Deprecated("Old v4 swap logic")
data class JupiterSwapMarketInformation(
Expand All @@ -26,7 +26,7 @@ data class JupiterSwapMarketInformation(
val mint: Base58String,
val percent: BigDecimal
) {
val formattedPercent: BigDecimal = percent.times(BigDecimal(100)).scaleShortOrFirstNotZero()
val formattedPercent: BigDecimal = percent.times(BigDecimal(100)).scaleTwoOrFirstNotZero()
}

@Deprecated("Old v4 swap logic")
Expand All @@ -35,6 +35,6 @@ data class JupiterSwapMarketInformation(
val mint: Base58String,
val percent: BigDecimal
) {
val formattedPercent: BigDecimal = percent.times(BigDecimal(100)).scaleShortOrFirstNotZero()
val formattedPercent: BigDecimal = percent.times(BigDecimal(100)).scaleTwoOrFirstNotZero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ sealed class MoonpayCurrency {
abstract val currencyId: String
abstract val amounts: MoonpayCurrencyAmounts

fun isSol() = this is CryptoToken && tokenSymbol.uppercase() == Constants.SOL_SYMBOL
// it's okay to keep equals by symbol here
// because we only have non-scam wrapped SOL in/out Moonpay
fun isSol() = this is CryptoToken && tokenSymbol.equals(Constants.SOL_SYMBOL, ignoreCase = true)

data class CryptoToken(
val tokenSymbol: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ class BuyInteractor(

fun getQuotes(): List<MoonpayBuyQuote> = quotes.toList()

suspend fun loadQuotes(currencies: List<FiatCurrency>, tokens: List<Token>): Unit = withContext(dispatchers.io) {
Timber.i("Loading quotes for buy: currencies=$currencies; tokens=${tokens.map(Token::mintAddress)}")
suspend fun loadQuotes(
currencies: List<FiatCurrency>,
tokensToBuy: List<Token>
): Unit = withContext(dispatchers.io) {
Timber.i("Loading quotes for buy: currencies=$currencies; tokens=${tokensToBuy.map(Token::mintAddress)}")
currencies.flatMap { currency ->
tokens.map { token ->
tokensToBuy.map { token ->
async { loadQuote(currency, token) }
}
}
Expand Down Expand Up @@ -102,31 +105,30 @@ class BuyInteractor(
tokenSymbol: String
): BigDecimal {
val quote = quotes.find {
it.currency == FiatCurrency.getFromAbbreviation(currencyCode) && it.token.tokenSymbol == tokenSymbol
it.currency == FiatCurrency.getFromAbbreviation(currencyCode) && it.tokenToBuy.tokenSymbol == tokenSymbol
}

return HARDCODED_MIN_BUY_CURRENCY_AMOUNT.toBigDecimal()
}

private suspend fun loadQuote(currency: FiatCurrency, token: Token) {
Timber.d("Load quote for currency=$currency; token=${token.tokenSymbol}")
private suspend fun loadQuote(currency: FiatCurrency, tokenToBuy: Token) {
Timber.d("Load quote for currency=$currency; token=${tokenToBuy.tokenSymbol}")
try {
val response = moonpayRepository.getBuyCurrencyData(
baseCurrencyAmount = CURRENCY_AMOUNT_FOR_PRICE_REQUEST,
quoteCurrencyAmount = null,
tokenToBuy = token,
tokenToBuy = tokenToBuy,
baseCurrencyCode = currency.abbreviation.lowercase(),
paymentMethod = DEFAULT_PAYMENT_TYPE
)
val result = MoonpayBuyQuote(
quotes += MoonpayBuyQuote(
currency = currency,
token = token,
tokenToBuy = tokenToBuy,
price = response.quoteCurrencyPrice,
minAmount = HARDCODED_MIN_BUY_CURRENCY_AMOUNT.toBigDecimal()
)
quotes += result
} catch (e: Throwable) {
Timber.e(e, "Error while loading quote for currency=$currency; token=${token.tokenSymbol}")
Timber.e(e, "Error while loading quote for currency=$currency; token=${tokenToBuy.tokenSymbol}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.p2p.wallet.moonpay.repository.sell.FiatCurrency

data class MoonpayBuyQuote(
val currency: FiatCurrency,
val token: Token,
val tokenToBuy: Token,
val price: BigDecimal,
val minAmount: BigDecimal
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.p2p.wallet.R
import org.p2p.wallet.common.analytics.interactor.ScreensAnalyticsInteractor
import org.p2p.wallet.common.mvp.BaseMvpFragment
import org.p2p.wallet.databinding.FragmentBuyBinding
import org.p2p.wallet.home.ui.select.bottomsheet.NewSelectTokenBottomSheet
import org.p2p.wallet.home.ui.select.bottomsheet.BuySelectTokenBottomSheet
import org.p2p.wallet.home.ui.select.bottomsheet.SelectCurrencyBottomSheet
import org.p2p.wallet.infrastructure.network.provider.TokenKeyProvider
import org.p2p.wallet.moonpay.model.BuyDetailsState
Expand Down Expand Up @@ -171,10 +171,10 @@ class BuyFragment :
}

override fun showTokensToBuy(selectedToken: Token, tokensToBuy: List<Token>) {
NewSelectTokenBottomSheet.show(
BuySelectTokenBottomSheet.show(
fm = childFragmentManager,
title = getString(R.string.buy_select_token_title),
tokens = tokensToBuy,
tokensToBuy = tokensToBuy,
preselectedToken = selectedToken,
requestKey = KEY_REQUEST,
resultKey = KEY_RESULT_TOKEN
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/org/p2p/wallet/moonpay/ui/new/BuyPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.p2p.core.utils.asCurrency
import org.p2p.core.utils.formatFiat
import org.p2p.core.utils.formatToken
import org.p2p.core.utils.isZero
import org.p2p.core.utils.scaleShort
import org.p2p.core.utils.scaleTwo
import org.p2p.core.utils.toBigDecimalOrZero
import org.p2p.uikit.components.FocusField
import org.p2p.wallet.R
Expand Down Expand Up @@ -177,7 +177,7 @@ class BuyPresenter(

override fun onSelectTokenClicked() {
buyInteractor.getQuotesByCurrency(selectedCurrency).forEach { quote ->
tokensToBuy.find { it.tokenSymbol == quote.token.tokenSymbol }?.let {
tokensToBuy.find { it.tokenSymbol == quote.tokenToBuy.tokenSymbol }?.let {
it.rate = quote.price
it.currency = quote.currency.abbreviation.uppercase()
}
Expand Down Expand Up @@ -405,13 +405,13 @@ class BuyPresenter(
val data = BuyViewData(
tokenSymbol = selectedToken.tokenSymbol,
currencySymbol = selectedCurrency.uiSymbol,
price = buyCurrencyInfo.price.scaleShort(),
price = buyCurrencyInfo.price.scaleTwo(),
receiveAmount = buyCurrencyInfo.receiveAmount,
processingFee = buyCurrencyInfo.feeAmount.scaleShort(),
networkFee = buyCurrencyInfo.networkFeeAmount.scaleShort(),
extraFee = buyCurrencyInfo.extraFeeAmount.scaleShort(),
processingFee = buyCurrencyInfo.feeAmount.scaleTwo(),
networkFee = buyCurrencyInfo.networkFeeAmount.scaleTwo(),
extraFee = buyCurrencyInfo.extraFeeAmount.scaleTwo(),
accountCreationCost = null,
total = buyCurrencyInfo.totalFiatAmount.scaleShort(),
total = buyCurrencyInfo.totalFiatAmount.scaleTwo(),
receiveAmountText = amount,
purchaseCostText = currencyForTokensAmount.asCurrency(selectedCurrency.uiSymbol)
)
Expand Down
Loading

0 comments on commit f914f6a

Please sign in to comment.