@@ -269,21 +269,20 @@ func (b *BtcRpc) getConfirmedUTXOs(address string) ([]blockstream.UTXO, error) {
269
269
// returns selected UTXOs and change amount
270
270
// change amount is the amount sent back to sender after sending total amount of selected UTXOs to recipient
271
271
// changeAmount = total amount of selected UTXOs - amountToSend - fee
272
- func (b * BtcRpc ) selectUTXOs (address string , amountToSend int64 ) (selected []blockstream.UTXO , changeAmount int64 , err error ) {
272
+ func (b * BtcRpc ) selectUTXOs (address string , amountToSend int64 ) (selected []blockstream.UTXO , changeAmount int64 , fee int64 , err error ) {
273
273
confirmedUTXOs , err := b .getConfirmedUTXOs (address )
274
274
if err != nil {
275
- return nil , 0 , err
275
+ return nil , 0 , 0 , err
276
276
}
277
277
278
278
// Get current fee rate from mempool
279
279
feeRates , err := b .blockstream .EstimateFees ()
280
280
if err != nil {
281
- return nil , 0 , err
281
+ return nil , 0 , 0 , err
282
282
}
283
283
284
284
// Iteratively select UTXOs until we have enough to cover amount + fee
285
285
var totalSelected int64
286
- var fee int64
287
286
288
287
for _ , utxo := range confirmedUTXOs {
289
288
selected = append (selected , utxo )
@@ -295,23 +294,23 @@ func (b *BtcRpc) selectUTXOs(address string, amountToSend int64) (selected []blo
295
294
// targetBlocks confirmations: widely accepted standard for bitcoin transactions
296
295
fee , err = b .calculateTxFee (feeRates , len (selected ), 2 , 6 )
297
296
if err != nil {
298
- return nil , 0 , err
297
+ return nil , 0 , 0 , err
299
298
}
300
299
301
300
if fee > amountToSend {
302
- return nil , 0 , fmt .Errorf ("fee exceeds amount to send: fee %d, amountToSend %d" , fee , amountToSend )
301
+ return nil , 0 , 0 , fmt .Errorf ("fee exceeds amount to send: fee %d, amountToSend %d" , fee , amountToSend )
303
302
}
304
303
305
304
satoshiRate , err := b .GetSatoshiUSDPrice ()
306
305
if err != nil {
307
- return nil , 0 , err
306
+ return nil , 0 , 0 , err
308
307
}
309
308
310
309
// calculate and round up to 1 decimal places
311
310
usdFee := math .Ceil (float64 (fee )/ satoshiRate * 10 ) / 10
312
311
313
312
if usdFee > b .appConfig .Bitcoin .MaxTxFeeUSD {
314
- return nil , 0 , fmt .Errorf ("fee exceeds maximum threshold: usdFee %0.1f, MaxTxFeeUSD %0.1f" , usdFee , b .appConfig .Bitcoin .MaxTxFeeUSD )
313
+ return nil , 0 , 0 , fmt .Errorf ("fee exceeds maximum threshold: usdFee %0.1f, MaxTxFeeUSD %0.1f" , usdFee , b .appConfig .Bitcoin .MaxTxFeeUSD )
315
314
}
316
315
317
316
// if we have enough to cover amount + current fee => return selected UTXOs and change amount
@@ -322,11 +321,11 @@ func (b *BtcRpc) selectUTXOs(address string, amountToSend int64) (selected []blo
322
321
"usdFee" : fmt .Sprintf ("%0.1f" , usdFee ),
323
322
})
324
323
changeAmount = totalSelected - amountToSend - fee
325
- return selected , changeAmount , nil
324
+ return selected , changeAmount , fee , nil
326
325
}
327
326
}
328
327
329
- return nil , 0 , fmt .Errorf (
328
+ return nil , 0 , 0 , fmt .Errorf (
330
329
"insufficient funds: have %d satoshis, need %d satoshis" ,
331
330
totalSelected ,
332
331
amountToSend + fee ,
0 commit comments