Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan-Cerovsky committed Aug 12, 2024
1 parent 0f2359e commit b231d27
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
19 changes: 10 additions & 9 deletions sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ import cash.z.ecc.android.sdk.internal.transaction.TransactionEncoder
import cash.z.ecc.android.sdk.internal.transaction.TransactionEncoderImpl
import cash.z.ecc.android.sdk.model.Account
import cash.z.ecc.android.sdk.model.BlockHeight
import cash.z.ecc.android.sdk.model.FastestServersResult
import cash.z.ecc.android.sdk.model.FetchFiatCurrencyResult
import cash.z.ecc.android.sdk.model.ObserveFiatCurrencyResult
import cash.z.ecc.android.sdk.model.FastestServersResult
import cash.z.ecc.android.sdk.model.PercentDecimal
import cash.z.ecc.android.sdk.model.Proposal
import cash.z.ecc.android.sdk.model.TransactionOverview
Expand Down Expand Up @@ -113,7 +113,7 @@ import kotlin.time.Duration.Companion.seconds
* @property processor saves the downloaded compact blocks to the cache and then scans those blocks for
* data related to this wallet.
*/
@Suppress("TooManyFunctions")
@Suppress("TooManyFunctions", "LongParameterList")
class SdkSynchronizer private constructor(
private val synchronizerKey: SynchronizerKey,
private val storage: DerivedDataRepository,
Expand Down Expand Up @@ -154,7 +154,7 @@ class SdkSynchronizer private constructor(
backend: TypesafeBackend,
fastestServerFetcher: FastestServerFetcher,
fetchExchangeChangeUsd: UsdExchangeRateFetcher,
): CloseableSynchronizer {
): CloseableSynchronizer {
val synchronizerKey = SynchronizerKey(zcashNetwork, alias)

return mutex.withLock {
Expand Down Expand Up @@ -217,25 +217,26 @@ class SdkSynchronizer private constructor(

private val refreshExchangeRateUsd = MutableSharedFlow<Unit>(replay = 1).apply { tryEmit(Unit) }

private var lastExchangeRateValue = ObserveFiatCurrencyResult()

@OptIn(ExperimentalCoroutinesApi::class)
override val exchangeRateUsd =
channelFlow {
var lastValue = ObserveFiatCurrencyResult()
refreshExchangeRateUsd
.flatMapLatest {
flow {
emit(lastValue.copy(isLoading = true))
lastValue =
emit(lastExchangeRateValue.copy(isLoading = true))
lastExchangeRateValue =
when (val result = fetchExchangeChangeUsd()) {
is FetchFiatCurrencyResult.Error -> lastValue.copy(isLoading = false)
is FetchFiatCurrencyResult.Error -> lastExchangeRateValue.copy(isLoading = false)

is FetchFiatCurrencyResult.Success ->
lastValue.copy(
lastExchangeRateValue.copy(
isLoading = false,
currencyConversion = result.currencyConversion
)
}
emit(lastValue)
emit(lastExchangeRateValue)
}
}
.onEach { send(it) }
Expand Down
9 changes: 5 additions & 4 deletions sdk-lib/src/main/java/cash/z/ecc/android/sdk/Synchronizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import cash.z.ecc.android.sdk.block.processor.CompactBlockProcessor
import cash.z.ecc.android.sdk.exception.InitializeException
import cash.z.ecc.android.sdk.ext.ZcashSdk
import cash.z.ecc.android.sdk.internal.Derivation
import cash.z.ecc.android.sdk.internal.Files
import cash.z.ecc.android.sdk.internal.FastestServerFetcher
import cash.z.ecc.android.sdk.internal.Files
import cash.z.ecc.android.sdk.internal.SaplingParamTool
import cash.z.ecc.android.sdk.internal.Twig
import cash.z.ecc.android.sdk.internal.db.DatabaseCoordinator
Expand Down Expand Up @@ -699,9 +699,10 @@ interface Synchronizer {
processor = processor,
backend = backend,
fastestServerFetcher = FastestServerFetcher(backend = backend, network = processor.network),
fetchExchangeChangeUsd = UsdExchangeRateFetcher(
torDir = Files.getTorDir(context)
)
fetchExchangeChangeUsd =
UsdExchangeRateFetcher(
torDir = Files.getTorDir(context)
)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,47 @@ import java.io.File

internal class UsdExchangeRateFetcher(private val torDir: File) {
@Suppress("TooGenericExceptionCaught", "ReturnCount")
suspend operator fun invoke(): FetchFiatCurrencyResult =
retry {
val tor =
suspend operator fun invoke(): FetchFiatCurrencyResult {
val tor =
retry {
try {
Twig.info { "[USD] Tor client bootstrap" }
TorClient.new(torDir)
} catch (e: Exception) {
Twig.error(e) { "[USD] To client bootstrap failed" }
return@retry FetchFiatCurrencyResult.Error(exception = e)
return FetchFiatCurrencyResult.Error(exception = e)
}
}

return retry {
val rate =
try {
Twig.info { "[USD] Fetch start" }
tor.getExchangeRateUsd()
} catch (e: Exception) {
Twig.error(e) { "[USD] Fetch failed" }
return@retry FetchFiatCurrencyResult.Error(e)
return FetchFiatCurrencyResult.Error(e)
} finally {
tor.dispose()
}

Twig.debug { "[USD] Fetch success: $rate" }

return@retry FetchFiatCurrencyResult.Success(
FetchFiatCurrencyResult.Success(
currencyConversion =
FiatCurrencyConversion(
priceOfZec = rate.toDouble(),
timestamp = Clock.System.now()
)
)
}
}

/**
* Retry with geometric order.
*/
@Suppress("TooGenericExceptionCaught", "ReturnCount", "SwallowedException")
private suspend fun <T> retry(
private suspend inline fun <T> retry(
times: Int = 3,
initialDelay: Long = 1000,
multiplier: Double = 2.0,
Expand Down

0 comments on commit b231d27

Please sign in to comment.