-
Notifications
You must be signed in to change notification settings - Fork 107
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
r0t0r-r0t0r
wants to merge
1
commit into
warden-protocol:main
Choose a base branch
from
r0t0r-r0t0r:burn-tx-fee
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
Committable suggestion