From 437457781609f9efcc87d86df679ad53211cd0b8 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Fri, 12 Jul 2024 16:32:13 +0700 Subject: [PATCH] chore: merge balance type and visibility into isolated property on apps table --- db/migrations/202407012100_transactions.go | 7 ++---- db/models.go | 4 +--- nip47/controllers/get_balance_controller.go | 11 +++------- nip47/event_handler.go | 2 +- transactions/transactions_service.go | 24 +++++++++++---------- 5 files changed, 20 insertions(+), 28 deletions(-) diff --git a/db/migrations/202407012100_transactions.go b/db/migrations/202407012100_transactions.go index 8db7af1c..a8e83055 100644 --- a/db/migrations/202407012100_transactions.go +++ b/db/migrations/202407012100_transactions.go @@ -43,11 +43,8 @@ CREATE TABLE transactions( DROP TABLE payments; -ALTER TABLE app_permissions ADD balance_type string; -ALTER TABLE app_permissions ADD visibility string; - -UPDATE app_permissions set balance_type = "full"; -UPDATE app_permissions set visibility = "full"; +ALTER TABLE apps ADD isolated boolean; +UPDATE apps set isolated = false; ALTER TABLE app_permissions RENAME COLUMN max_amount TO max_amount_sat; diff --git a/db/models.go b/db/models.go index b4bd9122..fb002319 100644 --- a/db/models.go +++ b/db/models.go @@ -18,6 +18,7 @@ type App struct { NostrPubkey string `validate:"required"` CreatedAt time.Time UpdatedAt time.Time + Isolated bool } type AppPermission struct { @@ -30,9 +31,6 @@ type AppPermission struct { ExpiresAt *time.Time CreatedAt time.Time UpdatedAt time.Time - - BalanceType string - Visibility string } type RequestEvent struct { diff --git a/nip47/controllers/get_balance_controller.go b/nip47/controllers/get_balance_controller.go index e991da16..a3d9b8fb 100644 --- a/nip47/controllers/get_balance_controller.go +++ b/nip47/controllers/get_balance_controller.go @@ -22,7 +22,7 @@ type getBalanceResponse struct { } // TODO: remove checkPermission - can it be a middleware? -func (controller *nip47Controller) HandleGetBalanceEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, appId uint, checkPermission checkPermissionFunc, publishResponse publishFunc) { +func (controller *nip47Controller) HandleGetBalanceEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, app *db.App, checkPermission checkPermissionFunc, publishResponse publishFunc) { // basic permissions check resp := checkPermission(0) if resp != nil { @@ -34,14 +34,9 @@ func (controller *nip47Controller) HandleGetBalanceEvent(ctx context.Context, ni "request_event_id": requestEventId, }).Info("Getting balance") - // TODO: optimize - var appPermission db.AppPermission - controller.db.Find(&appPermission, &db.AppPermission{ - AppId: appId, - }) balance := uint64(0) - if appPermission.BalanceType == "isolated" { - balance = queries.GetIsolatedBalance(controller.db, appPermission.AppId) + if app.Isolated { + balance = queries.GetIsolatedBalance(controller.db, app.ID) } else { balance_signed, err := controller.lnClient.GetBalance(ctx) balance = uint64(balance_signed) diff --git a/nip47/event_handler.go b/nip47/event_handler.go index fde62132..f40974ba 100644 --- a/nip47/event_handler.go +++ b/nip47/event_handler.go @@ -307,7 +307,7 @@ func (svc *nip47Service) HandleEvent(ctx context.Context, sub *nostr.Subscriptio HandlePayKeysendEvent(ctx, nip47Request, requestEvent.ID, &app, checkPermission, publishResponse, nostr.Tags{}) case models.GET_BALANCE_METHOD: controller. - HandleGetBalanceEvent(ctx, nip47Request, requestEvent.ID, app.ID, checkPermission, publishResponse) + HandleGetBalanceEvent(ctx, nip47Request, requestEvent.ID, &app, checkPermission, publishResponse) case models.MAKE_INVOICE_METHOD: controller. HandleMakeInvoiceEvent(ctx, nip47Request, requestEvent.ID, app.ID, checkPermission, publishResponse) diff --git a/transactions/transactions_service.go b/transactions/transactions_service.go index 6bd33f8b..7ba772eb 100644 --- a/transactions/transactions_service.go +++ b/transactions/transactions_service.go @@ -341,12 +341,11 @@ func (svc *transactionsService) LookupTransaction(ctx context.Context, paymentHa tx := svc.db if appId != nil { - // TODO: optimize - var appPermission db.AppPermission - svc.db.Find(&appPermission, &db.AppPermission{ - AppId: *appId, + var app db.App + svc.db.Find(&app, &db.App{ + ID: *appId, }) - if appPermission.Visibility == "isolated" { + if app.Isolated { tx = tx.Where("app_id == ?", *appId) } } @@ -388,12 +387,11 @@ func (svc *transactionsService) ListTransactions(ctx context.Context, from, unti } if appId != nil { - // TODO: optimize - var appPermission db.AppPermission - svc.db.Find(&appPermission, &db.AppPermission{ - AppId: *appId, + var app db.App + svc.db.Find(&app, &db.App{ + ID: *appId, }) - if appPermission.Visibility == "isolated" { + if app.Isolated { tx = tx.Where("app_id == ?", *appId) } } @@ -656,13 +654,17 @@ func (svc *transactionsService) validateCanPay(tx *gorm.DB, appId *uint, amount // ensure balance for isolated apps if appId != nil { + var app db.App + tx.Find(&app, &db.App{ + ID: *appId, + }) var appPermission db.AppPermission tx.Find(&appPermission, &db.AppPermission{ AppId: *appId, Scope: constants.PAY_INVOICE_SCOPE, }) - if appPermission.BalanceType == "isolated" { + if app.Isolated { balance := queries.GetIsolatedBalance(tx, appPermission.AppId) if amountWithFeeReserve > balance {