Updated on 2026-08-14

This commit is contained in:
Tangem 2024-09-03 12:36:09 +03:00
commit d617c9c30d
491 changed files with 15019 additions and 5050 deletions

View file

@ -12,6 +12,29 @@ import com.tangem.blockchain.common.Token as SdkToken
// FIXME: Make internal
class CryptoCurrencyFactory {
@Suppress("LongParameterList") // Yep, it's long
fun createToken(
network: Network,
rawId: String?,
name: String,
symbol: String,
decimals: Int,
contractAddress: String,
): CryptoCurrency.Token {
val id = getTokenId(network, rawId, contractAddress)
return CryptoCurrency.Token(
id = id,
network = network,
name = name,
symbol = symbol,
decimals = decimals,
iconUrl = rawId?.let(::getTokenIconUrlFromDefaultHost),
isCustom = isCustomToken(id, network),
contractAddress = contractAddress,
)
}
fun createToken(
sdkToken: SdkToken,
blockchain: Blockchain,
@ -49,15 +72,7 @@ class CryptoCurrencyFactory {
}
val network = getNetwork(blockchain, extraDerivationPath, derivationStyleProvider) ?: return null
return CryptoCurrency.Coin(
id = getCoinId(network, blockchain.toCoinId()),
network = network,
name = blockchain.fullName,
symbol = blockchain.currency,
iconUrl = getCoinIconUrl(blockchain),
decimals = blockchain.decimals(),
isCustom = isCustomCoin(network),
)
return createCoin(network)
}
fun createCoin(
@ -69,6 +84,20 @@ class CryptoCurrencyFactory {
return createCoin(blockchain, extraDerivationPath, derivationStyleProvider)
}
fun createCoin(network: Network): CryptoCurrency.Coin {
val blockchain = Blockchain.fromId(network.id.value)
return CryptoCurrency.Coin(
id = getCoinId(network, blockchain.toCoinId()),
network = network,
name = blockchain.fullName,
symbol = blockchain.currency,
iconUrl = getCoinIconUrl(blockchain),
decimals = blockchain.decimals(),
isCustom = isCustomCoin(network),
)
}
fun createToken(
token: Token,
networkId: String,

View file

@ -11,6 +11,22 @@ fun getBlockchain(networkId: Network.ID): Blockchain {
return Blockchain.fromId(networkId.value)
}
fun getNetwork(networkId: Network.ID, derivationPath: Network.DerivationPath): Network {
val blockchain = getBlockchain(networkId)
return Network(
id = networkId,
backendId = blockchain.toNetworkId(),
name = blockchain.getNetworkName(),
isTestnet = blockchain.isTestnet(),
derivationPath = derivationPath,
currencySymbol = blockchain.currency,
standardType = getNetworkStandardType(blockchain),
hasFiatFeeRate = blockchain.feePaidCurrency() !is FeePaidCurrency.FeeResource,
canHandleTokens = blockchain.canHandleTokens(),
)
}
fun getNetwork(
blockchain: Blockchain,
extraDerivationPath: String?,
@ -30,10 +46,11 @@ fun getNetwork(
currencySymbol = blockchain.currency,
standardType = getNetworkStandardType(blockchain),
hasFiatFeeRate = blockchain.feePaidCurrency() !is FeePaidCurrency.FeeResource,
canHandleTokens = blockchain.canHandleTokens(),
)
}
private fun getNetworkDerivationPath(
fun getNetworkDerivationPath(
blockchain: Blockchain,
extraDerivationPath: String?,
cardDerivationStyleProvider: DerivationStyleProvider?,

View file

@ -4,7 +4,6 @@ import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchain.common.IconsUtil
import com.tangem.blockchainsdk.utils.toCoinId
import com.tangem.datasource.api.tangemTech.models.UserTokensResponse
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.CryptoCurrency.ID
import com.tangem.domain.tokens.model.Network
import com.tangem.blockchain.common.Token as SdkToken
@ -65,10 +64,10 @@ fun getCoinIconUrl(blockchain: Blockchain): String? {
return coinId?.let(::getTokenIconUrlFromDefaultHost)
}
fun List<UserTokensResponse.Token>.hasCoinForToken(token: CryptoCurrency.Token): Boolean {
fun List<UserTokensResponse.Token>.hasCoinForToken(network: Network): Boolean {
return any {
val blockchain = getBlockchain(networkId = token.network.id)
val tokenDerivation = token.network.derivationPath.value
val blockchain = getBlockchain(networkId = network.id)
val tokenDerivation = network.derivationPath.value
it.id == blockchain.toCoinId() && it.derivationPath == tokenDerivation
}
}
@ -85,7 +84,7 @@ private fun getCurrencyIdBody(network: Network): CurrencyIdBody {
}
}
private fun getTokenIconUrlFromDefaultHost(tokenId: String): String {
fun getTokenIconUrlFromDefaultHost(tokenId: String): String {
return buildString {
append(DEFAULT_TOKENS_ICONS_HOST)
append('/')

View file

@ -7,8 +7,10 @@ import kotlinx.coroutines.yield
import timber.log.Timber
import kotlin.coroutines.cancellation.CancellationException
@Suppress("UnconditionalJumpStatementInLoop")
suspend fun <T> retryOnError(priority: Boolean = false, call: suspend () -> T): T {
@Suppress("UnconditionalJumpStatementInLoop", "MagicNumber")
suspend fun <T> retryOnError(priority: Boolean = false, startRetryDelay: Int = 500, call: suspend () -> T): T {
var currentDelay = startRetryDelay
var priorityCounter = 5
while (true) {
return try {
call()
@ -19,9 +21,12 @@ suspend fun <T> retryOnError(priority: Boolean = false, call: suspend () -> T):
Timber.e(e, "Error occurred during retryOnError block")
if (priority.not()) {
if (priority && priorityCounter > 0) {
--priorityCounter
} else {
yield()
delay(timeMillis = 500)
delay(timeMillis = currentDelay.toLong())
currentDelay *= 2
}
continue