-
Notifications
You must be signed in to change notification settings - Fork 19
/
transfers.go
187 lines (162 loc) · 6.46 KB
/
transfers.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package sqlstore
import (
"context"
"fmt"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/metrics"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"github.com/georgysavva/scany/pgxscan"
)
type Transfers struct {
*ConnectionSource
}
var transfersOrdering = TableOrdering{
ColumnOrdering{Name: "vega_time", Sorting: ASC},
ColumnOrdering{Name: "id", Sorting: ASC},
}
func NewTransfers(connectionSource *ConnectionSource) *Transfers {
return &Transfers{
ConnectionSource: connectionSource,
}
}
func (t *Transfers) Upsert(ctx context.Context, transfer *entities.Transfer) error {
defer metrics.StartSQLQuery("Transfers", "Upsert")()
query := `insert into transfers(
id,
tx_hash,
vega_time,
from_account_id,
to_account_id,
asset_id,
amount,
reference,
status,
transfer_type,
deliver_on,
start_epoch,
end_epoch,
factor,
dispatch_metric,
dispatch_metric_asset,
dispatch_markets,
reason
)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)
on conflict (id, vega_time) do update
set
from_account_id=EXCLUDED.from_account_id,
to_account_id=EXCLUDED.to_account_id,
asset_id=EXCLUDED.asset_id,
amount=EXCLUDED.amount,
reference=EXCLUDED.reference,
status=EXCLUDED.status,
transfer_type=EXCLUDED.transfer_type,
deliver_on=EXCLUDED.deliver_on,
start_epoch=EXCLUDED.start_epoch,
end_epoch=EXCLUDED.end_epoch,
factor=EXCLUDED.factor,
dispatch_metric=EXCLUDED.dispatch_metric,
dispatch_metric_asset=EXCLUDED.dispatch_metric_asset,
dispatch_markets=EXCLUDED.dispatch_markets,
reason=EXCLUDED.reason,
tx_hash=EXCLUDED.tx_hash
;`
if _, err := t.Connection.Exec(ctx, query, transfer.ID, transfer.TxHash, transfer.VegaTime, transfer.FromAccountID, transfer.ToAccountID,
transfer.AssetID, transfer.Amount, transfer.Reference, transfer.Status, transfer.TransferType,
transfer.DeliverOn, transfer.StartEpoch, transfer.EndEpoch, transfer.Factor, transfer.DispatchMetric, transfer.DispatchMetricAsset, transfer.DispatchMarkets, transfer.Reason); err != nil {
err = fmt.Errorf("could not insert transfer into database: %w", err)
return err
}
return nil
}
func (t *Transfers) GetTransfersToOrFromParty(ctx context.Context, partyID entities.PartyID, pagination entities.CursorPagination) ([]entities.Transfer,
entities.PageInfo, error,
) {
defer metrics.StartSQLQuery("Transfers", "GetTransfersToOrFromParty")()
transfers, pageInfo, err := t.getTransfers(ctx, pagination,
"where transfers_current.from_account_id in (select id from accounts where accounts.party_id=$1)"+
" or transfers_current.to_account_id in (select id from accounts where accounts.party_id=$1)", partyID)
if err != nil {
return nil, entities.PageInfo{}, fmt.Errorf("getting transfers to or from party:%w", err)
}
return transfers, pageInfo, nil
}
func (t *Transfers) GetTransfersFromParty(ctx context.Context, partyID entities.PartyID, pagination entities.CursorPagination) ([]entities.Transfer,
entities.PageInfo, error,
) {
defer metrics.StartSQLQuery("Transfers", "GetTransfersFromParty")()
transfers, pageInfo, err := t.getTransfers(ctx, pagination,
"where transfers_current.from_account_id in (select id from accounts where accounts.party_id=$1)", partyID)
if err != nil {
return nil, entities.PageInfo{}, fmt.Errorf("getting transfers from party:%w", err)
}
return transfers, pageInfo, nil
}
func (t *Transfers) GetTransfersToParty(ctx context.Context, partyID entities.PartyID, pagination entities.CursorPagination) ([]entities.Transfer, entities.PageInfo,
error,
) {
defer metrics.StartSQLQuery("Transfers", "GetTransfersToParty")()
transfers, pageInfo, err := t.getTransfers(ctx, pagination,
"where transfers_current.to_account_id in (select id from accounts where accounts.party_id=$1)", partyID)
if err != nil {
return nil, entities.PageInfo{}, fmt.Errorf("getting transfers to party:%w", err)
}
return transfers, pageInfo, nil
}
func (t *Transfers) GetTransfersFromAccount(ctx context.Context, accountID entities.AccountID, pagination entities.CursorPagination) ([]entities.Transfer,
entities.PageInfo, error,
) {
defer metrics.StartSQLQuery("Transfers", "GetTransfersFromAccount")()
transfers, pageInfo, err := t.getTransfers(ctx, pagination, "WHERE from_account_id = $1", accountID)
if err != nil {
return nil, entities.PageInfo{}, fmt.Errorf("getting transfers from account:%w", err)
}
return transfers, pageInfo, nil
}
func (t *Transfers) GetTransfersToAccount(ctx context.Context, accountID entities.AccountID, pagination entities.CursorPagination) ([]entities.Transfer,
entities.PageInfo, error,
) {
defer metrics.StartSQLQuery("Transfers", "GetTransfersToAccount")()
transfers, pageInfo, err := t.getTransfers(ctx, pagination, "WHERE to_account_id = $1", accountID)
if err != nil {
return nil, entities.PageInfo{}, fmt.Errorf("getting transfers to account:%w", err)
}
return transfers, pageInfo, nil
}
func (t *Transfers) GetAll(ctx context.Context, pagination entities.CursorPagination) ([]entities.Transfer,
entities.PageInfo, error,
) {
defer metrics.StartSQLQuery("Transfers", "GetAll")()
return t.getTransfers(ctx, pagination, "")
}
func (t *Transfers) getTransfers(ctx context.Context, pagination entities.CursorPagination, where string, args ...interface{}) ([]entities.Transfer,
entities.PageInfo, error,
) {
var (
pageInfo entities.PageInfo
err error
)
query := "select * from transfers_current " + where
query, args, err = PaginateQuery[entities.TransferCursor](query, args, transfersOrdering, pagination)
if err != nil {
return nil, pageInfo, err
}
var transfers []entities.Transfer
err = pgxscan.Select(ctx, t.Connection, &transfers, query, args...)
if err != nil {
return nil, pageInfo, fmt.Errorf("getting transfers:%w", err)
}
transfers, pageInfo = entities.PageEntities[*v2.TransferEdge](transfers, pagination)
return transfers, pageInfo, nil
}