Skip to content

Commit 6d502a9

Browse files
committed
fix: check for token accounts when receiving gift cards and create them on the fly
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent 4a66d5b commit 6d502a9

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ internal class ReceiveGiftCardTransactor(
8989
.getOrNull()
9090

9191
if (token == null) {
92-
onStep("intent")
92+
onStep("pre-claim checks")
9393
return@timedTraceSuspend logAndFail(ReceiveGiftTransactorError.Other(message = "Token not found"))
9494
}
9595

@@ -99,12 +99,24 @@ internal class ReceiveGiftCardTransactor(
9999

100100
val requestingTokenOwner = requestingOwner.withTimelockForToken(token)
101101

102-
return@timedTraceSuspend transactionController.receiveRemotely(
103-
giftCard = giftCard,
104-
amount = amount,
105-
owner = requestingTokenOwner,
106-
mint = token.address
107-
).fold(
102+
// 3. create an account if we don't currently have one for this token
103+
val userAccountResult = if (!accountController.hasAccountFor(token.address)) {
104+
accountController.createUserAccount(
105+
ownerForMint = requestingTokenOwner,
106+
mint = token.address
107+
)
108+
} else {
109+
Result.success(Unit)
110+
}
111+
112+
return@timedTraceSuspend userAccountResult.map {
113+
transactionController.receiveRemotely(
114+
giftCard = giftCard,
115+
amount = amount,
116+
owner = requestingTokenOwner,
117+
mint = token.address
118+
)
119+
}.fold(
108120
onSuccess = {
109121
onStep("intent")
110122
Result.success(token to amount)

0 commit comments

Comments
 (0)