Skip to content

Commit

Permalink
Move burn tax process to post handler
Browse files Browse the repository at this point in the history
  • Loading branch information
phamminh0811 committed Jul 18, 2024
1 parent d66c1f4 commit 3e816ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
13 changes: 2 additions & 11 deletions x/tax2gas/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (fd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, nex
}

// Try to deduct the gasConsumed fees
paidDenom, err := fd.tryDeductFee(ctx, feeTx, gasConsumedFees, simulate)
paidDenom, err := fd.tryDeductFee(ctx, feeTx, gasConsumedFees)
if err != nil {
return ctx, err
}
Expand All @@ -103,7 +103,7 @@ func (fd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, nex
return next(newCtx, tx, simulate)
}

func (fd FeeDecorator) tryDeductFee(ctx sdk.Context, feeTx sdk.FeeTx, taxes sdk.Coins, simulate bool) (string, error) {
func (fd FeeDecorator) tryDeductFee(ctx sdk.Context, feeTx sdk.FeeTx, taxes sdk.Coins) (string, error) {
if addr := fd.accountKeeper.GetModuleAddress(authtypes.FeeCollectorName); addr == nil {
return "", fmt.Errorf("fee collector module account (%s) has not been set", authtypes.FeeCollectorName)
}
Expand Down Expand Up @@ -172,15 +172,6 @@ func (fd FeeDecorator) tryDeductFee(ctx sdk.Context, feeTx sdk.FeeTx, taxes sdk.
if err != nil {
return "", err
}
if !simulate {
err := fd.BurnTaxSplit(ctx, foundCoins)
if err != nil {
return "", err
}

// Record tax proceeds
fd.treasuryKeeper.RecordEpochTaxProceeds(ctx, foundCoins)
}

events := sdk.Events{
sdk.NewEvent(
Expand Down
10 changes: 6 additions & 4 deletions x/tax2gas/ante/fee_burntax.go → x/tax2gas/post/burntax.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ante
package post

import (
errorsmod "cosmossdk.io/errors"
Expand All @@ -10,8 +10,8 @@ import (
)

// BurnTaxSplit splits
func (fd FeeDecorator) BurnTaxSplit(ctx sdk.Context, taxes sdk.Coins) (err error) {
burnSplitRate := fd.treasuryKeeper.GetBurnSplitRate(ctx)
func (tdd Tax2gasPostDecorator) BurnTaxSplit(ctx sdk.Context, taxes sdk.Coins) (err error) {
burnSplitRate := tdd.treasuryKeeper.GetBurnSplitRate(ctx)

if burnSplitRate.IsPositive() {
distributionDeltaCoins := sdk.NewCoins()
Expand All @@ -25,7 +25,7 @@ func (fd FeeDecorator) BurnTaxSplit(ctx sdk.Context, taxes sdk.Coins) (err error
}

if !taxes.IsZero() {
if err = fd.bankKeeper.SendCoinsFromModuleToModule(
if err = tdd.bankKeeper.SendCoinsFromModuleToModule(
ctx,
types.FeeCollectorName,
treasury.BurnModuleName,
Expand All @@ -35,5 +35,7 @@ func (fd FeeDecorator) BurnTaxSplit(ctx sdk.Context, taxes sdk.Coins) (err error
}
}

// Record tax proceeds
tdd.treasuryKeeper.RecordEpochTaxProceeds(ctx, taxes)
return nil
}
11 changes: 11 additions & 0 deletions x/tax2gas/post/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (tgd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate
}
}

taxes := sdk.NewCoins()
for _, feeCoin := range feeCoins {
feePayer := tgd.accountKeeper.GetAccount(ctx, feePayer)
found, gasPrice := tax2gasutils.GetGasPriceByDenom(gasPrices, feeCoin.Denom)
Expand All @@ -131,6 +132,7 @@ func (tgd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
taxes = taxes.Add(feeRequired)
gasRemaining = 0
break
}
Expand All @@ -139,9 +141,18 @@ func (tgd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
taxes = taxes.Add(feeCoin)
feeRemaining := sdk.NewDecCoinFromCoin(feeRequired.Sub(feeCoin))
gasRemaining = uint64(feeRemaining.Amount.Quo(gasPrice).Ceil().RoundInt64())
}

if !simulate {
err := tgd.BurnTaxSplit(ctx, taxes)
if err != nil {
return ctx, err
}
}

if gasRemaining > 0 {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "fees are not enough to pay for gas")
}
Expand Down

0 comments on commit 3e816ea

Please sign in to comment.