From 1ea5b888789c4c9cc8178e0f729125e6d2584a12 Mon Sep 17 00:00:00 2001 From: StrathCole <7449529+StrathCole@users.noreply.github.com> Date: Wed, 25 Sep 2024 06:54:37 +0200 Subject: [PATCH] handle excessive fee deduction during simulation (#518) --- custom/auth/ante/expected_keeper.go | 2 ++ custom/auth/ante/fee.go | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/custom/auth/ante/expected_keeper.go b/custom/auth/ante/expected_keeper.go index 05957c63..2d00f13a 100644 --- a/custom/auth/ante/expected_keeper.go +++ b/custom/auth/ante/expected_keeper.go @@ -30,6 +30,8 @@ type BankKeeper interface { SendCoins(ctx sdk.Context, from, to sdk.AccAddress, amt sdk.Coins) error SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error SendCoinsFromModuleToModule(ctx sdk.Context, senderModule string, recipientModule string, amt sdk.Coins) error + SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error + MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error } type DistrKeeper interface { diff --git a/custom/auth/ante/fee.go b/custom/auth/ante/fee.go index a292be06..389ca909 100644 --- a/custom/auth/ante/fee.go +++ b/custom/auth/ante/fee.go @@ -10,6 +10,7 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/cosmos/cosmos-sdk/x/auth/types" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" ) // FeeDecorator deducts fees from the first signer of the tx @@ -119,6 +120,28 @@ func (fd FeeDecorator) checkDeductFee(ctx sdk.Context, feeTx sdk.FeeTx, taxes sd feesOrTax = feesOrTax.Add(sdk.NewCoin(tax.Denom, missingAmount)) } } + + // a further issue arises from the fact that simulations are sometimes run with + // the full bank balance of the account, which can lead to a situation where + // the fees are deducted from the account during simulation and so the account + // balance is not enough to complete the simulation. + // So ONLY during simulation, we MINT the fees to the account to avoid this issue. + // We only mint the fees we are adding on top of the original fee (sent by user). + if !feesOrTax.IsZero() { + needMint := feesOrTax.Sort().Sub(fee.Sort()...) + if !needMint.IsZero() { + err := fd.bankKeeper.MintCoins(ctx, minttypes.ModuleName, needMint) + if err != nil { + return err + } + + // we need to add the fees to the account balance to avoid deduction errors + err = fd.bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, deductFeesFromAcc.GetAddress(), needMint) + if err != nil { + return err + } + } + } } if !feesOrTax.IsZero() {