-
Notifications
You must be signed in to change notification settings - Fork 0
/
fees.go
105 lines (88 loc) · 2.22 KB
/
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
package util
import (
"net/http"
"github.com/OpenBazaar/wallet-interface"
)
type httpClient interface {
Get(string) (*http.Response, error)
}
// Fees describe
type Fees struct {
FastestFee uint64
HalfHourFee uint64
HourFee uint64
}
type FeeProvider struct {
maxFee uint64
priorityFee uint64
normalFee uint64
economicFee uint64
superEconomicFee uint64
exchangeRates wallet.ExchangeRates
}
// We will target a fee per byte such that it would equal
// 0.1 USD cent for economic, 1 USD cents for normal and
// 5 USD cents for priority for a median (226 byte) transaction.
type FeeTargetInUSDCents float64
const (
EconomicTarget FeeTargetInUSDCents = 0.1
NormalTarget FeeTargetInUSDCents = 1
PriorityTarget FeeTargetInUSDCents = 5
AverageTransactionSize = 226
)
func NewFeeProvider(maxFee, priorityFee, normalFee, economicFee, superEconomicFee uint64, exchangeRates wallet.ExchangeRates) *FeeProvider {
return &FeeProvider{
maxFee: maxFee,
priorityFee: priorityFee,
normalFee: normalFee,
economicFee: economicFee,
superEconomicFee: superEconomicFee,
exchangeRates: exchangeRates,
}
}
func (fp *FeeProvider) GetFeePerByte(feeLevel wallet.FeeLevel) uint64 {
defaultFee := func() uint64 {
switch feeLevel {
case wallet.PRIOIRTY:
return fp.priorityFee
case wallet.NORMAL:
return fp.normalFee
case wallet.ECONOMIC:
return fp.economicFee
case wallet.FEE_BUMP:
return fp.priorityFee * 2
default:
return fp.normalFee
}
}
if fp.exchangeRates == nil {
return defaultFee()
}
rate, err := fp.exchangeRates.GetLatestRate("USD")
if err != nil || rate == 0 {
return defaultFee()
}
var target FeeTargetInUSDCents
switch feeLevel {
case wallet.PRIOIRTY:
target = PriorityTarget
case wallet.NORMAL:
target = NormalTarget
case wallet.ECONOMIC:
target = EconomicTarget
case wallet.SUPER_ECONOMIC:
return fp.superEconomicFee
case wallet.FEE_BUMP:
target = PriorityTarget * 2
default:
target = NormalTarget
}
feePerByte := (((float64(target) / 100) / rate) * 100000000) / AverageTransactionSize
if uint64(feePerByte) > fp.maxFee {
return fp.maxFee
}
if uint64(feePerByte) == 0 {
return 1
}
return uint64(feePerByte)
}