Updated on 2026-08-14
This commit is contained in:
parent
043602ebd6
commit
dd362fb32c
21 changed files with 133 additions and 184 deletions
|
|
@ -3,30 +3,37 @@ package com.tangem.domain.utils
|
|||
import com.tangem.blockchain.common.AmountType
|
||||
import com.tangem.blockchain.common.Token
|
||||
import com.tangem.domain.models.currency.CryptoCurrency
|
||||
import com.tangem.domain.models.currency.CryptoCurrencyStatus
|
||||
import java.math.BigDecimal
|
||||
import com.tangem.blockchain.common.Amount as SdkAmount
|
||||
|
||||
/** Converts `BigDecimal` [cryptoCurrency] to [SdkAmount] */
|
||||
fun BigDecimal.convertToSdkAmount(
|
||||
cryptoCurrency: CryptoCurrency,
|
||||
amountType: AmountType = getAmountTypeFromCryptoCurrency(cryptoCurrency),
|
||||
): SdkAmount = SdkAmount(
|
||||
currencySymbol = cryptoCurrency.symbol,
|
||||
value = this,
|
||||
decimals = cryptoCurrency.decimals,
|
||||
type = amountType,
|
||||
)
|
||||
|
||||
/**
|
||||
* Converts [CryptoCurrency] to [AmountType] based on its type
|
||||
*/
|
||||
private fun getAmountTypeFromCryptoCurrency(cryptoCurrency: CryptoCurrency) = when (cryptoCurrency) {
|
||||
is CryptoCurrency.Coin -> AmountType.Coin
|
||||
is CryptoCurrency.Token -> AmountType.Token(
|
||||
token = Token(
|
||||
symbol = cryptoCurrency.symbol,
|
||||
contractAddress = cryptoCurrency.contractAddress,
|
||||
decimals = cryptoCurrency.decimals,
|
||||
),
|
||||
/** Converts `BigDecimal` [cryptoCurrencyStatus] to [SdkAmount] */
|
||||
fun BigDecimal.convertToSdkAmount(cryptoCurrencyStatus: CryptoCurrencyStatus): SdkAmount {
|
||||
val cryptoCurrency = cryptoCurrencyStatus.currency
|
||||
val yieldSupplyStatus = cryptoCurrencyStatus.value.yieldSupplyStatus
|
||||
return SdkAmount(
|
||||
currencySymbol = cryptoCurrency.symbol,
|
||||
value = this,
|
||||
decimals = cryptoCurrency.decimals,
|
||||
type = when (cryptoCurrency) {
|
||||
is CryptoCurrency.Coin -> AmountType.Coin
|
||||
is CryptoCurrency.Token -> {
|
||||
val token = Token(
|
||||
symbol = cryptoCurrency.symbol,
|
||||
contractAddress = cryptoCurrency.contractAddress,
|
||||
decimals = cryptoCurrency.decimals,
|
||||
)
|
||||
if (yieldSupplyStatus == null) {
|
||||
AmountType.Token(token = token)
|
||||
} else {
|
||||
AmountType.TokenYieldSupply(
|
||||
token = token,
|
||||
isActive = yieldSupplyStatus.isActive,
|
||||
isInitialized = yieldSupplyStatus.isInitialized,
|
||||
isAllowedToSpend = yieldSupplyStatus.isAllowedToSpend,
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue