alby-hub/nip47/controllers/get_balance_controller.go
Roland e3373474f8
chore: remove json tags from lnclient models (#2375)
* chore: remove json tags from lnclient models

these should not be passed through the API directly

* fix: properly return not implemented errors

* fix: json tags on TLVRecord
2026-05-25 20:26:07 +05:30

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.TotalSpendableMsat
}
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{})
}