-
Notifications
You must be signed in to change notification settings - Fork 32
/
util.go
84 lines (73 loc) · 2.29 KB
/
util.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
package bus
import (
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/rpcclient"
"github.com/ledgerhq/satstack/utils"
log "github.com/sirupsen/logrus"
)
const fallbackFee = btcutil.Amount(1)
func (b *Bus) EstimateSmartFee(target int64, mode string) btcutil.Amount {
fee, err := b.mainClient.EstimateSmartFee(target, getMode(mode))
// If failed to get smart fee estimate, fallback to fallbackFee.
// Example: if the full-node is a regtest chain, there are normally
// no transactions in the mempool to analyze for estimating fees.
//
// TODO: Use Minimum Relay Fee instead of btcutil.Amount(1)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"target": target,
"mode": mode,
}).Error("Failed estimatesmartfee Bridge")
return fallbackFee
}
if len(fee.Errors) > 0 {
log.WithFields(log.Fields{
"error": fee.Errors,
"target": target,
"mode": mode,
}).Error("Failed estimatesmartfee Bridge")
return fallbackFee
}
return utils.ParseSatoshi(*fee.FeeRate)
}
func DeriveAddress(client *rpcclient.Client, descriptor string, index int) (*string, error) {
addresses, err := client.DeriveAddresses(
descriptor,
// Since we're interested in only the address at addressIndex,
// specifying the range as [begin, end] ensures that addresses
// between index 0 and end-1 are not derived uselessly.
&btcjson.DescriptorRange{Value: []int{index, index}},
)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"descriptor": descriptor,
"index": index,
}).Error("Failed to derive address")
return nil, err
}
return &(*addresses)[0], nil // *addresses is always a single-element slice
}
// GetCanonicalDescriptor returns the descriptor in canonical form, along with
// its computed checksum.
func GetCanonicalDescriptor(client *rpcclient.Client, descriptor string) (*string, error) {
info, err := client.GetDescriptorInfo(descriptor)
if err != nil {
return nil, err
}
return &info.Descriptor, nil
}
func getMode(s string) *btcjson.EstimateSmartFeeMode {
switch s {
case "UNSET":
return &btcjson.EstimateModeUnset
case "ECONOMICAL":
return &btcjson.EstimateModeEconomical
case "CONSERVATIVE":
return &btcjson.EstimateModeConservative
default:
return &btcjson.EstimateModeEconomical
}
}