|
| 1 | +package transaction_v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "google.golang.org/grpc/codes" |
| 8 | + "google.golang.org/grpc/status" |
| 9 | + |
| 10 | + commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1" |
| 11 | + transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2" |
| 12 | + |
| 13 | + async_account "github.com/code-payments/code-server/pkg/code/async/account" |
| 14 | + "github.com/code-payments/code-server/pkg/code/balance" |
| 15 | + "github.com/code-payments/code-server/pkg/code/common" |
| 16 | + "github.com/code-payments/code-server/pkg/code/data/account" |
| 17 | + "github.com/code-payments/code-server/pkg/code/data/action" |
| 18 | + "github.com/code-payments/code-server/pkg/grpc/client" |
| 19 | +) |
| 20 | + |
| 21 | +func (s *transactionServer) VoidGiftCard(ctx context.Context, req *transactionpb.VoidGiftCardRequest) (*transactionpb.VoidGiftCardResponse, error) { |
| 22 | + log := s.log.WithField("method", "VoidGiftCard") |
| 23 | + log = client.InjectLoggingMetadata(ctx, log) |
| 24 | + |
| 25 | + owner, err := common.NewAccountFromProto(req.Owner) |
| 26 | + if err != nil { |
| 27 | + log.WithError(err).Warn("invalid owner account") |
| 28 | + return nil, status.Error(codes.Internal, "") |
| 29 | + } |
| 30 | + log = log.WithField("owner_account", owner.PublicKey().ToBase58()) |
| 31 | + |
| 32 | + giftCardVault, err := common.NewAccountFromProto(req.GiftCardVault) |
| 33 | + if err != nil { |
| 34 | + log.WithError(err).Warn("invalid owner account") |
| 35 | + return nil, status.Error(codes.Internal, "") |
| 36 | + } |
| 37 | + log = log.WithField("gift_card_vault_account", giftCardVault.PublicKey().ToBase58()) |
| 38 | + |
| 39 | + signature := req.Signature |
| 40 | + req.Signature = nil |
| 41 | + if err := s.auth.Authenticate(ctx, owner, req, signature); err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + accountInfoRecord, err := s.data.GetAccountInfoByTokenAddress(ctx, giftCardVault.PublicKey().ToBase58()) |
| 46 | + switch err { |
| 47 | + case nil: |
| 48 | + if accountInfoRecord.AccountType != commonpb.AccountType_REMOTE_SEND_GIFT_CARD { |
| 49 | + return &transactionpb.VoidGiftCardResponse{ |
| 50 | + Result: transactionpb.VoidGiftCardResponse_NOT_FOUND, |
| 51 | + }, nil |
| 52 | + } |
| 53 | + case account.ErrAccountInfoNotFound: |
| 54 | + return &transactionpb.VoidGiftCardResponse{ |
| 55 | + Result: transactionpb.VoidGiftCardResponse_NOT_FOUND, |
| 56 | + }, nil |
| 57 | + default: |
| 58 | + log.WithError(err).Warn("failure getting gift card account info") |
| 59 | + return nil, status.Error(codes.Internal, "") |
| 60 | + } |
| 61 | + |
| 62 | + giftCardIssuedIntentRecord, err := s.data.GetOriginalGiftCardIssuedIntent(ctx, giftCardVault.PublicKey().ToBase58()) |
| 63 | + if err != nil { |
| 64 | + log.WithError(err).Warn("failure getting gift card issued intent record") |
| 65 | + return nil, status.Error(codes.Internal, "") |
| 66 | + } else if giftCardIssuedIntentRecord.InitiatorOwnerAccount != owner.PublicKey().ToBase58() { |
| 67 | + return &transactionpb.VoidGiftCardResponse{ |
| 68 | + Result: transactionpb.VoidGiftCardResponse_DENIED, |
| 69 | + }, nil |
| 70 | + } |
| 71 | + |
| 72 | + if time.Since(accountInfoRecord.CreatedAt) >= async_account.GiftCardExpiry { |
| 73 | + return &transactionpb.VoidGiftCardResponse{ |
| 74 | + Result: transactionpb.VoidGiftCardResponse_OK, |
| 75 | + }, nil |
| 76 | + } |
| 77 | + |
| 78 | + globalBalanceLock, err := balance.GetOptimisticVersionLock(ctx, s.data, giftCardVault) |
| 79 | + if err != nil { |
| 80 | + log.WithError(err).Warn("failure getting balance lock") |
| 81 | + return nil, status.Error(codes.Internal, "") |
| 82 | + } |
| 83 | + |
| 84 | + localAccountLock := s.getLocalAccountLock(giftCardVault) |
| 85 | + localAccountLock.Lock() |
| 86 | + defer localAccountLock.Unlock() |
| 87 | + |
| 88 | + claimedActionRecord, err := s.data.GetGiftCardClaimedAction(ctx, giftCardVault.PublicKey().ToBase58()) |
| 89 | + if err == nil { |
| 90 | + mintAccount, err := common.NewAccountFromPublicKeyString(accountInfoRecord.MintAccount) |
| 91 | + if err != nil { |
| 92 | + log.WithError(err).Warn("invalid mint account") |
| 93 | + return nil, status.Error(codes.Internal, "") |
| 94 | + } |
| 95 | + |
| 96 | + vmConfig, err := common.GetVmConfigForMint(ctx, s.data, mintAccount) |
| 97 | + if err != nil { |
| 98 | + log.WithError(err).Warn("failure getting vm config") |
| 99 | + return nil, status.Error(codes.Internal, "") |
| 100 | + } |
| 101 | + |
| 102 | + ownerTimelockAccounts, err := owner.GetTimelockAccounts(vmConfig) |
| 103 | + if err != nil { |
| 104 | + log.WithError(err).Warn("failure getting owner timelock accounts") |
| 105 | + return nil, status.Error(codes.Internal, "") |
| 106 | + } |
| 107 | + |
| 108 | + if *claimedActionRecord.Destination != ownerTimelockAccounts.Vault.PublicKey().ToBase58() { |
| 109 | + return &transactionpb.VoidGiftCardResponse{ |
| 110 | + Result: transactionpb.VoidGiftCardResponse_CLAIMED_BY_OTHER_USER, |
| 111 | + }, nil |
| 112 | + } |
| 113 | + return &transactionpb.VoidGiftCardResponse{ |
| 114 | + Result: transactionpb.VoidGiftCardResponse_OK, |
| 115 | + }, nil |
| 116 | + } else if err != action.ErrActionNotFound { |
| 117 | + log.WithError(err).Warn("failure getting gift card claimed action") |
| 118 | + return nil, status.Error(codes.Internal, "") |
| 119 | + } |
| 120 | + |
| 121 | + err = async_account.InitiateProcessToAutoReturnGiftCard(ctx, s.data, giftCardVault, true, globalBalanceLock) |
| 122 | + if err != nil { |
| 123 | + log.WithError(err).Warn("failure scheduling auto-return action") |
| 124 | + return nil, status.Error(codes.Internal, "") |
| 125 | + } |
| 126 | + |
| 127 | + // It's ok if this fails, the auto-return worker will just process this account |
| 128 | + // idempotently at a later time |
| 129 | + async_account.MarkAutoReturnCheckComplete(ctx, s.data, accountInfoRecord) |
| 130 | + |
| 131 | + return &transactionpb.VoidGiftCardResponse{ |
| 132 | + Result: transactionpb.VoidGiftCardResponse_OK, |
| 133 | + }, nil |
| 134 | +} |
0 commit comments