-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_fees_index.go
159 lines (132 loc) · 3.78 KB
/
actions_fees_index.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
package horizon
import (
"database/sql"
"github.com/go-errors/errors"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
"gitlab.com/tokend/go/xdr"
"gitlab.com/tokend/regources"
)
// This file contains the actions:
//
// FeesAllAction: renders all registration requests
//FeesAllAction show all fees
type FeesAllAction struct {
Action
AccountType *uint64
Account string
IsOverview bool
Response resource.FeesResponse
}
func (action *FeesAllAction) JSON() {
action.Do(
action.loadParams,
action.loadData,
func() {
hal.Render(action.W, action.Response)
},
)
}
func (action *FeesAllAction) loadParams() {
action.Account = action.GetString("account_id")
action.AccountType = action.GetOptionalUint64("account_type")
if action.Account != "" && action.AccountType != nil {
action.SetInvalidField("account_type", errors.New("It's not allowed to set both filters"))
}
}
func (action *FeesAllAction) getAccountType(name string) *int32 {
rawAccountType := action.GetInt32(name)
if action.Err != nil {
return nil
}
if rawAccountType == 0 {
return nil
}
action.SetInvalidField(name, errors.New("Invalid"))
return nil
}
func (action *FeesAllAction) loadData() {
q := action.CoreQ().FeeEntries()
// for the overview we need to return all the fee rules we have, so we just ignore filters
if !action.IsOverview {
// pass all the filters. Q will resolve them correctly
q = q.ForAccount(action.Account).ForAccountType(action.AccountType)
}
actualFees, err := q.Select()
if err != nil {
if err != sql.ErrNoRows {
action.Err = &problem.ServerError
action.Log.WithStack(err).WithError(err).Error("Could not get fee from the database")
return
}
err = nil
}
// convert to map of regourcess
action.Response.Fees = map[xdr.AssetCode][]regources.FeeEntry{}
var fee regources.FeeEntry
for _, coreFee := range actualFees {
fee = resource.NewFeeEntry(coreFee)
ac := xdr.AssetCode(coreFee.Asset)
action.Response.Fees[ac] = append(action.Response.Fees[ac], fee)
}
// for overview we do not need to populate default fees
if action.IsOverview {
return
}
assets, err := action.CoreQ().Assets().Select()
if err != nil {
action.Log.WithError(err).Error("Failed to load assets")
action.Err = &problem.ServerError
return
}
for _, asset := range assets {
ac := xdr.AssetCode(asset.Code)
action.Response.Fees[ac] = action.addDefaultEntriesForAsset(asset, action.Response.Fees[ac])
}
}
func feesContainsType(feeType int, entries []regources.FeeEntry) bool {
for _, entry := range entries {
if entry.FeeType == feeType {
return true
}
}
return false
}
func (action *FeesAllAction) addDefaultEntriesForAsset(asset core.Asset, entries []regources.FeeEntry) []regources.FeeEntry {
for _, feeType := range xdr.FeeTypeAll {
subtypes := []int64{0}
switch feeType {
case xdr.FeeTypePaymentFee:
subtypes = []int64{int64(xdr.PaymentFeeTypeIncoming), int64(xdr.PaymentFeeTypeOutgoing)}
case xdr.FeeTypeOperationFee:
subtypes = []int64{}
for _, subtype := range xdr.OperationTypeAll {
subtypes = append(subtypes, int64(subtype))
}
}
for _, subtype := range subtypes {
entries = append(entries, action.getDefaultFee(asset.Code, int(feeType), subtype))
}
}
return entries
}
func (action *FeesAllAction) getDefaultFee(asset string, feeType int, subType int64) regources.FeeEntry {
accountType := uint64(0)
if action.AccountType != nil {
accountType = *action.AccountType
}
return regources.FeeEntry{
Asset: asset,
FeeType: feeType,
Subtype: subType,
Percent: "0",
Fixed: "0",
LowerBound: "0",
UpperBound: "0",
AccountRole: accountType,
AccountID: action.Account,
FeeAsset: asset,
}
}