Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kosegor committed Sep 26, 2023
1 parent 0a293ae commit 246537f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 48 deletions.
8 changes: 8 additions & 0 deletions util/checkers/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package checkers

func MergeErrors(errs1, errs2 []error) []error {
if len(errs2) > 0 {
return append(errs1, errs2...)
}
return errs1
}
47 changes: 19 additions & 28 deletions x/metoken/keeper/metoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/umee-network/umee/v6/util/checkers"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -92,22 +94,16 @@ func (k Keeper) UpdateIndexes(
errs = append(errs, sdkerrors.ErrInvalidRequest.Wrapf("Emergency Group cannot register new indexes"))
}

if updErrs := validateEmergencyIndexUpdate(updateIndexes, registeredIndexes); len(updErrs) > 0 {
errs = append(errs, updErrs...)
}
errs = checkers.MergeErrors(errs, validateEGIndexUpdate(updateIndexes, registeredIndexes))

if len(errs) > 0 {
return errors.Join(errs...)
}
}

if addErrs := k.addIndexes(addIndexes, registeredIndexes, registeredAssets); len(addErrs) > 0 {
errs = append(errs, addErrs...)
}
errs = checkers.MergeErrors(errs, k.addIndexes(addIndexes, registeredIndexes, registeredAssets))

if updErrs := k.updateIndexes(updateIndexes, registeredIndexes, registeredAssets); len(updErrs) > 0 {
errs = append(errs, updErrs...)
}
errs = checkers.MergeErrors(errs, k.updateIndexes(updateIndexes, registeredIndexes, registeredAssets))

if len(errs) > 0 {
return errors.Join(errs...)
Expand All @@ -132,6 +128,7 @@ func (k Keeper) addIndexes(
index.Denom,
),
)
continue
}

if exists := k.hasIndexBalance(index.Denom); exists {
Expand All @@ -151,12 +148,11 @@ func (k Keeper) addIndexes(
aa.Denom,
),
)
continue
}
}

if errs := k.validateInLeverage(index); len(errs) > 0 {
indexErrs = append(indexErrs, errs...)
}
checkers.MergeErrors(indexErrs, k.validateInLeverage(index))

if len(indexErrs) > 0 {
allErrs = append(allErrs, indexErrs...)
Expand Down Expand Up @@ -252,9 +248,7 @@ func (k Keeper) updateIndexes(
}
}

if errs := k.validateInLeverage(index); len(errs) > 0 {
indexErrs = append(indexErrs, errs...)
}
checkers.MergeErrors(indexErrs, k.validateInLeverage(index))

