Skip to content

Commit e1bcf11

Browse files
committed
fix(tokens): only update reserve balance while buy/sell is idle
Also proactively update USDF token accounts post finalization when buying with reserves or selling Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent b8d111d commit e1bcf11

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import com.getcode.utils.trace
3737
import com.getcode.view.BaseViewModel2
3838
import com.getcode.view.LoadingSuccessState
3939
import dagger.hilt.android.lifecycle.HiltViewModel
40+
import kotlinx.coroutines.delay
4041
import kotlinx.coroutines.flow.combine
4142
import kotlinx.coroutines.flow.distinctUntilChanged
4243
import kotlinx.coroutines.flow.filter
@@ -367,6 +368,8 @@ class BuySellSwapTokenViewModel @Inject constructor(
367368
usdf = balance,
368369
nativeAmount = balance.convertingTo(rate),
369370
)
371+
}.filter {
372+
stateFlow.value.buyProgress.isIdle && stateFlow.value.sellProgress.isIdle
370373
}.onEach {
371374
dispatchEvent(Event.OnReservesUpdated(TokenWithBalance(Token.usdf, it.nativeAmount)))
372375
}.launchIn(viewModelScope)
@@ -540,9 +543,9 @@ class BuySellSwapTokenViewModel @Inject constructor(
540543
of = token,
541544
).onSuccess { swapId ->
542545
dispatchEvent(Event.OnPurchaseSubmitted(token, swapId))
546+
dispatchEvent(Event.UpdateBuyState(loading = false, success = true))
543547
// buy submitted from reserves, drop reserves balance
544548
tokenController.subtract(Token.usdf, amount)
545-
dispatchEvent(Event.UpdateBuyState(loading = false, success = true))
546549
}.onFailure {
547550
dispatchEvent(Event.UpdateBuyState(loading = false, success = false))
548551
BottomBarManager.showError(
@@ -569,9 +572,9 @@ class BuySellSwapTokenViewModel @Inject constructor(
569572
of = token,
570573
).onSuccess { swapId ->
571574
dispatchEvent(Event.OnSellSubmitted(token, swapId))
575+
dispatchEvent(Event.UpdateSellState(loading = false, success = true))
572576
// sell submitted, drop from balance
573577
tokenController.subtract(token, amount)
574-
dispatchEvent(Event.UpdateSellState(loading = false, success = true))
575578
}.onFailure {
576579
dispatchEvent(Event.UpdateSellState(loading = false, success = false))
577580
BottomBarManager.showError(
@@ -598,15 +601,19 @@ class BuySellSwapTokenViewModel @Inject constructor(
598601
}.onResult(
599602
onSuccess = {
600603
val token = stateFlow.value.tokenWithBalance!!.token
604+
val isUsingReserves = stateFlow.value.purpose is TokenSwapPurpose.Buy ||
605+
stateFlow.value.purpose is TokenSwapPurpose.Sell
601606
viewModelScope.launch { tokenController.updateTokenAccount(token) }
607+
if (isUsingReserves) {
608+
viewModelScope.launch { tokenController.updateTokenAccount(Mint.usdf) }
609+
}
602610
viewModelScope.launch {
603611
// update activity feed to grab the tx as a result of this buy/sell
604612
feedCoordinator.fetchSinceLatest()
605613
}
606614
dispatchEvent(Event.UpdateProcessingState(loading = false, success = true))
607615
},
608616
onError = {
609-
// TODO: show error
610617
dispatchEvent(Event.UpdateProcessingState(loading = false, success = false, error = true))
611618
}
612619
).launchIn(viewModelScope)

services/opencode/src/main/kotlin/com/getcode/opencode/controllers/TokenController.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class TokenController @Inject constructor(
320320

321321
trace(
322322
tag = TAG,
323-
message = "Token metadata cache miss for ${mint.base58()}..., fetching from network",
323+
message = "Token metadata cache miss for ${mint.base58()}, fetching from network",
324324
type = TraceType.Process
325325
)
326326

@@ -338,7 +338,7 @@ class TokenController @Inject constructor(
338338
.onFailure { error ->
339339
trace(
340340
tag = TAG,
341-
message = "Failed to fetch token metadata for ${mint.base58()}...: ${error.message}",
341+
message = "Failed to fetch token metadata for ${mint.base58()}: ${error.message}",
342342
type = TraceType.Error
343343
)
344344
}
@@ -357,7 +357,7 @@ class TokenController @Inject constructor(
357357
val token = _state.value.tokens[mint]
358358
trace(
359359
tag = TAG,
360-
message = "Token selected: ${token?.symbol ?: mint.base58()}...",
360+
message = "Token selected: ${token?.symbol ?: mint.base58()}",
361361
type = TraceType.User
362362
)
363363
selectedToken.edit { it[mintPreferenceKey] = mint.base58() }
@@ -382,7 +382,7 @@ class TokenController @Inject constructor(
382382
): Result<List<HistoricalMintData>> {
383383
trace(
384384
tag = TAG,
385-
message = "Fetching historical market cap for ${mint.base58()}..., range=$windowedRange",
385+
message = "Fetching historical market cap for ${mint.base58()}, range=$windowedRange",
386386
type = TraceType.Process
387387
)
388388

@@ -451,7 +451,7 @@ class TokenController @Inject constructor(
451451
if (!fetchingMints.add(mint)) {
452452
trace(
453453
tag = TAG,
454-
message = "Skipping duplicate fetch for ${mint.base58()}...",
454+
message = "Skipping duplicate fetch for ${mint.base58()}",
455455
type = TraceType.Process
456456
)
457457
return
@@ -460,7 +460,7 @@ class TokenController @Inject constructor(
460460
try {
461461
trace(
462462
tag = TAG,
463-
message = "Fetching token account for ${mint.base58()}...",
463+
message = "Fetching token account for ${mint.base58()}",
464464
type = TraceType.Process
465465
)
466466

@@ -473,7 +473,7 @@ class TokenController @Inject constructor(
473473
if (account == null) {
474474
trace(
475475
tag = TAG,
476-
message = "No primary account found for ${mint.base58()}...",
476+
message = "No primary account found for ${mint.base58()}",
477477
type = TraceType.Error
478478
)
479479
return

0 commit comments

Comments
 (0)