-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_order_book.go
91 lines (78 loc) · 2.22 KB
/
actions_order_book.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
package horizon
import (
"strconv"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
)
type OrderBookAction struct {
Action
OwnerID string
BaseAsset string
QuoteAsset string
IsBuy bool
OrderBookID *uint64
CoreRecords []core.Offer
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *OrderBookAction) JSON() {
action.Do(
action.loadParams,
action.checkIsSigned,
action.loadRecords,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *OrderBookAction) loadParams() {
action.BaseAsset = action.GetNonEmptyString("base_asset")
action.QuoteAsset = action.GetNonEmptyString("quote_asset")
action.OrderBookID = action.GetOptionalUint64("order_book_id")
action.OwnerID = action.GetString("owner_id")
action.IsBuy = action.GetBool("is_buy")
action.Page.Filters = map[string]string{
"base_asset": action.BaseAsset,
"quote_asset": action.QuoteAsset,
"is_buy": strconv.FormatBool(action.IsBuy),
"order_book_id": action.GetString("order_book_id"),
}
}
func (action *OrderBookAction) checkIsSigned() {
if action.OwnerID != "" {
action.IsAllowed(action.OwnerID)
return
}
if action.Signer != "" {
action.isAllowed(action.Signer)
}
}
func (action *OrderBookAction) loadRecords() {
q := action.CoreQ().
Offers().
ForAssets(action.BaseAsset, action.QuoteAsset).
IsBuy(action.IsBuy)
if action.OrderBookID != nil {
q = q.ForOrderBookID(*action.OrderBookID)
}
err := q.OrderByPrice(action.IsBuy).Select(&action.CoreRecords)
if err != nil {
action.Log.WithError(err).Error("Failed to get offers from core DB")
action.Err = &problem.ServerError
return
}
for i := range action.CoreRecords {
var result resource.OrderBookEntry
result.Populate(&action.CoreRecords[i].OrderBookEntry, action.BaseAsset, action.QuoteAsset, action.IsBuy)
if action.IsAdmin || action.OwnerID == action.CoreRecords[i].OwnerID {
result.OfferID = action.CoreRecords[i].OfferID
result.OwnerID = action.CoreRecords[i].OwnerID
}
action.Page.Add(&result)
}
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.PopulateLinks()
}