-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_account_limits_v2.go
110 lines (94 loc) · 2.75 KB
/
actions_account_limits_v2.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
package horizon
import (
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
)
// This file contains the actions:
//
// LimitsV2AccountShowAction: renders AccountTypeLimits for account
type LimitsV2AccountShowAction struct {
Action
AccountID string
Result resource.LimitsResponse
}
// JSON is a method for actions.JSON
func (action *LimitsV2AccountShowAction) JSON() {
action.Do(
action.loadParams,
action.loadData,
func() {
hal.Render(action.W, action.Result)
},
)
}
func (action *LimitsV2AccountShowAction) loadParams() {
action.AccountID = action.GetNonEmptyString("id")
}
func (action *LimitsV2AccountShowAction) checkAllowed() {
action.IsAllowed(action.AccountID)
}
func (action *LimitsV2AccountShowAction) loadData() {
account, err := action.CoreQ().Accounts().ByAddress(action.AccountID)
if err != nil {
action.Log.WithError(err).Error("Failed to load account by ID")
action.Err = &problem.ServerError
return
}
if account == nil {
action.Err = &problem.NotFound
return
}
limits, err := action.CoreQ().LimitsV2().ForAccount(account)
if err != nil {
action.Log.WithError(err).Error("Failed to load limits for account")
action.Err = &problem.ServerError
return
}
stats, err := action.loadStatistics(account.AccountID)
if err != nil {
action.Log.WithError(err).Error("failed to load stats for account")
action.Err = &problem.ServerError
return
}
for _, limit := range limits {
var result resource.LimitResponse
result.Limit.Populate(limit)
stat, ok := stats[statsKey{
StatsOpType: limit.StatsOpType,
AssetCode: limit.AssetCode,
IsConvertNeeded: limit.IsConvertNeeded,
}]
if ok {
result.Statistics.Populate(stat)
} else {
result.Statistics.IsConvertNeeded = limit.IsConvertNeeded
result.Statistics.AssetCode = limit.AssetCode
result.Statistics.StatsOpType = limit.StatsOpType
result.Statistics.AccountId = action.AccountID
}
action.Result.Limits = append(action.Result.Limits, result)
}
}
type statsKey struct {
StatsOpType int32
AssetCode string
IsConvertNeeded bool
}
func (action *LimitsV2AccountShowAction) loadStatistics(accountID string) (map[statsKey]core.StatisticsV2Entry, error) {
rawStats, err := action.CoreQ().StatisticsV2().ForAccount(accountID)
if err != nil {
return nil, errors.Wrap(err, "failed to load statistics for account")
}
result := map[statsKey]core.StatisticsV2Entry{}
for i := range rawStats {
result[statsKey{
StatsOpType: rawStats[i].StatsOpType,
AssetCode: rawStats[i].AssetCode,
IsConvertNeeded: rawStats[i].IsConvertNeeded,
}] = rawStats[i]
}
return result, nil
}