From ba89120f38c59a32eb61c2e71ceebdfaa1e4c39f Mon Sep 17 00:00:00 2001 From: tnv1 Date: Thu, 11 Jan 2024 15:59:51 +0700 Subject: [PATCH] Add error handling and some refactor. --- x/feeabs/keeper/config.go | 5 ++++- x/feeabs/keeper/exchange_rate.go | 7 +++++-- x/feeabs/keeper/keeper.go | 13 +++++++------ x/feeabs/keeper/msgserver.go | 2 +- x/feeabs/types/build_memo.go | 6 +++--- x/feeabs/types/build_memo_test.go | 2 +- x/feeabs/types/epoch.go | 8 ++++---- x/feeabs/types/errors.go | 7 ++++--- x/feeabs/types/genesis.go | 2 +- x/feeabs/types/msg.go | 8 +++----- x/feeabs/types/params.go | 12 ++++++------ 11 files changed, 39 insertions(+), 33 deletions(-) diff --git a/x/feeabs/keeper/config.go b/x/feeabs/keeper/config.go index 13a7a49d..6b69f83e 100644 --- a/x/feeabs/keeper/config.go +++ b/x/feeabs/keeper/config.go @@ -59,7 +59,10 @@ func (k Keeper) SetHostZoneConfig(ctx sdk.Context, chainConfig types.HostChainFe } func (k Keeper) DeleteHostZoneConfig(ctx sdk.Context, ibcDenom string) error { - hostZoneConfig, _ := k.GetHostZoneConfig(ctx, ibcDenom) + hostZoneConfig, ok := k.GetHostZoneConfig(ctx, ibcDenom) + if !ok { + return types.ErrHostZoneConfigNotFound + } store := ctx.KVStore(k.storeKey) key := types.GetKeyHostZoneConfigByFeeabsIBCDenom(ibcDenom) diff --git a/x/feeabs/keeper/exchange_rate.go b/x/feeabs/keeper/exchange_rate.go index 6a26c67e..72e84a69 100644 --- a/x/feeabs/keeper/exchange_rate.go +++ b/x/feeabs/keeper/exchange_rate.go @@ -14,7 +14,7 @@ func (k Keeper) GetTwapRate(ctx sdk.Context, ibcDenom string) (sdk.Dec, error) { key := types.GetKeyTwapExchangeRate(ibcDenom) bz := store.Get(key) if bz == nil { - return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrInvalidExchangeRate, "Osmosis does not have exchange rate data") + return sdk.Dec{}, sdkerrors.Wrapf(types.ErrInvalidExchangeRate, "Osmosis does not have exchange rate data") } var osmosisExchangeRate sdk.Dec @@ -27,7 +27,10 @@ func (k Keeper) GetTwapRate(ctx sdk.Context, ibcDenom string) (sdk.Dec, error) { func (k Keeper) SetTwapRate(ctx sdk.Context, ibcDenom string, osmosisTWAPExchangeRate sdk.Dec) { store := ctx.KVStore(k.storeKey) - bz, _ := osmosisTWAPExchangeRate.Marshal() + bz, err := osmosisTWAPExchangeRate.Marshal() + if err != nil { + panic(err) + } key := types.GetKeyTwapExchangeRate(ibcDenom) store.Set(key, bz) } diff --git a/x/feeabs/keeper/keeper.go b/x/feeabs/keeper/keeper.go index eebe9a5f..3cc741ab 100644 --- a/x/feeabs/keeper/keeper.go +++ b/x/feeabs/keeper/keeper.go @@ -3,6 +3,7 @@ package keeper import ( "fmt" + sdkerrors "cosmossdk.io/errors" ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper" "github.com/cosmos/cosmos-sdk/codec" @@ -80,12 +81,12 @@ func (k Keeper) GetDefaultBondDenom(ctx sdk.Context) string { func (k Keeper) CalculateNativeFromIBCCoins(ctx sdk.Context, ibcCoins sdk.Coins, chainConfig types.HostChainFeeAbsConfig) (coins sdk.Coins, err error) { err = k.verifyIBCCoins(ctx, ibcCoins) if err != nil { - return sdk.Coins{}, nil + return sdk.Coins{}, err } twapRate, err := k.GetTwapRate(ctx, chainConfig.IbcDenom) if err != nil { - return sdk.Coins{}, nil + return sdk.Coins{}, err } // mul @@ -111,11 +112,11 @@ func (k Keeper) verifyIBCCoins(ctx sdk.Context, ibcCoins sdk.Coins) error { return types.ErrInvalidIBCFees } - if k.HasHostZoneConfig(ctx, ibcCoins[0].Denom) { + ibcDenom := ibcCoins[0].Denom + if k.HasHostZoneConfig(ctx, ibcDenom) { return nil } - // TODO: we should register error for this - return fmt.Errorf("unallowed %s for tx fee", ibcCoins[0].Denom) + return sdkerrors.Wrapf(types.ErrUnsupportedDenom, "unsupported denom: %s", ibcDenom) } func (Keeper) Logger(ctx sdk.Context) log.Logger { @@ -136,7 +137,7 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { func (k Keeper) GetCapability(ctx sdk.Context, name string) *capabilitytypes.Capability { capability, ok := k.scopedKeeper.GetCapability(ctx, name) if !ok { - k.Logger(ctx).Error("Error ErrChannelCapabilityNotFound ") + k.Logger(ctx).Error(fmt.Sprintf("not found capability with given name: %s", name)) return nil } return capability diff --git a/x/feeabs/keeper/msgserver.go b/x/feeabs/keeper/msgserver.go index 0b8af384..08f92489 100644 --- a/x/feeabs/keeper/msgserver.go +++ b/x/feeabs/keeper/msgserver.go @@ -40,7 +40,7 @@ func (k Keeper) SwapCrossChain(goCtx context.Context, msg *types.MsgSwapCrossCha ctx := sdk.UnwrapSDKContext(goCtx) hostChainConfig, found := k.GetHostZoneConfig(ctx, msg.IbcDenom) if !found { - return &types.MsgSwapCrossChainResponse{}, types.ErrHostZoneConfigNotFound + return nil, types.ErrHostZoneConfigNotFound } _, err := sdk.AccAddressFromBech32(msg.FromAddress) if err != nil { diff --git a/x/feeabs/types/build_memo.go b/x/feeabs/types/build_memo.go index 6d452598..c4ebab4b 100644 --- a/x/feeabs/types/build_memo.go +++ b/x/feeabs/types/build_memo.go @@ -14,7 +14,7 @@ type OsmosisSwapMsg struct { OsmosisSwap Swap `json:"osmosis_swap"` } type Swap struct { - OutPutDenom string `json:"output_denom"` + OutputDenom string `json:"output_denom"` Slippage Twap `json:"slippage"` Receiver string `json:"receiver"` OnFailedDelivery string `json:"on_failed_delivery"` @@ -46,7 +46,7 @@ type ForwardMetadata struct { func NewOsmosisSwapMsg(outputDenom string, slippagePercentage string, windowSeconds uint64, receiver string) OsmosisSwapMsg { swap := Swap{ - OutPutDenom: outputDenom, + OutputDenom: outputDenom, Slippage: Twap{ Twap: TwapRouter{ SlippagePercentage: slippagePercentage, @@ -83,7 +83,7 @@ func ParseMsgToMemo(msg OsmosisSwapMsg, contractAddr string) (string, error) { func BuildCrossChainSwapMemo(outputDenom string, contractAddress string, receiverAddress string, chainName string) (string, error) { receiver := fmt.Sprintf("%s/%s", chainName, receiverAddress) swap := Swap{ - OutPutDenom: outputDenom, + OutputDenom: outputDenom, Slippage: Twap{ Twap: TwapRouter{ SlippagePercentage: "20", diff --git a/x/feeabs/types/build_memo_test.go b/x/feeabs/types/build_memo_test.go index 0ec42698..29a7284d 100644 --- a/x/feeabs/types/build_memo_test.go +++ b/x/feeabs/types/build_memo_test.go @@ -16,7 +16,7 @@ func TestParseMsgToMemo(t *testing.T) { } swap := types.Swap{ - OutPutDenom: "khanhyeuchau", + OutputDenom: "khanhyeuchau", Slippage: types.Twap{Twap: twapRouter}, Receiver: "123456", } diff --git a/x/feeabs/types/epoch.go b/x/feeabs/types/epoch.go index 2c99f6cf..d4b28bc4 100644 --- a/x/feeabs/types/epoch.go +++ b/x/feeabs/types/epoch.go @@ -6,10 +6,10 @@ import ( ) const ( - DefaultSwapPeriod time.Duration = time.Minute * 180 - DefaultQueryPeriod time.Duration = time.Minute * 60 - DefaultSwapEpochIdentifier string = "swap" - DefaultQueryEpochIdentifier string = "query" + DefaultSwapPeriod = time.Minute * 180 + DefaultQueryPeriod = time.Minute * 60 + DefaultSwapEpochIdentifier = "swap" + DefaultQueryEpochIdentifier = "query" ) func KeyPrefix(p string) []byte { diff --git a/x/feeabs/types/errors.go b/x/feeabs/types/errors.go index d941329b..cd456af4 100644 --- a/x/feeabs/types/errors.go +++ b/x/feeabs/types/errors.go @@ -7,7 +7,8 @@ import ( var ( ErrInvalidExchangeRate = sdkerrors.Register(ModuleName, 1, "invalid exchange rate") ErrInvalidIBCFees = sdkerrors.Register(ModuleName, 2, "invalid ibc fees") - ErrHostZoneConfigNotFound = sdkerrors.Register(ModuleName, 3, "host chain config not found") - ErrDuplicateHostZoneConfig = sdkerrors.Register(ModuleName, 4, "duplicate config") - ErrNotEnoughFundInModuleAddress = sdkerrors.Register(ModuleName, 6, "not have funding yet") + ErrHostZoneConfigNotFound = sdkerrors.Register(ModuleName, 3, "host zone config not found") + ErrDuplicateHostZoneConfig = sdkerrors.Register(ModuleName, 4, "duplicate host zone config") + ErrNotEnoughFundInModuleAddress = sdkerrors.Register(ModuleName, 5, "not have funding yet") + ErrUnsupportedDenom = sdkerrors.Register(ModuleName, 6, "unsupported denom") ) diff --git a/x/feeabs/types/genesis.go b/x/feeabs/types/genesis.go index 08f3e50c..df33c685 100644 --- a/x/feeabs/types/genesis.go +++ b/x/feeabs/types/genesis.go @@ -1,6 +1,6 @@ package types -import fmt "fmt" +import "fmt" // DefaultGenesis returns the incentive module's default genesis state. func DefaultGenesis() *GenesisState { diff --git a/x/feeabs/types/msg.go b/x/feeabs/types/msg.go index d303a3a1..36b034a9 100644 --- a/x/feeabs/types/msg.go +++ b/x/feeabs/types/msg.go @@ -1,8 +1,6 @@ package types import ( - sdkerrors "cosmossdk.io/errors" - "github.com/cosmos/cosmos-sdk/codec/legacy" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -33,7 +31,7 @@ func (m MsgSendQueryIbcDenomTWAP) GetSignBytes() []byte { func (m MsgSendQueryIbcDenomTWAP) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { - return sdkerrors.Wrap(err, "from address must be valid address") + return err } return nil } @@ -70,7 +68,7 @@ func (m MsgSwapCrossChain) GetSignBytes() []byte { func (m MsgSwapCrossChain) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { - return sdkerrors.Wrap(err, "from address must be valid address") + return err } return nil } @@ -108,7 +106,7 @@ func (m MsgFundFeeAbsModuleAccount) GetSignBytes() []byte { func (m MsgFundFeeAbsModuleAccount) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(m.FromAddress) if err != nil { - return sdkerrors.Wrap(err, "from address must be valid address") + return err } return nil } diff --git a/x/feeabs/types/params.go b/x/feeabs/types/params.go index c5f37c96..3c85733c 100644 --- a/x/feeabs/types/params.go +++ b/x/feeabs/types/params.go @@ -15,12 +15,12 @@ const ( // Parameter keys store keys. var ( - KeyOsmosisQueryTwapPath = []byte("osmosisquerytwappath") - KeyNativeIbcedInOsmosis = []byte("nativeibcedinosmosis") - KeyChainName = []byte("chainname") - KeyIbcTransferChannel = []byte("ibctransferchannel") - KeyIbcQueryIcqChannel = []byte("ibcqueryicqchannel") - KeyOsmosisCrosschainSwapAddress = []byte("osmosiscrosschainswapaddress") + KeyOsmosisQueryTwapPath = []byte("OsmosisQueryTwapPath") + KeyNativeIbcedInOsmosis = []byte("NativeIbcedInOsmosis") + KeyChainName = []byte("ChainName") + KeyIbcTransferChannel = []byte("IbcTransferChannel") + KeyIbcQueryIcqChannel = []byte("IbcQueryIcqChannel") + KeyOsmosisCrosschainSwapAddress = []byte("OsmosisCrosschainSwapAddress") _ paramtypes.ParamSet = &Params{} )