Skip to content

Commit 2222e24

Browse files
committed
feedback improvements [3]
1 parent a49a212 commit 2222e24

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

internal/adapters/entrypoints/rest/common.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math"
88
"math/big"
99
"net/http"
10+
"strconv"
1011
"time"
1112

1213
"github.com/go-playground/validator/v10"
@@ -26,17 +27,24 @@ var RequestValidator = validator.New(validator.WithRequiredStructEnabled())
2627

2728
func PositiveStringValidationRule(value string) bool {
2829
bigIntValue := new(big.Int)
29-
bigIntValue.SetString(value, 10)
30+
_, ok := bigIntValue.SetString(value, 10)
31+
if !ok {
32+
return false
33+
}
3034
return bigIntValue.Cmp(big.NewInt(0)) > 0
3135
}
3236

33-
func percentageFeeValidator(fl validator.FieldLevel) bool {
37+
func decimalPlacesValidator(fl validator.FieldLevel) bool {
3438
val := fl.Field().Float()
35-
if val < 0 || val >= 100 {
39+
param := fl.Param()
40+
maxDecimals, err := strconv.Atoi(param)
41+
if err != nil {
3642
return false
3743
}
38-
valTimes100 := val * 100
39-
return math.Mod(valTimes100, 1) == 0
44+
factor := math.Pow10(maxDecimals)
45+
valTimesFactor := val * factor
46+
diff := math.Abs(valTimesFactor - math.Round(valTimesFactor))
47+
return diff < 1e-9
4048
}
4149

4250
func init() {
@@ -52,8 +60,8 @@ func registerValidations() error {
5260
return fmt.Errorf("registering positive_string validation: %w", err)
5361
}
5462

55-
if err := RequestValidator.RegisterValidation("percentage_fee", percentageFeeValidator); err != nil {
56-
return fmt.Errorf("registering percentage_fee validation: %w", err)
63+
if err := RequestValidator.RegisterValidation("max_decimal_places", decimalPlacesValidator); err != nil {
64+
return fmt.Errorf("registering max_decimal_places validation: %w", err)
5765
}
5866
return nil
5967
}

internal/entities/common_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package entities_test
33
import (
44
"encoding/hex"
55
"encoding/json"
6+
"math"
7+
"testing"
8+
69
"github.com/ethereum/go-ethereum/crypto"
710
"github.com/rsksmart/liquidity-provider-server/internal/entities"
811
"github.com/rsksmart/liquidity-provider-server/internal/entities/liquidity_provider"
912
"github.com/rsksmart/liquidity-provider-server/internal/entities/utils"
1013
"github.com/stretchr/testify/require"
11-
"math"
12-
"testing"
1314
)
1415

1516
func TestSigned_CheckIntegrity(t *testing.T) {
@@ -56,8 +57,8 @@ func TestSigned_CheckIntegrity(t *testing.T) {
5657
signed entities.Signed[any]
5758
err error
5859
}{
59-
{signed: entities.Signed[any]{Value: peginConfig, Hash: "2f8528af602b84e2b8083ed9bbbf21bca3bcfcc78948ed970c65aa58f4271870"}},
60-
{signed: entities.Signed[any]{Value: pegoutConfig, Hash: "e405a29c1e0469a284f45aa8b26c95a84d8ee993664e0cf2cc5f1a963212e432"}},
60+
{signed: entities.Signed[any]{Value: peginConfig, Hash: "5ab75cad18e0ad640908a3b70d6bf2e3cdca66bb53544e91833c942c4f5430af"}},
61+
{signed: entities.Signed[any]{Value: pegoutConfig, Hash: "35a51729bb71bb891db62dd968f33ea2479ddb17143da32ca6bb55142a488052"}},
6162
{signed: entities.Signed[any]{Value: generalConfig, Hash: "77a1d9b2426955a2dbeb4e6b561607fbd8bd044de7a60c1ed77126e72ea3cb18"}},
6263
{signed: entities.Signed[any]{Value: peginConfig, Hash: "f3daab424654d2eeb2b50dc00b3e453e24ca1c690d80015f5f54d5f1fefaf900"}, err: entities.IntegrityError},
6364
{signed: entities.Signed[any]{Value: pegoutConfig, Hash: "3b3e7b075eb60b8c249f44a117f406c64992bafda1273f540277448abd14077e"}, err: entities.IntegrityError},

internal/entities/quote/pegin_quote.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package quote
22

33
import (
44
"context"
5+
"time"
6+
57
"github.com/rsksmart/liquidity-provider-server/internal/entities"
68
"github.com/rsksmart/liquidity-provider-server/internal/entities/utils"
7-
"time"
89
)
910

1011
const (
@@ -45,7 +46,7 @@ type CreatedPeginQuote struct {
4546

4647
type PeginCreationData struct {
4748
GasPrice *entities.Wei `json:"gasPrice" bson:"gas_price" validate:"required"`
48-
FeePercentage *utils.BigFloat `json:"percentageFee" bson:"percentage_fee" validate:"required"`
49+
FeePercentage *utils.BigFloat `json:"percentageFee" bson:"gte=0,lt=100,max_decimal_places=2" validate:"required"`
4950
FixedFee *entities.Wei `json:"fixedFee" bson:"fixed_fee" validate:"required"`
5051
}
5152

internal/entities/quote/pegout_quote.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package quote
22

33
import (
44
"context"
5+
"time"
6+
57
"github.com/rsksmart/liquidity-provider-server/internal/entities"
68
"github.com/rsksmart/liquidity-provider-server/internal/entities/liquidity_provider"
79
"github.com/rsksmart/liquidity-provider-server/internal/entities/utils"
8-
"time"
910
)
1011

1112
const (
@@ -52,7 +53,7 @@ type CreatedPegoutQuote struct {
5253

5354
type PegoutCreationData struct {
5455
FeeRate *utils.BigFloat `json:"feeRate" bson:"fee_rate" validate:"required"`
55-
FeePercentage *utils.BigFloat `json:"percentageFee" bson:"percentage_fee" validate:"required"`
56+
FeePercentage *utils.BigFloat `json:"percentageFee" bson:"gte=0,lt=100,max_decimal_places=2" validate:"required"`
5657
GasPrice *entities.Wei `json:"gasPrice" bson:"gas_price" validate:"required"`
5758
FixedFee *entities.Wei `json:"fixedFee" bson:"fixed_fee" validate:"required"`
5859
}

pkg/liquidity_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type PeginConfigurationDTO struct {
4747
CallTime uint32 `json:"callTime" validate:"required"`
4848
PenaltyFee string `json:"penaltyFee" validate:"required,numeric,positive_string"`
4949
FixedFee string `json:"fixedFee" validate:"required,numeric,min=0"`
50-
FeePercentage float64 `json:"feePercentage" validate:"numeric,percentage_fee"`
50+
FeePercentage float64 `json:"feePercentage" validate:"numeric,gte=0,lt=100,max_decimal_places=2"`
5151
MaxValue string `json:"maxValue" validate:"required,numeric,positive_string"`
5252
MinValue string `json:"minValue" validate:"required,numeric,positive_string"`
5353
}
@@ -61,7 +61,7 @@ type PegoutConfigurationDTO struct {
6161
ExpireTime uint32 `json:"expireTime" validate:"required"`
6262
PenaltyFee string `json:"penaltyFee" validate:"required,numeric,positive_string"`
6363
FixedFee string `json:"fixedFee" validate:"required,numeric,min=0"`
64-
FeePercentage float64 `json:"feePercentage" validate:"required,percentage_fee"`
64+
FeePercentage float64 `json:"feePercentage" validate:"required,gte=0,lt=100,max_decimal_places=2"`
6565
MaxValue string `json:"maxValue" validate:"required,numeric,positive_string"`
6666
MinValue string `json:"minValue" validate:"required,numeric,positive_string"`
6767
ExpireBlocks uint64 `json:"expireBlocks" validate:"required"`

0 commit comments

Comments
 (0)