Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response classes for Coin, BlockchainEntity and TokenEntity #19

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class MainViewModel(private val marketKit: MarketKit) : ViewModel() {
fun runTokensByTokenQuery() {
val queries = listOf(
TokenQuery(BlockchainType.Ethereum, TokenType.Native),
TokenQuery(BlockchainType.BinanceSmartChain, TokenType.Native),
TokenQuery(BlockchainType.BinanceSmartChain, TokenType.Native)
)

val coinsList = marketKit.tokens(queries)
Expand All @@ -376,6 +376,13 @@ class MainViewModel(private val marketKit: MarketKit) : ViewModel() {
}
}

fun runTokensReference() {
val coinsList = marketKit.tokens("0x11cdb42b0eb46d95f990bedd4695a6e3fa034978")
coinsList.forEach {
Log.w("AAA", "runTokensReference code: ${it.coin.code} name: ${it.coin.name} marketCapRank: ${it.coin.marketCapRank} coinType.id: ${it.type.id}")
}
}

fun runHistoricalPrice() {
val dateString = "01-12-2020"
val timestamp = SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.horizontalsystems.marketkit.models

data class BlockchainResponse(
val uid: String,
val name: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.os.Parcelable
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize

@Parcelize
Expand All @@ -18,9 +17,7 @@ data class Coin(
val uid: String,
val name: String,
val code: String,
@SerializedName("market_cap_rank")
val marketCapRank: Int? = null,
@SerializedName("coingecko_id")
val coinGeckoId: String? = null
) : Parcelable {
override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.horizontalsystems.marketkit.models

data class CoinResponse(
val uid: String,
val name: String,
val code: String,
val market_cap_rank: Int?,
val coingecko_id: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.horizontalsystems.marketkit.models

data class TokenResponse(
val coin_uid: String,
val blockchain_uid: String,
val type: String,
val decimals: Int?,
val address: String?,
val symbol: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ class HsProvider(baseUrl: String, apiKey: String) {
return service.getStatus()
}

fun allCoinsSingle(): Single<List<Coin>> {
fun allCoinsSingle(): Single<List<CoinResponse>> {
return service.getAllCoins()
}

fun allBlockchainsSingle(): Single<List<BlockchainEntity>> {
fun allBlockchainsSingle(): Single<List<BlockchainResponse>> {
return service.getAllBlockchains()
}

fun allTokensSingle(): Single<List<TokenEntity>> {
fun allTokensSingle(): Single<List<TokenResponse>> {
return service.getAllTokens()
}

Expand Down Expand Up @@ -394,13 +394,13 @@ class HsProvider(baseUrl: String, apiKey: String) {
fun getStatus(): Single<HsStatus>

@GET("coins/list")
fun getAllCoins(): Single<List<Coin>>
fun getAllCoins(): Single<List<CoinResponse>>

@GET("blockchains/list")
fun getAllBlockchains(): Single<List<BlockchainEntity>>
fun getAllBlockchains(): Single<List<BlockchainResponse>>

@GET("tokens/list")
fun getAllTokens(): Single<List<TokenEntity>>
fun getAllTokens(): Single<List<TokenResponse>>

companion object {
private const val marketInfoFields =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.horizontalsystems.marketkit.syncers

import android.util.Log
import io.horizontalsystems.marketkit.models.BlockchainEntity
import io.horizontalsystems.marketkit.models.Coin
import io.horizontalsystems.marketkit.models.TokenEntity
import io.horizontalsystems.marketkit.models.*
import io.horizontalsystems.marketkit.providers.HsProvider
import io.horizontalsystems.marketkit.storage.CoinStorage
import io.horizontalsystems.marketkit.storage.SyncerStateDao
Expand Down Expand Up @@ -37,9 +35,20 @@ class CoinSyncer(

if (!coinsOutdated && !blockchainsOutdated && !tokensOutdated) return

val coinsSingle = if (coinsOutdated) hsProvider.allCoinsSingle() else Single.just(listOf())
val blockchainsSingle = if (blockchainsOutdated) hsProvider.allBlockchainsSingle() else Single.just(listOf())
val tokensSingle = if (tokensOutdated) hsProvider.allTokensSingle() else Single.just(listOf())
val coinsSingle = if (coinsOutdated)
hsProvider.allCoinsSingle().map { it.map { coinResponse -> coinEntity(coinResponse) } }
else
Single.just(listOf())

val blockchainsSingle = if (blockchainsOutdated)
hsProvider.allBlockchainsSingle().map { it.map { blockchainResponse -> blockchainEntity(blockchainResponse) } }
else
Single.just(listOf())

val tokensSingle = if (tokensOutdated)
hsProvider.allTokensSingle().map { it.map { tokenResponse -> tokenEntity(tokenResponse) } }
else
Single.just(listOf())

disposable = Single.zip(coinsSingle, blockchainsSingle, tokensSingle) { r1, r2, r3 -> Triple(r1, r2, r3) }
.subscribeOn(Schedulers.io())
Expand All @@ -52,6 +61,32 @@ class CoinSyncer(
})
}

private fun coinEntity(response: CoinResponse): Coin =
Coin(
response.uid,
response.name,
response.code,
response.market_cap_rank,
response.coingecko_id
)

private fun blockchainEntity(response: BlockchainResponse): BlockchainEntity =
BlockchainEntity(response.uid, response.name)

private fun tokenEntity(response: TokenResponse): TokenEntity =
TokenEntity(
response.coin_uid,
response.blockchain_uid,
response.type,
response.decimals,

when (response.type) {
"eip20" -> response.address
"bep2" -> response.symbol
else -> response.address
}
)

fun stop() {
disposable?.dispose()
disposable = null
Expand Down