Updated on 2026-08-14
This commit is contained in:
parent
246ab4b47e
commit
c4aba9a0b2
16 changed files with 365 additions and 0 deletions
40
data/polymarket/build.gradle.kts
Normal file
40
data/polymarket/build.gradle.kts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
plugins {
|
||||
alias(deps.plugins.android.library)
|
||||
alias(deps.plugins.kotlin.android)
|
||||
alias(deps.plugins.kotlin.kapt)
|
||||
alias(deps.plugins.hilt.android)
|
||||
id("configuration")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "com.tangem.data.polymarket"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
// region Kotlin
|
||||
implementation(deps.kotlin.coroutines)
|
||||
implementation(deps.arrow.core)
|
||||
// endregion
|
||||
|
||||
// region DI
|
||||
implementation(deps.hilt.android)
|
||||
kapt(deps.hilt.kapt)
|
||||
// endregion
|
||||
|
||||
// region Core
|
||||
api(projects.core.datasource)
|
||||
api(projects.core.utils)
|
||||
// endregion
|
||||
|
||||
// region Domain
|
||||
api(projects.domain.polymarket)
|
||||
// endregion
|
||||
|
||||
// region tests
|
||||
testImplementation(deps.test.coroutine)
|
||||
testImplementation(deps.test.junit5)
|
||||
testImplementation(deps.test.mockk)
|
||||
testImplementation(deps.test.truth)
|
||||
// endregion
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.tangem.data.polymarket
|
||||
|
||||
import arrow.core.Either
|
||||
import com.tangem.data.polymarket.converter.PolymarketEventConverter
|
||||
import com.tangem.datasource.api.common.response.getOrThrow
|
||||
import com.tangem.datasource.api.polymarket.PolymarketApi
|
||||
import com.tangem.domain.core.error.DataError
|
||||
import com.tangem.domain.polymarket.PolymarketRepository
|
||||
import com.tangem.domain.polymarket.model.PolymarketEvent
|
||||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||
import kotlinx.coroutines.withContext
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class DefaultPolymarketRepository @Inject constructor(
|
||||
private val polymarketApi: PolymarketApi,
|
||||
private val eventConverter: PolymarketEventConverter,
|
||||
private val dispatchers: CoroutineDispatcherProvider,
|
||||
) : PolymarketRepository {
|
||||
|
||||
override suspend fun getEvents(): Either<DataError, List<PolymarketEvent>> = withContext(dispatchers.io) {
|
||||
Either.catch {
|
||||
polymarketApi.getEvents(limit = DEFAULT_LIMIT, cursor = null)
|
||||
.getOrThrow()
|
||||
.events
|
||||
.map(eventConverter::convert)
|
||||
}.mapLeft {
|
||||
// DataError exposes only NoInternetConnection as a network failure.
|
||||
DataError.NetworkError.NoInternetConnection
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
|
||||
const val DEFAULT_LIMIT = 20
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package com.tangem.data.polymarket.converter
|
||||
|
||||
import com.tangem.datasource.api.polymarket.models.PolymarketEventDto
|
||||
import com.tangem.datasource.api.polymarket.models.PolymarketMarketDto
|
||||
import com.tangem.datasource.api.polymarket.models.PolymarketOutcomeDto
|
||||
import com.tangem.domain.polymarket.model.PolymarketEvent
|
||||
import com.tangem.domain.polymarket.model.PolymarketMarket
|
||||
import com.tangem.domain.polymarket.model.PolymarketOutcome
|
||||
import com.tangem.utils.converter.Converter
|
||||
import java.math.BigDecimal
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class PolymarketEventConverter @Inject constructor() : Converter<PolymarketEventDto, PolymarketEvent> {
|
||||
|
||||
override fun convert(value: PolymarketEventDto): PolymarketEvent {
|
||||
return PolymarketEvent(
|
||||
id = value.eventId,
|
||||
slug = value.slug,
|
||||
title = value.title,
|
||||
iconUrl = value.icon,
|
||||
imageUrl = value.image,
|
||||
volume = value.volume?.let(BigDecimal::valueOf),
|
||||
totalMarketsCount = value.totalMarketsCount,
|
||||
markets = value.markets.map(::convertMarket),
|
||||
)
|
||||
}
|
||||
|
||||
private fun convertMarket(dto: PolymarketMarketDto): PolymarketMarket {
|
||||
return PolymarketMarket(
|
||||
id = dto.id,
|
||||
title = dto.question,
|
||||
iconUrl = dto.icon,
|
||||
volume = dto.volume?.let(BigDecimal::valueOf),
|
||||
outcomes = dto.outcomes.map(::convertOutcome),
|
||||
)
|
||||
}
|
||||
|
||||
private fun convertOutcome(dto: PolymarketOutcomeDto): PolymarketOutcome {
|
||||
return PolymarketOutcome(
|
||||
assetId = dto.assetId,
|
||||
title = dto.label,
|
||||
probability = dto.probability?.let(BigDecimal::valueOf),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.tangem.data.polymarket.di
|
||||
|
||||
import com.tangem.data.polymarket.DefaultPolymarketRepository
|
||||
import com.tangem.domain.polymarket.PolymarketRepository
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
internal interface PolymarketDataModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
fun bindPolymarketRepository(impl: DefaultPolymarketRepository): PolymarketRepository
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue