@@ -9,8 +9,11 @@ import com.getcode.opencode.model.accounts.AccountInfo
99import com.getcode.opencode.model.accounts.GiftCardAccount
1010import com.getcode.opencode.model.financial.LocalFiat
1111import com.getcode.opencode.model.transactions.TransactionMetadata
12+ import com.getcode.solana.keys.PublicKey
1213import com.getcode.utils.CodeServerError
1314import com.getcode.utils.ErrorUtils
15+ import com.getcode.utils.timedTrace
16+ import com.getcode.utils.timedTraceSuspend
1417import kotlinx.coroutines.CoroutineScope
1518import kotlinx.coroutines.cancel
1619
@@ -37,45 +40,60 @@ internal class ReceiveGiftCardTransactor(
3740 )
3841 )
3942
43+ return timedTraceSuspend(" Gift card claim processing" ) { onStep ->
44+
4045 // before we can receive the gift card
4146 // we need to determine the balance of it
4247 val accounts = accountController.getAccounts(
4348 accountOwner = giftCard.cluster,
4449 requestingOwner = ownerKey
45- ).getOrElse { return logAndFail(ReceiveGiftTransactorError .FailedToQuery ()) }
46- .takeIf { it.isNotEmpty() }
47- ? : return logAndFail(ReceiveGiftTransactorError .FailedToQuery ())
50+ ).getOrElse {
51+ onStep(" account query" )
52+ return @timedTraceSuspend logAndFail(ReceiveGiftTransactorError .FailedToQuery ())
53+ }.takeIf { it.isNotEmpty() }
54+ ? : run {
55+ onStep(" account query" )
56+ return @timedTraceSuspend logAndFail(ReceiveGiftTransactorError .FailedToQuery ())
57+ }
4858
49- val info = accounts.values.first()
59+ onStep(" account query" )
60+ val info = accounts.values.first()
5061
51- if (info.claimState == AccountInfo .ClaimState .Claimed ) {
52- return logAndFail(ReceiveGiftTransactorError .AlreadyClaimed ())
53- }
62+ if (info.claimState == AccountInfo .ClaimState .Claimed ) {
63+ onStep(" pre-claim checks" )
64+ return @timedTraceSuspend logAndFail(ReceiveGiftTransactorError .AlreadyClaimed ())
65+ }
5466
55- if (info.claimState == AccountInfo .ClaimState .Expired || info.claimState == AccountInfo .ClaimState .Unknown ) {
56- return logAndFail(ReceiveGiftTransactorError .Expired ())
57- }
67+ if (info.claimState == AccountInfo .ClaimState .Expired || info.claimState == AccountInfo .ClaimState .Unknown ) {
68+ onStep(" pre-claim checks" )
69+ return @timedTraceSuspend logAndFail(ReceiveGiftTransactorError .Expired ())
70+ }
5871
59- if (info.isGiftCardIssuer && ! claimIfOwned) {
60- return Result .failure(ReceiveGiftTransactorError .UsersGiftCard ())
61- }
72+ if (info.isGiftCardIssuer && ! claimIfOwned) {
73+ onStep(" pre-claim checks" )
74+ return @timedTraceSuspend Result .failure(ReceiveGiftTransactorError .UsersGiftCard ())
75+ }
6276
63- val exchangeData = info.originalExchangeData
64- val amount = LocalFiat (exchangeData)
77+ val exchangeData = info.originalExchangeData
78+ val amount = LocalFiat (exchangeData)
6579
66- return transactionController.receiveRemotely(
67- giftCard = giftCard,
68- amount = amount,
69- owner = ownerKey
70- ).fold(
71- onSuccess = { Result .success(amount) },
72- onFailure = {
73- if (it !is ReceiveGiftTransactorError ) {
74- ErrorUtils .handleError(it)
80+ return @timedTraceSuspend transactionController.receiveRemotely(
81+ giftCard = giftCard,
82+ amount = amount,
83+ owner = ownerKey
84+ ).fold(
85+ onSuccess = {
86+ onStep(" intent" )
87+ Result .success(amount) },
88+ onFailure = {
89+ onStep(" intent" )
90+ if (it !is ReceiveGiftTransactorError ) {
91+ ErrorUtils .handleError(it)
92+ }
93+ logAndFail(it)
7594 }
76- logAndFail(it)
77- }
78- )
95+ )
96+ }
7997 }
8098
8199 fun dispose () {
0 commit comments