@@ -11,6 +11,9 @@ import kotlinx.coroutines.flow.MutableStateFlow
1111import kotlinx.coroutines.flow.StateFlow
1212import kotlinx.coroutines.flow.asStateFlow
1313import androidx.core.content.edit
14+ import com.flowfoundation.wallet.network.ApiService
15+ import com.flowfoundation.wallet.network.retrofitApi
16+ import java.math.BigDecimal
1417
1518object 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
0 commit comments