Skip to content

Commit

Permalink
chore: remove check volume allowed function
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Dec 7, 2023
1 parent 62d7ffb commit bd92e6f
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions lib/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpay
}
}

if svc.Config.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 > svc.Config.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 @@ -153,7 +172,7 @@ func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpay
return &responses.NotEnoughBalanceError, nil
}

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

func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amount, userId int64) (result *responses.ErrorResponse, err error) {
Expand All @@ -164,56 +183,46 @@ func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amoun
}
}

if svc.Config.MaxAccountBalance > 0 {
currentBalance, err := svc.CurrentUserBalance(ctx, userId)
if svc.Config.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 > svc.Config.MaxAccountBalance {
svc.Logger.Errorf("Max account balance exceeded for user_id %d", userId)
return &responses.BalanceExceededError, nil
if volume > svc.Config.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, common.InvoiceTypeIncoming)
}

func (svc *LndhubService) CheckVolumeAllowed(ctx context.Context, userId int64, invoiceType string) (result *responses.ErrorResponse, err error) {
var maxVolume int64
if invoiceType == common.InvoiceTypeIncoming {
maxVolume = svc.Config.MaxReceiveVolume
} else {
maxVolume = svc.Config.MaxSendVolume
}
if maxVolume > 0 {
volume, err := svc.GetVolumeOverPeriod(ctx, userId, invoiceType, time.Duration(svc.Config.MaxVolumePeriod*int64(time.Second)))
if svc.Config.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 > svc.Config.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 bd92e6f

Please sign in to comment.