|
| 1 | +// Copyright (C) 2025 Creditor Corp. Group. |
| 2 | +// See LICENSE for copying information. |
| 3 | + |
| 4 | +package txbuilder |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "math/big" |
| 9 | + |
| 10 | + "github.com/btcsuite/btcd/btcutil" |
| 11 | + "github.com/btcsuite/btcd/chaincfg" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + // headerSizeVBytes defined rough tx header size in vBytes. |
| 16 | + headerSizeVBytes = big.NewInt(11) |
| 17 | +) |
| 18 | + |
| 19 | +// PaymentDataFees holds additional info to build more accurate fee estimation. |
| 20 | +type PaymentDataFees struct { |
| 21 | + InputSizeVBytes *big.Int // defines input size in virtual bytes. |
| 22 | + WitnessSizeVBytes *big.Int // defines witness size in virtual bytes. |
| 23 | + OutputSizeVBytes *big.Int // defines output size in virtual bytes. |
| 24 | +} |
| 25 | + |
| 26 | +// NewDefaultPaymentDataFees builds default estimation data for provided address. |
| 27 | +// NOTE: Default estimation data assumes the next type to script mapping: |
| 28 | +// |
| 29 | +// P2PKH - single signature |
| 30 | +// P2SH - 2-of-3 multisig |
| 31 | +// P2WPKH - single signature |
| 32 | +// P2WSH - 2-of-3 multisig |
| 33 | +// P2TR - key-spend path (single signature) |
| 34 | +// |
| 35 | +// INFO: Sizes were taken from this calculator: https://bitcoinops.org/en/tools/calc-size/. |
| 36 | +func NewDefaultPaymentDataFees(address btcutil.Address) (*PaymentDataFees, error) { |
| 37 | + var inputSizeVByte, witnessSizeVBytes, outputSizeVBytes int64 |
| 38 | + switch address.(type) { |
| 39 | + case *btcutil.AddressPubKey: |
| 40 | + return nil, errors.New("unsupported address type: too old (P2PK)") |
| 41 | + case *btcutil.AddressPubKeyHash: |
| 42 | + inputSizeVByte, witnessSizeVBytes, outputSizeVBytes = 41, 107, 34 |
| 43 | + case *btcutil.AddressScriptHash: |
| 44 | + inputSizeVByte, witnessSizeVBytes, outputSizeVBytes = 43, 254, 32 |
| 45 | + case *btcutil.AddressWitnessPubKeyHash: |
| 46 | + inputSizeVByte, witnessSizeVBytes, outputSizeVBytes = 41, 27, 31 |
| 47 | + case *btcutil.AddressWitnessScriptHash: |
| 48 | + inputSizeVByte, witnessSizeVBytes, outputSizeVBytes = 41, 64, 43 |
| 49 | + case *btcutil.AddressTaproot: |
| 50 | + inputSizeVByte, witnessSizeVBytes, outputSizeVBytes = 41, 17, 43 |
| 51 | + default: |
| 52 | + return nil, errors.New("unsupported address type") |
| 53 | + } |
| 54 | + |
| 55 | + return &PaymentDataFees{ |
| 56 | + InputSizeVBytes: big.NewInt(inputSizeVByte), |
| 57 | + WitnessSizeVBytes: big.NewInt(witnessSizeVBytes), |
| 58 | + OutputSizeVBytes: big.NewInt(outputSizeVBytes), |
| 59 | + }, nil |
| 60 | +} |
| 61 | + |
| 62 | +// Filled returns true is struct was properly initialized and all fields are filled. |
| 63 | +func (pdf *PaymentDataFees) Filled() bool { |
| 64 | + return pdf != nil && pdf.InputSizeVBytes != nil && pdf.WitnessSizeVBytes != nil && pdf.OutputSizeVBytes != nil |
| 65 | +} |
| 66 | + |
| 67 | +// RoughTxSize holds data to make rough estimation of tx in vBytes. |
| 68 | +type RoughTxSize struct { |
| 69 | + IncludeHeader bool // to include or not tx header vBytes size into a count. |
| 70 | + Elements []*EstimationElement // estimation elements. |
| 71 | + ExtraExpenses *big.Int // defines extra expenses (in vBytes) from previous estimation or for another logic needs. optional. |
| 72 | +} |
| 73 | + |
| 74 | +// EstimationElement holds data needed to estimate fee part for PaymentData group. |
| 75 | +type EstimationElement struct { |
| 76 | + *PaymentDataFees |
| 77 | + InputsNumber int // number of inputs of PaymentData address. |
| 78 | + OutputsNumber int // number of outputs of PaymentData address. |
| 79 | +} |
| 80 | + |
| 81 | +// Estimate returns rough estimation of tx in vBytes. |
| 82 | +func (params *RoughTxSize) Estimate() *big.Int { |
| 83 | + size := big.NewInt(0) |
| 84 | + if params.IncludeHeader { |
| 85 | + size.Add(size, headerSizeVBytes) |
| 86 | + } |
| 87 | + |
| 88 | + for _, element := range params.Elements { |
| 89 | + signedInputSize := new(big.Int).Add(element.InputSizeVBytes, element.WitnessSizeVBytes) |
| 90 | + inputsFee := new(big.Int).Mul(signedInputSize, big.NewInt(int64(element.InputsNumber))) |
| 91 | + size.Add(size, inputsFee) |
| 92 | + |
| 93 | + outputsFee := new(big.Int).Mul(element.OutputSizeVBytes, big.NewInt(int64(element.OutputsNumber))) |
| 94 | + size.Add(size, outputsFee) |
| 95 | + } |
| 96 | + |
| 97 | + if params.ExtraExpenses != nil { |
| 98 | + size.Add(size, params.ExtraExpenses) |
| 99 | + } |
| 100 | + |
| 101 | + return size |
| 102 | +} |
| 103 | + |
| 104 | +// NewEstimationElementFromStringAddress is a constructor for EstimationElement that additionally parses address from string. |
| 105 | +func NewEstimationElementFromStringAddress(address string, inputs, outputs int, networkParams *chaincfg.Params) (*EstimationElement, error) { |
| 106 | + addr, err := btcutil.DecodeAddress(address, networkParams) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + addressDataFees, err := NewDefaultPaymentDataFees(addr) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + if inputs < 0 || outputs < 0 { |
| 117 | + return nil, errors.New("inputs and outputs must be >= 0") |
| 118 | + } |
| 119 | + |
| 120 | + return &EstimationElement{ |
| 121 | + PaymentDataFees: addressDataFees, |
| 122 | + InputsNumber: inputs, |
| 123 | + OutputsNumber: outputs, |
| 124 | + }, nil |
| 125 | +} |
| 126 | + |
| 127 | +// CalculateTxFee calculates tx network fee from tx size in vBytes and sat/kVb price. |
| 128 | +func CalculateTxFee(txSizeVBytes, satoshiPerKVByte *big.Int) (fee *big.Int) { |
| 129 | + fee = new(big.Int).Mul(txSizeVBytes, satoshiPerKVByte) |
| 130 | + fee.Quo(fee, big.NewInt(1000)) |
| 131 | + |
| 132 | + return fee |
| 133 | +} |
0 commit comments