Skip to content

Commit cb0c128

Browse files
authored
Merge pull request #2416 from onflow/fix_transaction_issues
fix: transaction and coa connect
2 parents 875ef25 + 29a2869 commit cb0c128

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

app/src/main/java/com/flowfoundation/wallet/manager/evm/DAppEVMConnectionManager.kt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import kotlinx.coroutines.flow.MutableStateFlow
1111
import kotlinx.coroutines.flow.StateFlow
1212
import kotlinx.coroutines.flow.asStateFlow
1313
import androidx.core.content.edit
14+
import com.flowfoundation.wallet.network.ApiService
15+
import com.flowfoundation.wallet.network.retrofitApi
16+
import java.math.BigDecimal
1417

1518
object DAppEVMConnectionManager {
1619
private val TAG = DAppEVMConnectionManager::class.java.simpleName
@@ -24,6 +27,7 @@ object DAppEVMConnectionManager {
2427

2528
private val _availableAccounts = MutableStateFlow<List<DAppEVMAccount>>(emptyList())
2629
val availableAccounts: StateFlow<List<DAppEVMAccount>> = _availableAccounts.asStateFlow()
30+
private val service by lazy { retrofitApi().create(ApiService::class.java) }
2731

2832
init {
2933
loadSelectedAccount()
@@ -71,16 +75,36 @@ object DAppEVMConnectionManager {
7175
logd(TAG, "Found COA account: $coaAddress")
7276

7377
// Query COA balance
78+
var rawBalance: BigDecimal? = null
7479
val coaBalance = try {
75-
cadenceQueryCOATokenBalance()?.let { balance ->
76-
balance.formatPrice(includeSymbol = true)
77-
}
80+
rawBalance = cadenceQueryCOATokenBalance()
81+
rawBalance?.formatPrice(includeSymbol = true)
7882
} catch (e: Exception) {
7983
logd(TAG, "Error querying COA balance: ${e.message}")
8084
null
8185
}
8286

83-
accounts.add(DAppEVMAccount(coaAddress, DAppEVMAccountType.COA, coaBalance))
87+
val coaAccount = DAppEVMAccount(coaAddress, DAppEVMAccountType.COA, coaBalance)
88+
accounts.add(coaAccount)
89+
90+
// Check if COA should be auto-selected (Balance > 0 or has NFTs)
91+
val hasBalance = rawBalance != null && rawBalance > BigDecimal.ZERO
92+
var hasNFTs = false
93+
94+
if (!hasBalance) {
95+
try {
96+
val nftResponse = service.getEVMNFTCollections(coaAddress)
97+
val totalNftCount = nftResponse.data?.sumOf { it.count ?: 0 } ?: 0
98+
hasNFTs = nftResponse.data?.isNotEmpty() == true && totalNftCount > 0
99+
} catch (e: Exception) {
100+
// Ignore NFT API errors
101+
}
102+
}
103+
104+
if (hasBalance || hasNFTs) {
105+
logd(TAG, "COA account has balance or NFTs, setting as selected: $coaAddress")
106+
setSelectedAccount(coaAccount)
107+
}
84108
}
85109

86110
// Load EOA account

app/src/main/java/com/flowfoundation/wallet/reactnative/bridge/handlers/AccountBridgeHandler.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,11 @@ class AccountBridgeHandler(private val reactContext: ReactApplicationContext) {
267267

268268
fun getSignKeyIndex(): Double {
269269
return try {
270-
val address = WalletManager.selectedWalletAddress()
271-
if (address.isEmpty()) {
272-
logw(TAG, "getSignKeyIndex() - no selected address")
273-
return 0.0
274-
}
270+
val address = WalletManager.getCurrentFlowWalletAddress()
275271

276272
val cryptoProvider = CryptoProviderManager.getCurrentCryptoProvider()
277-
if (cryptoProvider == null) {
278-
logw(TAG, "getSignKeyIndex() - no crypto provider")
273+
274+
if (address.isNullOrBlank() || cryptoProvider == null) {
279275
return 0.0
280276
}
281277

@@ -536,7 +532,7 @@ class AccountBridgeHandler(private val reactContext: ReactApplicationContext) {
536532
// Filter for LocalSwitchAccount entries (profiles stored locally but not logged in)
537533
switchList.filterIsInstance<com.flowfoundation.wallet.manager.account.model.LocalSwitchAccount>().forEach { localAccount ->
538534
logd(TAG, "getRecoverableProfiles() - processing LocalSwitchAccount: ${localAccount.username}")
539-
535+
540536
val mainEmojiInfo = createEmojiInfo(localAccount.address)
541537
val mainAccount = RNBridge.WalletAccount(
542538
id = "main_${localAccount.address}",

0 commit comments

Comments
 (0)