/
actions_trade.go
282 lines (245 loc) · 8.56 KB
/
actions_trade.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package horizon
import (
"strconv"
gTime "time"
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/actions"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/render/sse"
"github.com/stellar/go/services/horizon/internal/resourceadapter"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/render/hal"
"github.com/stellar/go/support/time"
"github.com/stellar/go/xdr"
)
// Interface verifications
var _ actions.JSONer = (*TradeIndexAction)(nil)
var _ actions.EventStreamer = (*TradeIndexAction)(nil)
type TradeIndexAction struct {
Action
BaseAssetFilter xdr.Asset
HasBaseAssetFilter bool
CounterAssetFilter xdr.Asset
HasCounterAssetFilter bool
OfferFilter int64
AccountFilter string
PagingParams db2.PageQuery
Records []history.Trade
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *TradeIndexAction) JSON() error {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.loadRecords,
action.loadPage,
func() { hal.Render(action.W, action.Page) },
)
return action.Err
}
// SSE is a method for actions.SSE
func (action *TradeIndexAction) SSE(stream *sse.Stream) error {
action.Setup(
action.EnsureHistoryFreshness,
action.loadParams,
)
action.Do(
action.loadRecords,
func() {
stream.SetLimit(int(action.PagingParams.Limit))
records := action.Records[stream.SentCount():]
for _, record := range records {
var res horizon.Trade
resourceadapter.PopulateTrade(action.R.Context(), &res, record)
stream.Send(sse.Event{
ID: res.PagingToken(),
Data: res,
})
}
},
)
return action.Err
}
// loadParams sets action.Query from the request params
func (action *TradeIndexAction) loadParams() {
action.PagingParams = action.GetPageQuery()
action.BaseAssetFilter, action.HasBaseAssetFilter = action.MaybeGetAsset("base_")
action.CounterAssetFilter, action.HasCounterAssetFilter = action.MaybeGetAsset("counter_")
action.OfferFilter = action.GetInt64("offer_id")
action.AccountFilter = action.GetAddress("account_id")
if (!action.HasBaseAssetFilter && action.HasCounterAssetFilter) ||
(action.HasBaseAssetFilter && !action.HasCounterAssetFilter) {
action.SetInvalidField("base_asset_type,counter_asset_type", errors.New("this endpoint supports asset pairs but only one asset supplied"))
}
}
// loadRecords populates action.Records
func (action *TradeIndexAction) loadRecords() {
trades := action.HistoryQ().Trades()
if action.AccountFilter != "" {
trades.ForAccount(action.AccountFilter)
}
if action.HasBaseAssetFilter {
baseAssetId, err := action.HistoryQ().GetAssetID(action.BaseAssetFilter)
if err != nil {
action.Err = err
return
}
if action.HasCounterAssetFilter {
counterAssetId, err := action.HistoryQ().GetAssetID(action.CounterAssetFilter)
if err != nil {
action.Err = err
return
}
trades = action.HistoryQ().TradesForAssetPair(baseAssetId, counterAssetId)
} else {
action.Err = errors.New("this endpoint supports asset pairs but only one asset supplied")
return
}
}
if action.OfferFilter != 0 {
trades = trades.ForOffer(action.OfferFilter)
}
action.Err = trades.Page(action.PagingParams).Select(&action.Records)
}
// loadPage populates action.Page
func (action *TradeIndexAction) loadPage() {
for _, record := range action.Records {
var res horizon.Trade
resourceadapter.PopulateTrade(action.R.Context(), &res, record)
action.Page.Add(res)
}
action.Page.FullURL = action.FullURL()
action.Page.Limit = action.PagingParams.Limit
action.Page.Cursor = action.PagingParams.Cursor
action.Page.Order = action.PagingParams.Order
action.Page.PopulateLinks()
}
// Interface verification
var _ actions.JSONer = (*TradeAggregateIndexAction)(nil)
type TradeAggregateIndexAction struct {
Action
BaseAssetFilter xdr.Asset
CounterAssetFilter xdr.Asset
StartTimeFilter time.Millis
EndTimeFilter time.Millis
OffsetFilter int64
ResolutionFilter int64
PagingParams db2.PageQuery
Records []history.TradeAggregation
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *TradeAggregateIndexAction) JSON() error {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.loadRecords,
action.loadPage,
func() { hal.Render(action.W, action.Page) },
)
return action.Err
}
func (action *TradeAggregateIndexAction) loadParams() {
action.PagingParams = action.GetPageQuery()
action.BaseAssetFilter = action.GetAsset("base_")
action.CounterAssetFilter = action.GetAsset("counter_")
action.OffsetFilter = action.GetInt64("offset")
action.StartTimeFilter = action.GetTimeMillis("start_time")
action.EndTimeFilter = action.GetTimeMillis("end_time")
action.ResolutionFilter = action.GetInt64("resolution")
//check if resolution is legal
resolutionDuration := gTime.Duration(action.ResolutionFilter) * gTime.Millisecond
if history.StrictResolutionFiltering {
if _, ok := history.AllowedResolutions[resolutionDuration]; !ok {
action.SetInvalidField("resolution", errors.New("illegal or missing resolution. "+
"allowed resolutions are: 1 minute (60000), 5 minutes (300000), 15 minutes (900000), 1 hour (3600000), "+
"1 day (86400000) and 1 week (604800000)"))
}
}
// check if offset is legal
offsetDuration := gTime.Duration(action.OffsetFilter) * gTime.Millisecond
if offsetDuration%gTime.Hour != 0 || offsetDuration >= gTime.Hour*24 || offsetDuration > resolutionDuration {
action.SetInvalidField("offset", errors.New("illegal or missing offset. offset must be a multiple of an"+
" hour, less than or equal to the resolution, and less than 24 hours"))
}
}
// loadRecords populates action.Records
func (action *TradeAggregateIndexAction) loadRecords() {
historyQ := action.HistoryQ()
//get asset ids
baseAssetId, err := historyQ.GetCreateAssetID(action.BaseAssetFilter)
if err != nil {
action.Err = err
return
}
counterAssetId, err := historyQ.GetCreateAssetID(action.CounterAssetFilter)
if err != nil {
action.Err = err
return
}
//initialize the query builder with required params
tradeAggregationsQ, err := historyQ.GetTradeAggregationsQ(
baseAssetId, counterAssetId, action.ResolutionFilter, action.OffsetFilter, action.PagingParams)
if err != nil {
action.Err = err
return
}
//set time range if supplied
if !action.StartTimeFilter.IsNil() {
tradeAggregationsQ, err = tradeAggregationsQ.WithStartTime(action.StartTimeFilter)
if err != nil {
action.SetInvalidField("start_time", errors.New("illegal start time. adjusted start time must "+
"be less than the provided end time if the end time is greater than 0"))
return
}
}
if !action.EndTimeFilter.IsNil() {
tradeAggregationsQ, err = tradeAggregationsQ.WithEndTime(action.EndTimeFilter)
if err != nil {
action.SetInvalidField("end_time", errors.New("illegal end time. adjusted end time "+
"must be greater than the offset and greater than the provided start time"))
return
}
}
action.Err = historyQ.Select(&action.Records, tradeAggregationsQ.GetSql())
}
func (action *TradeAggregateIndexAction) loadPage() {
action.Page.Init()
for _, record := range action.Records {
var res horizon.TradeAggregation
action.Err = resourceadapter.PopulateTradeAggregation(action.R.Context(), &res, record)
if action.Err != nil {
return
}
action.Page.Add(res)
}
action.Page.Limit = action.PagingParams.Limit
action.Page.Order = action.PagingParams.Order
newUrl := action.FullURL() // preserve scheme and host for the new url links
q := newUrl.Query()
action.Page.Links.Self = hal.NewLink(newUrl.String())
//adjust time range for next page
if uint64(len(action.Records)) == 0 {
action.Page.Links.Next = action.Page.Links.Self
} else {
if action.PagingParams.Order == "asc" {
newStartTime := action.Records[len(action.Records)-1].Timestamp + action.ResolutionFilter
if newStartTime >= action.EndTimeFilter.ToInt64() {
newStartTime = action.EndTimeFilter.ToInt64()
}
q.Set("start_time", strconv.FormatInt(newStartTime, 10))
newUrl.RawQuery = q.Encode()
action.Page.Links.Next = hal.NewLink(newUrl.String())
} else { //desc
newEndTime := action.Records[len(action.Records)-1].Timestamp
if newEndTime <= action.StartTimeFilter.ToInt64() {
newEndTime = action.StartTimeFilter.ToInt64()
}
q.Set("end_time", strconv.FormatInt(newEndTime, 10))
newUrl.RawQuery = q.Encode()
action.Page.Links.Next = hal.NewLink(newUrl.String())
}
}
}