-
Notifications
You must be signed in to change notification settings - Fork 0
/
offer.go
213 lines (175 loc) · 5.16 KB
/
offer.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
package actions
import (
"context"
"net/http"
"github.com/shantanu-hashcash/go/protocols/aurora"
auroraContext "github.com/shantanu-hashcash/go/services/aurora/internal/context"
"github.com/shantanu-hashcash/go/services/aurora/internal/db2/history"
"github.com/shantanu-hashcash/go/services/aurora/internal/ledger"
"github.com/shantanu-hashcash/go/services/aurora/internal/resourceadapter"
"github.com/shantanu-hashcash/go/support/errors"
"github.com/shantanu-hashcash/go/support/render/hal"
)
// AccountOffersQuery query struct for offers end-point
type OfferByIDQuery struct {
OfferID uint64 `schema:"offer_id" valid:"-"`
}
// GetOfferByID is the action handler for the /offers/{id} endpoint
type GetOfferByID struct{}
// GetResource returns an offer by id.
func (handler GetOfferByID) GetResource(w HeaderWriter, r *http.Request) (interface{}, error) {
ctx := r.Context()
qp := OfferByIDQuery{}
if err := getParams(&qp, r); err != nil {
return nil, err
}
historyQ, err := auroraContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
record, err := historyQ.GetOfferByID(r.Context(), int64(qp.OfferID))
if err != nil {
return nil, err
}
ledger := &history.Ledger{}
err = historyQ.LedgerBySequence(
r.Context(),
ledger,
int32(record.LastModifiedLedger),
)
if historyQ.NoRows(err) {
ledger = nil
} else if err != nil {
return nil, err
}
var offerResponse aurora.Offer
resourceadapter.PopulateOffer(ctx, &offerResponse, record, ledger)
return offerResponse, nil
}
// OffersQuery query struct for offers end-point
type OffersQuery struct {
SellingBuyingAssetQueryParams `valid:"-"`
Seller string `schema:"seller" valid:"accountID,optional"`
Sponsor string `schema:"sponsor" valid:"accountID,optional"`
}
// URITemplate returns a rfc6570 URI template the query struct
func (q OffersQuery) URITemplate() string {
// building this manually since we don't want to include all the params in SellingBuyingAssetQueryParams
return "/offers{?selling,buying,seller,sponsor,cursor,limit,order}"
}
// Validate runs custom validations.
func (q OffersQuery) Validate() error {
return q.SellingBuyingAssetQueryParams.Validate()
}
// GetOffersHandler is the action handler for the /offers endpoint
type GetOffersHandler struct {
LedgerState *ledger.State
}
// GetResourcePage returns a page of offers.
func (handler GetOffersHandler) GetResourcePage(
w HeaderWriter,
r *http.Request,
) ([]hal.Pageable, error) {
ctx := r.Context()
qp := OffersQuery{}
err := getParams(&qp, r)
if err != nil {
return nil, err
}
pq, err := GetPageQuery(handler.LedgerState, r)
if err != nil {
return nil, err
}
selling, err := qp.Selling()
if err != nil {
return nil, err
}
buying, err := qp.Buying()
if err != nil {
return nil, err
}
query := history.OffersQuery{
PageQuery: pq,
SellerID: qp.Seller,
Sponsor: qp.Sponsor,
Selling: selling,
Buying: buying,
}
historyQ, err := auroraContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
offers, err := getOffersPage(ctx, historyQ, query)
if err != nil {
return nil, err
}
return offers, nil
}
// AccountOffersQuery query struct for offers end-point
type AccountOffersQuery struct {
AccountID string `schema:"account_id" valid:"accountID,required"`
}
// GetAccountOffersHandler is the action handler for the
// `/accounts/{account_id}/offers` endpoint when using experimental ingestion.
type GetAccountOffersHandler struct {
LedgerState *ledger.State
}
func (handler GetAccountOffersHandler) parseOffersQuery(r *http.Request) (history.OffersQuery, error) {
pq, err := GetPageQuery(handler.LedgerState, r)
if err != nil {
return history.OffersQuery{}, err
}
qp := AccountOffersQuery{}
if err = getParams(&qp, r); err != nil {
return history.OffersQuery{}, err
}
query := history.OffersQuery{
PageQuery: pq,
SellerID: qp.AccountID,
}
return query, nil
}
// GetResourcePage returns a page of offers for a given account.
func (handler GetAccountOffersHandler) GetResourcePage(
w HeaderWriter,
r *http.Request,
) ([]hal.Pageable, error) {
ctx := r.Context()
query, err := handler.parseOffersQuery(r)
if err != nil {
return nil, err
}
historyQ, err := auroraContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
offers, err := getOffersPage(ctx, historyQ, query)
if err != nil {
return nil, err
}
return offers, nil
}
func getOffersPage(ctx context.Context, historyQ *history.Q, query history.OffersQuery) ([]hal.Pageable, error) {
records, err := historyQ.GetOffers(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 offers []hal.Pageable
for _, record := range records {
var offerResponse aurora.Offer
var ledger *history.Ledger
if l, ok := ledgerCache.Records[int32(record.LastModifiedLedger)]; ok {
ledger = &l
}
resourceadapter.PopulateOffer(ctx, &offerResponse, record, ledger)
offers = append(offers, offerResponse)
}
return offers, nil
}