Skip to content
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
12 changes: 10 additions & 2 deletions Android/wallet/src/main/java/com/flow/wallet/wallet/BaseWallet.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.flow.wallet.wallet

import android.util.Log
import com.flow.wallet.account.Account
import com.flow.wallet.crypto.HasherImpl
import com.flow.wallet.errors.WalletError
Expand Down Expand Up @@ -163,6 +164,7 @@ abstract class BaseWallet(
// Try to load from cache first
val cachedAccounts = loadCache()
if (cachedAccounts != null) {
Log.d("BaseWallet", "Cache LOADED. Keys: ${cachedAccounts.accounts.keys}")
val accountsMap = mutableMapOf<ChainId, List<FlowAccount>>()

// Convert cached string keys back to ChainId
Expand Down Expand Up @@ -191,20 +193,26 @@ abstract class BaseWallet(
}

private suspend fun fetchAllNetworkAccounts() {
val newAccounts = mutableMapOf<ChainId, MutableList<Account>>()
val newFlowAccounts = mutableMapOf<ChainId, List<FlowAccount>>()
val newAccounts = _accounts.toMutableMap()
val newFlowAccounts = flowAccounts.toMutableMap()
Comment on lines +196 to +197
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initialization of newAccounts and newFlowAccounts creates mutable copies of the current state, but when network fetches return empty results, these copies won't contain the cached data. Consider initializing these maps with the existing cached data to ensure cached accounts are preserved.


// Fetch accounts from all networks in parallel
coroutineScope {
val networkFetches = networks.map { network ->
async {
try {
val accounts = fetchAccountsForNetwork(network)
Log.d("BaseWallet", "Fetch result for $network: ${accounts.size} accounts")
if (accounts.isNotEmpty()) {
newFlowAccounts[network] = accounts
newAccounts[network] = accounts.map {
Account(it, network, getKeyForAccount(), securityDelegate)
}.toMutableList()
} else {
val cached = flowAccounts[network]
if (!cached.isNullOrEmpty()) {
Log.w("BaseWallet", "Network returned 0 accounts for $network, but cache has ${cached.size}. KEEPING CACHE due to potential indexer latency.")
}
}
Comment on lines +211 to 216
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic logs a warning about keeping cache but doesn't actually preserve the cached accounts in the newFlowAccounts and newAccounts maps. The cached data should be explicitly added to these maps to prevent account loss.

} catch (e: Exception) {
println("Error fetching accounts for network $network: ${e.message}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class KeyWallet(
p256PublicKey?.let { publicKey ->
try {
val encodedKey = publicKey.toFlowIndexerHex()
Log.d(TAG, "Fetching P256 accounts for key: ${encodedKey.take(10)}...")
Log.d(TAG, "Fetching P256 accounts for key: ${encodedKey.take(10)}... on network $network")
val p256Accounts = withTimeout(baseTimeout) {
Network.findFlowAccountByKey(encodedKey, network)
}
Expand All @@ -148,7 +148,7 @@ class KeyWallet(
secp256k1PublicKey?.let { publicKey ->
try {
val encodedKey = publicKey.toFlowIndexerHex()
Log.d(TAG, "Fetching SECP256k1 accounts for key: ${encodedKey.take(10)}...")
Log.d(TAG, "Fetching SECP256k1 accounts for key: ${encodedKey.take(10)}... on network $network")
val secp256k1Accounts = withTimeout(baseTimeout) {
Network.findFlowAccountByKey(encodedKey, network)
}
Expand Down
Loading