-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
362 lines (299 loc) · 9.77 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
package postgres
import (
"context"
"database/sql"
"strings"
"time"
"github.com/jmoiron/sqlx"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
q "github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/payment"
"github.com/code-payments/code-server/pkg/code/data/transaction"
)
const (
tableName = "codewallet__core_payment"
tableColumns = `
block_id,
block_time,
transaction_id,
transaction_index,
rendezvous_key,
is_external,
source,
destination,
quantity,
exchange_currency,
region,
exchange_rate,
usd_market_value,
is_withdraw,
confirmation_state,
created_at
`
)
type model struct {
Id sql.NullInt64 `db:"id"`
BlockId sql.NullInt64 `db:"block_id"`
BlockTime sql.NullTime `db:"block_time"`
TransactionId string `db:"transaction_id"`
TransactionIndex uint32 `db:"transaction_index"`
Rendezvous sql.NullString `db:"rendezvous_key"`
IsExternal bool `db:"is_external"`
SourceId string `db:"source"`
DestinationId string `db:"destination"`
Quantity uint64 `db:"quantity"`
ExchangeCurrency string `db:"exchange_currency"`
Region sql.NullString `db:"region"`
ExchangeRate float64 `db:"exchange_rate"`
UsdMarketValue float64 `db:"usd_market_value"`
IsWithdraw bool `db:"is_withdraw"`
ConfirmationState transaction.Confirmation `db:"confirmation_state"`
CreatedAt time.Time `db:"created_at"`
}
func toModel(obj *payment.Record) *model {
m := &model{
Id: sql.NullInt64{Int64: int64(obj.Id), Valid: obj.Id > 0},
BlockId: sql.NullInt64{Int64: int64(obj.BlockId), Valid: obj.BlockId > 0},
BlockTime: sql.NullTime{Time: obj.BlockTime, Valid: !obj.BlockTime.IsZero()},
TransactionId: obj.TransactionId,
TransactionIndex: obj.TransactionIndex,
Rendezvous: sql.NullString{String: obj.Rendezvous, Valid: obj.Rendezvous != ""},
IsExternal: obj.IsExternal,
SourceId: obj.Source,
DestinationId: obj.Destination,
Quantity: obj.Quantity,
ExchangeCurrency: strings.ToLower(obj.ExchangeCurrency),
ExchangeRate: obj.ExchangeRate,
UsdMarketValue: obj.UsdMarketValue,
IsWithdraw: obj.IsWithdraw,
ConfirmationState: obj.ConfirmationState,
CreatedAt: obj.CreatedAt.UTC(),
}
if obj.Region != nil {
m.Region.Valid = true
m.Region.String = strings.ToLower(*obj.Region)
}
return m
}
func fromModel(obj *model) *payment.Record {
record := &payment.Record{
Id: uint64(obj.Id.Int64),
BlockId: uint64(obj.BlockId.Int64),
BlockTime: obj.BlockTime.Time.UTC(),
TransactionId: obj.TransactionId,
TransactionIndex: obj.TransactionIndex,
Rendezvous: obj.Rendezvous.String,
IsExternal: obj.IsExternal,
Source: obj.SourceId,
Destination: obj.DestinationId,
Quantity: obj.Quantity,
ExchangeCurrency: strings.ToLower(obj.ExchangeCurrency),
ExchangeRate: obj.ExchangeRate,
UsdMarketValue: obj.UsdMarketValue,
IsWithdraw: obj.IsWithdraw,
ConfirmationState: obj.ConfirmationState,
CreatedAt: obj.CreatedAt.UTC(),
}
if obj.Region.Valid {
record.Region = &obj.Region.String
}
return record
}
func (self *model) dbSave(ctx context.Context, db *sqlx.DB) error {
query := `INSERT INTO ` + tableName + ` (` + tableColumns + `)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) RETURNING *;`
err := db.QueryRowxContext(ctx, query,
self.BlockId,
self.BlockTime,
self.TransactionId,
self.TransactionIndex,
self.Rendezvous,
self.IsExternal,
self.SourceId,
self.DestinationId,
self.Quantity,
self.ExchangeCurrency,
self.Region,
self.ExchangeRate,
self.UsdMarketValue,
self.IsWithdraw,
self.ConfirmationState,
self.CreatedAt,
).StructScan(self)
return pgutil.CheckUniqueViolation(err, payment.ErrExists)
}
func (self *model) dbUpdate(ctx context.Context, db *sqlx.DB) error {
query := `UPDATE ` + tableName + `
SET
block_id = $2,
block_time = $3,
exchange_currency = $4,
region = $5,
exchange_rate = $6,
usd_market_value = $7,
confirmation_state = $8
WHERE id = $1 RETURNING *;`
err := db.QueryRowxContext(ctx, query,
self.Id,
self.BlockId,
self.BlockTime,
self.ExchangeCurrency,
self.Region,
self.ExchangeRate,
self.UsdMarketValue,
self.ConfirmationState,
).StructScan(self)
return pgutil.CheckNoRows(err, payment.ErrNotFound)
}
func makeSelectQuery(condition string, ordering q.Ordering) string {
return `SELECT * FROM ` + tableName + ` WHERE ` + condition + ` ORDER BY id ` + q.FromOrderingWithFallback(ordering, "asc")
}
func makeGetQuery(condition string, ordering q.Ordering) string {
return makeSelectQuery(condition, ordering) + ` LIMIT 1`
}
func makeGetAllQuery(condition string, ordering q.Ordering, withCursor bool) string {
var query string
query = "SELECT * FROM " + tableName + " WHERE"
if withCursor {
if ordering == q.Ascending {
query = query + " id > $3 AND"
} else {
query = query + " id < $3 AND"
}
} else {
// Nonsense condition to make sure we get all records
// TODO: This is a hack, we should use a proper way to get all records
query = query + " (id < $3 OR id >= $3) AND "
}
query = query + " (" + condition + ") "
query = query + " ORDER BY id " + q.FromOrderingWithFallback(ordering, "ASC")
query = query + " LIMIT $2"
return query
}
func dbGet(ctx context.Context, db *sqlx.DB, txId string, index uint32) (*model, error) {
res := &model{}
err := db.GetContext(ctx, res,
makeGetQuery("transaction_id = $1 AND transaction_index = $2", q.Descending),
txId,
index,
)
return res, pgutil.CheckNoRows(err, payment.ErrNotFound)
}
func dbGetAllForTransaction(ctx context.Context, db *sqlx.DB, txId string) ([]*model, error) {
res := []*model{}
err := db.SelectContext(ctx, &res,
makeSelectQuery("transaction_id = $1", q.Descending),
txId,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, payment.ErrNotFound)
}
if len(res) == 0 {
return nil, payment.ErrNotFound
}
return res, nil
}
func dbGetAllForAccount(ctx context.Context, db *sqlx.DB, account string, cursor uint64, limit uint, ordering q.Ordering) ([]*model, error) {
res := []*model{}
err := db.SelectContext(ctx, &res,
makeGetAllQuery("source = $1 OR destination = $1", ordering, cursor > 0),
account, limit, cursor,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, payment.ErrNotFound)
}
if len(res) == 0 {
return nil, payment.ErrNotFound
}
return res, nil
}
func dbGetAllForAccountByType(ctx context.Context, db *sqlx.DB, account string, cursor uint64, limit uint, ordering q.Ordering, paymentType payment.PaymentType) ([]*model, error) {
res := []*model{}
var condition string
if paymentType == payment.PaymentType_Send {
condition = "source = $1"
} else {
condition = "destination = $1"
}
err := db.SelectContext(ctx, &res,
makeGetAllQuery(condition, ordering, cursor > 0),
account, limit, cursor,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, payment.ErrNotFound)
}
if len(res) == 0 {
return nil, payment.ErrNotFound
}
return res, nil
}
func dbGetAllForAccountByTypeAfterBlock(ctx context.Context, db *sqlx.DB, account string, block uint64, cursor uint64, limit uint, ordering q.Ordering, paymentType payment.PaymentType) ([]*model, error) {
res := []*model{}
var condition string
if paymentType == payment.PaymentType_Send {
condition = "source = $1"
} else {
condition = "destination = $1"
}
condition += " AND block_id > $4"
err := db.SelectContext(ctx, &res,
makeGetAllQuery(condition, ordering, cursor > 0),
account, limit, cursor, block,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, payment.ErrNotFound)
}
if len(res) == 0 {
return nil, payment.ErrNotFound
}
return res, nil
}
func dbGetAllForAccountByTypeWithinBlockRange(ctx context.Context, db *sqlx.DB, account string, lowerBound, upperBound uint64, cursor uint64, limit uint, ordering q.Ordering, paymentType payment.PaymentType) ([]*model, error) {
res := []*model{}
var condition string
if paymentType == payment.PaymentType_Send {
condition = "source = $1"
} else {
condition = "destination = $1"
}
condition += " AND block_id > $4 AND block_id < $5"
err := db.SelectContext(ctx, &res,
makeGetAllQuery(condition, ordering, cursor > 0),
account, limit, cursor, lowerBound, upperBound,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, payment.ErrNotFound)
}
if len(res) == 0 {
return nil, payment.ErrNotFound
}
return res, nil
}
func dbGetAllExternalDepositsAfterBlock(ctx context.Context, db *sqlx.DB, account string, block uint64, cursor uint64, limit uint, ordering q.Ordering) ([]*model, error) {
res := []*model{}
condition := "destination = $1 AND block_id > $4 AND is_external"
err := db.SelectContext(ctx, &res,
makeGetAllQuery(condition, ordering, cursor > 0),
account, limit, cursor, block,
)
if err != nil {
return nil, pgutil.CheckNoRows(err, payment.ErrNotFound)
}
if len(res) == 0 {
return nil, payment.ErrNotFound
}
return res, nil
}
func dbGetExternalDepositAmount(ctx context.Context, db *sqlx.DB, account string) (uint64, error) {
var res sql.NullInt64
query := `SELECT SUM(quantity) FROM ` + tableName + `
WHERE destination = $1 AND is_external AND confirmation_state = 3`
err := db.GetContext(ctx, &res, query, account)
if err != nil {
return 0, err
}
if !res.Valid {
return 0, nil
}
return uint64(res.Int64), nil
}