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

feat(wardend): burn 30% of the tx fee #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions warden/app/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ var (

// module account permissions
moduleAccPerms = []*authmodulev1.ModuleAccountPermission{
{Account: authtypes.FeeCollectorName},
{Account: authtypes.FeeCollectorName, Permissions: []string{authtypes.Burner}},
{Account: distrtypes.ModuleName},
{Account: minttypes.ModuleName, Permissions: []string{authtypes.Minter}},
{Account: stakingtypes.BondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
Expand Down Expand Up @@ -257,8 +257,10 @@ var (
Config: appconfig.WrapAny(&paramsmodulev1.Module{}),
},
{
Name: "tx",
Config: appconfig.WrapAny(&txconfigv1.Config{}),
Name: "tx",
Config: appconfig.WrapAny(&txconfigv1.Config{
SkipAnteHandler: true,
}),
},
{
Name: genutiltypes.ModuleName,
Expand Down
61 changes: 61 additions & 0 deletions warden/x/warden/ante/ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
txsigning "cosmossdk.io/x/tx/signing"
"github.com/warden-protocol/wardenprotocol/warden/x/warden/types"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
type HandlerOptions struct {
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
ExtensionOptionChecker authante.ExtensionOptionChecker
FeegrantKeeper types.FeegrantKeeper
SignModeHandler *txsigning.HandlerMap
SigGasConsumer func(meter storetypes.GasMeter, sig signing.SignatureV2, params authtypes.Params) error
FeeBurnRatio math.LegacyDec
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
// numbers, checks signatures & account numbers, and deducts fees from the first
// signer.
func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.AccountKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "account keeper is required for ante builder")
}

if options.BankKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "bank keeper is required for ante builder")
}

if options.SignModeHandler == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}

anteDecorators := []sdk.AnteDecorator{
authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
authante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
authante.NewValidateBasicDecorator(),
authante.NewTxTimeoutHeightDecorator(),
authante.NewValidateMemoDecorator(options.AccountKeeper),
authante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
authante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, nil), // use default sdk FeeChecker
NewBurnFeeDecorator(options.BankKeeper, options.FeeBurnRatio),
authante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
authante.NewValidateSigCountDecorator(options.AccountKeeper),
authante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
authante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
authante.NewIncrementSequenceDecorator(options.AccountKeeper),
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
}
47 changes: 47 additions & 0 deletions warden/x/warden/ante/burn_fee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/warden-protocol/wardenprotocol/warden/x/warden/types"
)

// BurnFeeDecorator burns specified part of fee collected. This decorator must be called after DeductFeeDecorator
// because assumes that all fee related checks are already done and does not check anything by itself
type BurnFeeDecorator struct {
bankKeeper types.BankKeeper
burnRatio math.LegacyDec
}

func NewBurnFeeDecorator(bk types.BankKeeper, br math.LegacyDec) BurnFeeDecorator {
return BurnFeeDecorator{
bankKeeper: bk,
burnRatio: br,
}
}

func (bfd BurnFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate 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")
}

// Assume that default sdk FeeChecker is used. It follows that actual fee is always equal to feeTx.GetFee()
fee := feeTx.GetFee()

burnAmount, _ := sdk.NewDecCoinsFromCoins(fee...).MulDec(bfd.burnRatio).TruncateDecimal()

if !burnAmount.IsZero() {
err := bfd.bankKeeper.BurnCoins(ctx, authtypes.FeeCollectorName, burnAmount)
if err != nil {
return ctx, errorsmod.Wrap(sdkerrors.ErrLogic, "Failed to burn fee")
}
}

return next(ctx, tx, simulate)
Comment on lines +28 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance error message for BurnCoins failure.

The error message when burning coins fails could be more descriptive to aid in debugging.

- return ctx, errorsmod.Wrap(sdkerrors.ErrLogic, "Failed to burn fee")
+ return ctx, errorsmod.Wrapf(sdkerrors.ErrLogic, "Failed to burn fee: %v", err)
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (bfd BurnFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate 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")
}
// Assume that default sdk FeeChecker is used. It follows that actual fee is always equal to feeTx.GetFee()
fee := feeTx.GetFee()
burnAmount, _ := sdk.NewDecCoinsFromCoins(fee...).MulDec(bfd.burnRatio).TruncateDecimal()
if !burnAmount.IsZero() {
err := bfd.bankKeeper.BurnCoins(ctx, authtypes.FeeCollectorName, burnAmount)
if err != nil {
return ctx, errorsmod.Wrap(sdkerrors.ErrLogic, "Failed to burn fee")
}
}
return next(ctx, tx, simulate)
func (bfd BurnFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate 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")
}
// Assume that default sdk FeeChecker is used. It follows that actual fee is always equal to feeTx.GetFee()
fee := feeTx.GetFee()
burnAmount, _ := sdk.NewDecCoinsFromCoins(fee...).MulDec(bfd.burnRatio).TruncateDecimal()
if !burnAmount.IsZero() {
err := bfd.bankKeeper.BurnCoins(ctx, authtypes.FeeCollectorName, burnAmount)
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrLogic, "Failed to burn fee: %v", err)
}
}
return next(ctx, tx, simulate)

}
46 changes: 46 additions & 0 deletions warden/x/warden/ante/burn_fee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ante_test

import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/warden-protocol/wardenprotocol/warden/x/warden/ante"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"cosmossdk.io/math"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)

func TestDeductFees(t *testing.T) {
s := SetupTestSuite(t, false)
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

// keys and addresses
accs := s.CreateTestAccounts(1)

// msg and signatures
msg := testdata.NewTestMsg(accs[0].acc.GetAddress())
feeAmount := sdk.NewCoins(sdk.NewInt64Coin("atom", 150))
gasLimit := testdata.NewTestGasLimit()
require.NoError(t, s.txBuilder.SetMsgs(msg))
s.txBuilder.SetFeeAmount(feeAmount)
s.txBuilder.SetGasLimit(gasLimit)

privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{0}, []uint64{0}
tx, err := s.CreateTestTx(s.ctx, privs, accNums, accSeqs, s.ctx.ChainID(), signing.SignMode_SIGN_MODE_DIRECT)
require.NoError(t, err)

bfd := ante.NewBurnFeeDecorator(s.bankKeeper, math.LegacyNewDecWithPrec(3, 1))
antehandler := sdk.ChainAnteDecorators(bfd)

burntAmount := sdk.NewCoins(sdk.NewInt64Coin("atom", 45))
s.bankKeeper.EXPECT().BurnCoins(gomock.Any(), authtypes.FeeCollectorName, burntAmount).Return(nil)
_, err = antehandler(s.ctx, tx, false)

require.Nil(t, err, "Tx errored after account has been set with sufficient funds")
}
Loading
Loading