Updated on 2026-08-14

This commit is contained in:
Tangem 2024-07-16 13:26:37 +03:00
parent 0cd5575681
commit 0391e50dc8
12 changed files with 198 additions and 106 deletions

View file

@ -25,6 +25,9 @@ import com.tangem.domain.core.lce.LceFlow
import com.tangem.domain.core.lce.lceFlow
import com.tangem.domain.staking.model.*
import com.tangem.domain.staking.model.action.EnterAction
import com.tangem.domain.staking.model.action.StakingActionCommonType
import com.tangem.domain.staking.model.transaction.StakingGasEstimate
import com.tangem.domain.staking.model.transaction.ActionParams
import com.tangem.domain.staking.model.transaction.StakingTransaction
import com.tangem.domain.staking.repositories.StakingRepository
import com.tangem.domain.tokens.model.CryptoCurrency
@ -37,7 +40,6 @@ import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.math.BigDecimal
@Suppress("LargeClass", "LongParameterList")
internal class DefaultStakingRepository(
@ -153,29 +155,28 @@ internal class DefaultStakingRepository(
}
}
override suspend fun createEnterAction(
integrationId: String,
amount: BigDecimal,
address: String,
validatorAddress: String,
token: Token,
): EnterAction {
override suspend fun createEnterAction(params: ActionParams): EnterAction {
return withContext(dispatchers.io) {
val body = EnterActionRequestBody(
integrationId = integrationId,
addresses = Address(address),
args = EnterActionRequestBody.EnterActionRequestBodyArgs(
amount = amount.toFormattedString(token.decimals),
inputToken = tokenConverter.convertBack(token),
validatorAddress = validatorAddress,
),
)
val body = createActionRequestBody(params)
val response = stakeKitApi.createEnterAction(body)
enterActionResponseConverter.convert(response.getOrThrow())
}
}
override suspend fun estimateGas(params: ActionParams): StakingGasEstimate {
return withContext(dispatchers.io) {
val body = createActionRequestBody(params)
val gasEstimateDTO = when (params.actionCommonType) {
StakingActionCommonType.ENTER -> stakeKitApi.estimateGasOnEnter(body)
StakingActionCommonType.EXIT -> stakeKitApi.estimateGasOnExit(body)
}
gasEstimateConverter.convert(gasEstimateDTO.getOrThrow())
}
}
override suspend fun constructTransaction(transactionId: String): StakingTransaction {
return withContext(dispatchers.io) {
val transactionResponse = stakeKitApi.constructTransaction(
@ -423,6 +424,18 @@ internal class DefaultStakingRepository(
}
}
private fun createActionRequestBody(params: ActionParams): ActionRequestBody {
return ActionRequestBody(
integrationId = params.integrationId,
addresses = Address(params.address),
args = ActionRequestBody.EnterActionRequestBodyArgs(
amount = params.amount.toFormattedString(params.token.decimals),
inputToken = tokenConverter.convertBack(params.token),
validatorAddress = params.validatorAddress,
),
)
}
private fun findPrefetchedYield(yields: List<Yield>, currencyId: String, symbol: String): Yield? {
return yields.find { it.token.coinGeckoId == currencyId && it.token.symbol == symbol }
}