-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_account_fees.go
107 lines (93 loc) · 2.81 KB
/
actions_account_fees.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
package horizon
import (
"github.com/pkg/errors"
"gitlab.com/tokend/go/xdr"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/helper/fees"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
"gitlab.com/tokend/regources"
)
// This file contains the actions:
//
// FeesForAccount: renders all the fees for a specific account
//FeesForAccount show all fees for account
type AccountFeesAction struct {
Action
Account *core.Account
Records fees.SmartFeeTable
Assets []string
Response resource.FeesResponse
}
func (action *AccountFeesAction) JSON() {
action.Do(
action.loadParams,
action.loadAssets,
action.loadFees,
action.loadResponse,
func() {
hal.Render(action.W, action.Response)
},
)
}
func (action *AccountFeesAction) loadParams() {
action.Account = action.GetCoreAccount("id", action.CoreQ())
if action.Err != nil {
return
}
if action.Account == nil {
action.SetInvalidField("id", errors.New("Must not be empty"))
return
}
}
func (action *AccountFeesAction) loadAssets() {
accountBalances, err := action.CoreQ().Balances().ByAddress(action.Account.AccountID).Select()
if err != nil {
action.Log.WithError(err).Error("failed to load balances")
action.Err = &problem.ServerError
return
}
var assets []string
for _, balance := range accountBalances {
assets = append(assets, balance.Asset)
}
action.Assets = assets
}
func (action *AccountFeesAction) loadFees() {
forAccount, err := action.CoreQ().FeeEntries().ForAccount(action.Account.AccountID).Select()
if err != nil {
action.Err = &problem.ServerError
action.Log.WithError(err).Error("can't get account fees from the database")
return
}
forAccountType, err := action.CoreQ().FeeEntries().ForAccountType(&action.Account.RoleID).Select()
if err != nil {
action.Err = &problem.ServerError
action.Log.WithError(err).Error("can't get account type fees from the database")
return
}
//get general fees set for all, not to be confused with fees for General Account Type
generalFees, err := action.CoreQ().FeeEntries().ForAccountType(nil).Select()
if err != nil {
action.Err = &problem.ServerError
action.Log.WithError(err).Error("can't get general fees from the database")
return
}
sft := fees.NewSmartFeeTable(forAccount)
sft.Update(forAccountType)
sft.Update(generalFees)
sft.AddZeroFees(action.Assets)
action.Records = sft
}
func (action *AccountFeesAction) loadResponse() {
byAssets := action.Records.GetValuesByAsset()
action.Response.Fees = make(map[xdr.AssetCode][]regources.FeeEntry)
for _, feesForAsset := range byAssets {
for _, wrapper := range feesForAsset {
fee := resource.NewFeeEntryFromWrapper(wrapper)
ac := xdr.AssetCode(wrapper.Asset)
action.Response.Fees[ac] = append(action.Response.Fees[ac], fee)
}
}
}