Skip to content

Commit 3d22c4f

Browse files
committed
chore(tokens): update underlying token account when add/subtract
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 32a2d75 commit 3d22c4f

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

apps/flipcash/shared/tokens/src/main/kotlin/BuySellSwapTokenViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ class BuySellSwapTokenViewModel @Inject constructor(
492492
of = token,
493493
).onSuccess {
494494
dispatchEvent(Event.OnPurchaseSubmitted(token))
495+
// buy submitted from reserves, drop reserves balance
495496
tokenController.subtract(Token.usdf, amount)
496497
dispatchEvent(Event.UpdateBuyState(loading = false, success = true))
497498
}.onFailure {
@@ -503,7 +504,6 @@ class BuySellSwapTokenViewModel @Inject constructor(
503504
}
504505
}.launchIn(viewModelScope)
505506

506-
507507
eventFlow
508508
.filterIsInstance<Event.ProceedWithSale>()
509509
.map { it.amount }

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import androidx.datastore.preferences.preferencesDataStoreFile
1010
import androidx.lifecycle.DefaultLifecycleObserver
1111
import androidx.lifecycle.LifecycleOwner
1212
import androidx.lifecycle.ProcessLifecycleOwner
13-
import com.codeinc.opencode.gen.currency.v1.mint
1413
import com.getcode.opencode.exchange.Exchange
15-
import com.getcode.opencode.internal.manager.VerifiedProtoManager
1614
import com.getcode.opencode.internal.model.LiveMintDataResponse
1715
import com.getcode.opencode.internal.model.WindowedRange
1816
import com.getcode.opencode.model.accounts.AccountCluster
@@ -42,7 +40,6 @@ import kotlinx.coroutines.Job
4240
import kotlinx.coroutines.SupervisorJob
4341
import kotlinx.coroutines.flow.Flow
4442
import kotlinx.coroutines.flow.MutableStateFlow
45-
import kotlinx.coroutines.flow.combine
4643
import kotlinx.coroutines.flow.debounce
4744
import kotlinx.coroutines.flow.distinctUntilChanged
4845
import kotlinx.coroutines.flow.filter
@@ -226,7 +223,7 @@ class TokenController @Inject constructor(
226223
val balance = mintBalances.value[token.address] ?: Fiat.Zero
227224
if (balance.decimalValue == 0.0) {
228225
// attempt to fetch prior to modifying balance
229-
fetchBalanceForToken(token.address)
226+
updateTokenAccount(token.address)
230227
} else {
231228
val updatedBalance = operation(balance)
232229
mintBalances.update { it + (token.address to updatedBalance) }
@@ -237,12 +234,16 @@ class TokenController @Inject constructor(
237234
val balanceAdditionAmount =
238235
fiat.nativeAmount.convertingTo(exchange.rateToUsd(fiat.rate.currency)!!)
239236
modifyBalance(token) { it + balanceAdditionAmount }
237+
// fetch token account to get latest additional metadata (cost basis)
238+
updateTokenAccount(token.address)
240239
}
241240

242241
suspend fun subtract(token: Token, fiat: LocalFiat) {
243242
val balanceReductionAmount =
244243
fiat.nativeAmount.convertingTo(exchange.rateToUsd(fiat.rate.currency)!!)
245244
modifyBalance(token) { it - balanceReductionAmount }
245+
// fetch token account to get latest additional metadata (cost basis)
246+
updateTokenAccount(token.address)
246247
}
247248

248249
suspend fun update() {
@@ -368,7 +369,7 @@ class TokenController @Inject constructor(
368369
}
369370
}
370371

371-
private suspend fun fetchBalanceForToken(mint: Mint) {
372+
private suspend fun updateTokenAccount(mint: Mint) {
372373
val owner = cluster.value
373374
if (owner == null) {
374375
trace(
@@ -427,13 +428,31 @@ class TokenController @Inject constructor(
427428
val token = result.token
428429
when {
429430
account == null -> throw IllegalStateException("No account found for token with mint ${token.symbol}")
430-
else -> token to Fiat.tokenBalance(account.balance, token = token)
431+
else -> {
432+
// use the latest streamed supply value if available
433+
val currentSupply = tokens.value.find { it.address == account.mint }?.launchpadMetadata?.currentCirculatingSupplyQuarks
434+
?: token.launchpadMetadata?.currentCirculatingSupplyQuarks ?: 0
435+
436+
val updatedToken = token.copy(
437+
launchpadMetadata = token.launchpadMetadata?.copy(currentCirculatingSupplyQuarks = currentSupply)
438+
)
439+
val tokenBalance = Fiat.tokenBalance(account.balance, token = updatedToken)
440+
val costBasis = Fiat(fiat = account.usdCostBasis)
441+
442+
TokenWithBalance(
443+
token = updatedToken,
444+
balance = tokenBalance,
445+
appreciation = tokenBalance - costBasis
446+
)
447+
}
431448
}
432-
}?.onSuccess { (token, tokenBalance) ->
433-
mintBalances.update { it + (mint to tokenBalance) }
449+
}?.onSuccess { (token, balance, appreciation) ->
450+
mintBalances.update { it + (token.address to balance) }
451+
mintUsdAppreciationMap.update { it + (token.address to appreciation) }
452+
tokens.update { (it + token).distinctBy { t -> t.address } }
434453
trace(
435454
tag = "Tokens",
436-
message = "Updated balance for ${token.symbol} is ${tokenBalance.formatted()} USD",
455+
message = "Updated balance for ${token.symbol} is ${balance.formatted()} USD",
437456
type = TraceType.Process
438457
)
439458
tokenFetchState[mint]?.store(false)

0 commit comments

Comments
 (0)