diff --git a/x/token/class/errors.go b/x/token/class/errors.go deleted file mode 100644 index abf9df9602..0000000000 --- a/x/token/class/errors.go +++ /dev/null @@ -1,12 +0,0 @@ -package class - -import ( - sdkerrors "github.com/line/lbm-sdk/types/errors" -) - -const contractCodespace = "contract" - -var ( - ErrInvalidContractID = sdkerrors.Register(contractCodespace, 2, "invalid contractID") - ErrContractNotExist = sdkerrors.Register(contractCodespace, 3, "contract does not exist") -) diff --git a/x/token/class/validation.go b/x/token/class/validation.go index 98bee82d08..073c6e4b79 100644 --- a/x/token/class/validation.go +++ b/x/token/class/validation.go @@ -3,6 +3,8 @@ package class import ( "fmt" "regexp" + + sdkerrors "github.com/line/lbm-sdk/types/errors" ) var ( @@ -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 } diff --git a/x/token/errors.go b/x/token/errors.go index a86e5fb8eb..cc285c4872 100644 --- a/x/token/errors.go +++ b/x/token/errors.go @@ -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") ) diff --git a/x/token/keeper/msg_server.go b/x/token/keeper/msg_server.go index 3d7213cfba..638e407df1 100644 --- a/x/token/keeper/msg_server.go +++ b/x/token/keeper/msg_server.go @@ -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" ) @@ -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 { @@ -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) @@ -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) @@ -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 { diff --git a/x/token/keeper/send.go b/x/token/keeper/send.go index ede12e8e33..f3a3b95633 100644 --- a/x/token/keeper/send.go +++ b/x/token/keeper/send.go @@ -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 { @@ -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) @@ -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) { @@ -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) diff --git a/x/token/keeper/supply.go b/x/token/keeper/supply.go index 64e2521fc5..1fcd463241 100644 --- a/x/token/keeper/supply.go +++ b/x/token/keeper/supply.go @@ -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 @@ -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) @@ -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 { @@ -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 { @@ -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) { diff --git a/x/token/msgs.go b/x/token/msgs.go index 6e1fab6f77..a04d9fc647 100644 --- a/x/token/msgs.go +++ b/x/token/msgs.go @@ -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 } @@ -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 @@ -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 diff --git a/x/token/validation.go b/x/token/validation.go index 258c8bc2fb..84f8fa2a95 100644 --- a/x/token/validation.go +++ b/x/token/validation.go @@ -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" ) @@ -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 } @@ -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 } @@ -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 }