Skip to content

Commit

Permalink
chore: merge balance type and visibility into isolated property on ap…
Browse files Browse the repository at this point in the history
…ps table
  • Loading branch information
rolznz committed Jul 12, 2024
1 parent 770b37f commit 4374577
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
7 changes: 2 additions & 5 deletions db/migrations/202407012100_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type App struct {
NostrPubkey string `validate:"required"`
CreatedAt time.Time
UpdatedAt time.Time
Isolated bool
}

type AppPermission struct {
Expand All @@ -30,9 +31,6 @@ type AppPermission struct {
ExpiresAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time

BalanceType string
Visibility string
}

type RequestEvent struct {
Expand Down
11 changes: 3 additions & 8 deletions nip47/controllers/get_balance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion nip47/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 13 additions & 11 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4374577

Please sign in to comment.