LN invoice expiry param & use it for ctx timeout

This commit is contained in:
xx979xx 2024-06-03 13:33:30 +03:00
parent 5564bd95c3
commit b3ac9e0a6c
4 changed files with 16 additions and 2 deletions

View file

@ -32,3 +32,4 @@ sed -i "s/[(]'FEE_LIMIT_PERCENT'[^)]*[)]/(\'FEE_LIMIT_PERCENT\', \'0.5\')/" sql/
sed -i "s/[(]'FUNCTION_LNURLW'[^)]*[)]/(\'FUNCTION_LNURLW\', \'ENABLE\')/" sql/settings.sql sed -i "s/[(]'FUNCTION_LNURLW'[^)]*[)]/(\'FUNCTION_LNURLW\', \'ENABLE\')/" sql/settings.sql
sed -i "s/[(]'FUNCTION_LNURLP'[^)]*[)]/(\'FUNCTION_LNURLP\', \'DISABLE\')/" sql/settings.sql sed -i "s/[(]'FUNCTION_LNURLP'[^)]*[)]/(\'FUNCTION_LNURLP\', \'DISABLE\')/" sql/settings.sql
sed -i "s/[(]'FUNCTION_EMAIL'[^)]*[)]/(\'FUNCTION_EMAIL\', \'DISABLE\')/" sql/settings.sql sed -i "s/[(]'FUNCTION_EMAIL'[^)]*[)]/(\'FUNCTION_EMAIL\', \'DISABLE\')/" sql/settings.sql
sed -i "s/[(]'LN_INVOICE_EXPIRY_SEC'[^)]*[)]/(\'LN_INVOICE_EXPIRY_SEC\', \'3600\')/" sql/settings.sql

View file

@ -34,3 +34,4 @@ Here are the descriptions of values available to use in the `settings` table:
| FUNCTION_INTERNAL_API | DISABLE | system level switch for activating the internal API | | FUNCTION_INTERNAL_API | DISABLE | system level switch for activating the internal API |
| SENDGRID_API_KEY | | User API Key from SendGrid.com | | SENDGRID_API_KEY | | User API Key from SendGrid.com |
| SENDGRID_EMAIL_SENDER | | Single Sender email address verified by SendGrid | | SENDGRID_EMAIL_SENDER | | Single Sender email address verified by SendGrid |
| LN_INVOICE_EXPIRY_SEC | 3600 | LN invoice's expiry time in seconds |

View file

@ -81,6 +81,10 @@ func Add_invoice(amount_sat int64, metadata string) (payment_request string, r_h
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
ln_invoice_expiry, err := strconv.ParseInt(db.Get_setting("LN_INVOICE_EXPIRY_SEC"), 10, 64)
if err != nil {
return "", nil, err
}
dh := sha256.Sum256([]byte(metadata)) dh := sha256.Sum256([]byte(metadata))
@ -98,6 +102,7 @@ func Add_invoice(amount_sat int64, metadata string) (payment_request string, r_h
result, err := l_client.AddInvoice(ctx, &lnrpc.Invoice{ result, err := l_client.AddInvoice(ctx, &lnrpc.Invoice{
Value: amount_sat, Value: amount_sat,
DescriptionHash: dh[:], DescriptionHash: dh[:],
Expiry: ln_invoice_expiry,
}) })
if err != nil { if err != nil {
@ -120,6 +125,11 @@ func Monitor_invoice_state(r_hash []byte) {
log.Warn(err) log.Warn(err)
return return
} }
ln_invoice_expiry, err := strconv.Atoi(db.Get_setting("LN_INVOICE_EXPIRY_SEC"))
if err != nil {
log.Warn(err)
return
}
connection := getGrpcConn( connection := getGrpcConn(
db.Get_setting("LN_HOST"), db.Get_setting("LN_HOST"),
@ -129,7 +139,7 @@ func Monitor_invoice_state(r_hash []byte) {
i_client := invoicesrpc.NewInvoicesClient(connection) i_client := invoicesrpc.NewInvoicesClient(connection)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Duration(ln_invoice_expiry) * time.Second)
defer cancel() defer cancel()
stream, err := i_client.SubscribeSingleInvoice(ctx, &invoicesrpc.SubscribeSingleInvoiceRequest{ stream, err := i_client.SubscribeSingleInvoice(ctx, &invoicesrpc.SubscribeSingleInvoiceRequest{
@ -228,10 +238,11 @@ func PayInvoice(card_payment_id int, invoice string) {
bolt11, _ := decodepay.Decodepay(invoice) bolt11, _ := decodepay.Decodepay(invoice)
invoice_msats := bolt11.MSatoshi invoice_msats := bolt11.MSatoshi
invoice_expiry := bolt11.Expiry
fee_limit_product := int64((fee_limit_percent / 100) * (float64(invoice_msats) / 1000)) fee_limit_product := int64((fee_limit_percent / 100) * (float64(invoice_msats) / 1000))
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Duration(invoice_expiry) * time.Second)
defer cancel() defer cancel()
stream, err := r_client.SendPaymentV2(ctx, &routerrpc.SendPaymentRequest{ stream, err := r_client.SendPaymentV2(ctx, &routerrpc.SendPaymentRequest{

View file

@ -31,3 +31,4 @@ INSERT INTO settings (name, value) VALUES ('LNDHUB_URL', '');
INSERT INTO settings (name, value) VALUES ('FUNCTION_INTERNAL_API', ''); INSERT INTO settings (name, value) VALUES ('FUNCTION_INTERNAL_API', '');
INSERT INTO settings (name, value) VALUES ('SENDGRID_API_KEY', ''); INSERT INTO settings (name, value) VALUES ('SENDGRID_API_KEY', '');
INSERT INTO settings (name, value) VALUES ('SENDGRID_EMAIL_SENDER', ''); INSERT INTO settings (name, value) VALUES ('SENDGRID_EMAIL_SENDER', '');
INSERT INTO settings (name, value) VALUES ('LN_INVOICE_EXPIRY_SEC', '3600');