|
| 1 | +package ante_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/golang/mock/gomock" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 10 | + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" |
| 11 | + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" |
| 12 | + |
| 13 | + "github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/ante" |
| 14 | + "github.com/osmosis-labs/fee-abstraction/v7/x/feeabs/types" |
| 15 | +) |
| 16 | + |
| 17 | +func TestMempoolDecorator(t *testing.T) { |
| 18 | + gasLimit := uint64(200000) |
| 19 | + // mockHostZoneConfig is used to mock the host zone config, with ibcfee as the ibc fee denom to be used as alternative fee |
| 20 | + mockHostZoneConfig := types.HostChainFeeAbsConfig{ |
| 21 | + IbcDenom: "ibcfee", |
| 22 | + OsmosisPoolTokenDenomIn: "osmosis", |
| 23 | + PoolId: 1, |
| 24 | + Status: types.HostChainFeeAbsStatus_UPDATED, |
| 25 | + MinSwapAmount: 0, |
| 26 | + } |
| 27 | + testCases := []struct { |
| 28 | + name string |
| 29 | + feeAmount sdk.Coins |
| 30 | + minGasPrice sdk.DecCoins |
| 31 | + malleate func(*AnteTestSuite) |
| 32 | + isErr bool |
| 33 | + expErr error |
| 34 | + }{ |
| 35 | + { |
| 36 | + "empty fee, should fail", |
| 37 | + sdk.Coins{}, |
| 38 | + sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 100))...), |
| 39 | + func(suite *AnteTestSuite) { |
| 40 | + }, |
| 41 | + true, |
| 42 | + sdkerrors.ErrInsufficientFee, |
| 43 | + }, |
| 44 | + { |
| 45 | + "not enough native fee, should fail", |
| 46 | + sdk.NewCoins(sdk.NewInt64Coin("native", 100)), |
| 47 | + sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...), |
| 48 | + func(suite *AnteTestSuite) {}, |
| 49 | + true, |
| 50 | + sdkerrors.ErrInsufficientFee, |
| 51 | + }, |
| 52 | + { |
| 53 | + "enough native fee, should pass", |
| 54 | + sdk.NewCoins(sdk.NewInt64Coin("native", 1000*int64(gasLimit))), |
| 55 | + sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...), |
| 56 | + func(suite *AnteTestSuite) {}, |
| 57 | + false, |
| 58 | + nil, |
| 59 | + }, |
| 60 | + { |
| 61 | + "unknown ibc fee denom, should fail", |
| 62 | + sdk.NewCoins(sdk.NewInt64Coin("ibcfee", 1000*int64(gasLimit))), |
| 63 | + sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...), |
| 64 | + func(suite *AnteTestSuite) {}, |
| 65 | + true, |
| 66 | + sdkerrors.ErrInvalidCoins, |
| 67 | + }, |
| 68 | + { |
| 69 | + "not enough ibc fee, should fail", |
| 70 | + sdk.NewCoins(sdk.NewInt64Coin("ibcfee", 999*int64(gasLimit))), |
| 71 | + sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...), |
| 72 | + func(suite *AnteTestSuite) { |
| 73 | + err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, mockHostZoneConfig) |
| 74 | + require.NoError(t, err) |
| 75 | + suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", sdk.NewDec(1)) |
| 76 | + suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native").MinTimes(1) |
| 77 | + }, |
| 78 | + true, |
| 79 | + sdkerrors.ErrInsufficientFee, |
| 80 | + }, |
| 81 | + |
| 82 | + { |
| 83 | + "enough ibc fee, should pass", |
| 84 | + sdk.NewCoins(sdk.NewInt64Coin("ibcfee", 1000*int64(gasLimit))), |
| 85 | + sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...), |
| 86 | + func(suite *AnteTestSuite) { |
| 87 | + err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, mockHostZoneConfig) |
| 88 | + require.NoError(t, err) |
| 89 | + suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", sdk.NewDec(1)) |
| 90 | + suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native").MinTimes(1) |
| 91 | + }, |
| 92 | + false, |
| 93 | + nil, |
| 94 | + }, |
| 95 | + // TODO: Add support for multiple denom fees(--fees 50ibc,50native) |
| 96 | + // { |
| 97 | + // "half native fee, half ibc fee, should pass", |
| 98 | + // sdk.NewCoins(sdk.NewInt64Coin("native", 500*int64(gasLimit)), sdk.NewInt64Coin("ibcfee", 500*int64(gasLimit))), |
| 99 | + // sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...), |
| 100 | + // func(suite *AnteTestSuite) { |
| 101 | + // err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, types.HostChainFeeAbsConfig{ |
| 102 | + // IbcDenom: "ibcfee", |
| 103 | + // OsmosisPoolTokenDenomIn: "osmosis", |
| 104 | + // PoolId: 1, |
| 105 | + // Status: types.HostChainFeeAbsStatus_UPDATED, |
| 106 | + // MinSwapAmount: 0, |
| 107 | + // }) |
| 108 | + // require.NoError(t, err) |
| 109 | + // suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", sdk.NewDec(1)) |
| 110 | + // suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native").MinTimes(1) |
| 111 | + // }, |
| 112 | + // false, |
| 113 | + // nil, |
| 114 | + // }, |
| 115 | + } |
| 116 | + for _, tc := range testCases { |
| 117 | + t.Run(tc.name, func(t *testing.T) { |
| 118 | + suite := SetupTestSuite(t, true) |
| 119 | + |
| 120 | + tc.malleate(suite) |
| 121 | + suite.txBuilder.SetGasLimit(gasLimit) |
| 122 | + suite.txBuilder.SetFeeAmount(tc.feeAmount) |
| 123 | + suite.ctx = suite.ctx.WithMinGasPrices(tc.minGasPrice) |
| 124 | + |
| 125 | + // Construct tx and run through mempool decorator |
| 126 | + tx := suite.txBuilder.GetTx() |
| 127 | + mempoolDecorator := ante.NewFeeAbstrationMempoolFeeDecorator(suite.feeabsKeeper) |
| 128 | + antehandler := sdk.ChainAnteDecorators(mempoolDecorator) |
| 129 | + |
| 130 | + // Run the ante handler |
| 131 | + _, err := antehandler(suite.ctx, tx, false) |
| 132 | + |
| 133 | + if tc.isErr { |
| 134 | + require.Error(t, err) |
| 135 | + require.ErrorIs(t, err, tc.expErr) |
| 136 | + } else { |
| 137 | + require.NoError(t, err) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestDeductFeeDecorator(t *testing.T) { |
| 144 | + gasLimit := uint64(200000) |
| 145 | + minGasPrice := sdk.NewDecCoinsFromCoins(sdk.NewCoins(sdk.NewInt64Coin("native", 1000))...) |
| 146 | + feeAmount := sdk.NewCoins(sdk.NewInt64Coin("native", 1000*int64(gasLimit))) |
| 147 | + ibcFeeAmount := sdk.NewCoins(sdk.NewInt64Coin("ibcfee", 1000*int64(gasLimit))) |
| 148 | + // mockHostZoneConfig is used to mock the host zone config, with ibcfee as the ibc fee denom to be used as alternative fee |
| 149 | + mockHostZoneConfig := types.HostChainFeeAbsConfig{ |
| 150 | + IbcDenom: "ibcfee", |
| 151 | + OsmosisPoolTokenDenomIn: "osmosis", |
| 152 | + PoolId: 1, |
| 153 | + Status: types.HostChainFeeAbsStatus_UPDATED, |
| 154 | + MinSwapAmount: 0, |
| 155 | + } |
| 156 | + testCases := []struct { |
| 157 | + name string |
| 158 | + malleate func(*AnteTestSuite) |
| 159 | + isErr bool |
| 160 | + expErr error |
| 161 | + }{ |
| 162 | + { |
| 163 | + "not enough native fee in balance, should fail", |
| 164 | + func(suite *AnteTestSuite) { |
| 165 | + suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", sdk.NewDec(1)) |
| 166 | + // suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), types.ModuleName, feeAmount).Return(sdkerrors.ErrInsufficientFee).MinTimes(1) |
| 167 | + suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), authtypes.FeeCollectorName, feeAmount).Return(sdkerrors.ErrInsufficientFee).MinTimes(1) |
| 168 | + }, |
| 169 | + true, |
| 170 | + sdkerrors.ErrInsufficientFunds, |
| 171 | + }, |
| 172 | + { |
| 173 | + "enough native fee in balance, should pass", |
| 174 | + func(suite *AnteTestSuite) { |
| 175 | + suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", sdk.NewDec(1)) |
| 176 | + suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), authtypes.FeeCollectorName, feeAmount).Return(nil).MinTimes(1) |
| 177 | + }, |
| 178 | + false, |
| 179 | + nil, |
| 180 | + }, |
| 181 | + { |
| 182 | + "not enough ibc fee in balance, should fail", |
| 183 | + func(suite *AnteTestSuite) { |
| 184 | + err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, mockHostZoneConfig) |
| 185 | + require.NoError(t, err) |
| 186 | + suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", sdk.NewDec(1)) |
| 187 | + suite.txBuilder.SetFeeAmount(ibcFeeAmount) |
| 188 | + suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native").MinTimes(1) |
| 189 | + suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), types.ModuleName, ibcFeeAmount).Return(sdkerrors.ErrInsufficientFunds).MinTimes(1) |
| 190 | + }, |
| 191 | + true, |
| 192 | + sdkerrors.ErrInsufficientFunds, |
| 193 | + }, |
| 194 | + { |
| 195 | + "enough ibc fee in balance, should pass", |
| 196 | + func(suite *AnteTestSuite) { |
| 197 | + err := suite.feeabsKeeper.SetHostZoneConfig(suite.ctx, mockHostZoneConfig) |
| 198 | + require.NoError(t, err) |
| 199 | + suite.feeabsKeeper.SetTwapRate(suite.ctx, "ibcfee", sdk.NewDec(1)) |
| 200 | + suite.txBuilder.SetFeeAmount(ibcFeeAmount) |
| 201 | + suite.stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("native").MinTimes(1) |
| 202 | + suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), types.ModuleName, ibcFeeAmount).Return(nil).MinTimes(1) |
| 203 | + suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), authtypes.FeeCollectorName, feeAmount).Return(nil).MinTimes(1) |
| 204 | + }, |
| 205 | + false, |
| 206 | + nil, |
| 207 | + }, |
| 208 | + } |
| 209 | + for _, tc := range testCases { |
| 210 | + t.Run(tc.name, func(t *testing.T) { |
| 211 | + suite := SetupTestSuite(t, false) |
| 212 | + acc := suite.CreateTestAccounts(1)[0] |
| 213 | + // default value for gasLimit, feeAmount, feePayer. Use native token fee as default |
| 214 | + suite.txBuilder.SetGasLimit(gasLimit) |
| 215 | + suite.txBuilder.SetFeeAmount(feeAmount) |
| 216 | + suite.txBuilder.SetFeePayer(acc.acc.GetAddress()) |
| 217 | + suite.ctx = suite.ctx.WithMinGasPrices(minGasPrice) |
| 218 | + |
| 219 | + // mallate the test case, e.g. setup to pay fee in IBC token |
| 220 | + tc.malleate(suite) |
| 221 | + |
| 222 | + // Construct tx and run through mempool decorator |
| 223 | + tx := suite.txBuilder.GetTx() |
| 224 | + deductFeeDecorator := ante.NewFeeAbstractionDeductFeeDecorate(suite.accountKeeper, suite.bankKeeper, suite.feeabsKeeper, suite.feeGrantKeeper) |
| 225 | + antehandler := sdk.ChainAnteDecorators(deductFeeDecorator) |
| 226 | + _, err := antehandler(suite.ctx, tx, false) |
| 227 | + |
| 228 | + if tc.isErr { |
| 229 | + require.Error(t, err) |
| 230 | + require.ErrorIs(t, err, tc.expErr) |
| 231 | + } else { |
| 232 | + require.NoError(t, err) |
| 233 | + } |
| 234 | + }) |
| 235 | + } |
| 236 | +} |
0 commit comments