Updated on 2026-08-14

This commit is contained in:
Tangem 2024-08-12 22:01:26 +05:00
parent f748b8539d
commit f1238f5ef8
20 changed files with 528 additions and 88 deletions

View file

@ -16,6 +16,7 @@ import com.tangem.blockchain.common.transaction.Fee
import com.tangem.blockchainsdk.utils.fromNetworkId
import com.tangem.common.extensions.hexToBytes
import com.tangem.datasource.local.walletmanager.WalletManagersStore
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.Network
import com.tangem.domain.transaction.TransactionRepository
import com.tangem.domain.transaction.models.TransactionType
@ -24,6 +25,7 @@ import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.math.BigDecimal
import java.math.BigInteger
import com.tangem.blockchain.blockchains.tron.TransactionType as SdkTransactionType
@ -42,15 +44,15 @@ internal class DefaultTransactionRepository(
network: Network,
txExtras: TransactionExtras?,
hash: String?,
): TransactionData.Uncompiled? = withContext(coroutineDispatcherProvider.io) {
): TransactionData.Uncompiled = withContext(coroutineDispatcherProvider.io) {
val blockchain = Blockchain.fromId(network.id.value)
val walletManager = walletManagersFacade.getOrCreateWalletManager(
userWalletId = userWalletId,
blockchain = blockchain,
derivationPath = network.derivationPath.value,
)
) ?: error("Wallet manager not found")
return@withContext walletManager?.createTransactionDataInternal(
return@withContext walletManager.createTransactionDataInternal(
amount = amount,
fee = fee,
memo = memo,
@ -61,6 +63,48 @@ internal class DefaultTransactionRepository(
)
}
override suspend fun createApprovalTransaction(
amount: Amount,
fee: Fee,
contractAddress: String,
spenderAddress: String,
userWalletId: UserWalletId,
network: Network,
hash: String?,
): TransactionData.Uncompiled = withContext(coroutineDispatcherProvider.io) {
val blockchain = Blockchain.fromId(network.id.value)
val walletManager = walletManagersFacade.getOrCreateWalletManager(
userWalletId = userWalletId,
blockchain = blockchain,
derivationPath = network.derivationPath.value,
) ?: error("Wallet manager not found")
val approver = walletManager as? Approver ?: error("Cannot cast to Approver")
val approvalData = approver.getApproveData(
spenderAddress = spenderAddress,
value = amount,
)
val extras = createTransactionDataExtras(
data = approvalData,
network = network,
transactionType = TransactionType.APPROVE,
nonce = null,
gasLimit = null,
)
return@withContext createTransaction(
amount = amount,
fee = fee,
memo = null,
destination = contractAddress,
userWalletId = userWalletId,
network = network,
txExtras = extras,
hash = hash,
)
}
override suspend fun validateTransaction(
amount: Amount,
fee: Fee?,
@ -141,6 +185,28 @@ internal class DefaultTransactionRepository(
}
}
override suspend fun getAllowance(
userWalletId: UserWalletId,
cryptoCurrency: CryptoCurrency.Token,
spenderAddress: String,
): BigDecimal {
val walletManager = walletManagersFacade.getOrCreateWalletManager(userWalletId, cryptoCurrency.network)
val blockchain = Blockchain.fromId(cryptoCurrency.network.id.value)
val allowanceResult = (walletManager as? Approver)?.getAllowance(
spenderAddress,
Token(
symbol = blockchain.currency,
contractAddress = cryptoCurrency.contractAddress,
decimals = cryptoCurrency.decimals,
),
) ?: error("Cannot cast to Approver")
return allowanceResult.fold(
onSuccess = { it },
onFailure = { error(it) },
)
}
private fun convertToSdkTransactionType(transactionType: TransactionType): SdkTransactionType {
return when (transactionType) {
TransactionType.APPROVE -> SdkTransactionType.APPROVE