diff --git a/api/api.go b/api/api.go index d2c3ccc5..e6b2091f 100644 --- a/api/api.go +++ b/api/api.go @@ -145,7 +145,7 @@ func (api *api) UpdateApp(userApp *db.App, updateAppRequest *UpdateAppRequest) e App: *userApp, Scope: method, ExpiresAt: expiresAt, - MaxAmount: int(maxAmount), + MaxAmountSat: int(maxAmount), BudgetRenewal: budgetRenewal, } if err := tx.Create(&perm).Error; err != nil { @@ -195,7 +195,7 @@ func (api *api) GetApp(dbApp *db.App) *App { //renewsIn := "" budgetUsage := uint64(0) - maxAmount := uint64(paySpecificPermission.MaxAmount) + maxAmount := uint64(paySpecificPermission.MaxAmountSat) if maxAmount > 0 { budgetUsage = queries.GetBudgetUsageSat(api.db, &paySpecificPermission) } @@ -251,7 +251,7 @@ func (api *api) ListApps() ([]App, error) { apiApp.ExpiresAt = appPermission.ExpiresAt if appPermission.Scope == constants.PAY_INVOICE_SCOPE { apiApp.BudgetRenewal = appPermission.BudgetRenewal - apiApp.MaxAmountSat = uint64(appPermission.MaxAmount) + apiApp.MaxAmountSat = uint64(appPermission.MaxAmountSat) if apiApp.MaxAmountSat > 0 { apiApp.BudgetUsage = queries.GetBudgetUsageSat(api.db, &appPermission) } diff --git a/db/db_service.go b/db/db_service.go index 031ba46a..63b163e5 100644 --- a/db/db_service.go +++ b/db/db_service.go @@ -53,7 +53,7 @@ func (svc *dbService) CreateApp(name string, pubkey string, maxAmountSat uint64, Scope: scope, ExpiresAt: expiresAt, //these fields are only relevant for pay_invoice - MaxAmount: int(maxAmountSat), + MaxAmountSat: int(maxAmountSat), BudgetRenewal: budgetRenewal, } err = tx.Create(&appPermission).Error diff --git a/db/migrations/202407012100_transactions.go b/db/migrations/202407012100_transactions.go index b3fd8ae5..3b99be6f 100644 --- a/db/migrations/202407012100_transactions.go +++ b/db/migrations/202407012100_transactions.go @@ -47,6 +47,8 @@ ALTER TABLE app_permissions ADD visibility string; UPDATE app_permissions set balance_type = "full"; UPDATE app_permissions set visibility = "full"; +ALTER TABLE app_permissions RENAME COLUMN max_amount TO max_amount_sat; + `).Error; err != nil { return err } diff --git a/db/models.go b/db/models.go index f74111a3..a1a6efbb 100644 --- a/db/models.go +++ b/db/models.go @@ -25,7 +25,7 @@ type AppPermission struct { AppId uint `validate:"required"` App App Scope string `validate:"required"` - MaxAmount int // TODO: rename to MaxAmountSat + MaxAmountSat int BudgetRenewal string ExpiresAt *time.Time CreatedAt time.Time diff --git a/nip47/permissions/permissions.go b/nip47/permissions/permissions.go index c761d6fa..4e1f1987 100644 --- a/nip47/permissions/permissions.go +++ b/nip47/permissions/permissions.go @@ -60,7 +60,7 @@ func (svc *permissionsService) HasPermission(app *db.App, scope string, amountMs } if scope == constants.PAY_INVOICE_SCOPE { - maxAmount := appPermission.MaxAmount + maxAmount := appPermission.MaxAmountSat if maxAmount != 0 { budgetUsage := queries.GetBudgetUsageSat(svc.db, &appPermission) diff --git a/nip47/permissions/permissions_test.go b/nip47/permissions/permissions_test.go index a5224bcc..c5d0a6ed 100644 --- a/nip47/permissions/permissions_test.go +++ b/nip47/permissions/permissions_test.go @@ -39,7 +39,7 @@ func TestHasPermission_Expired(t *testing.T) { AppId: app.ID, App: *app, Scope: PAY_INVOICE_SCOPE, - MaxAmount: 100, + MaxAmountSat: 100, BudgetRenewal: budgetRenewal, ExpiresAt: &expiresAt, } @@ -67,7 +67,7 @@ func TestHasPermission_Exceeded(t *testing.T) { AppId: app.ID, App: *app, Scope: models.PAY_INVOICE_METHOD, - MaxAmount: 10, + MaxAmountSat: 10, BudgetRenewal: budgetRenewal, ExpiresAt: &expiresAt, } @@ -95,7 +95,7 @@ func TestHasPermission_OK(t *testing.T) { AppId: app.ID, App: *app, Scope: models.PAY_INVOICE_METHOD, - MaxAmount: 10, + MaxAmountSat: 10, BudgetRenewal: budgetRenewal, ExpiresAt: &expiresAt, } diff --git a/transactions/transactions_service.go b/transactions/transactions_service.go index ad07cc8e..221ea125 100644 --- a/transactions/transactions_service.go +++ b/transactions/transactions_service.go @@ -670,9 +670,9 @@ func (svc *transactionsService) validateCanPay(tx *gorm.DB, appId *uint, amount } } - if appPermission.MaxAmount > 0 { + if appPermission.MaxAmountSat > 0 { budgetUsageSat := queries.GetBudgetUsageSat(tx, &appPermission) - if int(amountWithFeeReserve/1000) > appPermission.MaxAmount-int(budgetUsageSat) { + if int(amountWithFeeReserve/1000) > appPermission.MaxAmountSat-int(budgetUsageSat) { return NewQuotaExceededError() } }