mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-19 13:44:53 +01:00
Replace hardcoded strings with common constants
This commit is contained in:
@@ -3,6 +3,7 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/getAlby/lndhub.go/common"
|
||||||
"github.com/getAlby/lndhub.go/lib"
|
"github.com/getAlby/lndhub.go/lib"
|
||||||
"github.com/getAlby/lndhub.go/lib/service"
|
"github.com/getAlby/lndhub.go/lib/service"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
@@ -21,7 +22,7 @@ func NewGetTXSController(svc *service.LndhubService) *GetTXSController {
|
|||||||
func (controller *GetTXSController) GetTXS(c echo.Context) error {
|
func (controller *GetTXSController) GetTXS(c echo.Context) error {
|
||||||
userId := c.Get("UserID").(int64)
|
userId := c.Get("UserID").(int64)
|
||||||
|
|
||||||
invoices, err := controller.svc.InvoicesFor(c.Request().Context(), userId, "outgoing")
|
invoices, err := controller.svc.InvoicesFor(c.Request().Context(), userId, common.InvoiceTypeOutgoing)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -34,7 +35,7 @@ func (controller *GetTXSController) GetTXS(c echo.Context) error {
|
|||||||
"payment_hash": rhash,
|
"payment_hash": rhash,
|
||||||
"payment_preimage": invoice.Preimage,
|
"payment_preimage": invoice.Preimage,
|
||||||
"value": invoice.Amount,
|
"value": invoice.Amount,
|
||||||
"type": "paid_invoice",
|
"type": common.InvoiceTypePaid,
|
||||||
"fee": 0, //TODO charge fees
|
"fee": 0, //TODO charge fees
|
||||||
"timestamp": invoice.CreatedAt.Unix(),
|
"timestamp": invoice.CreatedAt.Unix(),
|
||||||
"memo": invoice.Memo,
|
"memo": invoice.Memo,
|
||||||
@@ -46,7 +47,7 @@ func (controller *GetTXSController) GetTXS(c echo.Context) error {
|
|||||||
func (controller *GetTXSController) GetUserInvoices(c echo.Context) error {
|
func (controller *GetTXSController) GetUserInvoices(c echo.Context) error {
|
||||||
userId := c.Get("UserID").(int64)
|
userId := c.Get("UserID").(int64)
|
||||||
|
|
||||||
invoices, err := controller.svc.InvoicesFor(c.Request().Context(), userId, "incoming")
|
invoices, err := controller.svc.InvoicesFor(c.Request().Context(), userId, common.InvoiceTypeIncoming)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -60,11 +61,11 @@ func (controller *GetTXSController) GetUserInvoices(c echo.Context) error {
|
|||||||
"pay_req": invoice.PaymentRequest,
|
"pay_req": invoice.PaymentRequest,
|
||||||
"description": invoice.Memo,
|
"description": invoice.Memo,
|
||||||
"payment_hash": invoice.RHash,
|
"payment_hash": invoice.RHash,
|
||||||
"ispaid": invoice.State == "settled",
|
"ispaid": invoice.State == common.InvoiceStateSettled,
|
||||||
"amt": invoice.Amount,
|
"amt": invoice.Amount,
|
||||||
"expire_time": 3600 * 24,
|
"expire_time": 3600 * 24,
|
||||||
"timestamp": invoice.CreatedAt.Unix(),
|
"timestamp": invoice.CreatedAt.Unix(),
|
||||||
"type": "user_invoice",
|
"type": common.InvoiceTypeUser,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c.JSON(http.StatusOK, &response)
|
return c.JSON(http.StatusOK, &response)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
|
"github.com/getAlby/lndhub.go/common"
|
||||||
"github.com/getAlby/lndhub.go/db/models"
|
"github.com/getAlby/lndhub.go/db/models"
|
||||||
"github.com/labstack/gommon/random"
|
"github.com/labstack/gommon/random"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
@@ -49,18 +50,18 @@ func (svc *LndhubService) SendInternalPayment(ctx context.Context, tx *bun.Tx, i
|
|||||||
//SendInternalPayment()
|
//SendInternalPayment()
|
||||||
// find invoice
|
// find invoice
|
||||||
var incomingInvoice models.Invoice
|
var incomingInvoice models.Invoice
|
||||||
err := svc.DB.NewSelect().Model(&incomingInvoice).Where("type = ? AND payment_request = ? AND state = ? ", "incoming", invoice.PaymentRequest, "open").Limit(1).Scan(ctx)
|
err := svc.DB.NewSelect().Model(&incomingInvoice).Where("type = ? AND payment_request = ? AND state = ? ", common.InvoiceTypeIncoming, invoice.PaymentRequest, common.InvoiceStateOpen).Limit(1).Scan(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// invoice not found or already settled
|
// invoice not found or already settled
|
||||||
// TODO: logging
|
// TODO: logging
|
||||||
return sendPaymentResponse, err
|
return sendPaymentResponse, err
|
||||||
}
|
}
|
||||||
// Get the user's current and incoming account for the transaction entry
|
// Get the user's current and incoming account for the transaction entry
|
||||||
recipientCreditAccount, err := svc.AccountFor(ctx, "current", incomingInvoice.UserID)
|
recipientCreditAccount, err := svc.AccountFor(ctx, common.AccountTypeCurrent, incomingInvoice.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sendPaymentResponse, err
|
return sendPaymentResponse, err
|
||||||
}
|
}
|
||||||
recipientDebitAccount, err := svc.AccountFor(ctx, "incoming", incomingInvoice.UserID)
|
recipientDebitAccount, err := svc.AccountFor(ctx, common.AccountTypeIncoming, incomingInvoice.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sendPaymentResponse, err
|
return sendPaymentResponse, err
|
||||||
}
|
}
|
||||||
@@ -89,7 +90,7 @@ func (svc *LndhubService) SendInternalPayment(ctx context.Context, tx *bun.Tx, i
|
|||||||
sendPaymentResponse.PaymentRoute = &Route{TotalAmt: invoice.Amount, TotalFees: 0}
|
sendPaymentResponse.PaymentRoute = &Route{TotalAmt: invoice.Amount, TotalFees: 0}
|
||||||
|
|
||||||
incomingInvoice.Internal = true // mark incoming invoice as internal, just for documentation/debugging
|
incomingInvoice.Internal = true // mark incoming invoice as internal, just for documentation/debugging
|
||||||
incomingInvoice.State = "settled"
|
incomingInvoice.State = common.InvoiceStateSettled
|
||||||
incomingInvoice.SettledAt = schema.NullTime{Time: time.Now()}
|
incomingInvoice.SettledAt = schema.NullTime{Time: time.Now()}
|
||||||
_, err = tx.NewUpdate().Model(&incomingInvoice).WherePK().Exec(ctx)
|
_, err = tx.NewUpdate().Model(&incomingInvoice).WherePK().Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -144,11 +145,11 @@ func (svc *LndhubService) PayInvoice(ctx context.Context, invoice *models.Invoic
|
|||||||
userId := invoice.UserID
|
userId := invoice.UserID
|
||||||
|
|
||||||
// Get the user's current and outgoing account for the transaction entry
|
// Get the user's current and outgoing account for the transaction entry
|
||||||
debitAccount, err := svc.AccountFor(ctx, "current", userId)
|
debitAccount, err := svc.AccountFor(ctx, common.AccountTypeCurrent, userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
creditAccount, err := svc.AccountFor(ctx, "outgoing", userId)
|
creditAccount, err := svc.AccountFor(ctx, common.AccountTypeOutgoing, userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -203,7 +204,7 @@ func (svc *LndhubService) PayInvoice(ctx context.Context, invoice *models.Invoic
|
|||||||
|
|
||||||
// The payment was successful.
|
// The payment was successful.
|
||||||
invoice.Preimage = paymentResponse.PaymentPreimageStr
|
invoice.Preimage = paymentResponse.PaymentPreimageStr
|
||||||
invoice.State = "settled"
|
invoice.State = common.InvoiceStateSettled
|
||||||
invoice.SettledAt = schema.NullTime{Time: time.Now()}
|
invoice.SettledAt = schema.NullTime{Time: time.Now()}
|
||||||
|
|
||||||
_, err = tx.NewUpdate().Model(invoice).WherePK().Exec(ctx)
|
_, err = tx.NewUpdate().Model(invoice).WherePK().Exec(ctx)
|
||||||
@@ -227,10 +228,10 @@ func (svc *LndhubService) AddOutgoingInvoice(ctx context.Context, userID int64,
|
|||||||
destinationPubkeyHex := hex.EncodeToString(decodedInvoice.Destination.SerializeCompressed())
|
destinationPubkeyHex := hex.EncodeToString(decodedInvoice.Destination.SerializeCompressed())
|
||||||
expiresAt := decodedInvoice.Timestamp.Add(decodedInvoice.Expiry())
|
expiresAt := decodedInvoice.Timestamp.Add(decodedInvoice.Expiry())
|
||||||
invoice := models.Invoice{
|
invoice := models.Invoice{
|
||||||
Type: "outgoing",
|
Type: common.InvoiceTypeOutgoing,
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
PaymentRequest: paymentRequest,
|
PaymentRequest: paymentRequest,
|
||||||
State: "initialized",
|
State: common.InvoiceStateInitialized,
|
||||||
DestinationPubkeyHex: destinationPubkeyHex,
|
DestinationPubkeyHex: destinationPubkeyHex,
|
||||||
ExpiresAt: bun.NullTime{Time: expiresAt},
|
ExpiresAt: bun.NullTime{Time: expiresAt},
|
||||||
}
|
}
|
||||||
@@ -263,12 +264,12 @@ func (svc *LndhubService) AddIncomingInvoice(ctx context.Context, userID int64,
|
|||||||
expiry := time.Hour * 24 // invoice expires in 24h
|
expiry := time.Hour * 24 // invoice expires in 24h
|
||||||
// Initialize new DB invoice
|
// Initialize new DB invoice
|
||||||
invoice := models.Invoice{
|
invoice := models.Invoice{
|
||||||
Type: "incoming",
|
Type: common.InvoiceTypeIncoming,
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
Memo: memo,
|
Memo: memo,
|
||||||
DescriptionHash: descriptionHashStr,
|
DescriptionHash: descriptionHashStr,
|
||||||
State: "initialized",
|
State: common.InvoiceStateInitialized,
|
||||||
ExpiresAt: bun.NullTime{Time: time.Now().Add(expiry)},
|
ExpiresAt: bun.NullTime{Time: time.Now().Add(expiry)},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,7 +303,7 @@ func (svc *LndhubService) AddIncomingInvoice(ctx context.Context, userID int64,
|
|||||||
invoice.Preimage = hex.EncodeToString(preimage)
|
invoice.Preimage = hex.EncodeToString(preimage)
|
||||||
invoice.AddIndex = lnInvoiceResult.AddIndex
|
invoice.AddIndex = lnInvoiceResult.AddIndex
|
||||||
invoice.DestinationPubkeyHex = svc.GetIdentPubKeyHex() // Our node pubkey for incoming invoices
|
invoice.DestinationPubkeyHex = svc.GetIdentPubKeyHex() // Our node pubkey for incoming invoices
|
||||||
invoice.State = "open"
|
invoice.State = common.InvoiceStateOpen
|
||||||
|
|
||||||
_, err = svc.DB.NewUpdate().Model(&invoice).WherePK().Exec(ctx)
|
_, err = svc.DB.NewUpdate().Model(&invoice).WherePK().Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/getAlby/lndhub.go/common"
|
||||||
"github.com/getAlby/lndhub.go/db/models"
|
"github.com/getAlby/lndhub.go/db/models"
|
||||||
"github.com/getAlby/lndhub.go/lnd"
|
"github.com/getAlby/lndhub.go/lnd"
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
@@ -21,7 +22,11 @@ func (svc *LndhubService) ProcessInvoiceUpdate(ctx context.Context, rawInvoice *
|
|||||||
svc.Logger.Infof("Invoice update: r_hash:%s state:%v", rHashStr, rawInvoice.State.String())
|
svc.Logger.Infof("Invoice update: r_hash:%s state:%v", rHashStr, rawInvoice.State.String())
|
||||||
|
|
||||||
// Search for an incoming invoice with the r_hash that is NOT settled in our DB
|
// Search for an incoming invoice with the r_hash that is NOT settled in our DB
|
||||||
err := svc.DB.NewSelect().Model(&invoice).Where("type = ? AND r_hash = ? AND state <> ? AND expires_at > ?", "incoming", rHashStr, "settled", time.Now()).Limit(1).Scan(ctx)
|
err := svc.DB.NewSelect().Model(&invoice).Where("type = ? AND r_hash = ? AND state <> ? AND expires_at > ?",
|
||||||
|
common.InvoiceTypeIncoming,
|
||||||
|
rHashStr,
|
||||||
|
common.InvoiceStateSettled,
|
||||||
|
time.Now()).Limit(1).Scan(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
svc.Logger.Infof("Invoice not found. Ignoring. r_hash:%s", rHashStr)
|
svc.Logger.Infof("Invoice not found. Ignoring. r_hash:%s", rHashStr)
|
||||||
return nil
|
return nil
|
||||||
@@ -34,13 +39,13 @@ func (svc *LndhubService) ProcessInvoiceUpdate(ctx context.Context, rawInvoice *
|
|||||||
svc.Logger.Infof("Invoice update: invoice_id:%v settled:%v value:%v state:%v", invoice.ID, rawInvoice.Settled, rawInvoice.AmtPaidSat, rawInvoice.State)
|
svc.Logger.Infof("Invoice update: invoice_id:%v settled:%v value:%v state:%v", invoice.ID, rawInvoice.Settled, rawInvoice.AmtPaidSat, rawInvoice.State)
|
||||||
|
|
||||||
// Get the user's current account for the transaction entry
|
// Get the user's current account for the transaction entry
|
||||||
creditAccount, err := svc.AccountFor(ctx, "current", invoice.UserID)
|
creditAccount, err := svc.AccountFor(ctx, common.AccountTypeCurrent, invoice.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
svc.Logger.Errorf("Could not find current account user_id:%v invoice_id:%v", invoice.UserID, invoice.ID)
|
svc.Logger.Errorf("Could not find current account user_id:%v invoice_id:%v", invoice.UserID, invoice.ID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Get the user's incoming account for the transaction entry
|
// Get the user's incoming account for the transaction entry
|
||||||
debitAccount, err := svc.AccountFor(ctx, "incoming", invoice.UserID)
|
debitAccount, err := svc.AccountFor(ctx, common.AccountTypeIncoming, invoice.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
svc.Logger.Errorf("Could not find incoming account user_id:%v invoice_id:%v", invoice.UserID, invoice.ID)
|
svc.Logger.Errorf("Could not find incoming account user_id:%v invoice_id:%v", invoice.UserID, invoice.ID)
|
||||||
return err
|
return err
|
||||||
@@ -61,7 +66,7 @@ func (svc *LndhubService) ProcessInvoiceUpdate(ctx context.Context, rawInvoice *
|
|||||||
} else {
|
} else {
|
||||||
// if the invoice is settled we update the state and create an transaction entry to the current account
|
// if the invoice is settled we update the state and create an transaction entry to the current account
|
||||||
invoice.SettledAt = bun.NullTime{Time: time.Unix(rawInvoice.SettleDate, 0)}
|
invoice.SettledAt = bun.NullTime{Time: time.Unix(rawInvoice.SettleDate, 0)}
|
||||||
invoice.State = "settled"
|
invoice.State = common.InvoiceStateSettled
|
||||||
_, err = tx.NewUpdate().Model(&invoice).WherePK().Exec(ctx)
|
_, err = tx.NewUpdate().Model(&invoice).WherePK().Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
|
"github.com/getAlby/lndhub.go/common"
|
||||||
"github.com/getAlby/lndhub.go/db/models"
|
"github.com/getAlby/lndhub.go/db/models"
|
||||||
"github.com/getAlby/lndhub.go/lib/security"
|
"github.com/getAlby/lndhub.go/lib/security"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
@@ -28,7 +29,12 @@ func (svc *LndhubService) CreateUser(ctx context.Context) (user *models.User, er
|
|||||||
if _, err := tx.NewInsert().Model(user).Exec(ctx); err != nil {
|
if _, err := tx.NewInsert().Model(user).Exec(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
accountTypes := []string{"incoming", "current", "outgoing", "fees"}
|
accountTypes := []string{
|
||||||
|
common.AccountTypeIncoming,
|
||||||
|
common.AccountTypeCurrent,
|
||||||
|
common.AccountTypeOutgoing,
|
||||||
|
common.AccountTypeFees,
|
||||||
|
}
|
||||||
for _, accountType := range accountTypes {
|
for _, accountType := range accountTypes {
|
||||||
account := models.Account{UserID: user.ID, Type: accountType}
|
account := models.Account{UserID: user.ID, Type: accountType}
|
||||||
if _, err := tx.NewInsert().Model(&account).Exec(ctx); err != nil {
|
if _, err := tx.NewInsert().Model(&account).Exec(ctx); err != nil {
|
||||||
@@ -55,7 +61,7 @@ func (svc *LndhubService) FindUser(ctx context.Context, userId int64) (*models.U
|
|||||||
func (svc *LndhubService) CurrentUserBalance(ctx context.Context, userId int64) (int64, error) {
|
func (svc *LndhubService) CurrentUserBalance(ctx context.Context, userId int64) (int64, error) {
|
||||||
var balance int64
|
var balance int64
|
||||||
|
|
||||||
account, err := svc.AccountFor(ctx, "current", userId)
|
account, err := svc.AccountFor(ctx, common.AccountTypeCurrent, userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return balance, err
|
return balance, err
|
||||||
}
|
}
|
||||||
@@ -74,7 +80,7 @@ func (svc *LndhubService) InvoicesFor(ctx context.Context, userId int64, invoice
|
|||||||
|
|
||||||
query := svc.DB.NewSelect().Model(&invoices).Where("user_id = ?", userId)
|
query := svc.DB.NewSelect().Model(&invoices).Where("user_id = ?", userId)
|
||||||
if invoiceType != "" {
|
if invoiceType != "" {
|
||||||
query.Where("type = ? AND state <> ?", invoiceType, "initialized")
|
query.Where("type = ? AND state <> ?", invoiceType, common.InvoiceStateInitialized)
|
||||||
}
|
}
|
||||||
query.OrderExpr("id DESC").Limit(100)
|
query.OrderExpr("id DESC").Limit(100)
|
||||||
err := query.Scan(ctx)
|
err := query.Scan(ctx)
|
||||||
|
|||||||
Reference in New Issue
Block a user