Skip to content

Commit a6cc530

Browse files
committed
fix(ocp): serially fetch token verified state if not proviced or in cache
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 462a1f9 commit a6cc530

4 files changed

Lines changed: 35 additions & 13 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import kotlinx.coroutines.flow.Flow
1313
import kotlinx.coroutines.flow.SharingStarted
1414
import kotlinx.coroutines.flow.callbackFlow
1515
import kotlinx.coroutines.flow.emptyFlow
16+
import kotlinx.coroutines.flow.first
1617
import kotlinx.coroutines.flow.flatMapLatest
1718
import kotlinx.coroutines.flow.shareIn
1819
import kotlinx.coroutines.launch
20+
import kotlinx.coroutines.suspendCancellableCoroutine
1921
import javax.inject.Inject
2022
import javax.inject.Singleton
23+
import kotlin.coroutines.resume
2124

2225
@Singleton
2326
class CurrencyController @Inject constructor(
@@ -50,6 +53,19 @@ class CurrencyController @Inject constructor(
5053
}
5154
}
5255

56+
suspend fun getLiveMintData(
57+
scope: CoroutineScope,
58+
mint: Mint,
59+
tag: String? = null
60+
): Result<LiveMintDataResponse> = runCatching {
61+
callbackFlow {
62+
val reference = repository.streamMintData(scope = scope, mints = listOf(mint), tag = tag) {
63+
trySend(it)
64+
}
65+
awaitClose { reference.cancel() }
66+
}.first()
67+
}
68+
5369
suspend fun getMintMetadata(
5470
addresses: List<Mint>
5571
): Result<List<MintMetadata>> {

services/opencode/src/main/kotlin/com/getcode/opencode/internal/transactors/GiveBillTransactor.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.getcode.opencode.internal.transactors
22

33
import com.getcode.ed25519.Ed25519.KeyPair
4+
import com.getcode.opencode.controllers.CurrencyController
45
import com.getcode.opencode.controllers.MessagingController
56
import com.getcode.opencode.controllers.TransactionController
67
import com.getcode.opencode.internal.extensions.exchangeDataFor
@@ -25,6 +26,7 @@ import kotlinx.coroutines.cancel
2526
import kotlin.time.Duration
2627

2728
internal class GiveBillTransactor(
29+
private val currencyController: CurrencyController,
2830
private val messagingController: MessagingController,
2931
private val transactionController: TransactionController,
3032
private val scope: CoroutineScope,
@@ -80,14 +82,16 @@ internal class GiveBillTransactor(
8082
val sendingAmount = amount
8183
?: return logAndFail(GiveTransactorError.Other(message = "No amount. Did you call with() first?"))
8284

83-
val verifiedState = if (providedVerifiedState != null) {
84-
providedVerifiedState
85-
} else {
86-
verifiedProtoManager.getVerifiedStateFor(
87-
sendingAmount.rate.currency,
88-
desiredToken.address
89-
)
90-
} ?: return logAndFail(GiveTransactorError.Other(message = "No verified state found"))
85+
// Resolve the verified state for the given currency/token pair using a fallback chain:
86+
// 1. Use the provided state directly if available
87+
// 2. Otherwise, check the local proto store
88+
// 3. If still missing, fetch live mint data (which internally persists to the store) and re-query
89+
val verifiedState = providedVerifiedState
90+
?: verifiedProtoManager.getVerifiedStateFor(sendingAmount.rate.currency, desiredToken.address)
91+
?: currencyController.getLiveMintData(scope, desiredToken.address)
92+
.map { verifiedProtoManager.getVerifiedStateFor(sendingAmount.rate.currency, desiredToken.address) }
93+
.getOrNull()
94+
?: return logAndFail(GiveTransactorError.Other("Failed to get verified state"))
9195

9296
val exchangeData = verifiedState.exchangeDataFor(
9397
amount = sendingAmount,

services/opencode/src/main/kotlin/com/getcode/opencode/internal/transactors/ReceiveGiftCardTransactor.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ internal class ReceiveGiftCardTransactor(
6666
return@timedTraceSuspend logAndFail(ReceiveGiftTransactorError.FailedToQuery(message = "No accounts found"))
6767
}
6868

69-
onStep("account query")
7069
val info = accounts.values.first()
7170

7271
if (info.claimState == AccountInfo.ClaimState.Claimed) {

services/opencode/src/main/kotlin/com/getcode/opencode/managers/BillTransactionManager.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.getcode.opencode.managers
22

33
import com.getcode.opencode.controllers.AccountController
4+
import com.getcode.opencode.controllers.CurrencyController
45
import com.getcode.opencode.controllers.MessagingController
56
import com.getcode.opencode.controllers.TransactionController
67
import com.getcode.opencode.internal.manager.VerifiedProtoManager
@@ -33,6 +34,7 @@ import kotlin.time.Duration
3334
@Singleton
3435
class BillTransactionManager @Inject constructor(
3536
private val accountController: AccountController,
37+
private val currencyController: CurrencyController,
3638
private val messagingController: MessagingController,
3739
private val transactionController: TransactionController,
3840
private val tokenProvider: TokenMetadataProvider,
@@ -69,10 +71,11 @@ class BillTransactionManager @Inject constructor(
6971
val childScope = CoroutineScope(sharedScope.coroutineContext + Job())
7072

7173
val transactor = GiveBillTransactor(
72-
messagingController,
73-
transactionController,
74-
childScope,
75-
verifiedProtoManager,
74+
currencyController = currencyController,
75+
messagingController = messagingController,
76+
transactionController = transactionController,
77+
scope = childScope,
78+
verifiedProtoManager = verifiedProtoManager,
7679
).apply {
7780
with(token, amount, owner, billExchangeDataTimeout, verifiedState)
7881
}

0 commit comments

Comments
 (0)