Updated on 2026-08-14
This commit is contained in:
parent
5006ccd343
commit
f4755463b0
9 changed files with 42 additions and 19 deletions
|
|
@ -1,12 +1,17 @@
|
|||
package com.tangem.tap.network.auth
|
||||
|
||||
import com.tangem.datasource.local.preferences.AppPreferencesStore
|
||||
import com.tangem.datasource.local.preferences.PreferencesKeys
|
||||
import com.tangem.datasource.local.preferences.utils.getSyncOrDefault
|
||||
import com.tangem.datasource.local.userwallet.UserWalletsStore
|
||||
import com.tangem.lib.auth.ExpressAuthProvider
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
internal class DefaultExpressAuthProvider(
|
||||
private val userWalletsStore: UserWalletsStore,
|
||||
private val appPreferencesStore: AppPreferencesStore,
|
||||
) : ExpressAuthProvider {
|
||||
|
||||
private var uuid = AtomicReference(UUID.randomUUID())
|
||||
|
|
@ -18,4 +23,18 @@ internal class DefaultExpressAuthProvider(
|
|||
override fun getSessionId(): String {
|
||||
return uuid.get().toString()
|
||||
}
|
||||
|
||||
override fun getRefCode(): String {
|
||||
val addedWalletsWithRings = runBlocking {
|
||||
appPreferencesStore.getSyncOrDefault(
|
||||
key = PreferencesKeys.ADDED_WALLETS_WITH_RING_KEY,
|
||||
default = emptySet(),
|
||||
)
|
||||
}
|
||||
|
||||
val userWalletId = userWalletsStore.selectedUserWalletOrNull?.walletId?.stringValue
|
||||
?: error("No user id provided")
|
||||
|
||||
return if (addedWalletsWithRings.contains(userWalletId)) "ring" else ""
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package com.tangem.tap.network.auth.di
|
|||
|
||||
import com.tangem.datasource.api.common.AuthProvider
|
||||
import com.tangem.datasource.config.ConfigManager
|
||||
import com.tangem.datasource.local.preferences.AppPreferencesStore
|
||||
import com.tangem.datasource.local.userwallet.UserWalletsStore
|
||||
import com.tangem.lib.auth.ExpressAuthProvider
|
||||
import com.tangem.lib.auth.StakeKitAuthProvider
|
||||
|
|
@ -29,8 +30,14 @@ class AuthModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideExpressAuthProvider(userWalletsStore: UserWalletsStore): ExpressAuthProvider {
|
||||
return DefaultExpressAuthProvider(userWalletsStore = userWalletsStore)
|
||||
fun provideExpressAuthProvider(
|
||||
userWalletsStore: UserWalletsStore,
|
||||
appPreferencesStore: AppPreferencesStore,
|
||||
): ExpressAuthProvider {
|
||||
return DefaultExpressAuthProvider(
|
||||
userWalletsStore = userWalletsStore,
|
||||
appPreferencesStore = appPreferencesStore,
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
|
|
|||
|
|
@ -46,7 +46,9 @@ internal class SwitchEnvironmentInterceptor(
|
|||
|
||||
private fun Request.Builder.addHeaders(headers: Map<String, Provider<String>>): Request.Builder {
|
||||
headers.forEach { (name, valueProvider) ->
|
||||
addHeader(name = name, value = valueProvider())
|
||||
val value = valueProvider()
|
||||
|
||||
if (value.isNotBlank()) addHeader(name = name, value = value)
|
||||
}
|
||||
|
||||
return this
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ internal class Express(
|
|||
put(key = "user-id", value = Provider(expressAuthProvider::getUserId))
|
||||
put(key = "session-id", value = Provider(expressAuthProvider::getSessionId))
|
||||
putAll(from = RequestHeader.AppVersionPlatformHeaders(appVersionProvider).values)
|
||||
put(key = "refcode", value = Provider(expressAuthProvider::getRefCode))
|
||||
}
|
||||
|
||||
private fun getApiKey(isProd: Boolean): String {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,10 @@ import com.tangem.datasource.api.express.models.request.AssetsRequestBody
|
|||
import com.tangem.datasource.api.express.models.request.ExchangeSentRequestBody
|
||||
import com.tangem.datasource.api.express.models.request.PairsRequestBody
|
||||
import com.tangem.datasource.api.express.models.response.*
|
||||
import retrofit2.http.*
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.Query
|
||||
|
||||
/**
|
||||
* Interface of Tangem Express API (new swap mechanism)
|
||||
|
|
@ -33,7 +36,6 @@ interface TangemExpressApi {
|
|||
@Query("toDecimals") toDecimals: Int,
|
||||
@Query("providerId") providerId: String,
|
||||
@Query("rateType") rateType: String,
|
||||
@Header("refcode") refCode: String? = null,
|
||||
): ApiResponse<ExchangeQuoteResponse>
|
||||
|
||||
@GET("exchange-data")
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ internal class ProdApiConfigsManagerTest(private val model: Model) {
|
|||
every { appVersionProvider.versionName } returns VERSION_NAME
|
||||
every { expressAuthProvider.getUserId() } returns EXPRESS_USER_ID
|
||||
every { expressAuthProvider.getSessionId() } returns EXPRESS_SESSION_ID
|
||||
every { expressAuthProvider.getRefCode() } returns EXPRESS_REF_CODE
|
||||
every { stakeKitAuthProvider.getApiKey() } returns STAKE_KIT_API_KEY
|
||||
}
|
||||
|
||||
|
|
@ -65,6 +66,7 @@ internal class ProdApiConfigsManagerTest(private val model: Model) {
|
|||
const val VERSION_NAME = "debug"
|
||||
const val EXPRESS_USER_ID = "express_user_id"
|
||||
const val EXPRESS_SESSION_ID = "express_session_id"
|
||||
const val EXPRESS_REF_CODE = "express_ref_code"
|
||||
const val EXPRESS_API_KEY = "express_api_key"
|
||||
const val EXPRESS_DEV_API_KEY = "express_dev_api_key"
|
||||
const val STAKE_KIT_API_KEY = "stake_kit_api_key"
|
||||
|
|
@ -111,6 +113,7 @@ internal class ProdApiConfigsManagerTest(private val model: Model) {
|
|||
},
|
||||
"user-id" to Provider { EXPRESS_USER_ID },
|
||||
"session-id" to Provider { EXPRESS_SESSION_ID },
|
||||
"refcode" to Provider { EXPRESS_REF_CODE },
|
||||
"version" to Provider { VERSION_NAME },
|
||||
"platform" to Provider { "android" },
|
||||
),
|
||||
|
|
|
|||
|
|
@ -20,9 +20,6 @@ import com.tangem.datasource.api.express.models.response.SwapPair
|
|||
import com.tangem.datasource.api.express.models.response.SwapPairsWithProviders
|
||||
import com.tangem.datasource.api.express.models.response.TxDetails
|
||||
import com.tangem.datasource.crypto.DataSignatureVerifier
|
||||
import com.tangem.datasource.local.preferences.AppPreferencesStore
|
||||
import com.tangem.datasource.local.preferences.PreferencesKeys
|
||||
import com.tangem.datasource.local.preferences.utils.getSyncOrDefault
|
||||
import com.tangem.domain.common.util.derivationStyleProvider
|
||||
import com.tangem.domain.tokens.model.CryptoCurrency
|
||||
import com.tangem.domain.walletmanager.WalletManagersFacade
|
||||
|
|
@ -41,18 +38,16 @@ import timber.log.Timber
|
|||
import java.io.IOException
|
||||
import java.math.BigDecimal
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import com.tangem.datasource.api.express.models.request.LeastTokenInfo as NetworkLeastTokenInfo
|
||||
|
||||
@Suppress("LongParameterList", "LargeClass")
|
||||
internal class DefaultSwapRepository @Inject constructor(
|
||||
internal class DefaultSwapRepository(
|
||||
private val tangemExpressApi: TangemExpressApi,
|
||||
private val coroutineDispatcher: CoroutineDispatcherProvider,
|
||||
private val walletManagersFacade: WalletManagersFacade,
|
||||
private val userWalletsListManager: UserWalletsListManager,
|
||||
private val errorsDataConverter: ErrorsDataConverter,
|
||||
private val dataSignatureVerifier: DataSignatureVerifier,
|
||||
private val appPreferencesStore: AppPreferencesStore,
|
||||
moshi: Moshi,
|
||||
) : SwapRepository {
|
||||
|
||||
|
|
@ -204,11 +199,6 @@ internal class DefaultSwapRepository @Inject constructor(
|
|||
): Either<DataError, QuoteModel> {
|
||||
return withContext(coroutineDispatcher.io) {
|
||||
try {
|
||||
val addedWalletsWithRings = appPreferencesStore.getSyncOrDefault(
|
||||
key = PreferencesKeys.ADDED_WALLETS_WITH_RING_KEY,
|
||||
default = emptySet(),
|
||||
)
|
||||
|
||||
val response = tangemExpressApi.getExchangeQuote(
|
||||
fromContractAddress = fromContractAddress,
|
||||
fromNetwork = fromNetwork,
|
||||
|
|
@ -219,7 +209,6 @@ internal class DefaultSwapRepository @Inject constructor(
|
|||
toDecimals = toDecimals,
|
||||
providerId = providerId,
|
||||
rateType = rateType.name.lowercase(),
|
||||
refCode = if (addedWalletsWithRings.contains(userWalletId.stringValue)) "ring" else null,
|
||||
).getOrThrow()
|
||||
QuoteModel(
|
||||
toTokenAmount = createFromAmountWithOffset(response.toAmount, response.toDecimals),
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ internal class SwapDataModule {
|
|||
walletManagerFacade: WalletManagersFacade,
|
||||
userWalletsListManager: UserWalletsListManager,
|
||||
errorsDataConverter: ErrorsDataConverter,
|
||||
appPreferencesStore: AppPreferencesStore,
|
||||
@NetworkMoshi moshi: Moshi,
|
||||
): SwapRepository {
|
||||
return DefaultSwapRepository(
|
||||
|
|
@ -43,7 +42,6 @@ internal class SwapDataModule {
|
|||
userWalletsListManager = userWalletsListManager,
|
||||
errorsDataConverter = errorsDataConverter,
|
||||
dataSignatureVerifier = dataSignature,
|
||||
appPreferencesStore = appPreferencesStore,
|
||||
moshi = moshi,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,4 +5,6 @@ interface ExpressAuthProvider {
|
|||
fun getUserId(): String
|
||||
|
||||
fun getSessionId(): String
|
||||
|
||||
fun getRefCode(): String
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue