@@ -11,10 +11,8 @@ import androidx.lifecycle.DefaultLifecycleObserver
1111import androidx.lifecycle.LifecycleOwner
1212import androidx.lifecycle.ProcessLifecycleOwner
1313import com.flipcash.app.persistence.sources.TokenDataSource
14- import com.getcode.opencode.controllers.CurrencyController
1514import com.getcode.opencode.controllers.TokenController
1615import com.getcode.opencode.exchange.Exchange
17- import com.getcode.opencode.internal.model.LiveMintDataResponse
1816import com.getcode.opencode.internal.model.WindowedRange
1917import com.getcode.opencode.model.accounts.AccountCluster
2018import com.getcode.opencode.model.financial.CurrencyCode
@@ -48,7 +46,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
4846import kotlinx.coroutines.flow.debounce
4947import kotlinx.coroutines.flow.distinctUntilChanged
5048import kotlinx.coroutines.flow.filter
51- import kotlinx.coroutines.flow.filterIsInstance
5249import kotlinx.coroutines.flow.filterNotNull
5350import kotlinx.coroutines.flow.firstOrNull
5451import kotlinx.coroutines.flow.flatMapLatest
@@ -80,7 +77,6 @@ import javax.inject.Singleton
8077class TokenCoordinator @Inject constructor(
8178 @param:ApplicationContext private val context : Context ,
8279 private val tokenController : TokenController ,
83- private val currencyController : CurrencyController ,
8480 private val networkObserver : NetworkConnectivityListener ,
8581 private val exchange : Exchange ,
8682 private val dataSource : TokenDataSource ,
@@ -401,56 +397,54 @@ class TokenCoordinator @Inject constructor(
401397 delay(100 )
402398 trace(tag = TAG , message = " Reserve state stream started" , type = TraceType .Process )
403399
404- currencyController.streamLiveMintData (
400+ tokenController.streamReserveStates (
405401 scope = this ,
406402 mints = _state .map { it.tokens.keys.toList() }
407403 .distinctUntilChanged().debounce(300 ),
408- tag = " token-reserves"
409- ).filterIsInstance<LiveMintDataResponse .LaunchpadReserveState >()
410- .collect { response ->
411- trace(tag = TAG , message = " Received ${response.reserveStates.size} reserve state updates" , type = TraceType .Process )
412-
413- _state .update { state ->
414- var updatedTokens = state.tokens
415- var updatedBalances = state.balances
416-
417- response.reserveStates.forEach { update ->
418- val mint = update.reserveState.mint
419- val token = state.tokens[mint] ? : return @forEach
420- val launchpad = token.launchpadMetadata ? : return @forEach
421-
422- val updatedToken = token.copy(
423- launchpadMetadata = launchpad.copy(
424- currentCirculatingSupplyQuarks = update.reserveState.currentSupply
425- )
404+ ).collect { response ->
405+ trace(tag = TAG , message = " Received ${response.reserveStates.size} reserve state updates" , type = TraceType .Process )
406+
407+ _state .update { state ->
408+ var updatedTokens = state.tokens
409+ var updatedBalances = state.balances
410+
411+ response.reserveStates.forEach { update ->
412+ val mint = update.reserveState.mint
413+ val token = state.tokens[mint] ? : return @forEach
414+ val launchpad = token.launchpadMetadata ? : return @forEach
415+
416+ val updatedToken = token.copy(
417+ launchpadMetadata = launchpad.copy(
418+ currentCirculatingSupplyQuarks = update.reserveState.currentSupply
426419 )
427- updatedTokens = updatedTokens + (mint to updatedToken )
428-
429- state.balances[mint]?. let { balance ->
430- val exchangedValue = runCatching {
431- LocalFiat .valueExchangeIn(
432- amount = balance,
433- token = token ,
434- balance = balance ,
435- rate = Rate .oneToOne ,
436- debug = false ,
437- trace = false ,
438- ).underlyingTokenAmount
439- }.getOrNull()
440-
441- if (exchangedValue != null ) {
442- val newBalance = Fiat .tokenBalance(
443- quarks = exchangedValue.quarks,
444- token = token
445- )
446- updatedBalances = updatedBalances + (mint to newBalance )
447- }
420+ )
421+ updatedTokens = updatedTokens + (mint to updatedToken)
422+
423+ state.balances[mint]?. let { balance ->
424+ val exchangedValue = runCatching {
425+ LocalFiat .valueExchangeIn(
426+ amount = balance ,
427+ token = token ,
428+ balance = balance ,
429+ rate = Rate .oneToOne ,
430+ debug = false ,
431+ trace = false ,
432+ ).underlyingTokenAmount
433+ }.getOrNull()
434+
435+ if (exchangedValue != null ) {
436+ val newBalance = Fiat .tokenBalance(
437+ quarks = exchangedValue.quarks,
438+ token = token
439+ )
440+ updatedBalances = updatedBalances + (mint to newBalance)
448441 }
449442 }
450-
451- state.copy(tokens = updatedTokens, balances = updatedBalances)
452443 }
444+
445+ state.copy(tokens = updatedTokens, balances = updatedBalances)
453446 }
447+ }
454448 }
455449 }
456450
0 commit comments