Skip to content

Commit

Permalink
fix: simplify code and add license
Browse files Browse the repository at this point in the history
  • Loading branch information
HashEngineering committed Sep 20, 2023
1 parent 6aaa93b commit 48d18dc
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
/*
* Copyright 2023 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.dash.wallet.integrations.maya.api

open class MayaException(message: String): Exception(message) {
companion object {
const val SWAP_ERROR = "deposit_error"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
/*
* Copyright 2023 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.dash.wallet.integrations.maya.api

import org.dash.wallet.integrations.maya.model.ExchangeRateResponse
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query

/**
* https://exchangerate.host/#/docs
*/
interface ExchangeRateApi {
@GET("latest")
suspend fun getRates(@Query("base") baseCurrencyCode: String): Response<ExchangeRateResponse>

@GET("latest")
suspend fun getRate(
@Query("base") baseCurrencyCode: String,
@Query("symbols") resultCurrencyCode: String
): Response<ExchangeRateResponse>

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright 2023 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.dash.wallet.integrations.maya.api

import kotlinx.coroutines.CoroutineScope
Expand All @@ -14,7 +31,7 @@ import java.util.concurrent.TimeUnit
import javax.inject.Inject

class FiatExchangeRateApiAggregator @Inject constructor(
val exchangeRateApi: ExchangeRateApi
private val exchangeRateApi: ExchangeRateApi
) {
companion object {
val log: Logger = LoggerFactory.getLogger(FiatExchangeRateApiAggregator::class.java)
Expand All @@ -33,12 +50,11 @@ class FiatExchangeRateApiAggregator @Inject constructor(

interface FiatExchangeRateProvider {
val fiatExchangeRate: Flow<ExchangeRate>
fun observeFiatRates(): Flow<List<ExchangeRate>>
fun observeFiatRate(currencyCode: String): Flow<ExchangeRate?>
}

class FiatExchangeRateAggregatedProvider @Inject constructor(
val fiatExchangeRateApi: FiatExchangeRateApiAggregator
private val fiatExchangeRateApi: FiatExchangeRateApiAggregator
) : FiatExchangeRateProvider {
companion object {
private val log = LoggerFactory.getLogger(FiatExchangeRateApiAggregator::class.java)
Expand All @@ -50,9 +66,6 @@ class FiatExchangeRateAggregatedProvider @Inject constructor(
)
private var poolListLastUpdated: Long = 0
override val fiatExchangeRate = MutableStateFlow(ExchangeRate(MayaConstants.DEFAULT_EXCHANGE_CURRENCY, "1.0"))
override fun observeFiatRates(): Flow<List<ExchangeRate>> {
TODO("Not yet implemented")
}

override fun observeFiatRate(currencyCode: String): Flow<ExchangeRate?> {
if (shouldRefresh()) {
Expand All @@ -77,7 +90,7 @@ class FiatExchangeRateAggregatedProvider @Inject constructor(
return poolListLastUpdated == 0L || now - poolListLastUpdated > UPDATE_FREQ_MS
}

suspend fun updateExchangeRates(currencyCode: String) {
private suspend fun updateExchangeRates(currencyCode: String) {
fiatExchangeRateApi.getRate(currencyCode)?.let { rate ->
fiatExchangeRate.value = rate
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
/*
* Copyright 2023 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.dash.wallet.integrations.maya.api

import android.content.Context
import android.content.Intent
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand All @@ -15,16 +30,13 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.bitcoinj.utils.Fiat
import org.dash.wallet.common.Configuration
import org.dash.wallet.common.WalletDataProvider
import org.dash.wallet.common.services.AuthenticationManager
import org.dash.wallet.common.services.NotificationService
import org.dash.wallet.common.services.TransactionMetadataProvider
import org.dash.wallet.common.services.analytics.AnalyticsService
import org.dash.wallet.integrations.maya.MayaWebApi
import org.dash.wallet.integrations.maya.model.PoolInfo
import org.dash.wallet.integrations.maya.utils.MayaConfig
import org.dash.wallet.integrations.maya.utils.MayaConstants
import org.slf4j.LoggerFactory
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
Expand All @@ -38,7 +50,8 @@ interface MayaApi {

suspend fun swap()
suspend fun reset()
//fun observePoolList(): Flow<List<PoolInfo>>

// fun observePoolList(): Flow<List<PoolInfo>>
fun observePoolList(fiatExchangeRate: Fiat): Flow<List<PoolInfo>>
}

Expand All @@ -49,18 +62,12 @@ class MayaApiAggregator @Inject constructor(
private val notificationService: NotificationService,
private val analyticsService: AnalyticsService,
private val config: MayaConfig,
private val globalConfig: Configuration,
private val securityFunctions: AuthenticationManager,
private val transactionMetadataProvider: TransactionMetadataProvider,
@ApplicationContext private val appContext: Context
private val transactionMetadataProvider: TransactionMetadataProvider
): MayaApi {
companion object {
private val log = LoggerFactory.getLogger(MayaApiAggregator::class.java)
private val UPDATE_FREQ_MS = TimeUnit.SECONDS.toMillis(30)
private const val CONFIRMED_STATUS = "confirmed"
private const val VALID_STATUS = "valid"
private const val MESSAGE_RECEIVED_STATUS = "received"
private const val MESSAGE_FAILED_STATUS = "failed"
}

private val params = walletDataProvider.networkParameters
Expand Down Expand Up @@ -162,7 +169,7 @@ class MayaApiAggregator @Inject constructor(
// }

override fun observePoolList(fiatExchangeRate: Fiat): Flow<List<PoolInfo>> {
log.info("observePoolList(${fiatExchangeRate.toFriendlyString()})")
log.info("observePoolList(${fiatExchangeRate.toFriendlyString()})")
if (shouldRefresh()) {
refreshRates(fiatExchangeRate)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright 2023 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.dash.wallet.integrations.maya.api

import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
package org.dash.wallet.integrations.maya
/*
* Copyright 2023 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.dash.wallet.integrations.maya.api

import org.dash.wallet.common.services.analytics.AnalyticsService
import org.dash.wallet.integrations.maya.model.PoolInfo
Expand All @@ -8,7 +25,13 @@ import retrofit2.http.GET
import java.io.IOException
import javax.inject.Inject

/**
* https://docs.mayaprotocol.com/introduction/readme
*/
interface MayaEndpoint {
/**
* https://docs.mayaprotocol.com/dev-docs/mayachain/concepts/querying-mayachain
*/
@GET("pools")
suspend fun getPoolInfo(): Response<List<PoolInfo>>
}
Expand All @@ -29,7 +52,7 @@ open class MayaWebApi @Inject constructor(
return if (response.isSuccessful && response.body()?.isNotEmpty() == true) {
response.body()!!.toList()
} else {
log.error("getWithdrawalLimits not successful; ${response.code()} : ${response.message()}")
log.error("getPoolInfo not successful; ${response.code()} : ${response.message()}")
listOf()
}
} catch (ex: Exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright 2023 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.dash.wallet.integrations.maya.api

import okhttp3.OkHttpClient
Expand Down Expand Up @@ -30,4 +47,4 @@ class RemoteDataSource @Inject constructor() {
}
}.build()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright 2023 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.dash.wallet.integrations.maya.di

import dagger.Binds
Expand All @@ -8,7 +25,7 @@ import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import org.dash.wallet.common.WalletDataProvider
import org.dash.wallet.integrations.maya.MayaEndpoint
import org.dash.wallet.integrations.maya.api.MayaEndpoint
import org.dash.wallet.integrations.maya.api.ExchangeRateApi
import org.dash.wallet.integrations.maya.api.FiatExchangeRateAggregatedProvider
import org.dash.wallet.integrations.maya.api.FiatExchangeRateProvider
Expand Down Expand Up @@ -51,4 +68,4 @@ abstract class MayaModule {
@Binds
@Singleton
abstract fun bindFiatExchangeRateApi(fiatApi: FiatExchangeRateAggregatedProvider): FiatExchangeRateProvider
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
/*
* Copyright 2023 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.dash.wallet.integrations.maya.model

/**
* Response from api.exchangerates.host
*/
data class ExchangeRateResponse(
val motd: Motd,
val success: Boolean,
val base: String,
val date: String,
val rates: Map<String, Double>
)

data class Motd(
val msg: String,
val url: String
)
Loading

0 comments on commit 48d18dc

Please sign in to comment.