-
Notifications
You must be signed in to change notification settings - Fork 13
/
mempool.go
62 lines (49 loc) · 1.15 KB
/
mempool.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
package mempool
import (
"github.com/BoltzExchange/boltz-lnd/logger"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"math"
)
type lndFee interface {
EstimateFee(confTarget int32) (*walletrpc.EstimateFeeResponse, error)
}
type Mempool struct {
mc *client
lnd lndFee
}
func Init(lnd lndFee, mcEndpoint string) *Mempool {
var mc *client
if mcEndpoint != "" {
mc = initClient(mcEndpoint)
} else {
logger.Info("Disabled mempool.space integration")
}
return &Mempool{
mc: mc,
lnd: lnd,
}
}
func (m *Mempool) GetFeeEstimation() (int64, error) {
if m.mc != nil {
feeRecommendation, err := m.mc.getFeeRecommendation()
if err == nil {
return maxInt64(feeRecommendation.HalfHourFee, 2), nil
}
logger.Warning("Mempool fee estimation failed: " + err.Error())
logger.Info("Falling back to LND fee estimation")
}
return m.getLndFeeEstimation()
}
func (m *Mempool) getLndFeeEstimation() (int64, error) {
feeResponse, err := m.lnd.EstimateFee(2)
if err != nil {
return 0, err
}
return maxInt64(int64(math.Round(float64(feeResponse.SatPerKw)/1000)), 2), nil
}
func maxInt64(a int64, b int64) int64 {
if a > b {
return a
}
return b
}