delphi.market/lnd/withdrawal.go

41 lines
797 B
Go
Raw Normal View History

2023-12-03 05:21:57 +00:00
package lnd
import (
"context"
"database/sql"
"log"
"time"
"github.com/btcsuite/btcd/btcutil"
)
func (lnd *LNDClient) PayInvoice(tx *sql.Tx, bolt11 string) error {
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
2023-12-03 05:21:57 +00:00
log.Printf("attempting to pay bolt11 %s ...\n", bolt11)
maxFeeSats := btcutil.Amount(10)
2023-12-03 05:21:57 +00:00
payChan := lnd.Client.PayInvoice(ctx, bolt11, maxFeeSats, nil)
2023-12-03 05:21:57 +00:00
res := <-payChan
2023-12-03 05:21:57 +00:00
if res.Err != nil {
log.Printf("error paying bolt11: %s -- %s\n", bolt11, res.Err)
tx.Rollback()
return res.Err
}
2023-12-03 05:21:57 +00:00
log.Printf("successfully paid bolt11: %s\n", bolt11)
if _, err := tx.ExecContext(ctx,
"UPDATE withdrawals SET paid_at = CURRENT_TIMESTAMP WHERE bolt11 = $1",
bolt11,
); err != nil {
2023-12-03 05:21:57 +00:00
tx.Rollback()
return err
}
2023-12-03 05:21:57 +00:00
return res.Err
}