alby-hub/nip47/controllers/get_balance_controller.go
Anshuman 1d3f9ecd41
feat: suffix balance/amount fields with explicit unit (Sat or Msat) (#2153)
* feat: add both Sat and Msat companion fields for all ambiguous balance/amount properties

* feat: add sat and msat companion fields to backend API responses

* chore: align frontend types and other missing fields

* chore: keep old formula for calculating total fee sat

* chore: remove unnecessary balance assignments in cashu and phoenix

* chore: add deprecated comment to non unit fields

* chore: rename callers and variable to specify units

* chore: remove msat fields for channel size and liquidity fields

* chore: drop msat fields from onchain channel size and liquidity responses

* chore: further changes

* chore: remove amount msat field onchain tx

* chore: remove msats from onchain balance response

* chore: remove msat fields for swaps

* chore: remove msat fields for punishment reserves

* chore: simplify rebalancing fee calculation

* chore: remove unnecessary fields

* chore: mark deprecated fields in frontend types

---------

Co-authored-by: im-adithya <imadithyavardhan@gmail.com>
2026-04-17 13:53:28 +05:30

77 lines
2.1 KiB
Go

package controllers
import (
"context"
"github.com/getAlby/hub/db"
"github.com/getAlby/hub/db/queries"
"github.com/getAlby/hub/logger"
"github.com/getAlby/hub/nip47/models"
"github.com/nbd-wtf/go-nostr"
"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{})
}