-
Notifications
You must be signed in to change notification settings - Fork 1
/
estimate_fee.go
114 lines (101 loc) · 2.88 KB
/
estimate_fee.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
package main
import (
"context"
"flag"
"fmt"
cfd "github.com/cryptogarageinc/cfd-go"
)
// EstimateFeeCmd fee estimation.
type EstimateFeeCmd struct {
cmd string
flagSet *flag.FlagSet
txFilePath *string
tx *string
isElements *bool
feeRate *float64
asset *string
exponent *int64
minimumBits *int64
}
// NewEstimateFeeCmd returns a new EstimateFeeCmd struct.
func NewEstimateFeeCmd() *EstimateFeeCmd {
return &EstimateFeeCmd{}
}
// Command returns the command name.
func (cmd *EstimateFeeCmd) Command() string {
return cmd.cmd
}
// Parse parses the command arguments.
func (cmd *EstimateFeeCmd) Parse(args []string) {
cmd.flagSet.Parse(args)
}
// Init initializes the command.
func (cmd *EstimateFeeCmd) Init() {
cmd.cmd = "estimatefee"
cmd.flagSet = flag.NewFlagSet(cmd.cmd, flag.ExitOnError)
cmd.txFilePath = cmd.flagSet.String("file", "", "transaction data file path")
cmd.tx = cmd.flagSet.String("tx", "", "transaction in hex format")
cmd.isElements = cmd.flagSet.Bool("elements", false, "elements mode")
cmd.feeRate = cmd.flagSet.Float64("feerate", 20.0, "fee rate. (default: 20.0)")
cmd.asset = cmd.flagSet.String("asset", "", "fee asset")
cmd.exponent = cmd.flagSet.Int64("exponent", 0, "blind exponent")
cmd.minimumBits = cmd.flagSet.Int64("minimumbits", 52, "blind minimum bits")
}
// GetFlagSet returns the flag set for this command.
func (cmd *EstimateFeeCmd) GetFlagSet() *flag.FlagSet {
return cmd.flagSet
}
// Do performs the command action.
func (cmd *EstimateFeeCmd) Do(ctx context.Context) {
var err error
data := NewTransactionCacheData()
tx := *cmd.tx
if *cmd.tx == "" && *cmd.txFilePath != "" {
data, err = ReadTransactionCache(*cmd.txFilePath)
if err != nil {
fmt.Println(err)
return
}
tx = data.Hex
}
if tx == "" {
fmt.Println("tx is required")
return
}
option := cfd.NewCfdEstimateFeeOption()
option.EffectiveFeeRate = *cmd.feeRate
option.UseElements = *cmd.isElements
if *cmd.isElements {
option.FeeAsset = *cmd.asset
option.Exponent = *cmd.exponent
option.MinimumBits = *cmd.minimumBits
}
txinList := []cfd.CfdEstimateFeeInput{}
for _, utxo := range data.Utxos {
feeInput := cfd.CfdEstimateFeeInput{
Utxo: cfd.CfdUtxo{
Txid: utxo.Txid,
Vout: utxo.Vout,
Amount: utxo.Amount,
Asset: utxo.Asset,
Descriptor: utxo.Descriptor,
IsIssuance: false,
IsBlindIssuance: false,
IsPegin: false,
PeginBtcTxSize: 0,
ScriptSigTemplate: utxo.ScriptsigTemplate,
},
IsIssuance: false,
IsBlindIssuance: false,
IsPegin: false,
PeginBtcTxSize: 0,
}
txinList = append(txinList, feeInput)
}
total, txFee, inputFee, err := cfd.CfdGoEstimateFee(tx, txinList, option)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("fee = %d (tx: %d, input: %d)\n", total, txFee, inputFee)
}