Skip to content

Commit

Permalink
Merge branch 'task-refactor-checks' into task-limits-jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Dec 7, 2023
2 parents 5fd0d66 + bd92e6f commit 0385c17
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
1 change: 0 additions & 1 deletion integration_tests/internal_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func (suite *PaymentTestSuite) SetupSuite() {
suite.aliceToken = userTokens[0]
suite.bobLogin = users[1]
suite.bobToken = userTokens[1]

suite.echo.Use(tokens.Middleware([]byte(suite.service.Config.JWTSecret)))
suite.echo.GET("/balance", controllers.NewBalanceController(suite.service).Balance)
suite.echo.POST("/addinvoice", controllers.NewAddInvoiceController(suite.service).AddInvoice)
Expand Down
57 changes: 36 additions & 21 deletions lib/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpay
}
}

if limits.MaxSendVolume > 0 {
volume, err := svc.GetVolumeOverPeriod(ctx, userId, common.InvoiceTypeOutgoing, time.Duration(svc.Config.MaxVolumePeriod*int64(time.Second)))
if err != nil {
svc.Logger.Errorj(
log.JSON{
"message": "error fetching volume",
"error": err,
"lndhub_user_id": userId,
},
)
return nil, err
}
if volume > limits.MaxSendVolume {
svc.Logger.Errorf("Transaction volume exceeded for user_id %d", userId)
sentry.CaptureMessage(fmt.Sprintf("transaction volume exceeded for user %d", userId))
return &responses.TooMuchVolumeError, nil
}
}

currentBalance, err := svc.CurrentUserBalance(ctx, userId)
if err != nil {
svc.Logger.Errorj(
Expand All @@ -154,7 +173,7 @@ func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpay
return &responses.NotEnoughBalanceError, nil
}

return svc.CheckVolumeAllowed(ctx, userId, limits.MaxSendVolume, common.InvoiceTypeOutgoing)
return nil, nil
}

func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amount, userId int64, limits *models.Limits) (result *responses.ErrorResponse, err error) {
Expand All @@ -165,50 +184,46 @@ func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amoun
}
}

if limits.MaxAccountBalance > 0 {
currentBalance, err := svc.CurrentUserBalance(ctx, userId)
if limits.MaxReceiveVolume > 0 {
volume, err := svc.GetVolumeOverPeriod(ctx, userId, common.InvoiceTypeIncoming, time.Duration(svc.Config.MaxVolumePeriod*int64(time.Second)))
if err != nil {
svc.Logger.Errorj(
log.JSON{
"message": "error fetching balance",
"lndhub_user_id": userId,
"message": "error fetching volume",
"error": err,
"lndhub_user_id": userId,
},
)
return nil, err
}
if currentBalance+amount > limits.MaxAccountBalance {
svc.Logger.Errorf("Max account balance exceeded for user_id %d", userId)
return &responses.BalanceExceededError, nil
if volume > limits.MaxReceiveVolume {
svc.Logger.Errorf("Transaction volume exceeded for user_id %d", userId)
sentry.CaptureMessage(fmt.Sprintf("transaction volume exceeded for user %d", userId))
return &responses.TooMuchVolumeError, nil
}
}

return svc.CheckVolumeAllowed(ctx, userId, limits.MaxReceiveVolume, common.InvoiceTypeIncoming)
}

func (svc *LndhubService) CheckVolumeAllowed(ctx context.Context, userId, maxVolume int64, invoiceType string) (result *responses.ErrorResponse, err error) {
if maxVolume > 0 {
volume, err := svc.GetVolumeOverPeriod(ctx, userId, invoiceType, time.Duration(svc.Config.MaxVolumePeriod*int64(time.Second)))
if limits.MaxAccountBalance > 0 {
currentBalance, err := svc.CurrentUserBalance(ctx, userId)
if err != nil {
svc.Logger.Errorj(
log.JSON{
"message": "error fetching volume",
"error": err,
"message": "error fetching balance",
"lndhub_user_id": userId,
"error": err,
},
)
return nil, err
}
if volume > maxVolume {
svc.Logger.Errorf("Transaction volume exceeded for user_id %d", userId)
sentry.CaptureMessage(fmt.Sprintf("transaction volume exceeded for user %d", userId))
return &responses.TooMuchVolumeError, nil
if currentBalance+amount > limits.MaxAccountBalance {
svc.Logger.Errorf("Max account balance exceeded for user_id %d", userId)
return &responses.BalanceExceededError, nil
}
}

return nil, nil
}


func (svc *LndhubService) CalcFeeLimit(destination string, amount int64) int64 {
if svc.LndClient.IsIdentityPubkey(destination) {
return 0
Expand Down

0 comments on commit 0385c17

Please sign in to comment.