-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
429 lines (345 loc) · 12.1 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package postgres
import (
"context"
"database/sql"
"errors"
"time"
"github.com/jmoiron/sqlx"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
"github.com/code-payments/code-server/pkg/code/data/account"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
"github.com/code-payments/code-server/pkg/pointer"
)
const (
tableName = "codewallet__core_accountinfov2"
)
type model struct {
Id sql.NullInt64 `db:"id"`
OwnerAccount string `db:"owner_account"`
AuthorityAccount string `db:"authority_account"`
TokenAccount string `db:"token_account"`
MintAccount string `db:"mint_account"`
AccountType int `db:"account_type"`
Index uint64 `db:"index"`
RelationshipTo string `db:"relationship_to"`
RequiresDepositSync bool `db:"requires_deposit_sync"`
DepositsLastSyncedAt time.Time `db:"deposits_last_synced_at"`
RequiresAutoReturnCheck bool `db:"requires_auto_return_check"`
RequiresSwapRetry bool `db:"requires_swap_retry"`
LastSwapRetryAt time.Time `db:"last_swap_retry_at"`
CreatedAt time.Time `db:"created_at"`
}
func toModel(obj *account.Record) (*model, error) {
if err := obj.Validate(); err != nil {
return nil, err
}
return &model{
OwnerAccount: obj.OwnerAccount,
AuthorityAccount: obj.AuthorityAccount,
TokenAccount: obj.TokenAccount,
MintAccount: obj.MintAccount,
AccountType: int(obj.AccountType),
Index: obj.Index,
RelationshipTo: *pointer.StringOrDefault(obj.RelationshipTo, ""),
RequiresDepositSync: obj.RequiresDepositSync,
DepositsLastSyncedAt: obj.DepositsLastSyncedAt.UTC(),
RequiresAutoReturnCheck: obj.RequiresAutoReturnCheck,
RequiresSwapRetry: obj.RequiresSwapRetry,
LastSwapRetryAt: obj.LastSwapRetryAt,
CreatedAt: obj.CreatedAt.UTC(),
}, nil
}
func fromModel(obj *model) *account.Record {
return &account.Record{
Id: uint64(obj.Id.Int64),
OwnerAccount: obj.OwnerAccount,
AuthorityAccount: obj.AuthorityAccount,
TokenAccount: obj.TokenAccount,
MintAccount: obj.MintAccount,
AccountType: commonpb.AccountType(obj.AccountType),
Index: obj.Index,
RelationshipTo: pointer.StringIfValid(len(obj.RelationshipTo) > 0, obj.RelationshipTo),
RequiresDepositSync: obj.RequiresDepositSync,
DepositsLastSyncedAt: obj.DepositsLastSyncedAt,
RequiresAutoReturnCheck: obj.RequiresAutoReturnCheck,
RequiresSwapRetry: obj.RequiresSwapRetry,
LastSwapRetryAt: obj.LastSwapRetryAt,
CreatedAt: obj.CreatedAt,
}
}
func (m *model) dbInsert(ctx context.Context, db *sqlx.DB) error {
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
if m.CreatedAt.IsZero() {
m.CreatedAt = time.Now()
}
query := `INSERT INTO ` + tableName + `
(owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
RETURNING id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at
`
err := tx.QueryRowxContext(
ctx,
query,
m.OwnerAccount,
m.AuthorityAccount,
m.TokenAccount,
m.MintAccount,
m.AccountType,
m.Index,
m.RelationshipTo,
m.RequiresDepositSync,
m.DepositsLastSyncedAt,
m.RequiresAutoReturnCheck,
m.RequiresSwapRetry,
m.LastSwapRetryAt,
m.CreatedAt,
).StructScan(m)
if err == nil {
return nil
}
// There are multiple unique violations, which may indicate we have something
// invalid or the record exists as is. We need to query it to see what's
// up and return the right error code.
if pgutil.IsUniqueViolation(err) {
existingModel, err := dbGetByTokenAddress(ctx, db, m.TokenAccount)
if err == account.ErrAccountInfoNotFound {
return account.ErrInvalidAccountInfo
} else if err != nil {
return err
}
if equivalentModels(existingModel, m) {
return account.ErrAccountInfoExists
}
return account.ErrInvalidAccountInfo
}
return err
})
}
func (m *model) dbUpdate(ctx context.Context, db *sqlx.DB) error {
query := `UPDATE ` + tableName + `
SET requires_deposit_sync = $2, deposits_last_synced_at = $3, requires_auto_return_check = $4, requires_swap_retry = $5, last_swap_retry_at = $6
WHERE token_account = $1
RETURNING id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at
`
err := db.QueryRowxContext(
ctx,
query,
m.TokenAccount,
m.RequiresDepositSync,
m.DepositsLastSyncedAt,
m.RequiresAutoReturnCheck,
m.RequiresSwapRetry,
m.LastSwapRetryAt,
).StructScan(m)
if err != nil {
return pgutil.CheckNoRows(err, account.ErrAccountInfoNotFound)
}
return nil
}
func dbGetByTokenAddress(ctx context.Context, db *sqlx.DB, address string) (*model, error) {
res := &model{}
query := `SELECT id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at FROM ` + tableName + `
WHERE token_account = $1
`
err := db.QueryRowxContext(
ctx,
query,
address,
).StructScan(res)
if err != nil {
return nil, pgutil.CheckNoRows(err, account.ErrAccountInfoNotFound)
}
return res, nil
}
func dbGetByAuthorityAddress(ctx context.Context, db *sqlx.DB, address string) (*model, error) {
res := &model{}
query := `SELECT id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at FROM ` + tableName + `
WHERE authority_account = $1
`
err := db.QueryRowxContext(
ctx,
query,
address,
).StructScan(res)
if err != nil {
return nil, pgutil.CheckNoRows(err, account.ErrAccountInfoNotFound)
}
return res, nil
}
func dbGetLatestByOwnerAddress(ctx context.Context, db *sqlx.DB, address string) ([]*model, error) {
var res []*model
query := `SELECT DISTINCT ON (account_type, relationship_to) id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at FROM ` + tableName + `
WHERE owner_account = $1
ORDER BY account_type, relationship_to, index DESC
`
err := db.SelectContext(
ctx,
&res,
query,
address,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, account.ErrAccountInfoNotFound)
}
if len(res) == 0 {
return nil, account.ErrAccountInfoNotFound
}
return res, nil
}
func dbGetLatestByOwnerAddressAndType(ctx context.Context, db *sqlx.DB, address string, accountType commonpb.AccountType) (*model, error) {
if accountType == commonpb.AccountType_RELATIONSHIP {
return nil, errors.New("relationship account not supported")
}
res := &model{}
query := `SELECT id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at FROM ` + tableName + `
WHERE owner_account = $1 AND account_type = $2
ORDER BY index DESC
LIMIT 1
`
err := db.QueryRowxContext(
ctx,
query,
address,
accountType,
).StructScan(res)
if err != nil {
return nil, pgutil.CheckNoRows(err, account.ErrAccountInfoNotFound)
}
return res, nil
}
func dbGetRelationshipByOwnerAddress(ctx context.Context, db *sqlx.DB, address, relationshipTo string) (*model, error) {
res := &model{}
query := `SELECT id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at FROM ` + tableName + `
WHERE owner_account = $1 AND account_type = $2 AND index = 0 AND relationship_to = $3
`
err := db.QueryRowxContext(
ctx,
query,
address,
commonpb.AccountType_RELATIONSHIP,
relationshipTo,
).StructScan(res)
if err != nil {
return nil, pgutil.CheckNoRows(err, account.ErrAccountInfoNotFound)
}
return res, nil
}
func dbGetPrioritizedRequiringDepositSync(ctx context.Context, db *sqlx.DB, limit uint64) ([]*model, error) {
var res []*model
query := `SELECT id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at FROM ` + tableName + `
WHERE requires_deposit_sync = TRUE
ORDER BY deposits_last_synced_at ASC
LIMIT $1
`
err := db.SelectContext(
ctx,
&res,
query,
limit,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, account.ErrAccountInfoNotFound)
}
if len(res) == 0 {
return nil, account.ErrAccountInfoNotFound
}
return res, nil
}
func dbCountRequiringDepositSync(ctx context.Context, db *sqlx.DB) (uint64, error) {
var res uint64
query := `SELECT COUNT(*) FROM ` + tableName + `
WHERE requires_deposit_sync = TRUE
`
err := db.GetContext(ctx, &res, query)
if err != nil {
return 0, err
}
return res, nil
}
func dbGetPrioritizedRequiringAutoReturnChecks(ctx context.Context, db *sqlx.DB, minAge time.Duration, limit uint64) ([]*model, error) {
var res []*model
query := `SELECT id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at FROM ` + tableName + `
WHERE requires_auto_return_check = TRUE AND created_at <= $1
ORDER BY created_at ASC
LIMIT $2
`
err := db.SelectContext(
ctx,
&res,
query,
time.Now().Add(-minAge),
limit,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, account.ErrAccountInfoNotFound)
}
if len(res) == 0 {
return nil, account.ErrAccountInfoNotFound
}
return res, nil
}
func dbCountRequiringAutoReturnCheck(ctx context.Context, db *sqlx.DB) (uint64, error) {
var res uint64
query := `SELECT COUNT(*) FROM ` + tableName + `
WHERE requires_auto_return_check = TRUE
`
err := db.GetContext(ctx, &res, query)
if err != nil {
return 0, err
}
return res, nil
}
func dbGetPrioritizedRequiringSwapRetry(ctx context.Context, db *sqlx.DB, minAge time.Duration, limit uint64) ([]*model, error) {
var res []*model
query := `SELECT id, owner_account, authority_account, token_account, mint_account, account_type, index, relationship_to, requires_deposit_sync, deposits_last_synced_at, requires_auto_return_check, requires_swap_retry, last_swap_retry_at, created_at FROM ` + tableName + `
WHERE requires_swap_retry = TRUE AND last_swap_retry_at <= $1
ORDER BY last_swap_retry_at ASC
LIMIT $2
`
err := db.SelectContext(
ctx,
&res,
query,
time.Now().Add(-minAge),
limit,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, account.ErrAccountInfoNotFound)
}
if len(res) == 0 {
return nil, account.ErrAccountInfoNotFound
}
return res, nil
}
func dbCountRequiringSwapRetry(ctx context.Context, db *sqlx.DB) (uint64, error) {
var res uint64
query := `SELECT COUNT(*) FROM ` + tableName + `
WHERE requires_swap_retry = TRUE
`
err := db.GetContext(ctx, &res, query)
if err != nil {
return 0, err
}
return res, nil
}
func equivalentModels(obj1, obj2 *model) bool {
if obj1.OwnerAccount != obj2.OwnerAccount {
return false
}
if obj1.AuthorityAccount != obj2.AuthorityAccount {
return false
}
if obj1.TokenAccount != obj2.TokenAccount {
return false
}
if obj1.Index != obj2.Index {
return false
}
if obj1.AccountType != obj2.AccountType {
return false
}
if obj1.RelationshipTo != obj2.RelationshipTo {
return false
}
return true
}