events.go87 lines, 3.1 KB
| 1 | // Package walletevents is the wallet's event contract: the stream other |
| 2 | // services read, the events in it, and what each event's data holds. |
| 3 | package walletevents |
| 4 | |
| 5 | import ( |
| 6 | "strings" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/fiston-user/payments/internal/fx" |
| 10 | "github.com/google/uuid" |
| 11 | "github.com/nats-io/nats.go/jetstream" |
| 12 | ) |
| 13 | |
| 14 | // Stream is the JetStream stream with every event the wallet publishes. |
| 15 | const Stream = "WALLET" |
| 16 | |
| 17 | // Event types. A type names the service, what happened, and the version of |
| 18 | // its data. A breaking change is a new version, published next to the old one. |
| 19 | const ( |
| 20 | WalletCreated = "wallet.wallet.created.v1" |
| 21 | DepositCompleted = "wallet.deposit.completed.v1" |
| 22 | TransferCompleted = "wallet.transfer.completed.v1" |
| 23 | ConversionCompleted = "wallet.conversion.completed.v1" |
| 24 | ) |
| 25 | |
| 26 | // Subject is the subject events of type t are published on, like |
| 27 | // wallet.events.transfer.completed.v1. Consumers can filter on parts of it. |
| 28 | func Subject(t string) string { |
| 29 | return "wallet.events." + strings.TrimPrefix(t, "wallet.") |
| 30 | } |
| 31 | |
| 32 | // StreamConfig is the WALLET stream, kept on replicas servers. |
| 33 | func StreamConfig(replicas int) jetstream.StreamConfig { |
| 34 | return jetstream.StreamConfig{ |
| 35 | Name: Stream, |
| 36 | Description: "Everything that happens to wallets", |
| 37 | Subjects: []string{"wallet.events.>"}, |
| 38 | Storage: jetstream.FileStorage, |
| 39 | Replicas: replicas, |
| 40 | // Events stay for 30 days, read or not, so a new consumer can replay |
| 41 | // history, and a broken one can catch up after a fix. |
| 42 | Retention: jetstream.LimitsPolicy, |
| 43 | MaxAge: 30 * 24 * time.Hour, |
| 44 | MaxBytes: 1 << 30, |
| 45 | Discard: jetstream.DiscardOld, |
| 46 | // A message with a Nats-Msg-Id seen in the last ten minutes is |
| 47 | // acknowledged, and dropped: publishing twice stores it once. |
| 48 | Duplicates: 10 * time.Minute, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Wallet is a wallet, as events describe it. |
| 53 | type Wallet struct { |
| 54 | ID uuid.UUID `json:"id"` |
| 55 | OwnerID uuid.UUID `json:"owner_id"` |
| 56 | Currency string `json:"currency"` |
| 57 | } |
| 58 | |
| 59 | // Deposit is the data of a deposit.completed event: money came into a wallet |
| 60 | // from outside. |
| 61 | type Deposit struct { |
| 62 | TransactionID uuid.UUID `json:"transaction_id"` |
| 63 | Wallet Wallet `json:"wallet"` |
| 64 | Amount int64 `json:"amount"` // minor units of the wallet's currency |
| 65 | } |
| 66 | |
| 67 | // Transfer is the data of a transfer.completed event: money moved from one |
| 68 | // wallet to another. |
| 69 | type Transfer struct { |
| 70 | TransactionID uuid.UUID `json:"transaction_id"` |
| 71 | From Wallet `json:"from"` |
| 72 | To Wallet `json:"to"` |
| 73 | Amount int64 `json:"amount"` |
| 74 | } |
| 75 | |
| 76 | // Conversion is the data of a conversion.completed event: a customer sold one |
| 77 | // currency for another, from one of their wallets to the other. |
| 78 | type Conversion struct { |
| 79 | TransactionID uuid.UUID `json:"transaction_id"` |
| 80 | QuoteID uuid.UUID `json:"quote_id"` |
| 81 | From Wallet `json:"from"` |
| 82 | To Wallet `json:"to"` |
| 83 | Sold int64 `json:"sold"` // minor units of From's currency |
| 84 | Fee int64 `json:"fee"` // on top, in From's currency |
| 85 | Bought int64 `json:"bought"` // minor units of To's currency |
| 86 | Rate fx.Rate `json:"rate"` |
| 87 | } |