@@ -10,9 +10,7 @@ import androidx.datastore.preferences.preferencesDataStoreFile
1010import androidx.lifecycle.DefaultLifecycleObserver
1111import androidx.lifecycle.LifecycleOwner
1212import androidx.lifecycle.ProcessLifecycleOwner
13- import com.codeinc.opencode.gen.currency.v1.mint
1413import com.getcode.opencode.exchange.Exchange
15- import com.getcode.opencode.internal.manager.VerifiedProtoManager
1614import com.getcode.opencode.internal.model.LiveMintDataResponse
1715import com.getcode.opencode.internal.model.WindowedRange
1816import com.getcode.opencode.model.accounts.AccountCluster
@@ -42,7 +40,6 @@ import kotlinx.coroutines.Job
4240import kotlinx.coroutines.SupervisorJob
4341import kotlinx.coroutines.flow.Flow
4442import kotlinx.coroutines.flow.MutableStateFlow
45- import kotlinx.coroutines.flow.combine
4643import kotlinx.coroutines.flow.debounce
4744import kotlinx.coroutines.flow.distinctUntilChanged
4845import kotlinx.coroutines.flow.filter
@@ -226,7 +223,7 @@ class TokenController @Inject constructor(
226223 val balance = mintBalances.value[token.address] ? : Fiat .Zero
227224 if (balance.decimalValue == 0.0 ) {
228225 // attempt to fetch prior to modifying balance
229- fetchBalanceForToken (token.address)
226+ updateTokenAccount (token.address)
230227 } else {
231228 val updatedBalance = operation(balance)
232229 mintBalances.update { it + (token.address to updatedBalance) }
@@ -237,12 +234,16 @@ class TokenController @Inject constructor(
237234 val balanceAdditionAmount =
238235 fiat.nativeAmount.convertingTo(exchange.rateToUsd(fiat.rate.currency)!! )
239236 modifyBalance(token) { it + balanceAdditionAmount }
237+ // fetch token account to get latest additional metadata (cost basis)
238+ updateTokenAccount(token.address)
240239 }
241240
242241 suspend fun subtract (token : Token , fiat : LocalFiat ) {
243242 val balanceReductionAmount =
244243 fiat.nativeAmount.convertingTo(exchange.rateToUsd(fiat.rate.currency)!! )
245244 modifyBalance(token) { it - balanceReductionAmount }
245+ // fetch token account to get latest additional metadata (cost basis)
246+ updateTokenAccount(token.address)
246247 }
247248
248249 suspend fun update () {
@@ -368,7 +369,7 @@ class TokenController @Inject constructor(
368369 }
369370 }
370371
371- private suspend fun fetchBalanceForToken (mint : Mint ) {
372+ private suspend fun updateTokenAccount (mint : Mint ) {
372373 val owner = cluster.value
373374 if (owner == null ) {
374375 trace(
@@ -427,13 +428,31 @@ class TokenController @Inject constructor(
427428 val token = result.token
428429 when {
429430 account == null -> throw IllegalStateException (" No account found for token with mint ${token.symbol} " )
430- else -> token to Fiat .tokenBalance(account.balance, token = token)
431+ else -> {
432+ // use the latest streamed supply value if available
433+ val currentSupply = tokens.value.find { it.address == account.mint }?.launchpadMetadata?.currentCirculatingSupplyQuarks
434+ ? : token.launchpadMetadata?.currentCirculatingSupplyQuarks ? : 0
435+
436+ val updatedToken = token.copy(
437+ launchpadMetadata = token.launchpadMetadata?.copy(currentCirculatingSupplyQuarks = currentSupply)
438+ )
439+ val tokenBalance = Fiat .tokenBalance(account.balance, token = updatedToken)
440+ val costBasis = Fiat (fiat = account.usdCostBasis)
441+
442+ TokenWithBalance (
443+ token = updatedToken,
444+ balance = tokenBalance,
445+ appreciation = tokenBalance - costBasis
446+ )
447+ }
431448 }
432- }?.onSuccess { (token, tokenBalance) ->
433- mintBalances.update { it + (mint to tokenBalance) }
449+ }?.onSuccess { (token, balance, appreciation) ->
450+ mintBalances.update { it + (token.address to balance) }
451+ mintUsdAppreciationMap.update { it + (token.address to appreciation) }
452+ tokens.update { (it + token).distinctBy { t -> t.address } }
434453 trace(
435454 tag = " Tokens" ,
436- message = " Updated balance for ${token.symbol} is ${tokenBalance .formatted()} USD" ,
455+ message = " Updated balance for ${token.symbol} is ${balance .formatted()} USD" ,
437456 type = TraceType .Process
438457 )
439458 tokenFetchState[mint]?.store(false )
0 commit comments