-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
189 lines (151 loc) · 4.66 KB
/
model.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package postgres
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/code/data/deposit"
"github.com/code-payments/code-server/pkg/code/data/transaction"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
)
const (
tableName = "codewallet__core_externaldeposit"
)
type model struct {
Id sql.NullInt64 `db:"id"`
Signature string `db:"signature"`
Destination string `db:"destination"`
Amount uint64 `db:"amount"`
UsdMarketValue float64 `db:"usd_market_value"`
Slot uint64 `db:"slot"`
ConfirmationState int `db:"confirmation_state"`
CreatedAt time.Time `db:"created_at"`
}
func toModel(obj *deposit.Record) (*model, error) {
if err := obj.Validate(); err != nil {
return nil, err
}
return &model{
Signature: obj.Signature,
Destination: obj.Destination,
Amount: obj.Amount,
UsdMarketValue: obj.UsdMarketValue,
Slot: obj.Slot,
ConfirmationState: int(obj.ConfirmationState),
CreatedAt: obj.CreatedAt,
}, nil
}
func fromModel(obj *model) *deposit.Record {
return &deposit.Record{
Id: uint64(obj.Id.Int64),
Signature: obj.Signature,
Destination: obj.Destination,
Amount: obj.Amount,
UsdMarketValue: obj.UsdMarketValue,
Slot: obj.Slot,
ConfirmationState: transaction.Confirmation(obj.ConfirmationState),
CreatedAt: obj.CreatedAt,
}
}
func (m *model) dbSave(ctx context.Context, db *sqlx.DB) error {
query := `INSERT INTO ` + tableName + `
(signature, destination, amount, usd_market_value, slot, confirmation_state, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT(signature, destination)
DO UPDATE
SET slot = $5, confirmation_state = $6
WHERE ` + tableName + `.signature = $1 AND ` + tableName + `.destination = $2
RETURNING id, signature, destination, amount, usd_market_value, slot, confirmation_state, created_at`
if m.CreatedAt.IsZero() {
m.CreatedAt = time.Now()
}
return db.QueryRowxContext(
ctx,
query,
m.Signature,
m.Destination,
m.Amount,
m.UsdMarketValue,
m.Slot,
m.ConfirmationState,
m.CreatedAt,
).StructScan(m)
}
func dbGet(ctx context.Context, db *sqlx.DB, signature, account string) (*model, error) {
var res model
query := `SELECT id, signature, destination, amount, usd_market_value, slot, confirmation_state, created_at FROM ` + tableName + `
WHERE signature = $1 AND destination = $2
`
err := db.GetContext(ctx, &res, query, signature, account)
if err != nil {
return nil, pgutil.CheckNoRows(err, deposit.ErrDepositNotFound)
}
return &res, nil
}
func dbGetQuarkAmount(ctx context.Context, db *sqlx.DB, account string) (uint64, error) {
var res sql.NullInt64
query := `SELECT SUM(amount) FROM ` + tableName + `
WHERE destination = $1 AND confirmation_state = $2
`
err := pgutil.ExecuteInTx(ctx, db, sql.LevelRepeatableRead, func(tx *sqlx.Tx) error {
return db.GetContext(ctx, &res, query, account, transaction.ConfirmationFinalized)
})
if err != nil {
return 0, err
}
if !res.Valid {
return 0, nil
}
return uint64(res.Int64), nil
}
func dbGetQuarkAmountBatch(ctx context.Context, db *sqlx.DB, accounts ...string) (map[string]uint64, error) {
if len(accounts) == 0 {
return make(map[string]uint64), nil
}
type Row struct {
Destination string `db:"destination"`
Amount int64 `db:"amount"`
}
rows := []*Row{}
individualFilters := make([]string, len(accounts))
for i, vault := range accounts {
individualFilters[i] = fmt.Sprintf("'%s'", vault)
}
accountFilter := strings.Join(individualFilters, ",")
query := fmt.Sprintf(`SELECT destination, SUM(amount) AS amount FROM `+tableName+`
WHERE destination IN (%s) AND confirmation_state = $1
GROUP BY destination
`, accountFilter)
err := pgutil.ExecuteInTx(ctx, db, sql.LevelRepeatableRead, func(tx *sqlx.Tx) error {
return tx.SelectContext(ctx, &rows, query, transaction.ConfirmationFinalized)
})
if err != nil {
return nil, err
}
res := make(map[string]uint64)
for _, account := range accounts {
res[account] = 0
}
for _, row := range rows {
res[row.Destination] = uint64(row.Amount)
}
return res, nil
}
func dbGetUsdAmount(ctx context.Context, db *sqlx.DB, account string) (float64, error) {
var res sql.NullFloat64
query := `SELECT SUM(usd_market_value) FROM ` + tableName + `
WHERE destination = $1 AND confirmation_state = $2
`
err := pgutil.ExecuteInTx(ctx, db, sql.LevelRepeatableRead, func(tx *sqlx.Tx) error {
return db.GetContext(ctx, &res, query, account, transaction.ConfirmationFinalized)
})
if err != nil {
return 0, err
}
if !res.Valid {
return 0, nil
}
return res.Float64, nil
}