Skip to content

Commit

Permalink
Refactor x/token error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
0Tech committed Dec 2, 2022
1 parent c5c8aee commit 47d1e38
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 69 deletions.
12 changes: 0 additions & 12 deletions x/token/class/errors.go

This file was deleted.

4 changes: 3 additions & 1 deletion x/token/class/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package class
import (
"fmt"
"regexp"

sdkerrors "github.com/line/lbm-sdk/types/errors"
)

var (
Expand All @@ -14,7 +16,7 @@ var (
// ValidateID returns whether the contract id is valid
func ValidateID(id string) error {
if !reContractID.MatchString(id) {
return ErrInvalidContractID.Wrapf("invalid contract id: %s", id)
return sdkerrors.ErrInvalidRequest.Wrapf("invalid contract id; %s", id)
}
return nil
}
41 changes: 17 additions & 24 deletions x/token/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,21 @@ import (
const tokenCodespace = ModuleName

var (
ErrTokenExist = sdkerrors.Register(tokenCodespace, 2, "token already exists")
ErrTokenNotExist = sdkerrors.Register(tokenCodespace, 3, "token does not exist")
ErrTokenNotMintable = sdkerrors.Register(tokenCodespace, 4, "token is not mintable")
ErrInvalidTokenName = sdkerrors.Register(tokenCodespace, 5, "token name should not be empty")
ErrInvalidTokenDecimals = sdkerrors.Register(tokenCodespace, 6, "token decimals should be within the range in 0 ~ 18")
ErrInvalidAmount = sdkerrors.Register(tokenCodespace, 7, "invalid token amount")
ErrInvalidImageURILength = sdkerrors.Register(tokenCodespace, 8, "invalid token uri length")
ErrInvalidNameLength = sdkerrors.Register(tokenCodespace, 9, "invalid name length")
ErrInvalidTokenSymbol = sdkerrors.Register(tokenCodespace, 10, "invalid token symbol")
ErrTokenNoPermission = sdkerrors.Register(tokenCodespace, 11, "account does not have the permission")
ErrAccountExist = sdkerrors.Register(tokenCodespace, 12, "account already exists")
ErrAccountNotExist = sdkerrors.Register(tokenCodespace, 13, "account does not exists")
ErrInsufficientBalance = sdkerrors.Register(tokenCodespace, 14, "insufficient balance")
ErrSupplyExist = sdkerrors.Register(tokenCodespace, 15, "supply for token already exists")
ErrInsufficientSupply = sdkerrors.Register(tokenCodespace, 16, "insufficient supply")
ErrInvalidChangesFieldCount = sdkerrors.Register(tokenCodespace, 17, "invalid count of field changes")
ErrEmptyChanges = sdkerrors.Register(tokenCodespace, 18, "changes is empty")
ErrInvalidChangesField = sdkerrors.Register(tokenCodespace, 19, "invalid field of changes")
ErrDuplicateChangesField = sdkerrors.Register(tokenCodespace, 20, "invalid field of changes")
ErrInvalidMetaLength = sdkerrors.Register(tokenCodespace, 21, "invalid meta length")
ErrSupplyOverflow = sdkerrors.Register(tokenCodespace, 22, "supply for token reached maximum")
ErrApproverProxySame = sdkerrors.Register(tokenCodespace, 23, "approver is same with proxy")
ErrTokenNotApproved = sdkerrors.Register(tokenCodespace, 24, "proxy is not approved on the token")
ErrTokenAlreadyApproved = sdkerrors.Register(tokenCodespace, 25, "proxy is already approved on the token")
ErrContractNotFound = sdkerrors.Register(tokenCodespace, 3, "contract not found")
ErrNotMintable = sdkerrors.Register(tokenCodespace, 4, "not mintable")
ErrInvalidDecimals = sdkerrors.Register(tokenCodespace, 6, "invalid decimals")
ErrInvalidAmount = sdkerrors.Register(tokenCodespace, 7, "invalid amount")
ErrInvalidImageURI = sdkerrors.Register(tokenCodespace, 8, "invalid image_uri")
ErrInvalidName = sdkerrors.Register(tokenCodespace, 9, "invalid name")
ErrInvalidSymbol = sdkerrors.Register(tokenCodespace, 10, "invalid symbol")
ErrGrantNotFound = sdkerrors.Register(tokenCodespace, 11, "grant not found")
ErrInsufficientTokens = sdkerrors.Register(tokenCodespace, 14, "insufficient tokens")
ErrInvalidChanges = sdkerrors.Register(tokenCodespace, 17, "invalid changes")
ErrInvalidMeta = sdkerrors.Register(tokenCodespace, 21, "invalid meta")
ErrOperatorIsHolder = sdkerrors.Register(tokenCodespace, 23, "operator and holder should be different")
ErrAuthorizationNotFound = sdkerrors.Register(tokenCodespace, 24, "authorization not found")
ErrAuthorizationAlreadyExists = sdkerrors.Register(tokenCodespace, 25, "authorization already exists")
ErrInvalidPermission = sdkerrors.Register(tokenCodespace, 26, "invalid permission")
ErrGrantAlreadyExists = sdkerrors.Register(tokenCodespace, 27, "grant already exists")
ErrInvalidContractID = sdkerrors.Register(tokenCodespace, 28, "invalid contract id")
)
11 changes: 5 additions & 6 deletions x/token/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

sdk "github.com/line/lbm-sdk/types"
sdkerrors "github.com/line/lbm-sdk/types/errors"
"github.com/line/lbm-sdk/x/token"
)

Expand Down Expand Up @@ -57,7 +56,7 @@ func (s msgServer) TransferFrom(c context.Context, req *token.MsgTransferFrom) (
to := sdk.MustAccAddressFromBech32(req.To)

if _, err := s.keeper.GetAuthorization(ctx, req.ContractId, from, proxy); err != nil {
return nil, token.ErrTokenNotApproved.Wrap(err.Error())
return nil, err
}

if err := s.keeper.Send(ctx, req.ContractId, from, to, req.Amount); err != nil {
Expand Down Expand Up @@ -154,10 +153,10 @@ func (s msgServer) GrantPermission(c context.Context, req *token.MsgGrantPermiss
grantee := sdk.MustAccAddressFromBech32(req.To)
permission := token.Permission(token.LegacyPermissionFromString(req.Permission))
if _, err := s.keeper.GetGrant(ctx, req.ContractId, granter, permission); err != nil {
return nil, token.ErrTokenNoPermission.Wrap(err.Error())
return nil, err
}
if _, err := s.keeper.GetGrant(ctx, req.ContractId, grantee, permission); err == nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("%s is already granted for %s", grantee, permission)
return nil, token.ErrGrantAlreadyExists.Wrapf("%s already granted for %s", grantee, permission)
}

s.keeper.Grant(ctx, req.ContractId, granter, grantee, permission)
Expand All @@ -180,7 +179,7 @@ func (s msgServer) RevokePermission(c context.Context, req *token.MsgRevokePermi
grantee := sdk.MustAccAddressFromBech32(req.From)
permission := token.Permission(token.LegacyPermissionFromString(req.Permission))
if _, err := s.keeper.GetGrant(ctx, req.ContractId, grantee, permission); err != nil {
return nil, token.ErrTokenNoPermission.Wrap(err.Error())
return nil, err
}

s.keeper.Abandon(ctx, req.ContractId, grantee, permission)
Expand Down Expand Up @@ -242,7 +241,7 @@ func (s msgServer) Modify(c context.Context, req *token.MsgModify) (*token.MsgMo
grantee := sdk.MustAccAddressFromBech32(req.Owner)

if _, err := s.keeper.GetGrant(ctx, req.ContractId, grantee, token.PermissionModify); err != nil {
return nil, token.ErrTokenNoPermission.Wrap(err.Error())
return nil, err
}

if err := s.keeper.Modify(ctx, req.ContractId, grantee, req.Changes); err != nil {
Expand Down
10 changes: 4 additions & 6 deletions x/token/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package keeper

import (
sdk "github.com/line/lbm-sdk/types"
sdkerrors "github.com/line/lbm-sdk/types/errors"
"github.com/line/lbm-sdk/x/token"
)

func (k Keeper) Send(ctx sdk.Context, contractID string, from, to sdk.AccAddress, amount sdk.Int) error {
if !amount.IsPositive() {
panic(sdkerrors.ErrInvalidRequest.Wrap("amount must be positive"))
panic(token.ErrInvalidAmount.Wrap("amount must be positive"))
}

if err := k.subtractToken(ctx, contractID, from, amount); err != nil {
Expand All @@ -24,7 +23,7 @@ func (k Keeper) AuthorizeOperator(ctx sdk.Context, contractID string, holder, op
return err
}
if _, err := k.GetAuthorization(ctx, contractID, holder, operator); err == nil {
return token.ErrTokenAlreadyApproved.Wrap("Already authorized")
return token.ErrAuthorizationAlreadyExists.Wrapf("%s already authorized by %s", operator, holder)
}

k.setAuthorization(ctx, contractID, holder, operator)
Expand Down Expand Up @@ -56,7 +55,7 @@ func (k Keeper) GetAuthorization(ctx sdk.Context, contractID string, holder, ope
Operator: operator.String(),
}, nil
}
return nil, sdkerrors.ErrNotFound.Wrapf("no authorization to %s by %s", operator, holder)
return nil, token.ErrAuthorizationNotFound.Wrapf("%s not authorized by %s", operator, holder)
}

func (k Keeper) setAuthorization(ctx sdk.Context, contractID string, holder, operator sdk.AccAddress) {
Expand All @@ -75,8 +74,7 @@ func (k Keeper) subtractToken(ctx sdk.Context, contractID string, addr sdk.AccAd
balance := k.GetBalance(ctx, contractID, addr)
newBalance := balance.Sub(amount)
if newBalance.IsNegative() {
// Daphne emits ErrInsufficientFunds here, which is against to the spec.
return token.ErrInsufficientBalance.Wrapf("%s is smaller than %s", balance, amount)
return token.ErrInsufficientTokens.Wrapf("%s is smaller than %s", balance, amount)
}

k.setBalance(ctx, contractID, addr, newBalance)
Expand Down
12 changes: 6 additions & 6 deletions x/token/keeper/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (k Keeper) GetClass(ctx sdk.Context, contractID string) (*token.TokenClass,
store := ctx.KVStore(k.storeKey)
bz := store.Get(classKey(contractID))
if bz == nil {
return nil, token.ErrTokenNotExist.Wrapf("no class for %s", contractID)
return nil, token.ErrContractNotFound.Wrap(contractID)
}

var class token.TokenClass
Expand Down Expand Up @@ -110,7 +110,7 @@ func (k Keeper) Mint(ctx sdk.Context, contractID string, grantee, to sdk.AccAddr

func (k Keeper) mint(ctx sdk.Context, contractID string, grantee, to sdk.AccAddress, amount sdk.Int) error {
if _, err := k.GetGrant(ctx, contractID, grantee, token.PermissionMint); err != nil {
return token.ErrTokenNoPermission.Wrap(err.Error())
return err
}

k.mintToken(ctx, contractID, to, amount)
Expand Down Expand Up @@ -150,7 +150,7 @@ func (k Keeper) Burn(ctx sdk.Context, contractID string, from sdk.AccAddress, am

func (k Keeper) burn(ctx sdk.Context, contractID string, from sdk.AccAddress, amount sdk.Int) error {
if _, err := k.GetGrant(ctx, contractID, from, token.PermissionBurn); err != nil {
return token.ErrTokenNoPermission.Wrap(err.Error())
return err
}

if err := k.burnToken(ctx, contractID, from, amount); err != nil {
Expand Down Expand Up @@ -181,10 +181,10 @@ func (k Keeper) OperatorBurn(ctx sdk.Context, contractID string, operator, from
func (k Keeper) operatorBurn(ctx sdk.Context, contractID string, operator, from sdk.AccAddress, amount sdk.Int) error {
_, err := k.GetGrant(ctx, contractID, operator, token.PermissionBurn)
if err != nil {
return token.ErrTokenNoPermission.Wrap(err.Error())
return err
}
if _, err := k.GetAuthorization(ctx, contractID, from, operator); err != nil {
return token.ErrTokenNotApproved.Wrap(err.Error())
return err
}

if err := k.burnToken(ctx, contractID, from, amount); err != nil {
Expand Down Expand Up @@ -354,7 +354,7 @@ func (k Keeper) GetGrant(ctx sdk.Context, contractID string, grantee sdk.AccAddr
return grant, nil
}

return nil, sdkerrors.ErrNotFound.Wrapf("%s has no %s permission", grantee, permission)
return nil, token.ErrGrantNotFound.Wrapf("%s has no %s", grantee, permission)
}

func (k Keeper) setGrant(ctx sdk.Context, contractID string, grantee sdk.AccAddress, permission token.Permission) {
Expand Down
5 changes: 2 additions & 3 deletions x/token/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func (m MsgSend) ValidateBasic() error {
}

if err := validateAmount(m.Amount); err != nil {
// Daphne emits ErrInvalidCoins here, which is against to the spec.
return err
}

Expand Down Expand Up @@ -455,7 +454,7 @@ func (m MsgModify) ValidateBasic() error {
checkedFields := map[string]bool{}
for _, change := range m.Changes {
if checkedFields[change.Field] {
return ErrDuplicateChangesField.Wrapf("duplicate fields: %s", change.Field)
return ErrInvalidChanges.Wrapf("duplicate key: %s", change.Field)
}
checkedFields[change.Field] = true

Expand All @@ -464,7 +463,7 @@ func (m MsgModify) ValidateBasic() error {
}
}
if len(checkedFields) == 0 {
return ErrEmptyChanges.Wrapf("no field provided")
return ErrInvalidChanges.Wrapf("no field provided")
}

return nil
Expand Down
25 changes: 14 additions & 11 deletions x/token/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"unicode/utf8"

sdk "github.com/line/lbm-sdk/types"
sdkerrors "github.com/line/lbm-sdk/types/errors"
"github.com/line/lbm-sdk/x/token/class"
)

Expand All @@ -27,44 +26,44 @@ func stringInSize(str string, size int) bool {

func validateName(name string) error {
if len(name) == 0 {
return ErrInvalidTokenName.Wrap("name cannot be empty")
return ErrInvalidName.Wrap("cannot be empty")
} else if !stringInSize(name, maxName) {
return ErrInvalidNameLength.Wrapf("name cannot be longer than %d", maxName)
return ErrInvalidName.Wrapf("cannot be longer than %d", maxName)
}
return nil
}

func validateSymbol(symbol string) error {
if !reSymbol.MatchString(symbol) {
return ErrInvalidTokenSymbol.Wrapf("invalid symbol: %s, valid expression is: %s", symbol, reSymbolString)
return ErrInvalidSymbol.Wrapf("got; %s, valid expression is; %s", symbol, reSymbolString)
}
return nil
}

func validateImageURI(uri string) error {
if !stringInSize(uri, maxImageURI) {
return ErrInvalidImageURILength.Wrapf("image_uri cannot be longer than %d", maxImageURI)
return ErrInvalidImageURI.Wrapf("cannot be longer than %d", maxImageURI)
}
return nil
}

func validateMeta(meta string) error {
if !stringInSize(meta, maxMeta) {
return ErrInvalidMetaLength.Wrapf("meta cannot be longer than %d", maxMeta)
return ErrInvalidMeta.Wrapf("cannot be longer than %d", maxMeta)
}
return nil
}

func validateDecimals(decimals int32) error {
if decimals < 0 || decimals > 18 {
return ErrInvalidTokenDecimals.Wrapf("invalid decimals: %d", decimals)
return ErrInvalidDecimals.Wrapf("must be >=0 and <18, got; %d", decimals)
}
return nil
}

func validateAmount(amount sdk.Int) error {
if !amount.IsPositive() {
return ErrInvalidAmount.Wrapf("amount must be positive: %s", amount)
return ErrInvalidAmount.Wrapf("must be positive: %s", amount)
}
return nil
}
Expand All @@ -75,7 +74,7 @@ func validateLegacyPermission(permission string) error {

func ValidatePermission(permission Permission) error {
if p := Permission_value[Permission_name[int32(permission)]]; p == 0 {
return sdkerrors.ErrInvalidPermission.Wrapf("invalid permission: %s", permission)
return ErrInvalidPermission.Wrap(permission.String())
}
return nil
}
Expand All @@ -89,11 +88,15 @@ func validateChange(change Pair) error {

validator, ok := validators[change.Field]
if !ok {
return ErrInvalidChangesField.Wrapf("invalid field: %s", change.Field)
return ErrInvalidChanges.Wrapf("invalid key: %s", change.Field)
}
return validator(change.Value)
}

func ValidateContractID(id string) error {
return class.ValidateID(id)
if err := class.ValidateID(id); err != nil {
return ErrInvalidContractID.Wrap(id)
}

return nil
}

0 comments on commit 47d1e38

Please sign in to comment.