transfer.go75 lines, 2.4 KB
| 1 | package wallet |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | |
| 8 | "github.com/fiston-user/payments/internal/platform/apperr" |
| 9 | "github.com/fiston-user/payments/internal/wallet/walletdb" |
| 10 | "github.com/fiston-user/payments/internal/wallet/walletevents" |
| 11 | "github.com/google/uuid" |
| 12 | ) |
| 13 | |
| 14 | // ErrInsufficientFunds is returned when a transfer would take a wallet below |
| 15 | // zero. Clients get a 422, with the code insufficient_funds. |
| 16 | var ErrInsufficientFunds = apperr.NewRule("insufficient_funds", "insufficient funds") |
| 17 | |
| 18 | // Transfer moves amount from one account to another, as one balanced |
| 19 | // transaction, and fails with ErrInsufficientFunds if from can't cover it. |
| 20 | // The transfer.completed event commits with it. |
| 21 | func (s *Store) Transfer(ctx context.Context, src, dst Account, amount int64) (Transaction, error) { |
| 22 | from, to, currency := src.ID, dst.ID, src.Currency |
| 23 | var posted Transaction |
| 24 | err := s.inTx(ctx, func(q *walletdb.Queries) error { |
| 25 | // Lock both accounts until we commit. Another transfer that touches |
| 26 | // either one waits right here, so the balance below stays true. |
| 27 | if err := lockInOrder(ctx, q, from, to); err != nil { |
| 28 | return err |
| 29 | } |
| 30 | |
| 31 | b, err := q.GetAccountBalance(ctx, from) |
| 32 | if err != nil { |
| 33 | return fmt.Errorf("get balance %s: %w", from, err) |
| 34 | } |
| 35 | if b.Balance < amount { |
| 36 | return fmt.Errorf("wallet %s: %w", from, ErrInsufficientFunds) |
| 37 | } |
| 38 | |
| 39 | posted, err = insertTransaction(ctx, q, NewTransaction{ |
| 40 | Description: "transfer", |
| 41 | Postings: []Posting{ |
| 42 | {AccountID: from, Direction: Debit, Amount: amount, Currency: currency}, |
| 43 | {AccountID: to, Direction: Credit, Amount: amount, Currency: currency}, |
| 44 | }, |
| 45 | }) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | return enqueue(ctx, q, walletevents.TransferCompleted, from, walletevents.Transfer{ |
| 50 | TransactionID: posted.ID, |
| 51 | From: eventWallet(src), |
| 52 | To: eventWallet(dst), |
| 53 | Amount: amount, |
| 54 | }) |
| 55 | }) |
| 56 | if err != nil { |
| 57 | return Transaction{}, fmt.Errorf("transfer: %w", err) |
| 58 | } |
| 59 | return posted, nil |
| 60 | } |
| 61 | |
| 62 | // lockInOrder locks two accounts until we commit, the lower ID first. Every |
| 63 | // caller takes them in the same order, so two transactions can never each |
| 64 | // hold the lock the other one is waiting for: no deadlocks. |
| 65 | func lockInOrder(ctx context.Context, q *walletdb.Queries, a, b uuid.UUID) error { |
| 66 | if bytes.Compare(a[:], b[:]) > 0 { |
| 67 | a, b = b, a |
| 68 | } |
| 69 | for _, id := range []uuid.UUID{a, b} { |
| 70 | if _, err := q.LockAccount(ctx, id); err != nil { |
| 71 | return fmt.Errorf("lock account %s: %w", id, err) |
| 72 | } |
| 73 | } |
| 74 | return nil |
| 75 | } |