Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register error #50

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions x/multi-staking/handler.go
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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
}
}
}
10 changes: 4 additions & 6 deletions x/multi-staking/keeper/lock.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper

import (
"fmt"

"cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 9 additions & 8 deletions x/multi-staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"
"fmt"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 (
Expand Down
5 changes: 2 additions & 3 deletions x/multi-staking/keeper/proposal_handler.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand All @@ -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)

Expand Down
6 changes: 2 additions & 4 deletions x/multi-staking/keeper/unbonding.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down
40 changes: 37 additions & 3 deletions x/multi-staking/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
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",
)
)
5 changes: 2 additions & 3 deletions x/multi-staking/types/gov_add_bond_denom.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions x/multi-staking/types/gov_remove_bond_denom.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"fmt"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)

Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions x/multi-staking/types/gov_update_bond_denom.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions x/multi-staking/types/lock.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"fmt"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -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)
Expand Down