-
Notifications
You must be signed in to change notification settings - Fork 10
/
removefunds.go
89 lines (73 loc) · 2.63 KB
/
removefunds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"encoding/hex"
"errors"
"fmt"
"log"
"os"
"github.com/lightningnetwork/lnd/lnrpc"
"golang.org/x/sync/singleflight"
"google.golang.org/grpc/metadata"
)
const (
removeFundTimeout = 3600
)
var payReqGroup singleflight.Group
func createRemoveFundPaymentRequest(amount int64, address string) (string, error) {
clientCtx := metadata.AppendToOutgoingContext(context.Background(), "macaroon", os.Getenv("LND_MACAROON_HEX"))
addInoiceResp, err := client.AddInvoice(clientCtx, &lnrpc.Invoice{Value: amount, Memo: "Bitcoin Transfer", Expiry: removeFundTimeout})
if err != nil {
log.Printf("createPaymentRequest: failed to add invoice %v", err)
return "", err
}
payReqHash := addInoiceResp.RHash
err = updateKeyFields(hex.EncodeToString(payReqHash[:]), map[string]string{"address": address})
return addInoiceResp.PaymentRequest, err
}
func ensureOnChainPaymentSent(payReqHash string) (string, error) {
txID, err, _ := payReqGroup.Do(payReqHash, func() (interface{}, error) {
return sendCoinsForReceivedPayment(payReqHash)
})
return txID.(string), err
}
func sendCoinsForReceivedPayment(payReqHash string) (string, error) {
redisConn := redisPool.Get()
defer redisConn.Close()
removeFundRequest, err := getKeyFields(payReqHash)
if err != nil {
log.Printf("error querying payment request hash, %v", err)
return "", err
}
address := removeFundRequest["address"]
txID := removeFundRequest["txid"]
//no fund request associated with invoice, continue
if address == "" {
return "", fmt.Errorf("no address associated with hash: %v", payReqHash)
}
//if we already payed
if txID != "" {
return txID, nil
}
log.Printf("paying on chain to destination address: %v", address)
//1. fetch the invoice and check settled amount
clientCtx := metadata.AppendToOutgoingContext(context.Background(), "macaroon", os.Getenv("LND_MACAROON_HEX"))
invoice, err := client.LookupInvoice(clientCtx, &lnrpc.PaymentHash{RHashStr: payReqHash})
if err != nil {
return "", err
}
if !invoice.Settled {
return "", errors.New("fail to pay, haven't received any payment")
}
//2. send coins to the user
response, err := client.SendCoins(clientCtx, &lnrpc.SendCoinsRequest{Addr: address, Amount: invoice.AmtPaidSat, TargetConf: 48})
if err != nil {
return "", fmt.Errorf("fail to send coins: %v", err)
}
log.Printf("successfully sent coins to address %v", address)
//3. save txId in fund request
if err := updateKeyFields(payReqHash, map[string]string{"txid": response.Txid}); err != nil {
log.Printf("Fail to save tx id %v associated with invoice %v", response.Txid, invoice.PaymentRequest)
}
return response.Txid, nil
}