Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle excessive fee deduction during simulation #518

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions custom/auth/ante/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions custom/auth/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Loading