Skip to content

Commit

Permalink
ETH-1059 - send total fix (#2214)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardmaximovich authored Feb 26, 2024
1 parent 323a890 commit 23d6ca8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 44 deletions.
12 changes: 2 additions & 10 deletions app/src/main/java/org/p2p/wallet/send/SendFeeRelayerManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import org.p2p.solanaj.rpc.RpcSolanaRepository
import org.p2p.wallet.feerelayer.model.FeeCalculationState
import org.p2p.wallet.feerelayer.model.FeePayerSelectionStrategy
import org.p2p.wallet.feerelayer.model.FeeRelayerFee
import org.p2p.wallet.feerelayer.model.TransactionFeeLimits
import org.p2p.wallet.send.interactor.SendInteractor
import org.p2p.wallet.send.interactor.usecase.CalculateSendFeesUseCase
import org.p2p.wallet.send.interactor.usecase.CalculateToken2022TransferFeeUseCase
Expand Down Expand Up @@ -59,7 +58,6 @@ class SendFeeRelayerManager(
onStateUpdated?.invoke(newState)
}

private lateinit var feeLimitInfo: TransactionFeeLimits
private lateinit var recipientAddress: SearchResult
private lateinit var solToken: Token.Active
private var initializeCompleted = false
Expand Down Expand Up @@ -103,7 +101,6 @@ class SendFeeRelayerManager(
private suspend fun initializeWithToken(feePayerToken: Token.Active) {
Timber.tag(TAG).i("initialize for SendFeeRelayerManager with token ${feePayerToken.mintAddress}")
minRentExemption = sendInteractor.getMinRelayRentExemption()
feeLimitInfo = sendInteractor.getFreeTransactionsInfo()
currentSolanaEpoch = solanaRepository.getEpochInfo(useCache = true).epoch
sendInteractor.initialize(feePayerToken)
}
Expand Down Expand Up @@ -131,6 +128,8 @@ class SendFeeRelayerManager(

return SendFeeTotal(
currentAmount = currentAmount,
tokenTotalAmount = sourceToken.total,
isMaxButtonUsed = calculationMode.isMaxUsed(),
currentAmountUsd = calculationMode.getCurrentAmountUsd(),
receiveFormatted = currentAmount.formatTokenWithSymbol(
tokenSymbol = sourceToken.tokenSymbol,
Expand All @@ -140,7 +139,6 @@ class SendFeeRelayerManager(
sourceSymbol = sourceToken.tokenSymbol,
sendFee = (currentState as? UpdateFee)?.solanaFee,
recipientAddress = recipientAddress.address,
feeLimit = feeLimitInfo,
transferFeePercent = transferFeePercent,
interestBearingPercent = interestBearingPercent
)
Expand Down Expand Up @@ -180,7 +178,6 @@ class SendFeeRelayerManager(
is FeeCalculationState.NoFees -> {
currentState = UpdateFee(
solanaFee = null,
feeLimitInfo = feeLimitInfo,
tokenExtensions = tokenExtensions
)
sendInteractor.setFeePayerToken(feePayer)
Expand All @@ -195,7 +192,6 @@ class SendFeeRelayerManager(
)
currentState = UpdateFee(
solanaFee = solanaFee,
feeLimitInfo = feeLimitInfo,
tokenExtensions = tokenExtensions,
)
sendInteractor.setFeePayerToken(solToken)
Expand Down Expand Up @@ -315,7 +311,6 @@ class SendFeeRelayerManager(
validateFunds(sourceToken, fee, inputAmount)
currentState = UpdateFee(
solanaFee = fee,
feeLimitInfo = feeLimitInfo,
tokenExtensions = tokenExtensions,
)
} else {
Expand Down Expand Up @@ -443,7 +438,6 @@ class SendFeeRelayerManager(
is FeeCalculationState.NoFees -> {
currentState = UpdateFee(
solanaFee = null,
feeLimitInfo = feeLimitInfo,
tokenExtensions = tokenExtensions
)
}
Expand All @@ -457,7 +451,6 @@ class SendFeeRelayerManager(
)
currentState = UpdateFee(
solanaFee = solanaFee,
feeLimitInfo = feeLimitInfo,
tokenExtensions = tokenExtensions
)
sendInteractor.setFeePayerToken(solToken)
Expand All @@ -473,7 +466,6 @@ class SendFeeRelayerManager(
validateFunds(sourceToken, fee, inputAmount)
currentState = UpdateFee(
solanaFee = fee,
feeLimitInfo = feeLimitInfo,
tokenExtensions = tokenExtensions
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class CalculationMode(
var inputAmountDecimal: BigDecimal = BigDecimal.ZERO
private set

private var useMax = false

val formatInputAmount: String
get() = when (currencyMode) {
is CurrencyMode.Fiat -> inputAmountDecimal.formatFiat()
Expand Down Expand Up @@ -270,4 +272,10 @@ class CalculationMode(
currencyMode = newMode
return inputAmount
}

fun setUseMax(value: Boolean) {
useMax = value
}

fun isMaxUsed(): Boolean = useMax
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.p2p.wallet.send.model

import java.math.BigInteger
import org.p2p.solanaj.kits.TokenExtensionsMap
import org.p2p.wallet.feerelayer.model.TransactionFeeLimits

sealed interface FeeRelayerState {
object Idle : FeeRelayerState
Expand All @@ -13,7 +12,6 @@ sealed interface FeeRelayerState {

data class UpdateFee(
val solanaFee: SendSolanaFee?,
val feeLimitInfo: TransactionFeeLimits,
val tokenExtensions: TokenExtensionsMap
) : FeeRelayerState {
val hasToken2022Fee: Boolean
Expand Down
28 changes: 8 additions & 20 deletions app/src/main/java/org/p2p/wallet/send/model/SendFeeTotal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.p2p.wallet.send.model
import androidx.annotation.ColorInt
import android.os.Parcelable
import java.math.BigDecimal
import java.math.BigInteger
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import org.p2p.core.model.TextHighlighting
Expand All @@ -13,9 +12,6 @@ import org.p2p.core.utils.formatTokenWithSymbol
import org.p2p.core.utils.orZero
import org.p2p.uikit.utils.SpanUtils
import org.p2p.wallet.R
import org.p2p.wallet.feerelayer.model.Limits
import org.p2p.wallet.feerelayer.model.ProcessedFee
import org.p2p.wallet.feerelayer.model.TransactionFeeLimits

/**
* [SendSolanaFee] can be null only if total fees is Zero. (transaction fee and account creation fee)
Expand All @@ -28,11 +24,12 @@ import org.p2p.wallet.feerelayer.model.TransactionFeeLimits
@Parcelize
class SendFeeTotal constructor(
val currentAmount: BigDecimal,
val tokenTotalAmount: BigDecimal,
val isMaxButtonUsed: Boolean,
val currentAmountUsd: BigDecimal?,
val receiveFormatted: String,
val receiveUsd: BigDecimal?,
val sendFee: SendSolanaFee?,
val feeLimit: TransactionFeeLimits,
val sourceSymbol: String,
val recipientAddress: String,
val transferFeePercent: BigDecimal? = null,
Expand All @@ -42,25 +39,12 @@ class SendFeeTotal constructor(
companion object {
fun createEmpty(): SendFeeTotal = SendFeeTotal(
currentAmount = BigDecimal.ZERO,
tokenTotalAmount = BigDecimal.ZERO,
isMaxButtonUsed = false,
currentAmountUsd = null,
receiveFormatted = "",
receiveUsd = null,
sendFee = null,
feeLimit = TransactionFeeLimits(
limits = Limits(
maxFeeCountAllowed = 0,
maxFeeAmountAllowed = BigInteger.ZERO,
maxAccountCreationCountAllowed = 0,
maxAccountCreationAmountAllowed = BigInteger.ZERO
),
processedFee = ProcessedFee(
totalFeeAmountUsed = BigInteger.ZERO,
totalRentAmountUsed = BigInteger.ZERO,
totalFeeCountUsed = 0,
totalRentCountUsed = 0,
totalAmountUsed = BigInteger.ZERO
)
),
sourceSymbol = "",
recipientAddress = ""
)
Expand Down Expand Up @@ -147,6 +131,10 @@ class SendFeeTotal constructor(

val totalSumWithSymbol: String
get() {
if (isMaxButtonUsed) {
return tokenTotalAmount.formatTokenWithSymbol(sourceSymbol, SOL_DECIMALS)
}

val transferFee = transferFeePercent
?.multiply("0.01".toBigDecimal())
?.multiply(currentAmount)
Expand Down
25 changes: 13 additions & 12 deletions app/src/main/java/org/p2p/wallet/send/ui/NewSendPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,18 @@ class NewSendPresenter(
val currentState = sendFeeRelayerManager.getState()
handleFeeRelayerStateUpdate(currentState, view)

calculateMaxAvailableAmount(token)
}

private suspend fun calculateMaxAvailableAmount(token: Token.Active) {
maximumAmountCalculator.getMaxAvailableAmountToSend(
token = token,
recipient = recipientAddress.address.toBase58Instance()
)
// set max available amount
?.also { calculationMode.setMaxAmounts(it) }
?.also {
calculationMode.setMaxAmounts(it)
}
}

private suspend fun setupInitialToken(view: NewSendContract.View) {
Expand Down Expand Up @@ -223,18 +229,14 @@ class NewSendPresenter(
Timber.tag(TAG).e(SendFatalError("Couldn't find user's SOL account!"))
return
}
maximumAmountCalculator.getMaxAvailableAmountToSend(
token = initialToken,
recipient = recipientAddress.address.toBase58Instance()
)
?.also { calculationMode.setMaxAmounts(it) }

initializeFeeRelayer(
view = view,
sourceToken = requireToken(),
feePayerToken = feePayerToken,
solToken = solToken
)
calculateMaxAvailableAmount(initialToken)
initialAmount?.let(::setupDefaultFields)
}

Expand Down Expand Up @@ -375,12 +377,8 @@ class NewSendPresenter(
launch {
newSendAnalytics.logTokenChanged(newToken.tokenSymbol, flow)
token = newToken
maximumAmountCalculator.getMaxAvailableAmountToSend(
token = newToken,
recipient = recipientAddress.address.toBase58Instance()
)
// set max available amount
?.also { calculationMode.setMaxAmounts(it) }
calculationMode.setUseMax(false)
calculateMaxAvailableAmount(newToken)

checkTokenRatesAndSetSwitchAmountState(newToken)

Expand Down Expand Up @@ -431,6 +429,7 @@ class NewSendPresenter(

override fun updateInputAmount(amount: String) {
newSendAnalytics.logTokenAmountChanged(token?.tokenSymbol.orEmpty(), amount, flow)
calculationMode.setUseMax(false)
calculationMode.updateInputAmount(amount)
view?.showFeeViewVisible(isVisible = true)
showMaxButtonIfNeeded()
Expand All @@ -449,6 +448,7 @@ class NewSendPresenter(

override fun updateFeePayerToken(feePayerToken: Token.Active) {
try {
calculationMode.setUseMax(false)
launch {
sendInteractor.saveFeePayerToken(feePayerToken)
}
Expand All @@ -468,6 +468,7 @@ class NewSendPresenter(
Timber.tag(TAG).e(SendFatalError("Token can't be null"))
return@launch
}
calculationMode.setUseMax(true)
val totalAvailable = maximumAmountCalculator.getMaxAvailableAmountToSend(
token = token,
recipient = recipientAddress.address.toBase58Instance()
Expand Down

0 comments on commit 23d6ca8

Please sign in to comment.