|
9 | 9 |
|
10 | 10 | "github.com/checksum0/go-electrum/electrum" |
11 | 11 | "github.com/ipfs/go-log" |
| 12 | + "go.uber.org/zap" |
12 | 13 | "golang.org/x/exp/slices" |
13 | 14 |
|
14 | 15 | "github.com/keep-network/keep-common/pkg/wrappers" |
@@ -129,11 +130,38 @@ func (c *Connection) GetTransactionConfirmations( |
129 | 130 | panic("not implemented") |
130 | 131 | } |
131 | 132 |
|
| 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. |
132 | 138 | func (c *Connection) BroadcastTransaction( |
133 | 139 | transaction *bitcoin.Transaction, |
134 | 140 | ) 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 |
137 | 165 | } |
138 | 166 |
|
139 | 167 | // GetLatestBlockHeight gets the height of the latest block (tip). If the |
|
0 commit comments