if len(indexErrs) > 0 {
allErrs = append(allErrs, indexErrs...)
Expand Down Expand Up @@ -291,41 +285,38 @@ func (k Keeper) updateIndexes(
return nil
}

func validateEmergencyIndexUpdate(indexes []metoken.Index, registeredIndexes map[string]metoken.Index) []error {
// validateEGIndexUpdate checks if emergency group can perform updates.
func validateEGIndexUpdate(indexes []metoken.Index, registeredIndexes map[string]metoken.Index) []error {
var errs []error
for _, newIndex := range indexes {
oldIndex, ok := registeredIndexes[newIndex.Denom]
d := newIndex.Denom
oldIndex, ok := registeredIndexes[d]
if !ok {
errs = append(
errs, sdkerrors.ErrNotFound.Wrapf(
"update: index with denom %s not found",
newIndex.Denom,
),
)
errs = append(errs, sdkerrors.ErrNotFound.Wrapf("update: index with denom %s not found", d))
continue
}

if newIndex.Exponent != oldIndex.Exponent {
errs = append(errs, errors.New("exponent cannot be changed"))
errs = append(errs, errors.New(d+": exponent cannot be changed"))
}

if !newIndex.Fee.Equal(oldIndex.Fee) {
errs = append(errs, errors.New("fee cannot be changed"))
errs = append(errs, errors.New(d+": fee cannot be changed"))
}

if !newIndex.MaxSupply.Equal(oldIndex.MaxSupply) {
errs = append(errs, errors.New("max_supply cannot be changed"))
errs = append(errs, errors.New(d+": max_supply cannot be changed"))
}

for _, newAsset := range newIndex.AcceptedAssets {
oldAsset, i := oldIndex.AcceptedAsset(newAsset.Denom)
if i < 0 {
errs = append(errs, fmt.Errorf("new asset %s cannot be added", newAsset.Denom))
errs = append(errs, fmt.Errorf("%s: new asset %s cannot be added", d, newAsset.Denom))
continue
}

if !newAsset.ReservePortion.Equal(oldAsset.ReservePortion) {
errs = append(errs, fmt.Errorf("reserve_portion of %s cannot be changed", newAsset.Denom))
errs = append(errs, fmt.Errorf("%s: reserve_portion of %s cannot be changed", d, newAsset.Denom))
}
}
}
Expand Down
36 changes: 16 additions & 20 deletions x/metoken/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package keeper
import (
"context"

"github.com/umee-network/umee/v6/util/checkers"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/umee-network/umee/v6/util/sdkutil"

"github.com/umee-network/umee/v6/util/checkers"
"github.com/umee-network/umee/v6/util/sdkutil"
"github.com/umee-network/umee/v6/x/metoken"
)

Expand All @@ -25,18 +24,17 @@ func NewMsgServerImpl(kb Builder) metoken.MsgServer {

// Swap handles the request for the swap, delegates the execution and returns the response.
func (m msgServer) Swap(goCtx context.Context, msg *metoken.MsgSwap) (*metoken.MsgSwapResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
k := m.kb.Keeper(&ctx)

if err := msg.ValidateBasic(); err != nil {
ctx, err := sdkutil.StartMsg(goCtx, msg)
if err != nil {
return nil, err
}

userAddr, err := sdk.AccAddressFromBech32(msg.User)
if err != nil {
return nil, err
}


Check failure on line 36 in x/metoken/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofmt`-ed with `-s` (gofmt)
k := m.kb.Keeper(&ctx)
resp, err := k.swap(userAddr, msg.MetokenDenom, msg.Asset)
if err != nil {
return nil, err
Expand Down Expand Up @@ -68,10 +66,8 @@ func (m msgServer) Swap(goCtx context.Context, msg *metoken.MsgSwap) (*metoken.M

// Redeem handles the request for the redemption, delegates the execution and returns the response.
func (m msgServer) Redeem(goCtx context.Context, msg *metoken.MsgRedeem) (*metoken.MsgRedeemResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
k := m.kb.Keeper(&ctx)

if err := msg.ValidateBasic(); err != nil {
ctx, err := sdkutil.StartMsg(goCtx, msg)
if err != nil {
return nil, err
}

Expand All @@ -80,6 +76,7 @@ func (m msgServer) Redeem(goCtx context.Context, msg *metoken.MsgRedeem) (*metok
return nil, err
}

k := m.kb.Keeper(&ctx)
resp, err := k.redeem(userAddr, msg.Metoken, msg.AssetDenom)
if err != nil {
return nil, err
Expand Down Expand Up @@ -118,12 +115,12 @@ func (m msgServer) GovSetParams(goCtx context.Context, msg *metoken.MsgGovSetPar
*metoken.MsgGovSetParamsResponse,
error,
) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := msg.ValidateBasic(); err != nil {
ctx, err := sdkutil.StartMsg(goCtx, msg)
if err != nil {
return nil, err
}

if err := m.kb.Keeper(&ctx).SetParams(msg.Params); err != nil {
if err = m.kb.Keeper(&ctx).SetParams(msg.Params); err != nil {
return nil, err
}

Expand All @@ -135,15 +132,14 @@ func (m msgServer) GovUpdateRegistry(
goCtx context.Context,
msg *metoken.MsgGovUpdateRegistry,
) (*metoken.MsgGovUpdateRegistryResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
k := m.kb.Keeper(&ctx)

byEmergencyGroup, err := checkers.EmergencyGroupAuthority(msg.Authority, k.ugov(&ctx))
ctx, err := sdkutil.StartMsg(goCtx, msg)
if err != nil {
return nil, err
}

if err := msg.ValidateBasic(); err != nil {
k := m.kb.Keeper(&ctx)
byEmergencyGroup, err := checkers.EmergencyGroupAuthority(msg.Authority, k.ugov(&ctx))
if err != nil {
return nil, err
}

Expand Down

0 comments on commit 246537f

Please sign in to comment.