Skip to content

Commit

Permalink
chore: add denom validation for string type denom
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae committed May 8, 2024
1 parent c76058c commit 0b9cc3c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
13 changes: 13 additions & 0 deletions x/fswap/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Finschia/finschia-sdk/store/prefix"
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
"github.com/Finschia/finschia-sdk/types/query"
"github.com/Finschia/finschia-sdk/x/fswap/types"
)
Expand All @@ -23,6 +24,12 @@ func NewQueryServer(keeper Keeper) *QueryServer {

func (s QueryServer) Swapped(ctx context.Context, req *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) {
c := sdk.UnwrapSDKContext(ctx)
if err := sdk.ValidateDenom(req.GetFromDenom()); err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error())

Check warning on line 28 in x/fswap/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/grpc_query.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}
if err := sdk.ValidateDenom(req.GetToDenom()); err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error())

Check warning on line 31 in x/fswap/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/grpc_query.go#L30-L31

Added lines #L30 - L31 were not covered by tests
}

swapped, err := s.Keeper.getSwapped(c, req.GetFromDenom(), req.GetToDenom())
if err != nil {
Expand All @@ -37,6 +44,12 @@ func (s QueryServer) Swapped(ctx context.Context, req *types.QuerySwappedRequest

func (s QueryServer) TotalSwappableToCoinAmount(ctx context.Context, req *types.QueryTotalSwappableToCoinAmountRequest) (*types.QueryTotalSwappableToCoinAmountResponse, error) {
c := sdk.UnwrapSDKContext(ctx)
if err := sdk.ValidateDenom(req.GetFromDenom()); err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error())

Check warning on line 48 in x/fswap/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/grpc_query.go#L47-L48

Added lines #L47 - L48 were not covered by tests
}
if err := sdk.ValidateDenom(req.GetToDenom()); err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error())

Check warning on line 51 in x/fswap/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/grpc_query.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}

amount, err := s.Keeper.getSwappableNewCoinAmount(c, req.GetFromDenom(), req.GetToDenom())
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions x/fswap/types/fswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ import (

// ValidateBasic validates the set of Swap
func (s *Swap) ValidateBasic() error {
if s.FromDenom == "" {
return sdkerrors.ErrInvalidRequest.Wrap("from denomination cannot be empty")
if err := sdk.ValidateDenom(s.FromDenom); err != nil {
return sdkerrors.ErrInvalidRequest.Wrap(err.Error())

Check warning on line 13 in x/fswap/types/fswap.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/types/fswap.go#L12-L13

Added lines #L12 - L13 were not covered by tests
}
if s.ToDenom == "" {
return sdkerrors.ErrInvalidRequest.Wrap("to denomination cannot be empty")

if err := sdk.ValidateDenom(s.ToDenom); err != nil {
return sdkerrors.ErrInvalidRequest.Wrap(err.Error())

Check warning on line 17 in x/fswap/types/fswap.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/types/fswap.go#L16-L17

Added lines #L16 - L17 were not covered by tests
}

if s.FromDenom == s.ToDenom {
return sdkerrors.ErrInvalidRequest.Wrap("from denomination cannot be equal to to denomination")
}

if s.AmountCapForToDenom.LT(sdk.OneInt()) {
return sdkerrors.ErrInvalidRequest.Wrap("amount cannot be less than one")
}

if s.SwapRate.IsZero() {
return sdkerrors.ErrInvalidRequest.Wrap("swap rate cannot be zero")
}

return nil
}

Expand Down
12 changes: 6 additions & 6 deletions x/fswap/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func (m *MsgSwap) ValidateBasic() error {
return sdkerrors.ErrInvalidCoins.Wrap(m.FromCoinAmount.String())
}

if len(m.GetToDenom()) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("invalid denom (empty denom)")
if err := sdk.ValidateDenom(m.GetToDenom()); err != nil {
return sdkerrors.ErrInvalidRequest.Wrap(err.Error())

Check warning on line 27 in x/fswap/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/types/msgs.go#L26-L27

Added lines #L26 - L27 were not covered by tests
}

return nil
Expand Down Expand Up @@ -53,12 +53,12 @@ func (m *MsgSwapAll) ValidateBasic() error {
return sdkerrors.ErrInvalidAddress.Wrapf("Invalid address (%s)", err)
}

if len(m.GetFromDenom()) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("invalid denom (empty denom)")
if err := sdk.ValidateDenom(m.FromDenom); err != nil {
return sdkerrors.ErrInvalidRequest.Wrap(err.Error())

Check warning on line 57 in x/fswap/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/types/msgs.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}

if len(m.GetToDenom()) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("invalid denom (empty denom)")
if err := sdk.ValidateDenom(m.ToDenom); err != nil {
return sdkerrors.ErrInvalidRequest.Wrap(err.Error())

Check warning on line 61 in x/fswap/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/types/msgs.go#L60-L61

Added lines #L60 - L61 were not covered by tests
}

return nil
Expand Down

0 comments on commit 0b9cc3c

Please sign in to comment.