-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwallet.go
66 lines (58 loc) · 2.39 KB
/
wallet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package wallet defines a domain of the wallet functionality and entities.
// The layout is heavily inspired by Ben Johnson's https://github.com/benbjohnson/wtf.
package wallet
import (
"context"
"github.com/cockroachdb/apd"
)
const (
// DecimalMaxDigits is a decimal precision (defaults to 28 as in Python).
DecimalMaxDigits = 28
// DecimalPlaces defines how many decimal places are used to quantize a number.
DecimalPlaces = 2
)
// Transfer is a customer request to send money.
type Transfer struct {
// ID is a random string generated by a client for duplicate suppression.
ID string `json:"request_id"`
// From which account to send money.
From string `json:"from"`
Amount apd.Decimal `json:"amount"`
// To is a transfer recipient account.
To string `json:"to"`
// Partition is a number of a partition where the transfer request was stored.
Partition int32 `json:"-"`
// SequenceID is ID assigned internally. For example, in Kafka it is an offset of the message.
SequenceID int64 `json:"-"`
}
// Payment is an instruction that affects account balance.
// For example, outgoing 0.5 payment from Alice's account.
type Payment struct {
// RequestID is a client request ID from Transfer entity for duplicate suppression.
RequestID string `json:"request_id"`
// Account where the payment belongs to.
Account string `json:"account"`
// Direction defines whether payment is incoming or outgoing.
Direction string `json:"direction"`
Amount apd.Decimal `json:"amount"`
// Partition is a number of a partition where the payment was stored.
Partition int32 `json:"-"`
// SequenceID is ID assigned internally. For example, in Kafka it is an offset of the message.
SequenceID int64 `json:"-"`
}
// TransferService represents a service to store transfer requests.
type TransferService interface {
CreateTransfer(ctx context.Context, t *Transfer) error
FromOffset(ctx context.Context, partition int32, offset int64) (<-chan *Transfer, <-chan error)
}
// PaymentService represents a service to store payments which are created based on
// transfer requests.
type PaymentService interface {
CreatePayment(ctx context.Context, p *Payment) error
FromOffset(ctx context.Context, partition int32, offset int64) (<-chan *Payment, <-chan error)
}
// DedupService is responsible for requests deduplication.
type DedupService interface {
HasSeen(requestID string) (bool, error)
Save(requestID string) error
}