Skip to content

Commit 3a5099f

Browse files
committed
refactor(btcrpc): simplify error handling in broadcast methods
1 parent 210f9c3 commit 3a5099f

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

internal/btcrpc/blockstream/blockstream.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ func (c *blockstream) BroadcastTx(txHex string) (string, error) {
5555
bodyStr := string(body)
5656

5757
// Regex to extract minimum fee from error message
58-
minFeeRegex := regexp.MustCompile(`min relay fee not met, (\d+) < (\d+)`)
58+
minFeeRegex := regexp.MustCompile(`sendrawtransaction RPC error -26: min relay fee not met, (\d+) < (\d+)`)
5959
matches := minFeeRegex.FindStringSubmatch(bodyStr)
6060

61+
c.logger.Error("[blockstream.BroadcastTx] broadcast error", map[string]string{
62+
"error": bodyStr,
63+
"matches": fmt.Sprintf("%v", matches),
64+
"match_len": strconv.Itoa(len(matches)),
65+
})
66+
6167
if len(matches) == 3 {
6268
minFee, _ := strconv.ParseInt(matches[2], 10, 64)
6369

internal/btcrpc/btcrpc.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package btcrpc
33
import (
44
"fmt"
55
"strconv"
6-
"strings"
76

87
"github.com/btcsuite/btcd/btcutil"
98
"github.com/btcsuite/btcd/chaincfg"
@@ -117,7 +116,7 @@ func (b *BtcRpc) broadcastWithFeeAdjustment(
117116

118117
// Check if the error is specifically about minimum relay fee
119118
broadcastErr, ok := err.(*blockstream.BroadcastTxError)
120-
if ok && strings.Contains(broadcastErr.Error(), "min relay fee not met") {
119+
if ok {
121120
b.logger.Info("[btcrpc.Send][FeeAdjustment]", map[string]string{
122121
"message": "Attempting to adjust transaction fee",
123122
})

internal/btcrpc/helper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (b *BtcRpc) broadcast(tx *wire.MsgTx) (string, error) {
198198

199199
txID, err := b.blockstream.BroadcastTx(txHex)
200200
if err != nil {
201-
return "", fmt.Errorf("failed to broadcast transaction: %v", err)
201+
return "", err
202202
}
203203

204204
return txID, nil

0 commit comments

Comments
 (0)