-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfees.go
103 lines (87 loc) · 2.09 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
package bitcoincash
import (
"github.com/OpenBazaar/wallet-interface"
"net/http"
"time"
)
type httpClient interface {
Get(string) (*http.Response, error)
}
type feeCache struct {
fees *Fees
lastUpdated time.Time
}
type Fees struct {
FastestFee uint64
HalfHourFee uint64
HourFee uint64
}
type FeeProvider struct {
maxFee uint64
priorityFee uint64
normalFee uint64
economicFee uint64
exchangeRates wallet.ExchangeRates
cache *feeCache
}
// We will target a fee per byte such that it would equal
// 1 USD cent for economic, 5 USD cents for normal and
// 10 USD cents for priority for a median (226 byte) transaction.
type FeeTarget int
const (
EconomicTarget FeeTarget = 1
NormalTarget FeeTarget = 5
PriorityTarget FeeTarget = 10
)
func NewFeeProvider(maxFee, priorityFee, normalFee, economicFee uint64, exchangeRates wallet.ExchangeRates) *FeeProvider {
return &FeeProvider{
maxFee: maxFee,
priorityFee: priorityFee,
normalFee: normalFee,
economicFee: economicFee,
exchangeRates: exchangeRates,
cache: new(feeCache),
}
}
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 {
log.Errorf("Error using exchange rate to calculate fee: %s\n", err.Error())
return defaultFee()
}
var target FeeTarget
switch feeLevel {
case wallet.PRIOIRTY:
target = PriorityTarget
case wallet.NORMAL:
target = NormalTarget
case wallet.ECONOMIC:
target = EconomicTarget
case wallet.FEE_BUMP:
target = PriorityTarget * 2
default:
target = NormalTarget
}
feePerByte := (((float64(target) / 100) / rate) * 100000000) / 226
if uint64(feePerByte) > fp.maxFee {
return fp.maxFee
}
return uint64(feePerByte)
}