diff --git a/x/multi-staking/handler.go b/x/multi-staking/handler.go index e46f9c54..21c46e9f 100644 --- a/x/multi-staking/handler.go +++ b/x/multi-staking/handler.go @@ -1,7 +1,6 @@ package multistaking import ( - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" @@ -21,7 +20,7 @@ func NewBondDenomProposalHandler(k keeper.Keeper) govtypes.Handler { keeper.HandlerRemoveBondTokenProposal(ctx, &k, c) return nil default: - return fmt.Errorf("unrecognized brond denom proposal content type") + return types.ErrUnrecognized } } } diff --git a/x/multi-staking/keeper/lock.go b/x/multi-staking/keeper/lock.go index e403d048..f087d665 100644 --- a/x/multi-staking/keeper/lock.go +++ b/x/multi-staking/keeper/lock.go @@ -1,8 +1,6 @@ package keeper import ( - "fmt" - "cosmossdk.io/errors" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -20,7 +18,7 @@ func (k Keeper) LockedAmountToBondAmount( // get lock on source val lock, found := k.GetMultiStakingLock(ctx, lockID) if !found { - return math.Int{}, fmt.Errorf("can't find multi staking lock") + return math.Int{}, types.ErrNotFoundMultiStaking } return lock.LockedAmountToBondAmount(lockedAmount).RoundInt(), nil @@ -55,7 +53,7 @@ func (k Keeper) RemoveTokenFromLock( lockID := types.MultiStakingLockID(delAddr, valAddr) lock, found := k.GetMultiStakingLock(ctx, lockID) if !found { - return types.MultiStakingLock{}, fmt.Errorf("can't find multi staking lock") + return types.MultiStakingLock{}, types.ErrNotFoundMultiStaking } // remove token from lock on source val @@ -138,7 +136,7 @@ func (k Keeper) BurnBondTokenAndUnlockMultiStakingToken( lockID := types.MultiStakingLockID(delAddr, valAddr) multiStakingLock, found := k.GetMultiStakingLock(ctx, lockID) if !found { - return unlockedAmount, fmt.Errorf("StakingLock not exists") + return unlockedAmount, types.ErrStakingNotExitsts } // unlock amount @@ -148,7 +146,7 @@ func (k Keeper) BurnBondTokenAndUnlockMultiStakingToken( // check amount if unlockMultiStakingAmount.GT(multiStakingLock.LockedAmount) { - return unlockedAmount, fmt.Errorf("unlock amount greater than lock amount") + return unlockedAmount, types.ErrCheckInsufficientAmount } // burn bonded token diff --git a/x/multi-staking/keeper/msg_server.go b/x/multi-staking/keeper/msg_server.go index 7b6adbd2..69b3bb7b 100644 --- a/x/multi-staking/keeper/msg_server.go +++ b/x/multi-staking/keeper/msg_server.go @@ -2,7 +2,6 @@ package keeper import ( "context" - "fmt" errorsmod "cosmossdk.io/errors" "cosmossdk.io/math" @@ -96,7 +95,7 @@ func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*typ } if !k.IsAllowedToken(ctx, valAcc, msg.Amount) { - return nil, fmt.Errorf("not allowed token") + return nil, types.ErrNotAllowedToken } intermediaryAccount := types.IntermediaryAccount(delAcc) @@ -139,7 +138,7 @@ func (k msgServer) BeginRedelegate(goCtx context.Context, msg *types.MsgBeginRed } if !k.IsAllowedToken(ctx, srcValAcc, msg.Amount) || !k.IsAllowedToken(ctx, dstValAcc, msg.Amount) { - return nil, fmt.Errorf("not allowed Token") + return nil, types.ErrNotAllowedToken } bondAmount, err := k.LockedAmountToBondAmount(ctx, delAcc, srcValAcc, msg.Amount.Amount) @@ -176,11 +175,11 @@ func (k Keeper) GetDelegation(ctx sdk.Context, delAcc sdk.AccAddress, val sdk.Va func (k Keeper) AdjustUnbondAmount(ctx sdk.Context, delAcc sdk.AccAddress, valAcc sdk.ValAddress, amount math.Int) (adjustedAmount math.Int, err error) { delegation, found := k.GetDelegation(ctx, delAcc, valAcc) if !found { - return math.Int{}, fmt.Errorf("delegation not found") + return math.Int{}, types.ErrDelegationNotFound } validator, found := k.stakingKeeper.GetValidator(ctx, valAcc) if !found { - return math.Int{}, fmt.Errorf("validator not found") + return math.Int{}, types.ErrValidatorNotFound } shares, err := validator.SharesFromTokens(amount) @@ -212,13 +211,13 @@ func (k msgServer) Undelegate(goCtx context.Context, msg *types.MsgUndelegate) ( } if !k.IsAllowedToken(ctx, valAcc, msg.Amount) { - return nil, fmt.Errorf("not allowed token") + return nil, types.ErrNotAllowedToken } lockID := types.MultiStakingLockID(delAcc, valAcc) lock, found := k.GetMultiStakingLock(ctx, lockID) if !found { - return nil, fmt.Errorf("can't find multi staking lock") + return nil, types.ErrNotFoundMultiStaking } unbondAmount := lock.LockedAmountToBondAmount(msg.Amount.Amount).RoundInt() @@ -264,7 +263,9 @@ func (k msgServer) CancelUnbondingDelegation(goCtx context.Context, msg *types.M ubd, found := k.GetMultiStakingUnlock(ctx, delAcc, valAcc) if !found { - return nil, fmt.Errorf("not found unbonding recored") + return nil, errorsmod.Wrapf( + sdkerrors.ErrNotFound, "not found unbonding recored", + ) } var ( diff --git a/x/multi-staking/keeper/proposal_handler.go b/x/multi-staking/keeper/proposal_handler.go index 846c5535..a59723fe 100644 --- a/x/multi-staking/keeper/proposal_handler.go +++ b/x/multi-staking/keeper/proposal_handler.go @@ -1,7 +1,6 @@ package keeper import ( - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/realio-tech/multi-staking-module/x/multi-staking/types" @@ -10,7 +9,7 @@ import ( func HandlerAddBondDenomProposal(ctx sdk.Context, k *Keeper, p *types.AddBondDenomProposal) error { _, found := k.GetBondTokenWeight(ctx, p.BondTokenAdd) if found { - return fmt.Errorf("denom %s already exists", p.BondTokenAdd) + return types.ErrBrondDenomAlreadyExists } k.SetBondTokenWeight(ctx, p.BondTokenAdd, *p.BondTokenWeightAdd) @@ -20,7 +19,7 @@ func HandlerAddBondDenomProposal(ctx sdk.Context, k *Keeper, p *types.AddBondDen func HandlerUpdateBondTokenWeightProposals(ctx sdk.Context, k *Keeper, p *types.UpdateBondTokenWeightProposals) error { _, found := k.GetBondTokenWeight(ctx, p.BondDenomChange) if !found { - return fmt.Errorf("denom %s does not exist", p.BondDenomChange) + return types.ErrBrondDenomDoesNotExist } k.RemoveBondTokenWeight(ctx, p.BondDenomChange) diff --git a/x/multi-staking/keeper/unbonding.go b/x/multi-staking/keeper/unbonding.go index f0dd7f0c..900b7c5e 100644 --- a/x/multi-staking/keeper/unbonding.go +++ b/x/multi-staking/keeper/unbonding.go @@ -1,8 +1,6 @@ package keeper import ( - "fmt" - "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -22,7 +20,7 @@ func (k Keeper) CompleteUnbonding( // get unbonded record ubd, found := k.GetMultiStakingUnlock(ctx, delAddr, valAddr) if !found { - return unlockedAmount, fmt.Errorf("unbonded record not exists") + return unlockedAmount, types.ErrRecordNotExists } var ( unbondEntry types.UnlockEntry @@ -45,7 +43,7 @@ func (k Keeper) CompleteUnbonding( // check amount if unlockMultiStakingAmount.GT(unbondEntry.Balance) { - return unlockedAmount, fmt.Errorf("unlock amount greater than lock amount") + return unlockedAmount, types.ErrCheckInsufficientAmount } // burn bonded token diff --git a/x/multi-staking/types/errors.go b/x/multi-staking/types/errors.go index 1e8822c4..472b3911 100644 --- a/x/multi-staking/types/errors.go +++ b/x/multi-staking/types/errors.go @@ -6,6 +6,40 @@ import ( // x/multistaking module sentinel errors var ( - ErrInvalidAddBondTokenProposal = sdkerrors.Register(ModuleName, 2, "invalid add bond token proposal") - ErrInvalidChangeBondTokenWeightProposal = sdkerrors.Register(ModuleName, 3, "invalid change bond token weight proposal") -) \ No newline at end of file + ErrInvalidAddBondTokenProposal = sdkerrors.Register(ModuleName, 2, "invalid add bond token proposal") + ErrInvalidChangeBondTokenWeightProposal = sdkerrors.Register(ModuleName, 3, "invalid change bond token weight proposal") + + ErrNotFoundMultiStaking = sdkerrors.Register(ModuleName, 4, + "can't find multi staking", + ) + ErrStakingNotExitsts = sdkerrors.Register(ModuleName, 5, + "StakingLock not exists", + ) + ErrCheckInsufficientAmount = sdkerrors.Register(ModuleName, 6, + "unlock amount greater than lock amount", + ) + ErrRecordNotExists = sdkerrors.Register(ModuleName, 7, + "record not exists", + ) + ErrLessThanZero = sdkerrors.Register(ModuleName, 8, + "cannot be less than 0", + ) + ErrNotAllowedToken = sdkerrors.Register(ModuleName, 9, + "not allowed token", + ) + ErrDelegationNotFound = sdkerrors.Register(ModuleName, 10, + "delegation not found", + ) + ErrValidatorNotFound = sdkerrors.Register(ModuleName, 10, + "validator not found", + ) + ErrUnrecognized = sdkerrors.Register(ModuleName, 11, + "unrecognized brond denom proposal content type", + ) + ErrBrondDenomAlreadyExists = sdkerrors.Register(ModuleName, 12, + "brond denom already exists", + ) + ErrBrondDenomDoesNotExist = sdkerrors.Register(ModuleName, 13, + "brond denom does not exist", + ) +) diff --git a/x/multi-staking/types/gov_add_bond_denom.go b/x/multi-staking/types/gov_add_bond_denom.go index b0ac35f3..65b277f8 100644 --- a/x/multi-staking/types/gov_add_bond_denom.go +++ b/x/multi-staking/types/gov_add_bond_denom.go @@ -1,7 +1,6 @@ package types import ( - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" ) @@ -40,11 +39,11 @@ func (p *AddBondDenomProposal) ValidateBasic() error { } if p.BondTokenAdd == "" { - return fmt.Errorf("denom %s does not exist", p.BondTokenAdd) + return ErrBrondDenomDoesNotExist } if p.BondTokenWeightAdd.LT(sdk.ZeroDec()) { - return fmt.Errorf("BondTokenWeight cannot be less than 0") + return ErrLessThanZero } return nil diff --git a/x/multi-staking/types/gov_remove_bond_denom.go b/x/multi-staking/types/gov_remove_bond_denom.go index 4a53c92f..f334bade 100644 --- a/x/multi-staking/types/gov_remove_bond_denom.go +++ b/x/multi-staking/types/gov_remove_bond_denom.go @@ -1,7 +1,6 @@ package types import ( - "fmt" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" ) @@ -38,7 +37,7 @@ func (p *RemoveBondTokenProposal) ValidateBasic() error { } if p.BondTokenRemove == "" { - return fmt.Errorf("denom %s does not exist", p.BondTokenRemove) + return ErrBrondDenomDoesNotExist } return nil diff --git a/x/multi-staking/types/gov_update_bond_denom.go b/x/multi-staking/types/gov_update_bond_denom.go index 75ccf5a4..7c385b9d 100644 --- a/x/multi-staking/types/gov_update_bond_denom.go +++ b/x/multi-staking/types/gov_update_bond_denom.go @@ -1,7 +1,6 @@ package types import ( - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" ) @@ -40,11 +39,11 @@ func (p *UpdateBondTokenWeightProposals) ValidateBasic() error { } if p.BondDenomChange == "" { - return fmt.Errorf("denom %s does not exist", p.BondDenomChange) + return ErrBrondDenomDoesNotExist } if p.BondTokenWeightChange.LT(sdk.ZeroDec()) { - return fmt.Errorf("BondTokenWeight cannot be less than 0") + return ErrLessThanZero } return nil diff --git a/x/multi-staking/types/lock.go b/x/multi-staking/types/lock.go index f9de4e33..84571dd2 100644 --- a/x/multi-staking/types/lock.go +++ b/x/multi-staking/types/lock.go @@ -1,8 +1,6 @@ package types import ( - "fmt" - "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -18,7 +16,7 @@ func NewMultiStakingLock(lockedAmount math.Int, conversionRatio sdk.Dec, delAddr func (lock MultiStakingLock) RemoveTokenFromMultiStakingLock(removedAmount math.Int) (MultiStakingLock, error) { if removedAmount.GT(lock.LockedAmount) { - return MultiStakingLock{}, fmt.Errorf("removed amount greater than existing amount in lock") + return MultiStakingLock{}, ErrCheckInsufficientAmount.Wrap("removed amount greater than existing amount in lock") } lock.LockedAmount = lock.LockedAmount.Sub(removedAmount)