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
/
asset.go
171 lines (145 loc) · 4.24 KB
/
asset.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
package actions
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/xdbfoundation/go/protocols/frontier"
frontierContext "github.com/xdbfoundation/go/services/frontier/internal/context"
"github.com/xdbfoundation/go/services/frontier/internal/db2"
"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"
)
// AssetStatsHandler is the action handler for the /asset endpoint
type AssetStatsHandler struct {
LedgerState *ledger.State
}
func (handler AssetStatsHandler) validateAssetParams(code, issuer string, pq db2.PageQuery) error {
if code != "" {
if !xdr.ValidAssetCode.MatchString(code) {
return problem.MakeInvalidFieldProblem(
"asset_code",
fmt.Errorf("%s is not a valid asset code", code),
)
}
}
if issuer != "" {
if _, err := xdr.AddressToAccountId(issuer); err != nil {
return problem.MakeInvalidFieldProblem(
"asset_issuer",
fmt.Errorf("%s is not a valid asset issuer", issuer),
)
}
}
if pq.Cursor != "" {
parts := strings.SplitN(pq.Cursor, "_", 3)
if len(parts) != 3 {
return problem.MakeInvalidFieldProblem(
"cursor",
errors.New("the cursor is not a valid paging_token"),
)
}
cursorCode, cursorIssuer, assetType := parts[0], parts[1], parts[2]
if !xdr.ValidAssetCode.MatchString(cursorCode) {
return problem.MakeInvalidFieldProblem(
"cursor",
fmt.Errorf("%s is not a valid asset code", cursorCode),
)
}
if _, err := xdr.AddressToAccountId(cursorIssuer); err != nil {
return problem.MakeInvalidFieldProblem(
"cursor",
fmt.Errorf("%s is not a valid asset issuer", cursorIssuer),
)
}
if _, ok := xdr.StringToAssetType[assetType]; !ok {
return problem.MakeInvalidFieldProblem(
"cursor",
fmt.Errorf("%s is not a valid asset type", assetType),
)
}
}
return nil
}
func (handler AssetStatsHandler) findIssuersForAssets(
ctx context.Context,
historyQ *history.Q,
assetStats []history.ExpAssetStat,
) (map[string]history.AccountEntry, error) {
issuerSet := map[string]bool{}
issuers := []string{}
for _, assetStat := range assetStats {
if issuerSet[assetStat.AssetIssuer] {
continue
}
issuerSet[assetStat.AssetIssuer] = true
issuers = append(issuers, assetStat.AssetIssuer)
}
accountsByID := map[string]history.AccountEntry{}
accounts, err := historyQ.GetAccountsByIDs(ctx, issuers)
if err != nil {
return nil, err
}
for _, account := range accounts {
accountsByID[account.AccountID] = account
delete(issuerSet, account.AccountID)
}
// Note it's possible that no accounts can be found for certain issuers.
// That can occur because an account can be removed when there are only empty trustlines
// pointing to it. We still continue to serve asset stats for such issuers.
return accountsByID, nil
}
// GetResourcePage returns a page of offers.
func (handler AssetStatsHandler) GetResourcePage(
w HeaderWriter,
r *http.Request,
) ([]hal.Pageable, error) {
ctx := r.Context()
code, err := getString(r, "asset_code")
if err != nil {
return nil, err
}
issuer, err := getString(r, "asset_issuer")
if err != nil {
return nil, err
}
pq, err := GetPageQuery(handler.LedgerState, r, DisableCursorValidation)
if err != nil {
return nil, err
}
if err = handler.validateAssetParams(code, issuer, pq); err != nil {
return nil, err
}
historyQ, err := frontierContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
assetStats, err := historyQ.GetAssetStats(ctx, code, issuer, pq)
if err != nil {
return nil, err
}
issuerAccounts, err := handler.findIssuersForAssets(ctx, historyQ, assetStats)
if err != nil {
return nil, err
}
var response []hal.Pageable
for _, record := range assetStats {
var assetStatResponse frontier.AssetStat
err := resourceadapter.PopulateAssetStat(
ctx,
&assetStatResponse,
record,
issuerAccounts[record.AssetIssuer],
)
if err != nil {
return nil, err
}
response = append(response, assetStatResponse)
}
return response, nil
}