mirror of
https://github.com/getAlby/hub.git
synced 2026-08-13 12:33:39 +02:00
77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/getAlby/go-nostr"
|
|
"github.com/getAlby/hub/db"
|
|
"github.com/getAlby/hub/db/queries"
|
|
"github.com/getAlby/hub/logger"
|
|
"github.com/getAlby/hub/nip47/models"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
MSAT_PER_SAT = 1000
|
|
)
|
|
|
|
type getBalanceResponse struct {
|
|
Balance int64 `json:"balance"`
|
|
// MaxAmount int `json:"max_amount"`
|
|
// BudgetRenewal string `json:"budget_renewal"`
|
|
}
|
|
|
|
func (controller *nip47Controller) HandleGetBalanceEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, app *db.App, publishResponse publishFunc) {
|
|
|
|
logger.Logger.WithFields(logrus.Fields{
|
|
"request_event_id": requestEventId,
|
|
}).Debug("Getting balance")
|
|
|
|
balanceMsat := int64(0)
|
|
if app.Isolated {
|
|
var err error
|
|
balanceMsat, err = queries.GetIsolatedBalanceMsat(controller.db, app.ID)
|
|
if err != nil {
|
|
logger.Logger.WithFields(logrus.Fields{
|
|
"request_event_id": requestEventId,
|
|
}).WithError(err).Error("Failed to fetch isolated balance")
|
|
publishResponse(&models.Response{
|
|
ResultType: nip47Request.Method,
|
|
Error: mapNip47Error(err),
|
|
}, nostr.Tags{})
|
|
return
|
|
}
|
|
} else {
|
|
balances, err := controller.lnClient.GetBalances(ctx, true)
|
|
if err != nil {
|
|
logger.Logger.WithFields(logrus.Fields{
|
|
"request_event_id": requestEventId,
|
|
}).WithError(err).Error("Failed to fetch balance")
|
|
publishResponse(&models.Response{
|
|
ResultType: nip47Request.Method,
|
|
Error: mapNip47Error(err),
|
|
}, nostr.Tags{})
|
|
return
|
|
}
|
|
balanceMsat = balances.Lightning.TotalSpendable
|
|
}
|
|
|
|
responsePayload := &getBalanceResponse{
|
|
Balance: balanceMsat,
|
|
}
|
|
|
|
// this is not part of the spec and does not seem to be used
|
|
/*appPermission := db.AppPermission{}
|
|
controller.db.Where("app_id = ? AND request_method = ?", app.ID, models.PAY_INVOICE_METHOD).First(&appPermission)
|
|
|
|
maxAmount := appPermission.MaxAmount
|
|
if maxAmount > 0 {
|
|
responsePayload.MaxAmount = maxAmount * MSAT_PER_SAT
|
|
responsePayload.BudgetRenewal = appPermission.BudgetRenewal
|
|
}*/
|
|
|
|
publishResponse(&models.Response{
|
|
ResultType: nip47Request.Method,
|
|
Result: responsePayload,
|
|
}, nostr.Tags{})
|
|
}
|