-
Notifications
You must be signed in to change notification settings - Fork 19
/
funding_payments.go
96 lines (79 loc) · 2.91 KB
/
funding_payments.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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 FundingPayments struct {
*ConnectionSource
}
var fundingPaymentOrdering = TableOrdering{
ColumnOrdering{Name: "vega_time", Sorting: ASC},
ColumnOrdering{Name: "market_id", Sorting: ASC},
ColumnOrdering{Name: "funding_period_seq", Sorting: ASC},
ColumnOrdering{Name: "party_id", Sorting: ASC},
}
func NewFundingPayments(connectionSource *ConnectionSource) *FundingPayments {
return &FundingPayments{
ConnectionSource: connectionSource,
}
}
func (fp *FundingPayments) Add(
ctx context.Context,
fundingPayments []*entities.FundingPayment,
) error {
defer metrics.StartSQLQuery("FundingPayments", "Add")()
for _, v := range fundingPayments {
_, err := fp.Connection.Exec(ctx,
`insert into funding_payment(market_id, party_id, funding_period_seq, amount, vega_time, tx_hash)
values ($1, $2, $3, $4, $5, $6)`,
v.MarketID, v.PartyID, v.FundingPeriodSeq, v.Amount, v.VegaTime, v.TxHash)
if err != nil {
return err
}
}
return nil
}
func (fp *FundingPayments) List(
ctx context.Context,
partyID entities.PartyID,
marketID *entities.MarketID,
pagination entities.CursorPagination,
) ([]entities.FundingPayment, entities.PageInfo, error) {
defer metrics.StartSQLQuery("FundingPayments", "List")()
var fundingPayments []entities.FundingPayment
var pageInfo entities.PageInfo
var args []interface{}
var err error
query := fmt.Sprintf("select * from funding_payment where party_id = %s", nextBindVar(&args, partyID))
if marketID != nil {
query = fmt.Sprintf("%s and market_id = %s", query, nextBindVar(&args, *marketID))
}
query, args, err = PaginateQuery[entities.FundingPaymentCursor](query, args, fundingPaymentOrdering, pagination)
if err != nil {
return fundingPayments, pageInfo, err
}
err = pgxscan.Select(ctx, fp.Connection, &fundingPayments, query, args...)
if err != nil {
return fundingPayments, pageInfo, err
}
fundingPayments, pageInfo = entities.PageEntities[*v2.FundingPaymentEdge](fundingPayments, pagination)
return fundingPayments, pageInfo, nil
}