-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
105 lines (78 loc) · 2.37 KB
/
store.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package deposit
import (
"context"
"errors"
"time"
"github.com/code-payments/code-server/pkg/code/data/transaction"
)
var (
ErrDepositNotFound = errors.New("external deposit not found")
)
// Note: Only captures external deposits at the transaction level
type Record struct {
Id uint64
Signature string
Destination string
Amount uint64
UsdMarketValue float64
Slot uint64
ConfirmationState transaction.Confirmation
CreatedAt time.Time
}
type Store interface {
// Save saves a deposit record
Save(ctx context.Context, record *Record) error
// Get gets a deposit record for a signature and account
Get(ctx context.Context, signature, account string) (*Record, error)
// GetQuarkAmount gets the total deposited quark amount to an account
// for finalized transactions
GetQuarkAmount(ctx context.Context, account string) (uint64, error)
// GetQuarkAmountBatch is like GetQuarkAmount but for a batch of accounts
GetQuarkAmountBatch(ctx context.Context, accounts ...string) (map[string]uint64, error)
// GetUsdAmount gets the total deposited USD amount to an account for finalized
// transactions
GetUsdAmount(ctx context.Context, account string) (float64, error)
}
func (r *Record) Validate() error {
if len(r.Signature) == 0 {
return errors.New("signature is required")
}
if len(r.Destination) == 0 {
return errors.New("destination is required")
}
if r.Amount == 0 {
return errors.New("amount is required")
}
if r.UsdMarketValue <= 0 {
return errors.New("usd market value must be positive")
}
if r.ConfirmationState == transaction.ConfirmationUnknown {
return errors.New("confirmation state is required")
}
if r.ConfirmationState == transaction.ConfirmationFinalized && r.Slot == 0 {
return errors.New("slot is required for finalized deposits")
}
return nil
}
func (r *Record) Clone() Record {
return Record{
Id: r.Id,
Signature: r.Signature,
Destination: r.Destination,
Amount: r.Amount,
UsdMarketValue: r.UsdMarketValue,
Slot: r.Slot,
ConfirmationState: r.ConfirmationState,
CreatedAt: r.CreatedAt,
}
}
func (r *Record) CopyTo(dst *Record) {
dst.Id = r.Id
dst.Signature = r.Signature
dst.Destination = r.Destination
dst.Amount = r.Amount
dst.UsdMarketValue = r.UsdMarketValue
dst.Slot = r.Slot
dst.ConfirmationState = r.ConfirmationState
dst.CreatedAt = r.CreatedAt
}