-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_account_detailed_balances.go
238 lines (198 loc) · 5.91 KB
/
actions_account_detailed_balances.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
package horizon
import (
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/tokend/go/amount"
"gitlab.com/tokend/go/xdr"
"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"
)
type AccountDetailedBalancesAction struct {
Action
converter *exchange.Converter
AccountID string
Balances []core.Balance
Assets []core.Asset
Sales []history.Sale
AssetCodes []string
Resource []resource.Balance
}
func (action *AccountDetailedBalancesAction) JSON() {
action.Do(
action.loadParams,
action.checkAllowed,
action.loadBalances,
action.groupBalancesByAsset,
action.loadAssets,
action.createConverter,
action.loadSales,
action.loadResource,
func() {
if action.Resource == nil {
action.Resource = make([]resource.Balance, 0)
}
hal.Render(action.W, action.Resource)
},
)
}
func (action *AccountDetailedBalancesAction) loadParams() {
action.AccountID = action.GetNonEmptyString("id")
}
func (action *AccountDetailedBalancesAction) checkAllowed() {
action.IsAllowed(action.AccountID)
}
func (action *AccountDetailedBalancesAction) getConvertToAsset(fieldName string) string {
code := action.GetString(fieldName)
if code != "" {
return code
}
statsQuoteAsset, err := action.CoreQ().Assets().ForPolicy(uint32(xdr.AssetPolicyStatsQuoteAsset)).Select()
if err != nil {
action.Log.WithError(err).Error("failed to load stats quote asset")
action.Err = &problem.ServerError
return ""
}
if len(statsQuoteAsset) == 0 {
action.SetInvalidField(fieldName, errors.New("stats quote asset is not specified. Explicitly specify convert_to asset or create stats quote asset"))
return ""
}
if len(statsQuoteAsset) > 1 {
action.Log.Error("unexpected number of stats quote assets. Expected [0,1]")
action.Err = &problem.ServerError
return ""
}
return statsQuoteAsset[0].Code
}
func (action *AccountDetailedBalancesAction) loadBalances() {
var err error
action.Balances, err = action.CoreQ().Balances().ByAddress(action.AccountID).Select()
if err != nil {
action.Log.WithError(err).Error("Failed to load balances")
action.Err = &problem.ServerError
return
}
}
func (action *AccountDetailedBalancesAction) groupBalancesByAsset() {
assetsMap := map[string]bool{}
for _, balance := range action.Balances {
if _, ok := assetsMap[balance.Asset]; ok {
continue
}
assetsMap[balance.Asset] = true
action.AssetCodes = append(action.AssetCodes, balance.Asset)
}
}
func (action *AccountDetailedBalancesAction) loadAssets() {
if len(action.AssetCodes) == 0 {
return
}
var err error
action.Assets, err = action.CoreQ().Assets().ForCodes(action.AssetCodes).Select()
if err != nil {
action.Log.WithError(err).Error("Failed to load assets for codes")
action.Err = &problem.ServerError
return
}
}
func (action *AccountDetailedBalancesAction) createConverter() {
var err error
action.converter, err = action.CreateConverter()
if err != nil {
action.Log.WithError(err).Error("Failed to init converter")
action.Err = &problem.ServerError
return
}
}
func (action *AccountDetailedBalancesAction) loadSales() {
if len(action.AssetCodes) == 0 {
return
}
var err error
action.Sales, err = selectSalesWithCurrentCap(action.HistoryQ().Sales().ForBaseAssets(action.AssetCodes...), action.converter)
if err != nil {
action.Log.WithError(err).Error("Failed to load sales")
action.Err = &problem.ServerError
return
}
}
func (action *AccountDetailedBalancesAction) loadResource() {
var convertToAsset string
if len(action.Balances) != 0 {
convertToAsset = action.getConvertToAsset("convert_to")
if action.Err != nil {
return
}
}
for _, record := range action.Balances {
var r resource.Balance
r.Populate(record)
asset := findAssetByAssetCode(record.Asset, action.Assets)
if asset == nil {
action.Log.WithField("asset_code", record.Asset).Error("Failed to find asset for existing balance")
action.Err = &problem.ServerError
return
}
r.AssetDetails = asset
var err error
r.AssetDetails.Sales, err = findAllSalesForAsset(action.CoreQ(), asset.Code, action.Sales)
if err != nil {
action.Log.WithError(err).Error("failed to find all sales for asset")
action.Err = &problem.ServerError
return
}
r.ConvertedBalance, err = convertAmount(record.Amount, r.Asset, convertToAsset, action.converter)
if err != nil {
action.Log.WithError(err).Error("Failed to convert balance")
action.Err = &problem.ServerError
return
}
r.ConvertedLocked, err = convertAmount(record.Locked, r.Asset, convertToAsset, action.converter)
if err != nil {
action.Log.WithError(err).Error("failed to convert locked amount")
action.Err = &problem.ServerError
return
}
r.ConvertedToAsset = convertToAsset
action.Resource = append(action.Resource, r)
}
}
func convertAmount(balance int64, fromAsset, toAsset string, converter *exchange.Converter) (string, error) {
convertedAmount, err := converter.TryToConvertWithOneHop(balance, fromAsset, toAsset)
if err != nil {
return "", err
}
if convertedAmount == nil {
return amount.String(0), nil
}
return amount.String(*convertedAmount), nil
}
func findAssetByAssetCode(code string, assets []core.Asset) *resource.Asset {
for i := range assets {
if code != assets[i].Code {
continue
}
var result resource.Asset
result.Populate(&assets[i])
return &result
}
return nil
}
func findAllSalesForAsset(q core.QInterface, code string, sales []history.Sale) ([]resource.Sale, error) {
var result []resource.Sale
for i := range sales {
if sales[i].BaseAsset != code {
continue
}
var sale resource.Sale
sale.Populate(&sales[i])
err := populateSaleWithStats(sales[i].ID, &sale, q)
if err != nil {
return nil, errors.Wrap(err, "failed to populate sale with stats")
}
result = append(result, sale)
}
return result, nil
}