docs(x/account/auth): Improve error handling and comments in fee.go (#20426)
Signed-off-by: Hwangjae Lee <meetrick@gmail.com> Co-authored-by: son trinh <trinhleson2000@gmail.com>
This commit is contained in:
parent
7eef3b3705
commit
a2aaed66e8
@ -12,14 +12,14 @@ import (
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
)
|
||||
|
||||
// TxFeeChecker check if the provided fee is enough and returns the effective fee and tx priority,
|
||||
// the effective fee should be deducted later, and the priority should be returned in abci response.
|
||||
// TxFeeChecker checks if the provided fee is enough and returns the effective fee and tx priority.
|
||||
// The effective fee should be deducted later, and the priority should be returned in the ABCI response.
|
||||
type TxFeeChecker func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error)
|
||||
|
||||
// DeductFeeDecorator deducts fees from the fee payer. The fee payer is the fee granter (if specified) or first signer of the tx.
|
||||
// If the fee payer does not have the funds to pay for the fees, return an InsufficientFunds error.
|
||||
// Call next AnteHandler if fees successfully deducted.
|
||||
// CONTRACT: Tx must implement FeeTx interface to use DeductFeeDecorator
|
||||
// Call next AnteHandler if fees are successfully deducted.
|
||||
// CONTRACT: The Tx must implement the FeeTx interface to use DeductFeeDecorator.
|
||||
type DeductFeeDecorator struct {
|
||||
accountKeeper AccountKeeper
|
||||
bankKeeper types.BankKeeper
|
||||
@ -43,7 +43,7 @@ func NewDeductFeeDecorator(ak AccountKeeper, bk types.BankKeeper, fk FeegrantKee
|
||||
func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
|
||||
feeTx, ok := tx.(sdk.FeeTx)
|
||||
if !ok {
|
||||
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
|
||||
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must implement the FeeTx interface")
|
||||
}
|
||||
|
||||
txService := dfd.accountKeeper.GetEnvironment().TransactionService
|
||||
@ -76,10 +76,11 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, nex
|
||||
func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee sdk.Coins) error {
|
||||
feeTx, ok := sdkTx.(sdk.FeeTx)
|
||||
if !ok {
|
||||
return errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
|
||||
return errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must implement the FeeTx interface")
|
||||
}
|
||||
|
||||
if addr := dfd.accountKeeper.GetModuleAddress(types.FeeCollectorName); addr == nil {
|
||||
addr := dfd.accountKeeper.GetModuleAddress(types.FeeCollectorName)
|
||||
if len(addr) == 0 {
|
||||
return fmt.Errorf("fee collector module account (%s) has not been set", types.FeeCollectorName)
|
||||
}
|
||||
|
||||
@ -87,8 +88,8 @@ func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee
|
||||
feeGranter := feeTx.FeeGranter()
|
||||
deductFeesFrom := feePayer
|
||||
|
||||
// if feegranter set deduct fee from feegranter account.
|
||||
// this works with only when feegrant enabled.
|
||||
// if feegranter set, deduct fee from feegranter account.
|
||||
// this works only when feegrant is enabled.
|
||||
if feeGranter != nil {
|
||||
feeGranterAddr := sdk.AccAddress(feeGranter)
|
||||
|
||||
@ -132,7 +133,7 @@ func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc []byte, fees s
|
||||
|
||||
err := bankKeeper.SendCoinsFromAccountToModule(ctx, sdk.AccAddress(acc), types.FeeCollectorName, fees)
|
||||
if err != nil {
|
||||
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
|
||||
return fmt.Errorf("failed to deduct fees: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Loading…
Reference in New Issue
Block a user