Skip to content

Commit

Permalink
chore: further refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Dec 7, 2023
1 parent d3208f2 commit 5fd0d66
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
1 change: 0 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ func main() {
logMw := transport.CreateLoggingMiddleware(logger)
// strict rate limit for requests for sending payments
strictRateLimitMiddleware := transport.CreateRateLimitMiddleware(c.StrictRateLimit, c.BurstRateLimit)

secured := e.Group("", tokens.Middleware(c.JWTSecret), logMw)
securedWithStrictRateLimit := e.Group("", tokens.Middleware(c.JWTSecret), strictRateLimitMiddleware, logMw)

Expand Down
3 changes: 2 additions & 1 deletion controllers_v2/keysend.ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

"github.com/getAlby/lndhub.go/common"
"github.com/getAlby/lndhub.go/db/models"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/getAlby/lndhub.go/lnd"
Expand Down Expand Up @@ -164,7 +165,7 @@ func (controller *KeySendController) MultiKeySend(c echo.Context) error {
return c.JSON(status, result)
}

func (controller *KeySendController) checkKeysendPaymentAllowed(ctx context.Context, amount, userID int64, limits *lnd.Limits) (resp *responses.ErrorResponse) {
func (controller *KeySendController) checkKeysendPaymentAllowed(ctx context.Context, amount, userID int64, limits *models.Limits) (resp *responses.ErrorResponse) {
syntheticPayReq := &lnd.LNPayReq{
PayReq: &lnrpc.PayReq{
NumSatoshis: amount,
Expand Down
8 changes: 8 additions & 0 deletions db/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type User struct {
Deactivated bool
}

type Limits struct {
MaxSendVolume int64
MaxSendAmount int64
MaxReceiveVolume int64
MaxReceiveAmount int64
MaxAccountBalance int64
}

func (u *User) BeforeAppendModel(ctx context.Context, query bun.Query) error {
switch query.(type) {
case *bun.UpdateQuery:
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (
)

func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *service.LndhubService, err error) {
dbUri := "postgresql://im-adithya:password@localhost:5432/lndhub?sslmode=disable"
dbUri := "postgresql://user:password@localhost/lndhub?sslmode=disable"
c := &service.Config{
DatabaseUri: dbUri,
DatabaseMaxConns: 1,
Expand Down
18 changes: 9 additions & 9 deletions lib/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (svc *LndhubService) FindUserByLogin(ctx context.Context, login string) (*m
return &user, nil
}

func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpayReq *lnd.LNPayReq, userId int64, limits *lnd.Limits) (result *responses.ErrorResponse, err error) {
func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpayReq *lnd.LNPayReq, userId int64, limits *models.Limits) (result *responses.ErrorResponse, err error) {
if limits.MaxSendAmount > 0 {
if lnpayReq.PayReq.NumSatoshis > limits.MaxSendAmount {
svc.Logger.Errorf("Max send amount exceeded for user_id %v (amount:%v)", userId, lnpayReq.PayReq.NumSatoshis)
Expand Down Expand Up @@ -157,7 +157,7 @@ func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpay
return svc.CheckVolumeAllowed(ctx, userId, limits.MaxSendVolume, common.InvoiceTypeOutgoing)
}

func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amount, userId int64, limits *lnd.Limits) (result *responses.ErrorResponse, err error) {
func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amount, userId int64, limits *models.Limits) (result *responses.ErrorResponse, err error) {
if limits.MaxReceiveAmount > 0 {
if amount > limits.MaxReceiveAmount {
svc.Logger.Errorf("Max receive amount exceeded for user_id %d", userId)
Expand Down Expand Up @@ -275,27 +275,27 @@ func (svc *LndhubService) GetVolumeOverPeriod(ctx context.Context, userId int64,
return result, nil
}

func (svc *LndhubService) GetLimits(c echo.Context) (limits *lnd.Limits) {
limits = &lnd.Limits{
func (svc *LndhubService) GetLimits(c echo.Context) (limits *models.Limits) {
limits = &models.Limits{
MaxSendVolume: svc.Config.MaxSendVolume,
MaxSendAmount: svc.Config.MaxSendAmount,
MaxReceiveVolume: svc.Config.MaxReceiveVolume,
MaxReceiveAmount: svc.Config.MaxReceiveAmount,
MaxAccountBalance: svc.Config.MaxAccountBalance,
}
if val, ok := c.Get("MaxSendVolume").(int64); ok {
if val, ok := c.Get("MaxSendVolume").(int64); ok && val > 0 {
limits.MaxSendVolume = val
}
if val, ok := c.Get("MaxSendAmount").(int64); ok {
if val, ok := c.Get("MaxSendAmount").(int64); ok && val > 0 {
limits.MaxSendAmount = val
}
if val, ok := c.Get("MaxReceiveVolume").(int64); ok {
if val, ok := c.Get("MaxReceiveVolume").(int64); ok && val > 0 {
limits.MaxReceiveVolume = val
}
if val, ok := c.Get("MaxReceiveAmount").(int64); ok {
if val, ok := c.Get("MaxReceiveAmount").(int64); ok && val > 0 {
limits.MaxReceiveAmount = val
}
if val, ok := c.Get("MaxAccountBalance").(int64); ok {
if val, ok := c.Get("MaxAccountBalance").(int64); ok && val > 0 {
limits.MaxAccountBalance = val
}

Expand Down
8 changes: 0 additions & 8 deletions lnd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ type Config struct {
LNDClusterPubkeys string `envconfig:"LND_CLUSTER_PUBKEYS"` //comma-seperated list of public keys of the cluster
}

type Limits struct {
MaxSendVolume int64
MaxSendAmount int64
MaxReceiveVolume int64
MaxReceiveAmount int64
MaxAccountBalance int64
}

func LoadConfig() (c *Config, err error) {
c = &Config{}
err = envconfig.Process("", c)
Expand Down

0 comments on commit 5fd0d66

Please sign in to comment.