bit4sat/ln/api.go

128 lines
2.6 KiB
Go
Raw Permalink Normal View History

2019-03-26 21:01:54 +00:00
package ln
import (
"encoding/json"
"fmt"
"log"
"net/http"
2019-04-03 22:12:03 +00:00
"strings"
2019-03-26 21:01:54 +00:00
"git.sp4ke.com/sp4ke/bit4sat/btc"
2019-04-03 22:12:03 +00:00
"git.sp4ke.com/sp4ke/bit4sat/bus"
"git.sp4ke.com/sp4ke/bit4sat/db"
"git.sp4ke.com/sp4ke/bit4sat/lndrpc"
"git.sp4ke.com/sp4ke/bit4sat/utils"
2019-04-03 22:12:03 +00:00
"github.com/mediocregopher/radix/v3"
2019-03-26 21:01:54 +00:00
)
var (
2019-03-30 18:42:41 +00:00
Client *http.Client
2019-03-26 21:01:54 +00:00
)
2019-04-03 22:12:03 +00:00
// Quick check if invoice was paid
2019-04-02 17:40:25 +00:00
func CheckInvoice(id string) (*Invoice, error) {
2019-04-03 22:12:03 +00:00
lnInvoice, err := lndrpc.LookupInvoiceRhashStr(id)
2019-04-02 17:40:25 +00:00
if err != nil {
return nil, err
}
2019-04-03 22:12:03 +00:00
invoice := InvoiceFromLndIn(lnInvoice)
2019-04-02 17:40:25 +00:00
2019-04-03 22:12:03 +00:00
return invoice, nil
2019-04-02 17:40:25 +00:00
}
2019-04-03 22:12:03 +00:00
// This will watch on the pubsub for paid invoices given an invoice id
func PollPaidInvoice(invoiceId string, invoicePaidChan chan<- *Invoice, errorChan chan<- error) {
2019-04-05 18:14:23 +00:00
defer func() {
log.Printf("quitting poller %s", invoiceId)
}()
2019-04-03 22:12:03 +00:00
watchPaidChannel := make(chan radix.PubSubMessage)
2019-04-05 18:14:23 +00:00
busName := fmt.Sprintf("%s:%s", bus.InvoicePaidChannelPrefix, invoiceId)
2019-04-03 22:12:03 +00:00
2019-04-05 18:14:23 +00:00
err := db.DB.RedisPubSub.Subscribe(watchPaidChannel, busName)
2019-04-03 22:12:03 +00:00
if err != nil {
2019-04-05 18:14:23 +00:00
log.Printf("error in poll invoice: %s", err)
2019-04-03 22:12:03 +00:00
errorChan <- err
return
}
2019-04-05 18:14:23 +00:00
log.Printf("poller: %s, listening to watchPaidChannel", invoiceId)
for msg := range watchPaidChannel {
log.Printf("poller received message on %s", msg.Channel)
2019-04-03 22:12:03 +00:00
2019-04-04 00:27:09 +00:00
split := strings.Split(msg.Channel, ":")
if len(split) != 2 {
errorChan <- fmt.Errorf("error in pubsub channel parsing %s", msg.Channel)
}
targetInvoiceId := strings.Split(msg.Channel, ":")[1]
log.Printf("target invoice is %s", targetInvoiceId)
2019-04-03 22:12:03 +00:00
if targetInvoiceId == invoiceId {
invoice := Invoice{}
err := json.Unmarshal(msg.Message, &invoice)
if err != nil {
errorChan <- err
return
}
invoicePaidChan <- &invoice
2019-04-05 18:14:23 +00:00
close(invoicePaidChan)
2019-04-03 22:12:03 +00:00
return
}
}
}
type InvoiceOpts struct {
Amount float64
Curr Currency
Memo string
2019-03-29 18:50:11 +00:00
}
func NewInvoice(opts InvoiceOpts) (*Invoice, error) {
var err error
var satValue int64
if opts.Curr == CurMSat {
log.Println("cur is msat")
// Convert to MSAT, meaning values under 1000 MSAT are ignored
satValue = int64(opts.Amount / 1000)
// Other supported currecies
}
if opts.Curr != CurSat {
log.Println("cur is custom")
// Get Sat satValue for this invoice
msatVal, err := btc.FiatToMsat(CurrencyString[opts.Curr], opts.Amount)
if err != nil {
return nil, err
}
satValue = msatVal / 1000
} else {
satValue = int64(opts.Amount)
}
lndInvoice, err := lndrpc.AddInvoiceSat(opts.Memo, satValue)
if err != nil {
return nil, err
}
invoice := InvoiceFromLndIn(lndInvoice)
invoice.QuotedCurrency = CurrencyString[opts.Curr]
invoice.QuotedAmount = opts.Amount
return invoice, nil
}
2019-03-30 18:42:41 +00:00
func init() {
Client = utils.NewHttpClient()
2019-03-30 18:42:41 +00:00
}