-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_sale_show.go
185 lines (154 loc) · 5.23 KB
/
actions_sale_show.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
package horizon
import (
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/tokend/go/amount"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/db2/history"
"gitlab.com/tokend/horizon/exchange"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
)
// SaleShowAction renders a sale found by its ID.
type SaleShowAction struct {
Action
RequestID uint64
Record *history.Sale
result resource.Sale
}
// JSON is a method for actions.JSON
func (action *SaleShowAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.loadRecord,
action.populateResult,
func() {
hal.Render(action.W, action.result)
},
)
}
func (action *SaleShowAction) loadParams() {
action.RequestID = action.GetUInt64("id")
}
func (action *SaleShowAction) loadRecord() {
var err error
action.Record, err = action.HistoryQ().Sales().ByID(action.RequestID)
if err != nil {
action.Log.WithError(err).
WithField("request_id", action.RequestID).
Error("failed to load sale")
action.Err = &problem.ServerError
return
}
if action.Record == nil {
action.Err = &problem.NotFound
return
}
err = action.populateTotalCurrentCap()
if err != nil {
action.Log.WithError(err).Error("failed to populate total current cap")
action.Err = &problem.ServerError
return
}
}
func (action *SaleShowAction) populateTotalCurrentCap() error {
converter, err := action.CreateConverter()
if err != nil {
return errors.Wrap(err, "failed to init converter")
}
totalCapInDefaultQuoteAsset, err := getCurrentCapInDefaultQuote(*action.Record, converter)
if err != nil {
return errors.Wrap(err, "failed to get current cap in default quote asset")
}
action.Record.CurrentCap = amount.String(totalCapInDefaultQuoteAsset)
for i := range action.Record.QuoteAssets.QuoteAssets {
quoteAsset := &action.Record.QuoteAssets.QuoteAssets[i]
totalCapInQuote, err := converter.TryToConvertWithOneHop(totalCapInDefaultQuoteAsset, action.Record.DefaultQuoteAsset, quoteAsset.Asset)
if err != nil {
return errors.Wrap(err, "failed to convert total cap in default to quote")
}
if totalCapInQuote == nil {
return errors.New("failed to convert total cap in default to quote: failed to find path")
}
quoteAsset.TotalCurrentCap = amount.String(*totalCapInQuote)
hardCapInQuote, err := converter.TryToConvertWithOneHop(int64(action.Record.HardCap), action.Record.DefaultQuoteAsset, quoteAsset.Asset)
if err != nil {
return errors.Wrap(err, "failed to convert hard cap")
}
if hardCapInQuote == nil {
return errors.New("failed to convert hard cap to quote asset")
}
quoteAsset.HardCap = amount.String(*hardCapInQuote)
}
return nil
}
func (action *SaleShowAction) populateResult() {
action.result.Populate(action.Record)
err := populateSaleWithStats(action.Record.ID, &action.result, action.CoreQ())
if err != nil {
action.Log.WithError(err).
WithField("request_id", action.RequestID).
Error("failed to populate stat for sale")
action.Err = &problem.ServerError
return
}
}
func populateSaleWithStats(saleID uint64, sale *resource.Sale, q core.QInterface) error {
var offers []core.Offer
err := q.Offers().ForOrderBookID(saleID).Select(&offers)
if err != nil {
return errors.Wrap(err, "failed to load offers for sale", map[string]interface{}{
"sale_id": saleID,
})
}
balances, err := q.Balances().ByAsset(sale.BaseAsset).Select()
if err != nil {
return errors.Wrap(err, "failed to load balances for sale's base asset", map[string]interface{}{
"sale_id": saleID,
})
}
err = sale.PopulateStat(offers, balances)
if err != nil {
return errors.Wrap(err, "failed to populate stats for sale", map[string]interface{}{
"sale_id": saleID,
})
}
return nil
}
func selectSalesWithCurrentCap(q history.SalesQ, converter *exchange.Converter) ([]history.Sale, error) {
result, err := q.Select()
if err != nil {
return nil, errors.Wrap(err, "failed to load sales")
}
for i := range result {
currentCapInDefaultQuote, err := getCurrentCapInDefaultQuote(result[i], converter)
if err != nil {
return nil, errors.Wrap(err, "failed to calculate current cap in default quote")
}
result[i].CurrentCap = amount.String(currentCapInDefaultQuote)
}
return result, nil
}
func getCurrentCapInDefaultQuote(sale history.Sale, converter *exchange.Converter) (int64, error) {
totalCapInDefaultQuoteAsset := int64(0)
for _, quoteAsset := range sale.QuoteAssets.QuoteAssets {
currentCap, err := amount.Parse(quoteAsset.CurrentCap)
if err != nil {
return 0, errors.Wrap(err, "failed to parse current cap")
}
currentCapInDefaultQuoteAsset, err := converter.TryToConvertWithOneHop(currentCap, quoteAsset.Asset, sale.DefaultQuoteAsset)
if err != nil {
return 0, errors.Wrap(err, "failed to convert current cap to default quote asset")
}
if currentCapInDefaultQuoteAsset == nil {
return 0, errors.New("failed to convert to current cap: no path found")
}
var isOk bool
totalCapInDefaultQuoteAsset, isOk = amount.SafePositiveSum(totalCapInDefaultQuoteAsset, *currentCapInDefaultQuoteAsset)
if !isOk {
return 0, errors.New("failed to find total cap in default quote asset: overflow")
}
}
return totalCapInDefaultQuoteAsset, nil
}