Skip to content

Commit 1d60e39

Browse files
committed
fix(tokens): resolve token via tokenCoordinator when stateFlow race loses
The ConfirmPhantomTransaction and Coinbase purchase handlers read stateFlow.value.tokenWithBalance?.token synchronously. After the amount-entry refactor the async combine flow that populates tokenWithBalance can lose the race, leaving the snapshot null. Extract resolveToken() that falls back to tokenCoordinator.getTokenMetadata(mint) (memory -> Room -> network) when the flow hasn't delivered yet. Also bumps phantom-connect-kmp 2.0.2-1.0.0 -> 2.0.2-1.0.1. Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent ff6fd4a commit 1d60e39

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

apps/flipcash/shared/tokens/src/main/kotlin/com/flipcash/app/tokens/ui/SwapViewModel.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,12 @@ class SwapViewModel @Inject constructor(
599599
)
600600
)
601601
dispatchEvent(Event.UpdateBuyState(loading = true))
602-
val token = stateFlow.value.tokenWithBalance?.token ?: run {
603-
handlePhantomError(IllegalStateException("Token not available"))
604-
return@onEach
605-
}
602+
val token = resolveToken()
603+
?: run {
604+
handlePhantomError(IllegalStateException("Token not available"))
605+
return@onEach
606+
}
607+
606608
signAndSendPhantomTransaction(token, amountFiat)
607609
} catch (e: CancellationException) {
608610
throw e
@@ -839,8 +841,7 @@ class SwapViewModel @Inject constructor(
839841
)
840842
)
841843
dispatchEvent(Event.UpdateBuyState(loading = true))
842-
val token = stateFlow.value.tokenWithBalance?.token ?: return@onEach
843-
844+
val token = resolveToken() ?: return@onEach
844845
executeCoinbasePurchase(amountFiat, token)
845846
}
846847

@@ -861,6 +862,14 @@ class SwapViewModel @Inject constructor(
861862
}.launchIn(viewModelScope)
862863
}
863864

865+
private suspend fun resolveToken(): Token? {
866+
val mint = stateFlow.value.purpose?.mint
867+
val token = stateFlow.value.tokenWithBalance?.token
868+
?: mint?.let { tokenCoordinator.getTokenMetadata(it).getOrNull()?.token }
869+
870+
return token
871+
}
872+
864873
private suspend fun executeCoinbasePurchase(
865874
amountFiat: VerifiedFiat,
866875
token: Token,

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ bugsnag-gradle-plugin = "1.1.0"
9393
rinku = "1.6.0"
9494
compose-unstyled = "2.8.0"
9595
haze = "2.0.0-alpha03"
96-
phantom-connect-kmp = "2.0.2-1.0.0"
96+
phantom-connect-kmp = "2.0.2-1.0.1"
9797
vico = "3.2.3"
9898

9999
androidxTestRules = "1.7.0"

0 commit comments

Comments
 (0)