-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_fees_show.go
135 lines (115 loc) · 3.15 KB
/
actions_fees_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
package horizon
import (
"gitlab.com/distributed_lab/logan/v3"
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/tokend/go/amount"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/exchange"
"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:
//
// FeesShowAction: renders fees for operationType
type FeesShowAction struct {
Action
converter *exchange.Converter
FeeType int
Asset string
Subtype int64
AccountID string
Account *core.Account
Amount string
Fee regources.FeeEntry
}
// JSON is a method for actions.JSON
func (action *FeesShowAction) JSON() {
action.Do(
action.loadParams,
action.createConverter,
action.loadData,
func() {
hal.Render(action.W, action.Fee)
},
)
}
func (action *FeesShowAction) loadParams() {
action.FeeType = int(action.GetInt32("fee_type"))
action.Asset = action.GetNonEmptyString("asset")
action.Subtype = action.GetInt64("subtype")
action.AccountID = action.GetString("account")
action.Amount = action.GetString("amount")
}
func (action *FeesShowAction) 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 *FeesShowAction) loadData() {
var err error
if action.AccountID != "" {
action.Account, err = action.CoreQ().Accounts().ByAddress(action.AccountID)
}
if err != nil {
action.Log.WithError(err).Error("Failed to load account to get fee")
action.Err = &problem.ServerError
return
}
am := int64(0)
if action.Amount != "" {
amXdr, _ := amount.Parse(action.Amount)
am = int64(amXdr)
}
result, err := action.CoreQ().FeeByTypeAssetAccount(action.FeeType, action.Asset, action.Subtype, action.Account, am)
if err != nil {
action.Log.WithError(err).Error("Failed to load fee by asset and type")
action.Err = &problem.ServerError
return
}
if result == nil {
result = new(core.FeeEntry)
result.Asset = action.Asset
result.FeeType = action.FeeType
}
percentFee := action.GetPercentFee(result.Percent)
if action.Err != nil {
return
}
result.Percent = percentFee
action.Fee = resource.NewFeeEntry(*result)
}
func (action *FeesShowAction) GetPercentFee(percentFee int64) int64 {
// request does not require to calculate
if action.Amount == "" {
return percentFee
}
am, err := amount.Parse(action.Amount)
if err != nil {
action.SetInvalidField("amount", errors.Wrap(err, "failed to parse"))
return 0
}
asset, err := action.CoreQ().Assets().ByCode(action.Asset)
if err != nil {
action.Log.WithError(err).Error("failed to load asset from core db")
action.Err = &problem.ServerError
return 0
}
if asset == nil {
action.SetInvalidField("asset", errors.From(errors.New("does not exist"), logan.F{
"asset": action.Asset,
}))
return 0
}
res, isOverflow := action.CalculatePercentFee(percentFee, am, asset.GetMinimumAmount())
if isOverflow {
action.SetInvalidField("amount", errors.New("is too big - overflow"))
return 0
}
return res
}