Skip to content

Commit

Permalink
Add error handling and some refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
tnv1 committed Jan 11, 2024
1 parent 4d1abe7 commit ba89120
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 33 deletions.
5 changes: 4 additions & 1 deletion x/feeabs/keeper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions x/feeabs/keeper/exchange_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
13 changes: 7 additions & 6 deletions x/feeabs/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"fmt"

sdkerrors "cosmossdk.io/errors"

Check failure on line 6 in x/feeabs/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) -s prefix(github.com/cometbft/cometbft) -s prefix(github.com/osmosis-labs/fee-abstraction) --custom-order (gci)
ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper"

Check failure on line 8 in x/feeabs/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) -s prefix(github.com/cometbft/cometbft) -s prefix(github.com/osmosis-labs/fee-abstraction) --custom-order (gci)
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/feeabs/keeper/msgserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions x/feeabs/types/build_memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion x/feeabs/types/build_memo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestParseMsgToMemo(t *testing.T) {
}

swap := types.Swap{
OutPutDenom: "khanhyeuchau",
OutputDenom: "khanhyeuchau",
Slippage: types.Twap{Twap: twapRouter},
Receiver: "123456",
}
Expand Down
8 changes: 4 additions & 4 deletions x/feeabs/types/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions x/feeabs/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
2 changes: 1 addition & 1 deletion x/feeabs/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

import fmt "fmt"
import "fmt"

// DefaultGenesis returns the incentive module's default genesis state.
func DefaultGenesis() *GenesisState {
Expand Down
8 changes: 3 additions & 5 deletions x/feeabs/types/msg.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions x/feeabs/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
)
Expand Down

0 comments on commit ba89120

Please sign in to comment.