Updated on 2026-08-14

This commit is contained in:
Tangem 2021-11-03 16:42:43 +03:00
parent fb3d9cd2d6
commit 939f6e87b6
17 changed files with 129 additions and 81 deletions

View file

@ -4,13 +4,20 @@ import retrofit2.http.GET
import retrofit2.http.Query
interface MoonpayApi {
@GET( MOOONPAY_IP_ADDRESS_REQUEST_URL)
@GET(MOOONPAY_IP_ADDRESS_REQUEST_URL)
suspend fun getUserStatus(
@Query("apiKey") moonpayApiKey: String
@Query("apiKey") moonpayApiKey: String,
): MoonPayUserStatus
@GET(MOOONPAY_CURRENCIES_REQUEST_URL)
suspend fun getCurrencies(
@Query("apiKey") moonpayApiKey: String,
): List<MoonpayCurrencies>
companion object {
const val MOOONPAY_BASE_URL = "https://api.moonpay.com/v4/"
const val MOOONPAY_IP_ADDRESS_REQUEST_URL = "ip_address/"
const val MOOONPAY_BASE_URL = "https://api.moonpay.com/"
const val MOOONPAY_IP_ADDRESS_REQUEST_URL = "v4/ip_address/"
const val MOOONPAY_CURRENCIES_REQUEST_URL = "v3/currencies/"
}
}

View file

@ -1,9 +1,12 @@
package com.tangem.tap.network.moonpay
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.tangem.common.services.Result
import com.tangem.common.services.performRequest
import com.tangem.common.services.retryIO
import com.tangem.tap.network.createRetrofitInstance
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
class MoonpayService {
@ -12,13 +15,75 @@ class MoonpayService {
.create(MoonpayApi::class.java)
}
suspend fun getUserStatus(moonpayApiKey: String): Result<MoonPayUserStatus> {
return performRequest { moonpayApi.getUserStatus(moonpayApiKey) }
suspend fun getMoonpayStatus(moonpayApiKey: String): Result<MoonpayStatus> {
return try {
coroutineScope {
val userStatusDeferred =
retryIO { async { moonpayApi.getUserStatus(moonpayApiKey) } }
val currenciesDeferred =
retryIO { async { moonpayApi.getCurrencies(moonpayApiKey) } }
val userStatus = userStatusDeferred.await()
val currenciesToBuy = mutableListOf<String>()
val currenciesToSell = mutableListOf<String>()
currenciesDeferred.await().forEach { currencyStatus ->
if (currencyStatus.type != "crypto" || currencyStatus.isSuspended ||
!currencyStatus.supportsLiveMode
) {
return@forEach
}
if (userStatus.countryCode == "USA") {
if (!currencyStatus.isSupportedInUS) return@forEach
if (currencyStatus.notAllowedUSStates.contains(userStatus.stateCode)) return@forEach
}
val currencyCode = currencyStatus.code.uppercase()
currenciesToBuy.add(currencyCode)
if (currencyStatus.isSellSupported) currenciesToSell.add(currencyCode)
}
Result.Success(MoonpayStatus(
isBuyAllowed = userStatus.isBuyAllowed,
isSellAllowed = userStatus.isSellAllowed,
availableToBuy = if (userStatus.isBuyAllowed) currenciesToBuy else emptyList(),
availableToSell = if (userStatus.isSellAllowed) currenciesToSell else emptyList()
))
}
} catch (error: Error) {
Result.Failure(error)
}
}
}
data class MoonpayStatus(
val isBuyAllowed: Boolean,
val isSellAllowed: Boolean,
val availableToBuy: List<String>,
val availableToSell: List<String>,
)
@JsonClass(generateAdapter = true)
data class MoonPayUserStatus(
val isBuyAllowed: Boolean,
val isSellAllowed: Boolean
val isSellAllowed: Boolean,
@Json(name = "isAllowed")
val isMoonpayAllowed: Boolean,
@Json(name = "alpha3")
val countryCode: String,
@Json(name = "state")
val stateCode: String,
)
@JsonClass(generateAdapter = true)
data class MoonpayCurrencies(
val type: String,
val code: String,
val supportsLiveMode: Boolean = false,
val isSuspended: Boolean = true,
val isSupportedInUS: Boolean = false,
val isSellSupported: Boolean = false,
val notAllowedUSStates: List<String> = emptyList(),
)