This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
claimable_balance.go
183 lines (156 loc) · 4.87 KB
/
claimable_balance.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
package actions
import (
"context"
"net/http"
"strings"
"github.com/xdbfoundation/go/protocols/frontier"
protocol "github.com/xdbfoundation/go/protocols/frontier"
frontierContext "github.com/xdbfoundation/go/services/frontier/internal/context"
"github.com/xdbfoundation/go/services/frontier/internal/db2/history"
"github.com/xdbfoundation/go/services/frontier/internal/ledger"
"github.com/xdbfoundation/go/services/frontier/internal/resourceadapter"
"github.com/xdbfoundation/go/support/errors"
"github.com/xdbfoundation/go/support/render/hal"
"github.com/xdbfoundation/go/support/render/problem"
"github.com/xdbfoundation/go/xdr"
)
// GetClaimableBalanceByIDHandler is the action handler for all end-points returning a claimable balance.
type GetClaimableBalanceByIDHandler struct{}
// ClaimableBalanceQuery query struct for claimables_balances/id end-point
type ClaimableBalanceQuery struct {
ID string `schema:"id" valid:"claimableBalanceID,required"`
}
// GetResource returns an claimable balance page.
func (handler GetClaimableBalanceByIDHandler) GetResource(w HeaderWriter, r *http.Request) (interface{}, error) {
ctx := r.Context()
qp := ClaimableBalanceQuery{}
err := getParams(&qp, r)
if err != nil {
return nil, err
}
historyQ, err := frontierContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
cb, err := historyQ.FindClaimableBalanceByID(ctx, qp.ID)
if err != nil {
return nil, err
}
ledger := &history.Ledger{}
err = historyQ.LedgerBySequence(ctx,
ledger,
int32(cb.LastModifiedLedger),
)
if historyQ.NoRows(err) {
ledger = nil
} else if err != nil {
return nil, errors.Wrap(err, "LedgerBySequence error")
}
var resource protocol.ClaimableBalance
err = resourceadapter.PopulateClaimableBalance(ctx, &resource, cb, ledger)
if err != nil {
return nil, err
}
return resource, nil
}
// ClaimableBalancesQuery query struct for claimable_balances end-point
type ClaimableBalancesQuery struct {
AssetFilter string `schema:"asset" valid:"asset,optional"`
SponsorFilter string `schema:"sponsor" valid:"accountID,optional"`
ClaimantFilter string `schema:"claimant" valid:"accountID,optional"`
}
func (q ClaimableBalancesQuery) asset() *xdr.Asset {
if len(q.AssetFilter) > 0 {
switch q.AssetFilter {
case "native":
asset := xdr.MustNewNativeAsset()
return &asset
default:
parts := strings.Split(q.AssetFilter, ":")
asset := xdr.MustNewCreditAsset(parts[0], parts[1])
return &asset
}
}
return nil
}
func (q ClaimableBalancesQuery) sponsor() *xdr.AccountId {
if q.SponsorFilter != "" {
return xdr.MustAddressPtr(q.SponsorFilter)
}
return nil
}
func (q ClaimableBalancesQuery) claimant() *xdr.AccountId {
if q.ClaimantFilter != "" {
return xdr.MustAddressPtr(q.ClaimantFilter)
}
return nil
}
// URITemplate returns a rfc6570 URI template the query struct
func (q ClaimableBalancesQuery) URITemplate() string {
return getURITemplate(&q, "claimable_balances", true)
}
type GetClaimableBalancesHandler struct {
LedgerState *ledger.State
}
// GetResourcePage returns a page of claimable balances.
func (handler GetClaimableBalancesHandler) GetResourcePage(
w HeaderWriter,
r *http.Request,
) ([]hal.Pageable, error) {
ctx := r.Context()
qp := ClaimableBalancesQuery{}
err := getParams(&qp, r)
if err != nil {
return nil, err
}
pq, err := GetPageQuery(handler.LedgerState, r, DisableCursorValidation)
if err != nil {
return nil, err
}
query := history.ClaimableBalancesQuery{
PageQuery: pq,
Asset: qp.asset(),
Sponsor: qp.sponsor(),
Claimant: qp.claimant(),
}
_, _, err = query.Cursor()
if err != nil {
return nil, problem.MakeInvalidFieldProblem(
"cursor",
errors.New("The first part should be a number higher than 0 and the second part should be a valid claimable balance ID"),
)
}
historyQ, err := frontierContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
claimableBalances, err := getClaimableBalancesPage(ctx, historyQ, query)
if err != nil {
return nil, err
}
return claimableBalances, nil
}
func getClaimableBalancesPage(ctx context.Context, historyQ *history.Q, query history.ClaimableBalancesQuery) ([]hal.Pageable, error) {
records, err := historyQ.GetClaimableBalances(ctx, query)
if err != nil {
return nil, err
}
ledgerCache := history.LedgerCache{}
for _, record := range records {
ledgerCache.Queue(int32(record.LastModifiedLedger))
}
if err := ledgerCache.Load(ctx, historyQ); err != nil {
return nil, errors.Wrap(err, "failed to load ledger batch")
}
var claimableBalances []hal.Pageable
for _, record := range records {
var response frontier.ClaimableBalance
var ledger *history.Ledger
if l, ok := ledgerCache.Records[int32(record.LastModifiedLedger)]; ok {
ledger = &l
}
resourceadapter.PopulateClaimableBalance(ctx, &response, record, ledger)
claimableBalances = append(claimableBalances, response)
}
return claimableBalances, nil
}