Skip to content

Commit

Permalink
handle excessive fee deduction during simulation (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
StrathCole authored Sep 25, 2024
1 parent a936511 commit 1ea5b88
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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

0 comments on commit 1ea5b88

Please sign in to comment.