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
|
|
|
|
2019-04-03 18:18:51 +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"
|
2019-04-03 18:18:51 +00:00
|
|
|
"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]
|
2019-04-04 18:26:43 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 18:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 01:45:50 +00:00
|
|
|
type InvoiceOpts struct {
|
|
|
|
Amount float64
|
|
|
|
Curr Currency
|
|
|
|
Memo string
|
2019-03-29 18:50:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 01:45:50 +00:00
|
|
|
func NewInvoice(opts InvoiceOpts) (*Invoice, error) {
|
2019-04-03 18:18:51 +00:00
|
|
|
var err error
|
|
|
|
var satValue int64
|
|
|
|
|
2019-04-06 01:45:50 +00:00
|
|
|
if opts.Curr == CurMSat {
|
2019-04-03 18:18:51 +00:00
|
|
|
log.Println("cur is msat")
|
|
|
|
// Convert to MSAT, meaning values under 1000 MSAT are ignored
|
2019-04-06 01:45:50 +00:00
|
|
|
satValue = int64(opts.Amount / 1000)
|
2019-04-03 18:18:51 +00:00
|
|
|
|
|
|
|
// Other supported currecies
|
|
|
|
}
|
|
|
|
|
2019-04-06 01:45:50 +00:00
|
|
|
if opts.Curr != CurSat {
|
2019-04-03 18:18:51 +00:00
|
|
|
log.Println("cur is custom")
|
|
|
|
|
|
|
|
// Get Sat satValue for this invoice
|
2019-04-06 01:45:50 +00:00
|
|
|
msatVal, err := btc.FiatToMsat(CurrencyString[opts.Curr], opts.Amount)
|
2019-04-03 18:18:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
satValue = msatVal / 1000
|
|
|
|
} else {
|
2019-04-06 01:45:50 +00:00
|
|
|
satValue = int64(opts.Amount)
|
2019-04-03 18:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 01:45:50 +00:00
|
|
|
lndInvoice, err := lndrpc.AddInvoiceSat(opts.Memo, satValue)
|
2019-04-03 18:18:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice := InvoiceFromLndIn(lndInvoice)
|
2019-04-06 01:45:50 +00:00
|
|
|
invoice.QuotedCurrency = CurrencyString[opts.Curr]
|
|
|
|
invoice.QuotedAmount = opts.Amount
|
2019-04-03 18:18:51 +00:00
|
|
|
|
|
|
|
return invoice, nil
|
|
|
|
}
|
|
|
|
|
2019-03-30 18:42:41 +00:00
|
|
|
func init() {
|
2019-04-03 18:18:51 +00:00
|
|
|
Client = utils.NewHttpClient()
|
2019-03-30 18:42:41 +00:00
|
|
|
}
|