Skip to content

Commit 8b5b5b3

Browse files
committed
Implement BroadcastTransaction function
The function submits a serialized transaction to the chain.
1 parent a3fe8af commit 8b5b5b3

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

pkg/bitcoin/electrum/electrum.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/checksum0/go-electrum/electrum"
1111
"github.com/ipfs/go-log"
12+
"go.uber.org/zap"
1213
"golang.org/x/exp/slices"
1314

1415
"github.com/keep-network/keep-common/pkg/wrappers"
@@ -129,11 +130,38 @@ func (c *Connection) GetTransactionConfirmations(
129130
panic("not implemented")
130131
}
131132

133+
// BroadcastTransaction broadcasts the given transaction over the
134+
// network of the Bitcoin chain nodes. If the broadcast action could not be
135+
// done, this function returns an error. This function does not give any
136+
// guarantees regarding transaction mining. The transaction may be mined or
137+
// rejected eventually.
132138
func (c *Connection) BroadcastTransaction(
133139
transaction *bitcoin.Transaction,
134140
) error {
135-
// TODO: Implementation.
136-
panic("not implemented")
141+
rawTx := transaction.Serialize()
142+
143+
rawTxLogger := logger.With(
144+
zap.String("rawTx", hex.EncodeToString(rawTx)),
145+
)
146+
rawTxLogger.Debugf("broadcasting transaction")
147+
148+
var response string
149+
err := wrappers.DoWithDefaultRetry(c.requestRetryTimeout, func(ctx context.Context) error {
150+
var err error
151+
response, err = c.client.BroadcastTransaction(c.ctx, string(rawTx))
152+
if err != nil {
153+
return fmt.Errorf("BroadcastTransaction failed: [%w]", err)
154+
}
155+
156+
rawTxLogger.Infof("transaction broadcast successful: [%s]", response)
157+
158+
return nil
159+
})
160+
if err != nil {
161+
return fmt.Errorf("failed to broadcast the transaction: [%w]", err)
162+
}
163+
164+
return nil
137165
}
138166

139167
// GetLatestBlockHeight gets the height of the latest block (tip). If the

0 commit comments

Comments
 (0)