Updated on 2026-08-14

This commit is contained in:
Tangem 2024-06-21 21:35:19 +05:00
parent 0b671b4ac1
commit 1d75e884c8
15 changed files with 340 additions and 23 deletions

View file

@ -5,6 +5,7 @@ import com.squareup.moshi.Json
data class YieldBalanceRequestBody(
@Json(name = "addresses") val addresses: Address,
@Json(name = "args") val args: YieldBalanceRequestArgs,
@Json(name = "integrationId") val integrationId: String? = null,
) {
data class YieldBalanceRequestArgs(

View file

@ -1,25 +1,50 @@
package com.tangem.datasource.local.token
import com.tangem.datasource.api.stakekit.models.response.model.BalanceDTO
import com.tangem.datasource.api.stakekit.models.response.model.YieldBalanceWrapperDTO
import com.tangem.datasource.local.datastore.core.StringKeyDataStore
import com.tangem.utils.extensions.addOrReplace
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
internal class DefaultStakingBalanceStore(
private val dataStore: StringKeyDataStore<List<BalanceDTO>>,
private val dataStore: StringKeyDataStore<List<YieldBalanceWrapperDTO>>,
) : StakingBalanceStore {
override fun get(): Flow<List<BalanceDTO>> {
override fun get(): Flow<List<YieldBalanceWrapperDTO>> {
return dataStore.get(STAKING_BALANCE_KEY)
}
override suspend fun getSyncOrNull(): List<BalanceDTO>? {
override suspend fun getSyncOrNull(): List<YieldBalanceWrapperDTO>? {
return dataStore.getSyncOrNull(STAKING_BALANCE_KEY)
}
override suspend fun store(items: List<BalanceDTO>) {
override suspend fun store(items: List<YieldBalanceWrapperDTO>) {
return dataStore.store(STAKING_BALANCE_KEY, items)
}
override fun get(integrationId: String): Flow<List<BalanceDTO>> {
return dataStore.get(STAKING_BALANCE_KEY)
.map { balances ->
balances.filter { it.integrationId == integrationId }
.flatMap { it.balances }
}
}
override suspend fun getSyncOrNull(integrationId: String): List<BalanceDTO>? {
return dataStore.getSyncOrNull(STAKING_BALANCE_KEY)
?.firstOrNull { it.integrationId == integrationId }?.balances
}
override suspend fun store(integrationId: String, item: YieldBalanceWrapperDTO) {
val balances = dataStore.getSyncOrNull(STAKING_BALANCE_KEY)
?.toMutableList()
?.addOrReplace(item) { item.integrationId == integrationId }
?: listOf(item)
return dataStore.store(STAKING_BALANCE_KEY, balances)
}
companion object {
private const val STAKING_BALANCE_KEY = "STAKING_BALANCE_KEY"
}

View file

@ -1,13 +1,20 @@
package com.tangem.datasource.local.token
import com.tangem.datasource.api.stakekit.models.response.model.BalanceDTO
import com.tangem.datasource.api.stakekit.models.response.model.YieldBalanceWrapperDTO
import kotlinx.coroutines.flow.Flow
interface StakingBalanceStore {
fun get(): Flow<List<BalanceDTO>>
fun get(): Flow<List<YieldBalanceWrapperDTO>>
suspend fun getSyncOrNull(): List<BalanceDTO>?
suspend fun getSyncOrNull(): List<YieldBalanceWrapperDTO>?
suspend fun store(items: List<BalanceDTO>)
suspend fun store(items: List<YieldBalanceWrapperDTO>)
fun get(integrationId: String): Flow<List<BalanceDTO>>
suspend fun getSyncOrNull(integrationId: String): List<BalanceDTO>?
suspend fun store(integrationId: String, item: YieldBalanceWrapperDTO)
}