-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
93 lines (75 loc) · 2.41 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
package postgres
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/database/query"
timelock_token "github.com/code-payments/code-server/pkg/solana/timelock/v1"
"github.com/code-payments/code-server/pkg/code/data/timelock"
)
type store struct {
db *sqlx.DB
}
// New returns a new postgres-backed timelock.Store
func New(db *sql.DB) timelock.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// Save implements timelock.Store.Save
func (s *store) Save(ctx context.Context, record *timelock.Record) error {
model, err := toModel(record)
if err != nil {
return err
}
if err := model.dbSave(ctx, s.db); err != nil {
return err
}
res := fromModel(model)
res.CopyTo(record)
return nil
}
// GetByAddress implements timelock.Store.GetByAddress
func (s *store) GetByAddress(ctx context.Context, address string) (*timelock.Record, error) {
model, err := dbGetByAddress(ctx, s.db, address)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// GetByVault implements timelock.Store.GetByVault
func (s *store) GetByVault(ctx context.Context, vault string) (*timelock.Record, error) {
model, err := dbGetByVault(ctx, s.db, vault)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// GetByVaultBatch implements timelock.Store.GetByVaultBatch
func (s *store) GetByVaultBatch(ctx context.Context, vaults ...string) (map[string]*timelock.Record, error) {
models, err := dbGetByVaultBatch(ctx, s.db, vaults...)
if err != nil {
return nil, err
}
timelocksByVault := make(map[string]*timelock.Record, len(models))
for _, model := range models {
timelocksByVault[model.VaultAddress] = fromModel(model)
}
return timelocksByVault, nil
}
// GetOldestByState implements timelock.Store.GetAllByState
func (s *store) GetAllByState(ctx context.Context, state timelock_token.TimelockState, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*timelock.Record, error) {
res, err := dbGetAllByState(ctx, s.db, state, cursor, limit, direction)
if err != nil {
return nil, err
}
timelocks := make([]*timelock.Record, len(res))
for i, model := range res {
timelocks[i] = fromModel(model)
}
return timelocks, nil
}
// GetCountByState implements timelock.Store.GetCountByState
func (s *store) GetCountByState(ctx context.Context, state timelock_token.TimelockState) (uint64, error) {
return dbGetCountByState(ctx, s.db, state)
}