diff --git a/x/feeabs/ante/ante_test.go b/x/feeabs/ante/ante_test.go index 56d9222c..97421996 100644 --- a/x/feeabs/ante/ante_test.go +++ b/x/feeabs/ante/ante_test.go @@ -93,26 +93,53 @@ func TestMempoolDecorator(t *testing.T) { false, nil, }, - // TODO: Add support for multiple denom fees(--fees 50ibc,50native) - // { - // "half native fee, half ibc fee, should pass", - // sdk.NewCoins(sdk.NewInt64Coin("native", 500*int64(gasLimit)), sdk.NewInt64Coin("ibcfee", 500*int64(gasLimit))), - // sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...), - // func(suite *AnteTestSuite) { - // err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, types.HostChainFeeAbsConfig{ - // IbcDenom: "ibcfee", - // OsmosisPoolTokenDenomIn: "osmosis", - // PoolId: 1, - // Status: types.HostChainFeeAbsStatus_UPDATED, - // MinSwapAmount: 0, - // }) - // require.NoError(t, err) - // suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", math.LegacyNewDec(1)) - // suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native").MinTimes(1) - // }, - // false, - // nil, - // }, + { + "half native fee, half ibc fee, should pass", + sdk.NewCoins(sdk.NewInt64Coin("native", 500*int64(gasLimit)), sdk.NewInt64Coin("ibcfee", 500*int64(gasLimit))), + sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...), + func(suite *AnteTestSuite) { + err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, types.HostChainFeeAbsConfig{ + IbcDenom: "ibcfee", + OsmosisPoolTokenDenomIn: "osmosis", + PoolId: 1, + Status: types.HostChainFeeAbsStatus_UPDATED, + // MinSwapAmount: 0, + }) + require.NoError(t, err) + suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", math.LegacyNewDec(1)) + suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native", nil).MinTimes(1) + }, + false, + nil, + }, + { + "3/4 ibc fee, 1/4 another ibc fee, should pass", + sdk.NewCoins(sdk.NewInt64Coin("ibcfee", 750*int64(gasLimit)), sdk.NewInt64Coin("ibcfee2", 250*int64(gasLimit))), + sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...), + func(suite *AnteTestSuite) { + err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, types.HostChainFeeAbsConfig{ + IbcDenom: "ibcfee", + OsmosisPoolTokenDenomIn: "osmosis", + PoolId: 1, + Status: types.HostChainFeeAbsStatus_UPDATED, + // MinSwapAmount: 0, + }) + require.NoError(t, err) + err = suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, types.HostChainFeeAbsConfig{ + IbcDenom: "ibcfee2", + OsmosisPoolTokenDenomIn: "osmosis", + PoolId: 2, + Status: types.HostChainFeeAbsStatus_UPDATED, + // MinSwapAmount: 0, + }) + require.NoError(t, err) + suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", math.LegacyNewDec(1)) + suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee2", math.LegacyNewDec(1)) + suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native", nil).MinTimes(1) + }, + false, + nil, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/x/feeabs/ante/decorate.go b/x/feeabs/ante/decorate.go index 07bb17c8..00dd9082 100644 --- a/x/feeabs/ante/decorate.go +++ b/x/feeabs/ante/decorate.go @@ -271,28 +271,32 @@ func (famfd FeeAbstrationMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk // Check if feeDenom is defined in feeabs // If so, replace the amount of feeDenom in feeCoins with the // corresponding amount of native denom that allow to pay fee - // TODO: Support more fee token in feeRequired for fee-abstraction - if feeCoinsNonZeroDenom.Len() == 1 { - feeDenom := feeCoinsNonZeroDenom.GetDenomByIndex(0) - hostChainConfig, found := famfd.feeabsKeeper.GetHostZoneConfig(ctx, feeDenom) - if found { - if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_FROZEN { - return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneFrozen, "cannot deduct fee as host zone is frozen") + // Support more fee tokens in feeRequired for fee-abstraction + if feeCoinsNonZeroDenom.Len() > 0 { + nativeCoinsFees := sdk.NewCoins() + for _, feeCoin := range feeCoinsNonZeroDenom { + feeDenom := feeCoin.Denom + hostChainConfig, found := famfd.feeabsKeeper.GetHostZoneConfig(ctx, feeDenom) + if found { + if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_FROZEN { + return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneFrozen, "cannot deduct fee as host zone is frozen") + } + + // if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_OUTDATED { + // return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneOutdated, "cannot deduct fee as host zone is outdated") + // } + + nativeCoinFee, err := famfd.feeabsKeeper.CalculateNativeFromIBCCoins(ctx, sdk.NewCoins(feeCoin), hostChainConfig) + if err != nil { + return ctx, sdkerrors.Wrapf(errorstypes.ErrInsufficientFee, "unable to calculate native fee from ibc fee: %s", err) + } + nativeCoinsFees = nativeCoinsFees.Add(nativeCoinFee...) + } else { + nativeCoinsFees = nativeCoinsFees.Add(feeCoin) } - - // if hostChainConfig.Status == feeabstypes.HostChainFeeAbsStatus_OUTDATED { - // return ctx, sdkerrors.Wrapf(feeabstypes.ErrHostZoneOutdated, "cannot deduct fee as host zone is outdated") - // } - - nativeCoinsFees, err := famfd.feeabsKeeper.CalculateNativeFromIBCCoins(ctx, feeCoinsNonZeroDenom, hostChainConfig) - if err != nil { - return ctx, sdkerrors.Wrapf(errorstypes.ErrInsufficientFee, "unable to calculate native fees from ibc fees: %s", err) - } - fmt.Println("nativeCoinsFees", nativeCoinsFees) - feeCoinsNonZeroDenom = nativeCoinsFees } - } else if feeCoinsNonZeroDenom.Len() > 1 { - return ctx, sdkerrors.Wrapf(errorstypes.ErrNotSupported, "should have only one fee denom in feeCoinsNonZeroDenom, got %d", feeCoinsNonZeroDenom.Len()) + fmt.Println("nativeCoinsFees", nativeCoinsFees) + feeCoinsNonZeroDenom = nativeCoinsFees } // After replace the feeCoinsNonZeroDenom, feeCoinsNonZeroDenom must be in denom subset of nonZeroCoinFeesReq