-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
268 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ nostr-wallet-connect | |
nwc.db | ||
.breez | ||
.data | ||
.idea | ||
|
||
frontend/dist | ||
frontend/node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"github.com/getAlby/hub/db/queries" | ||
"github.com/nbd-wtf/go-nostr" | ||
|
||
"github.com/getAlby/hub/db" | ||
"github.com/getAlby/hub/logger" | ||
"github.com/getAlby/hub/nip47/models" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type getBudgetResponse struct { | ||
UsedBudget uint64 `json:"used_budget"` | ||
TotalBudget uint64 `json:"total_budget"` | ||
RenewsAt uint64 `json:"renews_at"` | ||
RenewalPeriod string `json:"renewal_period"` | ||
} | ||
|
||
func (controller *nip47Controller) HandleGetBudgetEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, app *db.App, publishResponse publishFunc) { | ||
|
||
logger.Logger.WithFields(logrus.Fields{ | ||
"request_event_id": requestEventId, | ||
}).Debug("Getting budget") | ||
|
||
appPermission := db.AppPermission{} | ||
controller.db.Where("app_id = ? AND scope = ?", app.ID, models.PAY_INVOICE_METHOD).First(&appPermission) | ||
|
||
maxAmount := appPermission.MaxAmountSat | ||
if maxAmount == 0 { | ||
publishResponse(&models.Response{ | ||
ResultType: nip47Request.Method, | ||
Result: struct{}{}, | ||
}, nostr.Tags{}) | ||
return | ||
} | ||
|
||
usedBudget := queries.GetBudgetUsageSat(controller.db, &appPermission) | ||
responsePayload := &getBudgetResponse{ | ||
TotalBudget: uint64(maxAmount * 1000), | ||
UsedBudget: usedBudget * 1000, | ||
RenewalPeriod: appPermission.BudgetRenewal, | ||
RenewsAt: uint64(queries.GetBudgetRenewsAt(appPermission.BudgetRenewal)), | ||
} | ||
|
||
publishResponse(&models.Response{ | ||
ResultType: nip47Request.Method, | ||
Result: responsePayload, | ||
}, nostr.Tags{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/nbd-wtf/go-nostr" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/getAlby/hub/constants" | ||
"github.com/getAlby/hub/db" | ||
"github.com/getAlby/hub/nip47/models" | ||
"github.com/getAlby/hub/nip47/permissions" | ||
"github.com/getAlby/hub/tests" | ||
"github.com/getAlby/hub/transactions" | ||
) | ||
|
||
const nip47GetBudgetJson = ` | ||
{ | ||
"method": "get_budget" | ||
} | ||
` | ||
|
||
func TestHandleGetBudgetEvent_noneUsed(t *testing.T) { | ||
ctx := context.TODO() | ||
defer tests.RemoveTestService() | ||
svc, err := tests.CreateTestService() | ||
assert.NoError(t, err) | ||
|
||
nip47Request := &models.Request{} | ||
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request) | ||
assert.NoError(t, err) | ||
|
||
app, _, err := tests.CreateApp(svc) | ||
assert.NoError(t, err) | ||
now := time.Now() | ||
|
||
appPermission := &db.AppPermission{ | ||
AppId: app.ID, | ||
App: *app, | ||
Scope: constants.PAY_INVOICE_SCOPE, | ||
MaxAmountSat: 400, | ||
BudgetRenewal: constants.BUDGET_RENEWAL_MONTHLY, | ||
} | ||
err = svc.DB.Create(appPermission).Error | ||
assert.NoError(t, err) | ||
|
||
dbRequestEvent := &db.RequestEvent{} | ||
err = svc.DB.Create(&dbRequestEvent).Error | ||
assert.NoError(t, err) | ||
|
||
var publishedResponse *models.Response | ||
|
||
publishResponse := func(response *models.Response, tags nostr.Tags) { | ||
publishedResponse = response | ||
} | ||
|
||
permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher) | ||
transactionsSvc := transactions.NewTransactionsService(svc.DB, svc.EventPublisher) | ||
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc). | ||
HandleGetBudgetEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse) | ||
|
||
assert.Equal(t, uint64(400000), publishedResponse.Result.(*getBudgetResponse).TotalBudget) | ||
assert.Equal(t, uint64(0), publishedResponse.Result.(*getBudgetResponse).UsedBudget) | ||
renewsAt := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).AddDate(0, 1, 0).Unix() | ||
assert.Equal(t, uint64(renewsAt), publishedResponse.Result.(*getBudgetResponse).RenewsAt) | ||
assert.Equal(t, constants.BUDGET_RENEWAL_MONTHLY, publishedResponse.Result.(*getBudgetResponse).RenewalPeriod) | ||
assert.Nil(t, publishedResponse.Error) | ||
} | ||
|
||
func TestHandleGetBudgetEvent_halfUsed(t *testing.T) { | ||
ctx := context.TODO() | ||
defer tests.RemoveTestService() | ||
svc, err := tests.CreateTestService() | ||
assert.NoError(t, err) | ||
|
||
nip47Request := &models.Request{} | ||
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request) | ||
assert.NoError(t, err) | ||
|
||
app, _, err := tests.CreateApp(svc) | ||
assert.NoError(t, err) | ||
now := time.Now() | ||
|
||
appPermission := &db.AppPermission{ | ||
AppId: app.ID, | ||
App: *app, | ||
Scope: constants.PAY_INVOICE_SCOPE, | ||
MaxAmountSat: 400, | ||
BudgetRenewal: constants.BUDGET_RENEWAL_MONTHLY, | ||
} | ||
err = svc.DB.Create(appPermission).Error | ||
assert.NoError(t, err) | ||
|
||
svc.DB.Create(&db.Transaction{ | ||
AppId: &app.ID, | ||
State: constants.TRANSACTION_STATE_SETTLED, | ||
Type: constants.TRANSACTION_TYPE_OUTGOING, | ||
AmountMsat: 200000, | ||
}) | ||
|
||
dbRequestEvent := &db.RequestEvent{} | ||
err = svc.DB.Create(&dbRequestEvent).Error | ||
assert.NoError(t, err) | ||
|
||
var publishedResponse *models.Response | ||
|
||
publishResponse := func(response *models.Response, tags nostr.Tags) { | ||
publishedResponse = response | ||
} | ||
|
||
permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher) | ||
transactionsSvc := transactions.NewTransactionsService(svc.DB, svc.EventPublisher) | ||
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc). | ||
HandleGetBudgetEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse) | ||
|
||
assert.Equal(t, uint64(400000), publishedResponse.Result.(*getBudgetResponse).TotalBudget) | ||
assert.Equal(t, uint64(200000), publishedResponse.Result.(*getBudgetResponse).UsedBudget) | ||
renewsAt := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).AddDate(0, 1, 0).Unix() | ||
assert.Equal(t, uint64(renewsAt), publishedResponse.Result.(*getBudgetResponse).RenewsAt) | ||
assert.Equal(t, constants.BUDGET_RENEWAL_MONTHLY, publishedResponse.Result.(*getBudgetResponse).RenewalPeriod) | ||
assert.Nil(t, publishedResponse.Error) | ||
} | ||
|
||
func TestHandleGetBudgetEvent_noBudget(t *testing.T) { | ||
ctx := context.TODO() | ||
defer tests.RemoveTestService() | ||
svc, err := tests.CreateTestService() | ||
assert.NoError(t, err) | ||
|
||
nip47Request := &models.Request{} | ||
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request) | ||
assert.NoError(t, err) | ||
|
||
app, _, err := tests.CreateApp(svc) | ||
assert.NoError(t, err) | ||
|
||
appPermission := &db.AppPermission{ | ||
AppId: app.ID, | ||
App: *app, | ||
Scope: constants.PAY_INVOICE_SCOPE, | ||
} | ||
err = svc.DB.Create(appPermission).Error | ||
assert.NoError(t, err) | ||
|
||
svc.DB.Create(&db.Transaction{ | ||
AppId: &app.ID, | ||
State: constants.TRANSACTION_STATE_SETTLED, | ||
Type: constants.TRANSACTION_TYPE_OUTGOING, | ||
AmountMsat: 200000, | ||
}) | ||
|
||
dbRequestEvent := &db.RequestEvent{} | ||
err = svc.DB.Create(&dbRequestEvent).Error | ||
assert.NoError(t, err) | ||
|
||
var publishedResponse *models.Response | ||
|
||
publishResponse := func(response *models.Response, tags nostr.Tags) { | ||
publishedResponse = response | ||
} | ||
|
||
permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher) | ||
transactionsSvc := transactions.NewTransactionsService(svc.DB, svc.EventPublisher) | ||
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc). | ||
HandleGetBudgetEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse) | ||
|
||
assert.Equal(t, struct{}{}, publishedResponse.Result) | ||
assert.Nil(t, publishedResponse.Error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.