Skip to content

Commit

Permalink
chore: add volume check while creating invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Nov 29, 2023
1 parent 6db26d4 commit 770f9f5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions controllers/invoice.ctrl.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package controllers

import (
"fmt"
"net/http"
"time"

"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/getsentry/sentry-go"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
)

// InvoiceController : Add invoice controller struct
Expand All @@ -24,5 +28,23 @@ func (controller *InvoiceController) Invoice(c echo.Context) error {
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}

if controller.svc.Config.MaxVolume > 0 {
volume, err := controller.svc.GetVolumeOverPeriod(c.Request().Context(), user.ID, time.Duration(controller.svc.Config.MaxVolumePeriod*int64(time.Second)))
if err != nil {
c.Logger().Errorj(
log.JSON{
"message": "error creating invoice",
"error": err,
"lndhub_user_id": user.ID,
},
)
return c.JSON(http.StatusInternalServerError, responses.GeneralServerError)
}
if volume > controller.svc.Config.MaxVolume {
sentry.CaptureMessage(fmt.Sprintf("transaction volume exceeded for user %d", user.ID))
return c.JSON(http.StatusInternalServerError, responses.TooMuchVolumeError)
}
}

return AddInvoice(c, controller.svc, user.ID)
}
19 changes: 19 additions & 0 deletions controllers_v2/invoice.ctrl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v2controllers

import (
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -178,6 +179,24 @@ func (controller *InvoiceController) AddInvoice(c echo.Context) error {
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}

if controller.svc.Config.MaxVolume > 0 {
volume, err := controller.svc.GetVolumeOverPeriod(c.Request().Context(), userID, time.Duration(controller.svc.Config.MaxVolumePeriod*int64(time.Second)))
if err != nil {
c.Logger().Errorj(
log.JSON{
"message": "error creating invoice",
"error": err,
"lndhub_user_id": userID,
},
)
return c.JSON(http.StatusInternalServerError, responses.GeneralServerError)
}
if volume > controller.svc.Config.MaxVolume {
sentry.CaptureMessage(fmt.Sprintf("transaction volume exceeded for user %d", userID))
return c.JSON(http.StatusInternalServerError, responses.TooMuchVolumeError)
}
}

c.Logger().Infof("Adding invoice: user_id:%v memo:%s value:%v description_hash:%s", userID, body.Description, body.Amount, body.DescriptionHash)

invoice, err := controller.svc.AddIncomingInvoice(c.Request().Context(), userID, body.Amount, body.Description, body.DescriptionHash)
Expand Down

0 comments on commit 770f9f5

Please sign in to comment.