-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_trades.go
71 lines (61 loc) · 1.76 KB
/
actions_trades.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
package horizon
import (
"gitlab.com/tokend/horizon/db2"
"gitlab.com/tokend/horizon/db2/history"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
)
type TradesAction struct {
Action
BaseAsset string
QuoteAsset string
PagingParams db2.PageQuery
OrderBookID *uint64
Trades []history.Trades
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *TradesAction) JSON() {
action.Do(
action.loadParams,
action.loadRecords,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *TradesAction) loadParams() {
action.BaseAsset = action.GetNonEmptyString("base_asset")
action.QuoteAsset = action.GetNonEmptyString("quote_asset")
action.OrderBookID = action.GetOptionalUint64("order_book_id")
action.PagingParams = action.GetPageQuery()
action.Page.Filters = map[string]string{
"base_asset": action.BaseAsset,
"quote_asset": action.QuoteAsset,
"order_book_id": action.GetString("order_book_id"),
}
}
func (action *TradesAction) loadRecords() {
q := action.HistoryQ().Trades().ForPair(action.BaseAsset, action.QuoteAsset).Page(action.PagingParams)
if action.OrderBookID != nil {
q = q.ForOrderBook(*action.OrderBookID)
}
err := q.Select(&action.Trades)
if err != nil {
action.Log.WithError(err).Error("Failed to get trades")
action.Err = &problem.ServerError
return
}
for i := range action.Trades {
var result resource.Trades
result.Populate(&action.Trades[i])
action.Page.Add(&result)
}
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.Limit = action.PagingParams.Limit
action.Page.Cursor = action.PagingParams.Cursor
action.Page.Order = action.PagingParams.Order
action.Page.PopulateLinks()
}